<template>
  <ele-modal
    title="合格证列表"
    :visible.sync="visible"
    width="90%"
    append-to-body
  >
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <search @search="reload" />
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :columns="columns"
          :datasource="datasource"
          row-key="code"
          :page-size="20"
          @columns-change="handleColumnChange"
          :cache-key="cacheKeyUrl"
          :selection.sync="selection"
          :needPage="false"
          height="500px"
          full-height="calc(100vh - 200px)"
        >
          <template v-slot:type="{ row }">{{
            getDictValue('质检计划类型', row.type)
          }}</template>
        </ele-pro-table>
      </el-card>
    </div>
    <div slot="footer">
      <el-button type="primary" @click="save()"> 确认 </el-button>
      <el-button @click="visible = false">关闭</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import search from './search.vue';

  import tabMixins from '@/mixins/tableColumnsMixin';
  import { getList } from '@/api/certificateManagement';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    components: {
      search
    },
    mixins: [dictMixins, tabMixins],
    data() {
      return {
        cacheKeyUrl: 'qsm-c2e9664a-certificateManagement',
        selection: [],
        orderTypeList: [
          {
            id: 0,
            label: '库存性订单'
          },
          {
            id: 1,
            label: '生产性订单'
          },
          {
            id: 2,
            label: '无客户生产性订单'
          },
          {
            id: 4,
            label: '不定向订单'
          }
        ],
        visible: false,
        // 表格列配置
        columns: []
      };
    },
    created() {
      this.requestDict('质检计划类型');
      this.requestDict('取样类型');
    },

    methods: {
      open() {
        this.visible = true;
        this.setColumns();
      },

      duplicateChecking() {
        let is = false;
        let key = ['type', 'batchNo', 'produceTaskName', 'orderNo'];
        key.forEach((_key) => {
          if (
            this.selection.some((item) => item[_key] != this.selection[0][_key])
          ) {
            is = true;
          }
        });
        //  if(){

        //  }
        return is;
      },
      save() {
        if (!this.selection.length) {
          this.$message.error('请选择合格证');
          return;
        }

        if (this.selection.length > 1 && this.duplicateChecking()) {
          this.$message.warning(
            '合格证类型、批次号、工序、订单号必须保持一致！'
          );
          return;
        }

        this.visible = false;
        let data = this.selection[0];

        this.$emit('success', {
          batchNo: data.batchNo,
          certificateId: this.selection.map((item) => item.id).toString(),
          contractor: data.contractor,
          generateType: 2,
          isMerged: 1,
          luxuryProductCode: data.luxuryProductCode,
          luxuryProductName: data.luxuryProductName,
          orderNo: data.orderNo,
          orderType: data.orderType,
          productCode: data.productCode,
          productName: data.productName,
          processingType: data.processingType,
          produceTaskCode: data.produceTaskCode,
          produceTaskId: data.produceTaskId,
          produceTaskName: data.produceTaskName,
          qualityWorkOrderId: data.qualityWorkOrderId,
          // reportTemplateId: data.reportTemplateId,
          // reportTemplateCode: data.reportTemplateCode,
          // reportTemplateName: data.reportTemplateName,
          type: data.type,
          workAme: data.workAme,
          qualifiedQuantity: this.selection.reduce(
            (total, item) => total + item.qualifiedQuantity,
            0
          ),
          quantity: this.selection.reduce(
            (total, item) => total + item.quantity,
            0
          )
        });
      },
      setColumns() {
        this.columns = [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            prop: 'code',
            slot: 'code',
            label: '合格证号',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 130
          },
          {
            prop: 'quantity',
            label: '交验数量',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 100
          },
          {
            prop: 'qualifiedQuantity',
            label: '合格数量',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'type',
            label: '类型',
            slot: 'type',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },

          // {
          //   prop: 'remark',
          //   minWidth: 180,
          //   label: '重要记事',
          //   align: 'center',
          //   showOverflowTooltip: true
          // },
          {
            prop: 'produceTaskName',
            minWidth: 110,
            label: '工序名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'produceTaskCode',
            minWidth: 110,
            label: '工序编号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            minWidth: 110,
            label: '批次号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'orderNo',
            label: '订单号',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 140
          },
          {
            prop: 'luxuryProductCode',
            minWidth: 110,
            label: '顶级产品编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'luxuryProductName',
            minWidth: 110,
            label: '顶级产品名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productCode',
            minWidth: 110,
            label: '编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productName',
            minWidth: 110,
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },
          // {
          //   prop: 'executeDeptName',
          //   label: '下发数量',
          //   showOverflowTooltip: true,
          //   align: 'center',
          //   minWidth: 110
          // },

          {
            prop: 'measureUnit',
            minWidth: 110,
            label: '计量单位',
            align: 'center',
            showOverflowTooltip: true
          },

             {
            prop: 'processingType',
            minWidth: 110,
            label: '加工类型',
            align: 'center',
            formatter: (row) => {
              return row.processingType == 2
                ? '加工'
                : row.processingType == 3
                ? '装配'
                : '';
            },
            showOverflowTooltip: true
          },
          {
            prop: 'workAme',
            minWidth: 110,
            label: '作业名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'contractor',
            minWidth: 110,
            label: '承制单位',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'orderType',
            minWidth: 110,
            label: '订单类型',
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              let obj = this.orderTypeList.find(
                (el) => el.id == _row.orderType
              );
              return obj ? obj.label : '';
            },
            showOverflowTooltip: true
          },

          {
            prop: 'createUserName',
            minWidth: 110,
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'createTime',
            minWidth: 110,
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'approvalUserName',
            minWidth: 110,
            label: '审批人',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'approvalTime',
            minWidth: 110,
            label: '审批时间',
            align: 'center',
            showOverflowTooltip: true
          }
        ].filter((el) => !el.isNone);
      },
      /*回显类型 */

      /* 表格数据源 */
      datasource({ page, where, limit }) {
        return getList({
          ...where,
          generateType: 1,
          isMerged: 0,
          pageNum: 1,
          size: 999
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      }
    }
  };
</script>
