<template>
  <div class="padding" style="height: 700px; overflow: auto">
    <div class="padding">
      <div style="font-weight: bold; font-size: 16px">订单信息</div>
      <div class="back_color padding marginTop" v-if="baseData1">
        <a-descriptions title="" :column="2">
          <a-descriptions-item label="生产订单号">{{ baseData1.orderNo }}</a-descriptions-item>
          <a-descriptions-item label="销售单号">{{ baseData1.saleOrderNo }}</a-descriptions-item>
          <a-descriptions-item label="产品编码">{{ baseData1.productNo }}</a-descriptions-item>
          <a-descriptions-item label="产品名称">{{ baseData1.productName }}</a-descriptions-item>
          <a-descriptions-item label="规格型号">{{ baseData1.speModel }}</a-descriptions-item>
          <a-descriptions-item label="单位">{{ baseData1.unity }} </a-descriptions-item>
          <a-descriptions-item label="计划产量">{{ baseData1.planNum }}</a-descriptions-item>
          <a-descriptions-item label="上线日期">{{ baseData1.onlineDate }}</a-descriptions-item>
          <a-descriptions-item label="创建人">{{ baseData1.createUserName }}</a-descriptions-item>

          <a-descriptions-item label="创建时间">{{ baseData1.createDate }}</a-descriptions-item>
        </a-descriptions>
        <a-descriptions title="" :column="1">
          <a-descriptions-item label="优先级">
            <span v-if="baseData1.priority">
              <span v-for="(re, i) in baseData1.priority.split(',')" :key="i">
                <span v-for="item in priorityOptions" :key="item.key">
                  <span v-if="item.value == re">{{ i > 0 ? ',' : '' }}{{ item.label }}</span>
                </span>
              </span>
            </span>
          </a-descriptions-item>
          <a-descriptions-item label="备注">{{ baseData1.remark }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </div>
    <div class="padding">
      <div style="font-weight: bold; font-size: 16px">生产明细</div>
      <div style="font-size: 12px">根据产品对应的BOM表，自动生成以下生产明细</div>

      <!-- <div style="float: right">
        <a-button type="primary" @click="multipleDialog = true" class="select-btn">选择 </a-button>
      </div> -->
      <a-table
        :columns="filterColum(columnsProp.columns)"
        :bordered="true"
        :pagination="false"
        :data-source="detailList"
        :scroll="{ x: 'max-content' }"
      >
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.key !== 'action'">
            <template v-if="column.dataIndex === 'index'">
              {{ index + 1 }}
            </template>
          </template>
          <template v-if="column.key == 'action'">
            <a title="删除" @click="removeList(index)">
              <MinusCircleOutlined style="padding-bottom: 20px" />
            </a>
          </template>
          <template v-if="column.dataIndex === 'carId'">
            <a-select
              :options="carIdOptions"
              placeholder="请选择"
              v-model:value="record.carId"
              :disabled="!rowData.isUpdate"
              allowClear
            >
            </a-select>
          </template>
          <template v-if="column.dataIndex === 'lineId'">
            <a-select
              :options="lineOptionsList[record.carId]"
              placeholder="请选择"
              v-model:value="record.lineId"
              :disabled="!rowData.isUpdate"
              allowClear
            >
            </a-select>
          </template>
          <template v-if="column.dataIndex === 'craftId'">
            <a-select
              :options="stepOptionsList[record.materialId]"
              placeholder="请选择"
              v-model:value="record.craftId"
              :disabled="!rowData.isUpdate"
              allowClear
            >
            </a-select>
          </template>
        </template>
      </a-table>
      <MultipleSelect
        v-if="multipleDialog"
        ref="MultipleSelectRef"
        v-model:multipleDialog="multipleDialog"
        :params="{ itemId }"
        :datasourceType="columnsProp.preloadType"
        :dicOptions="columnsProp.dicOptions"
        :apiConfig="columnsProp.apiConfig"
        :isSubFormUse="true"
        @submit="renderSubFormList"
        popupType="preload"
      />
    </div>
    <div class="padding" v-show="false">
      <div style="font-weight: bold; font-size: 16px">订单信息（需保持的）</div>
      <a-form ref="formRef" :model="formData" :labelCol="{ span: 3 }">
        <a-col :span="24">
          <a-form-item label="订单id" name="orderId" required>
            <a-input v-model:value="formData.orderId" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订单编号" name="orderNo" required>
            <a-input v-model:value="formData.orderNo" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品ID" name="productId" required>
            <a-input v-model:value="formData.productId" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品编号" name="productNo">
            <a-input v-model:value="formData.productNo" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="产品名称" name="productName">
            <a-input v-model:value="formData.productName" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="优先级" name="priority">
            <a-input v-model:value="formData.priority" :disabled="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题描述" name="remark">
            <a-textarea
              :disabled="true"
              v-model:value="formData.remark"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-item>
        </a-col>
      </a-form>
    </div>
    <a-divider />
    <div
      style="
        padding: 10px 16px;
        text-align: right;
        background: white;
        border-top: 1px solid rgb(240, 240, 240);
        border-radius: 0px 0px 2px 2px;
        position: absolute;
        bottom: 1px;
        width: 98%;
      "
      v-if="rowData.isUpdate"
    >
      <a-button @click="handleClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" style="margin-left: 12px">确认</a-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { uploadApi } from '/@/api/sys/upload';
  import { PlusOutlined, MinusCircleOutlined } from '@ant-design/icons-vue';
  import { ref, reactive, watch, onMounted, createVNode } from 'vue';
  // import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import {
    getDicItem,
    getMesCarLineTreeList,
    getProcessTechList,
    getBomListByProduct,
  } from '/@/api/mes/common';

  import { getMesProOrder } from '/@/api/mes/proorder';
  import {
    addMesProOrderplan,
    getMesProOrderplan,
    updateMesProOrderplan,
  } from '/@/api/mes/proorderplan';
  // import SelectUser from '/@/components/Form/src/components/SelectUser.vue';
  import AutoCodeRule from '/@/components/Form/src/components/AutoCodeRule.vue';
  import { MultipleSelect } from '/@/components/MultiplePopup';
  // import Upload from '/@/components/Form/src/components/Upload.vue';
  // import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  // import { Modal } from 'ant-design-vue';
  const { notification } = useMessage();
  const { t } = useI18n();
  let props = defineProps(['rowData']);
  const rowData: {
    id: string;
    orderId: string;
    isUpdate: boolean;
    flag: string;
  } = reactive({
    id: '',
    orderId: '',
    isUpdate: false,
    flag: '',
  });
  const columnsProp = {
    mainKey: 'mesProOrderplan2List',
    columns: [
      {
        key: '64a4b928c6e344f3b1527007ad9e03a4',
        title: '层级',
        dataIndex: 'level',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'level',
        },
      },
      {
        key: '81bcbfb7783c48c699e96f11a7bfcddd',
        title: '物料ID',
        dataIndex: 'materialId',
        componentType: 'Input',
        defaultValue: '',
        show: false,
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '物料ID请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'material_id',
        },
      },
      {
        key: '928f0a24681e4e2f9f59ee249713cda5',
        title: '物料编码',
        dataIndex: 'materialNo',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'code',
        },
      },
      {
        key: '0288fffa48424cc39acb1cffde170037',
        title: '物料名称',
        dataIndex: 'materialName',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'name',
        },
      },
      {
        key: '4e0cdc1060714f28b5939e43b10c7b25',
        title: '规格型号',
        dataIndex: 'speModel',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'model',
        },
      },
      {
        key: 'a894c0cfd0884c79979de1ff561b93fb',
        title: '物料类别',
        dataIndex: 'materialType',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'type_name',
        },
      },
      {
        key: '303f008cd27642b697ebbb3ecc92600c',
        title: '物料属性',
        dataIndex: 'materialProperty',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'property_name',
        },
      },
      {
        key: '6445102957584c75bedb9e633fd6e3a9',
        title: '单位',
        dataIndex: 'unity',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'unit_name',
        },
      },
      {
        key: 'ffa27c618711495cb01371828295a7f4',
        title: '计划产量',
        dataIndex: 'planNum',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          width: '100%',
          disabled: false,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
          prestrainField: 'count',
        },
      },
      {
        key: '94f1f0c9e25943b787905515d179aed4',
        title: '车间',
        dataIndex: 'carId',
        componentType: 'XjrSelect',
        componentProps: {
          clearable: false,
          defaultSelect: '',
          rules: [],
          valueField: 'value',
          showLabel: true,
          required: true,
          labelField: 'label',
          datasourceType: 'api',
          dicOptions: [],
          showSearch: false,
          width: '100%',
          apiConfig: {
            method: 'GET',
            script:
              'var condition="";\r\nif(factory_type!=null&&factory_type!=""){ //0-工厂、1-车间、2产线,3-工位\r\n    condition+=" and factory_type=#{factory_type}";\r\n}\r\nif(factory_id!=null&&factory_id!=""){ \r\n    condition+=" and factory_id=#{factory_id}";\r\n}\r\nif(car_id!=null&&car_id!=""){ \r\n    condition+=" and car_id=#{car_id}";\r\n}\r\nif(parent_id!=null&&parent_id!=""){ \r\n    condition+=" and parent_id=#{parent_id}";\r\n}\r\n\r\nreturn db.select("SELECT id AS `key` ,id AS `value`,CONCAT(factory_name) AS label,t.* FROM mes_base_factory t where 1=1 "+condition)',
            apiParams: [
              {
                tableInfo: [
                  {
                    dataType: 'String',
                    bindType: 'value',
                    required: false,
                    name: 'factory_type',
                    value: '1',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    required: false,
                    name: 'factory_id',
                    value: '',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    required: false,
                    name: 'car_id',
                    value: '',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    required: false,
                    name: 'parent_id',
                    value: '',
                  },
                ],
                title: 'Query Params',
                key: '1',
              },
              { tableInfo: [], title: 'Header', key: '2' },
              { title: 'Body', key: '3' },
            ],
            path: '/mes/mesFactory',
            apiId: '6b4b40b3db664831b709f56bc98a7f04',
          },
          disabled: false,
          placeholder: '请选择',
          staticOptions: [
            { label: 'Option 1', value: 'Option 1', key: 1 },
            { label: 'Option 2', value: 'Option 2', key: 2 },
            { label: 'Option 3', value: 'Option 3', key: 3 },
          ],
          events: {},
          span: '',
        },
      },
      {
        key: '1c11989d58cd4212b9402de138f7a641',
        title: '产线',
        dataIndex: 'lineId',
        componentType: 'XjrSelect',
        componentProps: {
          clearable: false,
          defaultSelect: '',
          rules: [],
          valueField: 'value',
          showLabel: true,
          required: true,
          labelField: 'label',
          datasourceType: 'api',
          dicOptions: [],
          showSearch: false,
          width: '100%',
          apiConfig: {
            method: 'GET',
            script:
              'var condition="";\r\nif(factory_type!=null&&factory_type!=""){ //0-工厂、1-车间、2产线,3-工位\r\n    condition+=" and factory_type=#{factory_type}";\r\n}\r\nif(factory_id!=null&&factory_id!=""){ \r\n    condition+=" and factory_id=#{factory_id}";\r\n}\r\nif(car_id!=null&&car_id!=""){ \r\n    condition+=" and car_id=#{car_id}";\r\n}\r\nif(parent_id!=null&&parent_id!=""){ \r\n    condition+=" and parent_id=#{parent_id}";\r\n}\r\n\r\nreturn db.select("SELECT id AS `key` ,id AS `value`,CONCAT(factory_name) AS label,t.* FROM mes_base_factory t where 1=1 "+condition)',
            apiParams: [
              {
                tableInfo: [
                  {
                    dataType: 'String',
                    bindType: 'value',
                    required: false,
                    name: 'factory_type',
                    value: '2',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    required: false,
                    name: 'factory_id',
                    value: '',
                  },
                  { dataType: 'String', required: false, name: 'car_id', value: '' },
                  {
                    dataType: 'String',
                    bindType: '',
                    required: false,
                    name: 'parent_id',
                    value: '',
                  },
                ],
                title: 'Query Params',
                key: '1',
              },
              { tableInfo: [], title: 'Header', key: '2' },
              { title: 'Body', key: '3' },
            ],
            path: '/mes/mesFactory',
            apiId: '6b4b40b3db664831b709f56bc98a7f04',
          },
          disabled: false,
          placeholder: '请选择',
          staticOptions: [
            { label: 'Option 1', value: 'Option 1', key: 1 },
            { label: 'Option 2', value: 'Option 2', key: 2 },
            { label: 'Option 3', value: 'Option 3', key: 3 },
          ],
          events: {},
          span: '',
        },
      },
      {
        key: 'cdf524dbaad146b1b3c2c54eba5ab973',
        title: '生产工艺',
        dataIndex: 'craftId',
        componentType: 'XjrSelect',
        componentProps: {
          clearable: false,
          defaultSelect: '',
          rules: [],
          valueField: 'value',
          showLabel: true,
          required: true,
          labelField: 'label',
          datasourceType: 'api',
          dicOptions: [],
          showSearch: false,
          width: '100%',
          apiConfig: {
            method: 'GET',
            script:
              'var condition="";\r\n\r\nreturn db.select("SELECT t.id AS `key` ,t.id AS `value`,t.process_name AS label,t.*  FROM mes_process_technology t WHERE 1=1  "+condition)',
            apiParams: [
              {
                tableInfo: [
                  { dataType: 'String', bindType: '', required: false, name: 'keyword' },
                  {
                    defaultValue: 'name',
                    dataType: 'String',
                    bindType: 'value',
                    description: '回显字段',
                    required: false,
                    name: 'showLabel',
                    value: 'name',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    description: '查询类型',
                    required: false,
                    name: 'queryType',
                  },
                  {
                    dataType: 'String',
                    bindType: '',
                    description: '状态',
                    required: false,
                    name: 'state',
                  },
                ],
                title: 'Query Params',
                key: '1',
              },
              { tableInfo: [], title: 'Header', key: '2' },
              { title: 'Body', key: '3' },
            ],
            path: '/mes/getWorkOrderList_copy',
            apiId: 'copy1691738160514d20686',
          },
          disabled: false,
          placeholder: '请选择',
          staticOptions: [
            { label: 'Option 1', value: 'Option 1', key: 1 },
            { label: 'Option 2', value: 'Option 2', key: 2 },
            { label: 'Option 3', value: 'Option 3', key: 3 },
          ],
          events: {},
          span: '',
        },
      },
      // { title: '操作', key: 'action', fixed: 'right', width: '50px' },
    ],
    buttonName: '选择数据',
    showIndex: false,
    useSelectButton: false,
    preloadType: 'api',
    showLabel: true,
    itemId: '',
    multipleHeads: [],
    dicOptions: [],
    showComponentBorder: true,
    apiConfig: {
      path: '/mes/getBomListByProduct',
      method: 'GET',
      apiId: 'copy1692322438121d88503',
      apiParams: [
        {
          key: '1',
          title: 'Query Params',
          tableInfo: [
            {
              name: 'keyword',
              value: null,
              description: null,
              required: false,
              dataType: 'String',
              type: null,
              defaultValue: null,
              validateType: null,
              error: null,
              expression: null,
              children: null,
              bindType: '',
            },
            {
              name: 'showLabel',
              value: '',
              description: '回显字段',
              required: false,
              dataType: 'String',
              type: null,
              defaultValue: 'code',
              validateType: null,
              error: null,
              expression: null,
              children: null,
              bindType: '',
            },
            {
              name: 'queryType',
              value: null,
              description: '查询类型',
              required: false,
              dataType: 'String',
              type: null,
              defaultValue: null,
              validateType: null,
              error: null,
              expression: null,
              children: null,
              bindType: '',
            },
            {
              name: 'material_id',
              value: 'product_id',
              description: '物料产品ID',
              required: false,
              dataType: 'String',
              type: null,
              defaultValue: null,
              validateType: null,
              error: null,
              expression: null,
              children: null,
              bindType: 'data',
            },
            {
              name: 'plan_num',
              value: 'plan_num',
              description: '计划产量',
              required: false,
              dataType: null,
              type: null,
              defaultValue: null,
              validateType: null,
              error: null,
              expression: null,
              children: null,
              bindType: 'data',
            },
          ],
        },
        { key: '2', title: 'Header', tableInfo: [] },
        { key: '3', title: 'Body' },
      ],
      script:
        'var condition=" and material_id=#{material_id} ";\r\n// if(material_id!=null&&material_id!=""){ \r\n//     condition+=" and material_id=#{material_id}";\r\n// }\r\nreturn db.select("SELECT t.id AS `key` ,t.id AS `value`,t.level AS label,t.*,#{plan_num} as plan_num FROM case_erp_bom t where 1=1  "+condition)',
    },
    showFormBorder: true,
    span: '24',
  };
  const detailList = ref<any[]>([]);
  // const format = ref('YYYY-MM-DD HH:mm:ss');
  const multipleDialog = ref<boolean>(false);
  function filterColum(column) {
    return column.filter((o) => {
      return o.show == false ? false : true;
    });
  }
  const renderSubFormList = async (list) => {
    // detailList.value = [];
    list?.forEach((x) => {
      const dataObj = {};
      columnsProp.columns?.map((item) => {
        if (!item?.dataIndex) return;
        dataObj[item.dataIndex as string] = item.componentProps?.prestrainField
          ? x[item.componentProps.prestrainField]
          : null;
      });

      //判断是否存在。
      let exist = false;
      detailList.value.forEach((e) => {
        // console.log(JSON.stringify(e), JSON.stringify(dataObj));
        if (JSON.stringify(e) == JSON.stringify(dataObj)) exist = true;
      });
      if (!exist) detailList.value.push(dataObj);
    });
  };

  const removeList = (index) => {
    detailList.value.splice(index, 1);
  };
  const baseData1 = ref<any>({});
  const formData = ref<any>({
    orderId: '',
    orderNo: '',
    remark: '',
    productId: '',
    productNo: '',
    productName: '',
    priority: '',
  });

  const emit = defineEmits(['cancel', 'success']);
  const formRef = ref();
  onMounted(async () => {
    rowData.id = props.rowData.id;
    rowData.orderId = props.rowData.orderId;
    rowData.isUpdate = props.rowData.isUpdate;
    rowData.flag = props.rowData.flag;
    console.log('row===', rowData);

    getDetailData();

    priorityOptions.value = await getDicItem('1689840965516009473');
    unityOptions.value = await getDicItem('1688808244736008194');

    carIdOptions.value = await getMesCarLineTreeList();
    stepOptions.value = await getProcessTechList('');

    //按物料分组
    stepOptions.value?.forEach(async (ele) => {
      stepOptionsList[ele.material_id] = stepOptions.value.filter((s) => {
        return s.material_id == ele.material_id;
      });
    });
    //按车间分组
    carIdOptions.value?.forEach(async (ele) => {
      lineOptionsList[ele.value] = ele.children;
    });
  });
  watch(
    () => props.rowData,
    (val) => {
      console.log('监听', val);
      if (val) {
        rowData.id = props.rowData.id;
        rowData.orderId = props.rowData.orderId;
        rowData.isUpdate = props.rowData.isUpdate;
        rowData.flag = props.rowData.flag;
        getDetailData();
      }
    },
  );

  const priorityOptions = ref<any[]>([]);
  const unityOptions = ref<any[]>([]);
  const carIdOptions = ref<any[]>([]);
  // const lineIdOptions = ref<any[]>([]);
  const stepOptions = ref<any[]>([]);
  let stepOptionsList = {};
  let lineOptionsList = {};

  // async function handleSysChange() {
  //   console.log('chg===', rowData, formData, formData.value.isSysNumBoolean);
  //   if (!rowData.isUpdate) return;

  //   if (formData.value.isSysNumBoolean) {
  //     formData.value.errorNo = await getAutoCode('mesSemifinErrorNo');
  //     formData.value.isSysNum = '1';
  //   } else {
  //     formData.value.errorNo = '';
  //     formData.value.isSysNum = '';
  //   }
  // }

  const getDetailData = async () => {
    const data = await getMesProOrder(rowData.orderId);
    console.log(data, '上部分信息');
    baseData1.value = data;

    formData.value.orderId = data.id;
    formData.value.orderNo = data.orderNo;
    formData.value.remark = data.remark;
    formData.value.productId = data.productId;
    formData.value.productNo = data.productNo;
    formData.value.productName = data.productName;
    formData.value.priority = data.priority;
    formData.value.onlineDate = data.onlineDate;
    formData.value.saleOrderNo = data.saleOrderNo;
    formData.value.saleOrderId = data.saleOrderId;

    if (!rowData.id) {
      // formData.value.isSysNum = '1';
      // handleSysChange();
      //查询明细记录
      // if (formData.value.productId)

      let bomlist = getBomListByProduct(formData.value.productId, 'sub');
      bomlist?.then((result) => {
        //Promise中如何取到[[PromiseResult]]
        console.log('result', result);
        if (result && result instanceof Array)
          result.forEach((ele) => {
            var level = ele.bom_level?.split('-');
            detailList.value.push({
              orderId: formData.value.orderId,
              orderNo: formData.value.orderNo,
              productId: formData.value.productId,
              productNo: formData.value.productNo,
              productName: formData.value.productName,
              remark: formData.value.remark,
              priority: formData.value.priority,
              saleOrderNo: formData.value.saleOrderNo,
              saleOrderId: formData.value.saleOrderId,
              onlineDate: formData.value.onlineDate,
              level: level ? level.length : 0,
              materialId: ele.material_id,
              materialNo: ele.code,
              materialName: ele.name,
              speModel: ele.model,
              materialType: ele.type_name,
              materialProperty: ele.property_name,
              unity: ele.unit_name,
              planNum: parseInt(ele.count),
            });
          });
      });
    } else {
      const res = await getMesProOrderplan(rowData.id);
      if (res) {
        formData.value.id = res.id;
        detailList.value = res.mesProOrderplan2List;
      }
    }
  };

  async function handleSubmit() {
    try {
      let values = await formRef.value?.validate();
      // values.orderId = formData.value.orderId;
      // values.orderNo = formData.value.orderNo;
      // values.remark = formData.value.remark;
      // values.productId = formData.value.productId;
      // values.productNo = formData.value.productNo;
      // values.productName = formData.value.productName;
      // values.onlineDate = formData.value.onlineDate;
      // values.saleOrderNo = formData.value.saleOrderNo;
      // values.saleOrderId = formData.value.saleOrderId;

      values.id = formData.value.id;
      values.isSysNum = formData.value.isSysNumBoolean ? '1' : '';
      if (!detailList.value || detailList.value.length == 0) {
        notification.error({
          message: 'Tip',
          description: t('生产明细不能为空'),
        });
        return;
      }
      values.mesProOrderplan2List = detailList.value;
      console.log('values', values, formData.value, baseData1);

      // TODO custom api
      if (!values.id) {
        //false 新增
        await addMesProOrderplan(values);
        notification.success({
          message: 'Tip',
          description: t('创建生产计划成功！'),
        }); //提示消息
      } else {
        await updateMesProOrderplan(values);
        notification.success({
          message: 'Tip',
          description: t('修改生产计划成功！'),
        }); //提示消息
      }
      // formRef.value.resetFields();
      emit('success');
    } finally {
    }
  }

  function handleClose() {
    emit('cancel');
  }
</script>

<style lang="less" scoped>
  .padding {
    padding: 12px;
  }

  .back_color {
    background-color: hsl(0, 0%, 98%);
    position: relative;
  }

  .marginTop {
    margin-top: 12px;
  }
</style>
