<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.reportNo }}</a-descriptions-item>
          <a-descriptions-item label="报告名称">{{ baseData1.reportName }}</a-descriptions-item>
          <a-descriptions-item label="车间">
            <span v-if="baseData1.carId">
              <span v-for="(re, i) in baseData1.carId.split(',')" :key="i">
                <span v-for="item in carIdOptions" :key="item.key">
                  <span v-if="item.value == re">{{ i > 0 ? ',' : '' }}{{ item.label }}</span>
                </span>
              </span>
            </span></a-descriptions-item
          >
          <a-descriptions-item label="产线">
            <span v-if="baseData1.lineId">
              <span v-for="(re, i) in baseData1.lineId.split(',')" :key="i">
                <span v-for="item in lineIdOptions" :key="item.key">
                  <span v-if="item.value == re">{{ i > 0 ? ',' : '' }}{{ item.label }}</span>
                </span>
              </span>
            </span></a-descriptions-item
          >
          <a-descriptions-item label="班组">
            <span v-if="baseData1.stationId">
              <span v-for="(re, i) in baseData1.stationId.split(',')" :key="i">
                <span v-for="item in groupOptions" :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.checkDate }}</a-descriptions-item>
          <a-descriptions-item label="检验员">{{ baseData1.checkUserName }}</a-descriptions-item>

          <a-descriptions-item label="报告生成日期">{{ baseData1.createDate }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </div>
    <div class="padding">
      <div style="font-weight: bold; font-size: 16px">异常数据</div>

      <div style="float: right" v-if="rowData.isUpdate">
        <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)" v-if="rowData.isUpdate">
              <MinusCircleOutlined style="padding-bottom: 20px" />
            </a>
          </template>
          <template v-if="column.dataIndex === 'checkType'">
            <span v-if="record.checkType">
              <span v-for="(re, i) in record.checkType.split(',')" :key="i">
                <span v-for="item in checkTypeOptions" :key="item.key">
                  <span v-if="item.value == re">{{ i > 0 ? ',' : '' }}{{ item.label }}</span>
                </span>
              </span>
            </span>
          </template>
          <template v-if="column.dataIndex === 'checkProduct'">
            <span v-if="record.checkProduct">
              <span v-for="(re, i) in record.checkProduct.split(',')" :key="i">
                <span v-for="item in checkProductOptions" :key="item.key">
                  <span v-if="item.value == re">{{ i > 0 ? ',' : '' }}{{ item.label }}</span>
                </span>
              </span>
            </span>
          </template>
          <template v-if="column.dataIndex === 'imgFile'">
            <Upload
              v-model:value="record.imgFile"
              :disabled="true"
              listType="dragger"
              :style="{ display: 'none' }"
            />
          </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">
      <div style="font-weight: bold; font-size: 16px">异常报告填写</div>
      <a-form ref="formRef" :model="formData" :labelCol="{ span: 6 }">
        <a-row>
          <a-col :span="12">
            <a-form-item label="异常报告编号" name="errorNo" required>
              <a-input
                v-model:value="formData.errorNo"
                placeholder="请输入编码"
                :readonly="formData.isSysNum == '1'"
                :disabled="!rowData.isUpdate"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="" name="isSysNum">
              <a-checkbox
                :disabled="!rowData.isUpdate"
                v-model:checked="formData.isSysNumBoolean"
                @change="handleSysChange"
              >
                用系统编号
              </a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="异常等级" name="errorLevel" required>
              <a-select
                :options="errorLevelOptions"
                placeholder="请选择"
                v-model:value="formData.errorLevel"
                :disabled="!rowData.isUpdate"
                allowClear
              >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="处置判定" name="disJudge" required>
              <a-select
                :options="disJudgeOptions"
                placeholder="请选择"
                v-model:value="formData.disJudge"
                :disabled="!rowData.isUpdate"
                allowClear
              >
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="判定依据" name="judgeResult" required>
              <a-select
                :options="judgeResultOptions"
                placeholder="请选择"
                v-model:value="formData.judgeResult"
                :disabled="!rowData.isUpdate"
                allowClear
              >
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-col :span="24">
          <a-form-item label="问题描述" name="remark" :labelCol="{ span: 3 }">
            <a-textarea
              :disabled="!rowData.isUpdate"
              v-model:value="formData.remark"
              placeholder="请输入"
              :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 {
    getMesQbCheckstandardList,
    getDicItem,
    getAutoCode,
    getMesQbCheckTypeList,
    getMesQbCheckProductList,
    getMesFactoryListByType,
    getMesGoupList,
  } from '/@/api/mes/common';

  import { getMesQpProcessPatrol } from '/@/api/mes/mesprocesspartol';
  import {
    addMesQpProcessPatrolerror,
    getMesQpProcessPatrolerror,
    updateMesQpProcessPatrolerror,
  } from '/@/api/mes/mesprocessplerror';
  // 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;
    reportId: string;
    isUpdate: boolean;
    flag: string;
  } = reactive({
    id: '',
    reportId: '',
    isUpdate: false,
    flag: '',
  });
  const columnsProp = {
    mainKey: 'mesQpProcessPatrolerrordataList',
    columns: [
      { dataIndex: 'index', title: '#' },
      {
        key: 'b7723f6e394b41369b35e7175965fc50',
        title: '巡检ID',
        dataIndex: 'reportId',
        componentType: 'Input',
        defaultValue: '',
        show: false,
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'report_id',
          width: '100%',
          disabled: true,
          placeholder: '请输入巡检ID',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '43f851570253440ca916a6f2516cd8c1',
        title: '首件报告ID',
        dataIndex: 'firstId',
        componentType: 'Input',
        show: false,
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'first_id',
          width: '100%',
          disabled: true,
          placeholder: '请输入首件报告ID',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },

      {
        key: '60b7c8cbae9a4f0c922543e3b76b2664',
        title: '首件报告编号',
        dataIndex: 'firstNo',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'first_no',
          width: '100%',
          disabled: true,
          placeholder: '请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '094fc5760b9c42cd8440b2e5d1efa4a7',
        title: '销售订单ID',
        dataIndex: 'saleId',
        componentType: 'Input',
        show: false,
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'sale_id',
          width: '100%',
          disabled: true,
          placeholder: '请输入销售订单ID',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'f58efceec29d4f3e8c87f2d5925e8a7a',
        title: '销售订单号',
        dataIndex: 'saleNo',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'sale_no',
          width: '100%',
          disabled: true,
          placeholder: '请输入销售订单号',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '826263d5fb164656ba2ca610072452d3',
        title: '产品ID',
        dataIndex: 'productId',
        componentType: 'Input',
        show: false,
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'product_id',
          width: '100%',
          disabled: true,
          placeholder: '请输入产品ID',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'ff1c375829a6483e9eb9203d60694a13',
        title: '产品编码',
        dataIndex: 'productCode',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'product_code',
          width: '100%',
          disabled: true,
          placeholder: '请输入产品编码',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '674d4749aadb4680b45c4895901349fb',
        title: '产品名称',
        dataIndex: 'productName',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'product_name',
          width: '100%',
          disabled: true,
          placeholder: '请输入产品名称',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '41d62464c8a94f3987f123b46394353b',
        title: '物料ID',
        dataIndex: 'materialId',
        componentType: 'Input',
        show: false,
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'material_id',
          width: '100%',
          disabled: true,
          placeholder: '请输入物料ID',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'a2d6cd97d8c14b99a309e6598e5be51e',
        title: '物料编码',
        dataIndex: 'materialNo',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'material_no',
          width: '100%',
          disabled: true,
          placeholder: '请输入物料编码',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '9ffc74bf304941468f933d55f2394aa4',
        title: '物料名称',
        dataIndex: 'materialName',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'material_name',
          width: '100%',
          disabled: true,
          placeholder: '请输入物料名称',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'ae4f6134abe8481f9725638625e59021',
        title: '巡检时间',
        dataIndex: 'checkTime',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'check_time',
          width: '100%',
          disabled: true,
          placeholder: '巡检时间请输入',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '99ae6cc567284ff1a270f429d7925c74',
        title: '检测数量',
        dataIndex: 'checkNum',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'check_num',
          width: '100%',
          disabled: true,
          placeholder: '请输入检测数量',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '3203753edbab4ed0ae9573e5c52d1d9e',
        title: '单位',
        dataIndex: 'checkUnit',
        componentType: 'XjrSelect',
        componentProps: {
          width: '100%',
          span: '',
          placeholder: '请选择',
          showLabel: true,
          showSearch: false,
          clearable: false,
          disabled: true,
          staticOptions: [
            { key: 1, label: 'Option 1', value: 'Option 1' },
            { key: 2, label: 'Option 2', value: 'Option 2' },
            { key: 3, label: 'Option 3', value: 'Option 3' },
          ],
          defaultSelect: '',
          datasourceType: 'api',
          params: null,
          labelField: 'label',
          valueField: 'value',
          apiConfig: {
            path: '/mes/getUnlityList',
            method: 'GET',
            apiId: 'copy1691393740624d1194',
            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: 'name',
                    description: '回显字段',
                    required: false,
                    dataType: 'String',
                    type: null,
                    defaultValue: 'code',
                    validateType: null,
                    error: null,
                    expression: null,
                    children: null,
                    bindType: 'value',
                  },
                  {
                    name: 'queryType',
                    value: null,
                    description: '查询类型',
                    required: false,
                    dataType: 'String',
                    type: null,
                    defaultValue: null,
                    validateType: null,
                    error: null,
                    expression: null,
                    children: null,
                    bindType: '',
                  },
                ],
              },
              { key: '2', title: 'Header', tableInfo: [] },
              { key: '3', title: 'Body' },
            ],
            script:
              'var condition="";\r\n\r\nreturn db.select("SELECT t.id AS `key` ,t.id AS `value`,t.name AS label,t.* FROM case_erp_unit t where state=1 "+condition)',
          },
          dicOptions: [],
          required: false,
          rules: [],
          events: {},
          prestrainField: 'check_unit',
        },
      },
      {
        key: '527702f88cc94b8686f0881801c125a5',
        title: '检测类别',
        dataIndex: 'checkType',
        componentType: 'XjrSelect',
        componentProps: {
          mode: 'multiple', //多选
          width: '100%',
          span: '',
          placeholder: '请选择',
          showLabel: true,
          showSearch: false,
          clearable: false,
          disabled: true,
          staticOptions: [
            { key: 1, label: 'Option 1', value: 'Option 1' },
            { key: 2, label: 'Option 2', value: 'Option 2' },
            { key: 3, label: 'Option 3', value: 'Option 3' },
          ],
          defaultSelect: '',
          datasourceType: 'api',
          params: null,
          labelField: 'label',
          valueField: 'value',
          apiConfig: {
            path: '/mes/getCheckType',
            method: 'GET',
            apiId: '7142de95ccfe43cb8beffe37a62474d2',
            apiParams: [
              { key: '1', title: 'Query Params', tableInfo: [] },
              { key: '2', title: 'Header', tableInfo: [] },
              { key: '3', title: 'Body' },
            ],
            script:
              'var condition="";\r\n\r\nreturn db.select("SELECT id AS `key` ,id AS `value`,CONCAT(name) AS label FROM mes_qb_checktype where state=1 "+condition)',
          },
          dicOptions: [],
          required: false,
          rules: [],
          events: {},
          prestrainField: 'check_type',
        },
      },
      {
        key: '47c5c050a22e4b7b8e2d3605ae7eae4c',
        title: '检测项目',
        dataIndex: 'checkProduct',
        componentType: 'XjrSelect',
        componentProps: {
          width: '100%',
          span: '',
          placeholder: '请选择',
          showLabel: true,
          showSearch: false,
          clearable: false,
          disabled: true,
          staticOptions: [
            { key: 1, label: 'Option 1', value: 'Option 1' },
            { key: 2, label: 'Option 2', value: 'Option 2' },
            { key: 3, label: 'Option 3', value: 'Option 3' },
          ],
          defaultSelect: '',
          datasourceType: 'api',
          params: null,
          labelField: 'label',
          valueField: 'value',
          apiConfig: {
            path: '/mes/getCheckProduct',
            method: 'GET',
            apiId: '662ac83978054327a0947126993b1175',
            apiParams: [
              { key: '1', title: 'Query Params', tableInfo: [] },
              { key: '2', title: 'Header', tableInfo: [] },
              { key: '3', title: 'Body' },
            ],
            script:
              'var condition="";\r\n\r\nreturn db.select("SELECT id AS `key` ,id AS `value`,CONCAT(name) AS label FROM mes_qb_checkproduct where state=1 "+condition)',
          },
          dicOptions: [],
          required: false,
          rules: [],
          events: {},
          prestrainField: 'check_product',
        },
      },
      {
        key: '13d5009d8b8040cf8113b2f4d5c7cdd9',
        title: '基准值',
        dataIndex: 'referenceValue',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'reference_value',
          width: '100%',
          disabled: true,
          placeholder: '请输入基准值',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'da7504d643b3409bb3c6c05057416188',
        title: '上公差',
        dataIndex: 'upTolerance',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'up_tolerance',
          width: '100%',
          disabled: true,
          placeholder: '请输入上公差',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'f884783b1eae464aa23cb5dbeb1064e4',
        title: '下公差',
        dataIndex: 'downTolerance',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'down_tolerance',
          width: '100%',
          disabled: true,
          placeholder: '请输入下公差',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '901d01ab98ae47a59b35137ad3f92345',
        title: '测量值',
        dataIndex: 'measuredValue',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'measured_value',
          width: '100%',
          disabled: true,
          placeholder: '请输入测量值',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: 'b8d84f9eec1842e2be3147236e0661e0',
        title: '不良数',
        dataIndex: 'errorNum',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'error_num',
          width: '100%',
          disabled: true,
          placeholder: '请输入不良数',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '281ac1ecc4594aaba40f269794123354',
        title: '不良处理方式',
        dataIndex: 'errorMethod',
        componentType: 'Input',
        defaultValue: '',
        componentProps: {
          defaultValue: '',
          prefix: '',
          rules: [],
          suffix: '',
          addonAfter: '',
          showLabel: true,
          required: false,
          prestrainField: 'error_method',
          width: '100%',
          disabled: true,
          placeholder: '请输入不良处理方式',
          allowClear: false,
          addonBefore: '',
          events: {},
          span: '',
        },
      },
      {
        key: '1edfd32a3a51484ca0b2691b5ae0d38e',
        title: '附件',
        dataIndex: 'imgFile',
        componentType: 'Upload',
        componentProps: {
          api: uploadApi,
          defaultValue: [],
          multiple: false,
          maxSize: 5,
          showLabel: true,
          required: false,
          accept: '',
          maxNumber: 5,
          disabled: false,
          events: {},
          span: '',
        },
      },
      { title: '操作', key: 'action', fixed: 'right', width: '50px' },
    ],
    buttonName: '选择',
    showIndex: false,
    useSelectButton: true,
    useAddButton: false, //不显示新增按钮
    preloadType: 'api',
    showLabel: true,
    itemId: '',
    multipleHeads: [],
    dicOptions: [],
    showComponentBorder: true,
    apiConfig: {
      method: 'GET',
      script:
        'var condition="";\r\nif(report_id!=null&&report_id!=""){ \r\n    condition+=" and report_id=#{report_id}";\r\n}\r\nreturn db.select("SELECT t.id AS `key` ,t.id AS `value`,t.report_no AS label,t.* FROM mes_qp_process_patroldata t where 1=1  "+condition)',
      apiParams: [
        {
          tableInfo: [
            { dataType: 'String', bindType: '', required: false, name: 'keyword' },
            {
              defaultValue: 'code',
              dataType: 'String',
              bindType: '',
              description: '回显字段',
              required: false,
              name: 'showLabel',
              value: '',
            },
            {
              dataType: 'String',
              bindType: '',
              description: '查询类型',
              required: false,
              name: 'queryType',
            },
            {
              dataType: 'String',
              bindType: 'data',
              description: '报告ID',
              required: false,
              name: 'report_id',
              value: 'report_id',
            },
          ],
          title: 'Query Params',
          key: '1',
        },
        { tableInfo: [], title: 'Header', key: '2' },
        { title: 'Body', key: '3' },
      ],
      path: '/mes/getProcessPatroldataDetailList',
      outputParams: [
        { name: 'first_no', tableTitle: '首件报告编号' },
        { name: 'sale_no', tableTitle: '销售单号' },
        { name: 'material_no', tableTitle: '物料编码' },
        { name: 'material_name', tableTitle: '物料名称' },
        { name: 'check_num', tableTitle: '检测数量' },
        { name: 'error_num', tableTitle: '不良数' },
        { name: 'measured_value', tableTitle: '测量值' },
      ],
      apiId: 'copy1691477860530d90153',
    },
    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>({
    errorNo: '',
    isSysNum: '',
    isSysNumBoolean: true,
    disJudge: '',
    errorLevel: '',
    remark: '',
  });

  const emit = defineEmits(['cancel', 'success']);
  const formRef = ref();
  onMounted(async () => {
    rowData.id = props.rowData.id;
    rowData.reportId = props.rowData.reportId;
    rowData.isUpdate = props.rowData.isUpdate;
    rowData.flag = props.rowData.flag;
    console.log('row===', rowData);
    columnsProp.apiConfig.path =
      '/mes/getProcessPatroldataDetailList?report_id=' + rowData.reportId;
    getDetailData();

    errorLevelOptions.value = await getDicItem('1688808042268565506');
    disJudgeOptions.value = await getDicItem('1688808244736008194');
    judgeResultOptions.value = await getMesQbCheckstandardList();

    checkTypeOptions.value = await getMesQbCheckTypeList();
    checkProductOptions.value = await getMesQbCheckProductList();
    carIdOptions.value = await getMesFactoryListByType(1, '');
    lineIdOptions.value = await getMesFactoryListByType(2, '');
    groupOptions.value = await getMesGoupList();
  });

  watch(
    () => props.rowData,
    (val) => {
      console.log('监听', val);
      if (val) {
        rowData.id = props.rowData.id;
        rowData.reportId = props.rowData.reportId;
        rowData.isUpdate = props.rowData.isUpdate;
        rowData.flag = props.rowData.flag;
        getDetailData();
      }
    },
  );
  const errorLevelOptions = ref<any[]>([]);
  const judgeResultOptions = ref<any[]>([]);
  const disJudgeOptions = ref<any[]>([]);
  const checkProductOptions = ref<any[]>([]);
  const checkTypeOptions = ref<any[]>([]);
  const carIdOptions = ref<any[]>([]);
  const lineIdOptions = ref<any[]>([]);
  const groupOptions = ref<any[]>([]);

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

    if (formData.value.isSysNumBoolean) {
      formData.value.errorNo = await getAutoCode('mesProcessCkErrorNo');
      formData.value.isSysNum = '1';
    } else {
      formData.value.errorNo = '';
      formData.value.isSysNum = '';
    }
  }
  const getDetailData = async () => {
    const data = await getMesQpProcessPatrol(rowData.reportId);
    console.log(data, '上部分信息');
    baseData1.value = data;

    formData.value.reportId = data.id;
    formData.value.reportNo = data.reportNo;
    formData.value.reportName = data.reportName;
    if (!rowData.id) {
      formData.value.isSysNum = '1';
      handleSysChange();
    } else {
      const res = await getMesQpProcessPatrolerror(rowData.id);
      if (res) {
        formData.value.id = res.id;
        formData.value.reportId = res.reportId;
        formData.value.reportNo = res.reportNo;
        formData.value.reportName = res.reportName;
        formData.value.errorNo = res.errorNo;
        formData.value.isSysNum = res.isSysNum;
        formData.value.disJudge = res.disJudge;
        formData.value.errorLevel = res.errorLevel;
        formData.value.remark = res.remark;
        formData.value.judgeResult = res.judgeResult;
        detailList.value = res.mesQpProcessPatrolerrordataList;
        console.log('==========', formData.value, detailList.value);
      }
    }
  };
  async function handleSubmit() {
    try {
      let values = await formRef.value?.validate();
      values.reportId = formData.value.reportId;
      values.reportNo = formData.value.reportNo;
      values.reportName = formData.value.reportName;
      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.mesQpProcessPatrolerrordataList = detailList.value;
      console.log('values', values, formData.value, baseData1);

      // TODO custom api
      if (!values.id) {
        //false 新增
        await addMesQpProcessPatrolerror(values);
        notification.success({
          message: 'Tip',
          description: t('新增异常报告成功！'),
        }); //提示消息
      } else {
        await updateMesQpProcessPatrolerror(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>
