<template>
  <ele-modal
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="true"
    :close-on-press-escape="false"
    :maxable="true"
    append-to-body
    width="90%"
    title="质检记录表"
  >
    <div class="ele-body">
      <el-card shadow="never">
        <!-- <search ref="search" @search="search"></search> -->
        <!-- <seek-page :seekList="seekList" @search="search"></seek-page> -->

        <ele-pro-table
          v-if="visible"
          ref="table"
          :columns="columns"
          :datasource="datasource"
          :pageSize="20"
          :pageSizes="[20, 30, 40, 50, 100]"
        >
          <!-- <ele-pro-table
          ref="table"
          :columns="columns"
          :datasource="sourceList"
          :pageSize="20"
          :pageSizes="[20, 30, 40, 50, 100]"
        > -->
          <!-- 表头工具栏 -->
          <!--        <template v-slot:toolbar>-->
          <!--          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit('add')">-->
          <!--            添加-->
          <!--          </el-button>-->
          <!--        </template>-->

          <template v-slot:code="{ row }">
            <el-link
              type="primary"
              :underline="false"
              @click="openEdit('detail', row)"
              >{{ row.code }}</el-link
            >
          </template>

          <!--        <template v-slot:qualityPlanName="{ row }">-->
          <!--          <div v-if="row.qualityPlanId"> {{ row.qualityPlanName }}</div>-->
          <!--          <div v-else><el-tag size="mini">自建</el-tag></div>-->
          <!--        </template>-->
          <template v-slot:sourceCode="{ row }">
            <div v-if="row.qualityType == 1">{{ row.qualityPlanCode }}</div>
            <div v-if="row.qualityType == 2">{{ row.workOrderCode }}</div>
          </template>

          <template v-slot:files="scope">
            <el-link
              v-for="link in scope.row.files"
              :key="link.id"
              type="primary"
              :underline="false"
              @click="downloadFile(link)"
              >{{ link.name }}</el-link
            >
          </template>

          <template v-slot:qualityType="{ row }">{{
            getDictValue('质检计划类型', row.qualityType)
          }}</template>
          <template v-slot:qualityMode="{ row }">{{
            getDictValue('取样类型', row.qualityMode)
          }}</template>
          <template v-slot:accessory="scope">
            <el-link
              v-for="link in scope.row.accessory"
              :key="link.id"
              type="primary"
              :underline="false"
              @click="downloadFile(link)"
              >{{ link.name }}</el-link
            >
          </template>
          <!-- 操作列 -->
          <template v-slot:action="{ row }">
            <el-link
              type="primary"
              :underline="false"
              v-if="row.status == 0"
              @click="openEdit('edit', row)"
              >报工</el-link
            >
            <el-link type="primary" :underline="false" @click="openNumber(row)"
              >合格证</el-link
            >
            <jimureportBrowse
              style="display: inline-block; margin-left: 5px"
              v-if="row.status == 1"
              text="质检报告"
              :businessId="row.id"
              businessCode="qmsqualityinspectionprint"
            ></jimureportBrowse>
            <!-- <el-link type="primary" :underline="false">
            质检报告
          </el-link>-->
            <!-- <el-popconfirm class="ele-action" title="确定要删除吗？" @confirm="remove(row)" v-if="row.status == 0">
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>-->
          </template>
        </ele-pro-table>
        <!--  -->
        <!-- <el-dialog title="修改" :visible.sync="certificateVisible" width="25%" :before-close="handleClose">
        <el-form :inline="true" :model="formData" class="demo-form-inline">
          <el-form-item label="合格证号">
            <el-input v-model="formData.certificateNumber" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </el-dialog>-->
        <!--  -->
      </el-card>
      <Certificate
        @search="search"
        v-if="addOpen"
        :rowData="rowData"
        :addOpen="addOpen"
        @closeModel="closeModel"
      />

      <edit ref="editRef" @close="close" />
    </div>
  </ele-modal>
</template>
<script>
  import Certificate from './components/Certificate.vue';
  import search from './components/search.vue';
  import edit from './edit.vue';
  import jimureportBrowse from '@/components/jimureport/browseModal.vue';
  import {
    getList,
    removeItem,
    updateCertificateNumber,
    verificationQualityInspector
  } from '@/api/inspectionWork';
  import dictMixins from '@/mixins/dictMixins';
  import { getFile } from '@/api/system/file';
  import { getByCode } from '@/api/system/dictionary-data';

  export default {
    mixins: [dictMixins],
    components: {
      search,
      jimureportBrowse,
      Certificate,
      edit
    },
    data() {
      return {
        columns: [
          {
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号',
            width: 55,
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '质检工单编码',
            slot: 'code',
            align: 'center',
            width: 180,
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'name',
            slot: 'name',
            width: 120,
            label: '质检工单名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            label: '来源单号',
            prop: 'sourceCode',
            slot: 'sourceCode',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            label: '类型',
            prop: 'qualityType',
            slot: 'qualityType',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            label: '质检方式',
            prop: 'qualityMode',
            slot: 'qualityMode',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            prop: 'qualityName',
            label: '质检人',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            prop: 'qualityTime',
            label: '质检时间',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },

          {
            prop: 'productCode',
            width: 120,
            label: '编码',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'productName',
            width: 120,
            label: '名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'produceTaskName',
            label: '工序',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            prop: 'total',
            label: '总数量',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'qualifiedNumber',
            label: '合格数',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'qualificationRate',
            label: '合格率',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'noQualifiedNumber',
            label: '不合格数',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'noQualificationRate',
            label: '不合格率',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'hours',
            label: '工时',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            label: '附件',
            prop: 'accessory',
            align: 'center',
            slot: 'accessory',
            showOverflowTooltip: true
          },
          {
            label: '创建时间',
            prop: 'createTime',
            align: 'center',
            width: 160
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            width: 80,
            formatter: (row, column, cellValue) => {
              return cellValue == 0 ? '未报工' : cellValue == 1 ? '已报工' : '';
            },
            fixed: 'right'
          },
          {
            columnKey: 'action',
            label: '操作',
            align: 'center',
            width: 200,
            resizable: false,
            slot: 'action',
            fixed: 'right'
          }
        ],
        formData: {
          certificateNumber: ''
        },
        // certificateVisible: false,
        rowData: {},
        typeList: [], //类型列表
        qualityMode: [], //取样类型
        statusList: [
          {
            value: 0,
            label: '未报工'
          },
          {
            value: 1,
            label: '已报工'
          }
        ],
        addOpen: false,
        visible: false,
        dataWork: {},
        sourceList: [],
        listId: []
      };
    },
    created() {
      this.requestDict('质检计划类型');
      this.requestDict('不良品处理类型');
      this.requestDict('取样类型');
      this.getTnspectionPlanType();
      this.getQualityMethodCode();
    },
    computed: {
      seekList() {
        return [
          {
            label: '工单编码：',
            value: 'code',
            type: 'input',
            placeholder: ''
          },
          {
            label: '工单名称：',
            value: 'name',
            type: 'input',
            placeholder: ''
          },
          {
            label: '来源单号：',
            value: 'sourceCode',
            type: 'input',
            placeholder: ''
          },
          {
            label: '类型：',
            value: 'qualityType',
            type: 'select',
            placeholder: '',
            planList: this.typeList
          },
          {
            label: '质检方式：',
            value: 'qualityMode',
            type: 'select',
            placeholder: '',
            planList: this.qualityMode
          },
          {
            label: '编码：',
            value: 'productCode',
            type: 'input',
            placeholder: ''
          },
          {
            label: '名称：',
            value: 'productName',
            type: 'input',
            placeholder: ''
          },
          {
            label: '状态：',
            value: 'status',
            type: 'select',
            placeholder: '',
            planList: this.statusList
          }
        ];
      }
    },
    methods: {
      // datasource({ page, where, limit }) {
      //   // return getList({
      //   //   ...where,
      //   //   pageNum: page,
      //   //   size: limit
      //   // });
      //   return getList({
      //     // ...where,
      //     produceSourceTaskId: this.dataWork.taskId,
      //     sourceCode: this.dataWork.data.code,
      //     pageNum: page,
      //     size: limit
      //   });
      // },

      async datasource({ page, limit }) {
        const res = await getList({
          produceSourceTaskId: this.dataWork.taskId,
          workOrderCodeList: this.listId,
          pageNum: page,
          size: limit
        });

        return res;
      },

      search(where) {
        console.log(1111);
        this.$refs.table.reload({
          limit: 20,
          page: 1
        });
      },
      open(req) {
        this.listId = [];
        this.visible = true;
        this.dataWork = req;
        this.dataWork.data.forEach((item) => {
          this.listId.push(item.code);
        });
      },

      async openEdit(type, row) {
        if (type == 'edit') {
          const code = await verificationQualityInspector(row.id);
          if (code == '-1') {
            console.log('不是报工人');
            return;
          }
        }

        const req = {
          workOrderId: row.id,
          data: row,
          type
        };

        this.$refs.editRef.open(req);
      },
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      remove(row) {
        removeItem([row.id])
          .then((message) => {
            this.$message.success(message);
            this.done();
          })
          .catch((e) => {});
      },
      done() {
        this.datasource({
          page: 1,
          limit: 20
        });
      },
      openNumber(row) {
        this.formData.certificateNumber = row.certificateNumber;
        this.rowData = row;
        this.addOpen = true;
        // this.certificateVisible = true
      },
      closeModel() {
        this.addOpen = false;
      },

      handleClose() {
        this.visible = false;
        this.$emit('close', false);
      },

      close() {
        console.log('关闭报工界面');
        this.done();
      },

      async getTnspectionPlanType() {
        let res = await getByCode('inspection_plan_type');
        if (res?.code == 0) {
          let list = res.data.map((item) => {
            let key = Object.keys(item)[0];
            return { value: key, label: item[key] };
          });
          this.typeList = list;
        }
      },

      async getQualityMethodCode() {
        let res = await getByCode('quality_method_code');
        if (res?.code == 0) {
          let list = res.data.map((item) => {
            let key = Object.keys(item)[0];
            return { value: key, label: item[key] };
          });
          this.qualityMode = list;
        }
      }
    }
  };
</script>
