<template>
  <div class="ele-body">
    <el-card shadow="never">
      <search ref="search" @search="search"></search>
      <ele-pro-table ref="table" :columns="columns" :datasource="datasource">
        <!-- 表头工具栏 -->
        <!--        <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: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-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-card>
  </div>
</template>
<script>
import search from './components/search.vue';

import { getList, removeItem } from '@/api/inspectionWork';
import dictMixins from '@/mixins/dictMixins';
import { getFile } from '@/api/system/file';

export default {
  mixins: [dictMixins],
  components: {
    search
  },
  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'
        },
        {
          label: '来源生产工单号',
          prop: 'workOrderCode',
          align: 'center',
          width: 160,
          showOverflowTooltip: true
        },
        {
          prop: 'batchNo',
          label: '批次号',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'qualityPlanName',
          slot: 'qualityPlanName',
          width: 120,
          label: '质检计划名称',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: 'productName',
          width: 120,
          label: '名称',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'productCode',
          width: 120,
          label: '编码',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'specification',
          label: '规格',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'brandNo',
          label: '牌号',
          align: 'center',
          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: '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
        },
        {
          prop: 'qualityTime',
          label: '质检时间',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          prop: 'qualityName',
          label: '质检人',
          align: 'center',
          width: 120,
          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: 120,
          resizable: false,
          slot: 'action',
          fixed: 'right'
        }
      ]
    };
  },
  created() {
    this.requestDict('质检计划类型');
    this.requestDict('不良品处理类型');
    this.requestDict('取样类型');
  },
  methods: {
    datasource({ page, where, limit }) {
      return getList({
        ...where,
        pageNum: page,
        size: limit
      });
    },
    search(where) {
      this.$refs.table.reload({
        where: where,
        page: 1
      });
    },
    openEdit(type, row) {
      let id = '';
      let qualityType = '';
      if (type != 'add') {
        id = row.id;
        qualityType = row.qualityType;

      }
      // this.$refs.edit.open(type, row);
      this.$router.push({
        path: '/inspectionWork/edit',
        query: {
          type: type,
          id: id || '',
          qualityType: qualityType
        }
      });
    },
    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.$refs.search.search();
    }
  }
};
</script>
