<template>
  <div class="ele-body">
    <el-card shadow="never">
      <search ref="search" @search="reload"></search>
      <ele-pro-table ref="tableRef" :columns="columns" :datasource="datasource" :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:type="{ row }">
          {{ getDictValue('质检计划类型', row.type) }}
        </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" @click="openEdit('edit', row)" v-if="row.status == 0">
            修改
          </el-link>
          <el-link type="primary" :underline="false" v-if="row.status == 0" @click="openEdit('issued', 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>
    <edit ref="edit" @done="reload"></edit>
  </div>
</template>
<script>
import search from './components/search.vue';
import edit from './components/edit.vue';
import { getList, removeItem } from '@/api/inspectionPlan';
import dictMixins from '@/mixins/dictMixins';
import { getFile } from '@/api/system/file';

export default {
  mixins: [dictMixins],
  components: {
    search,
    edit
  },
  data() {
    return {
      columns: [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },

        {
          prop: 'code',
          slot: 'code',
          label: '质检计划编码',
          width: 160,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          label: '质检计划名称',
          prop: 'name',
          width: 160,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          label: '来源单据编码',
          prop: 'planSourceCode',
          width: 160,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'productCode',
          width: 120,
          label: '编码',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'productName',
          width: 120,
          label: '名称',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'batchNo',
          label: '批次号',
          width: 160,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          label: '类型',
          prop: 'type',
          slot: 'type',
          width: 120,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          label: '检验方式',
          prop: 'qualityMode',
          slot: 'qualityMode',
          width: 120,
          align: 'center',
          showOverflowTooltip: true
        },
        {
          label: '自动派单',
          prop: 'autoOrder',
          align: 'center',
          showOverflowTooltip: true,
          formatter: (row, column, cellValue) => {
            return cellValue == 1 ? '是' : '否';
          }
        },
        // {
        //   prop: 'productName',
        //   label: '产品名称',
        // },
        // {
        //   prop: 'productCode',
        //   label: '产品编码',
        // },
        // {
        //   prop: 'specification',
        //   label: '规格',
        // },
        // {
        //   prop: 'brandNo',
        //   label: '牌号',
        // },

        // {
        //   prop: 'productNumber',
        //   label: '数量',
        //   width: 60
        // },

        {
          label: '执行部门',
          prop: 'groupName',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          label: '执行人',
          prop: 'executeName',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          label: '开始时间',
          prop: 'planStartTime',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        {
          label: '结束时间',
          prop: 'planEndTime',
          align: 'center',
          width: 120,
          showOverflowTooltip: true
        },
        // {
        //   label: '附件',
        //   prop: 'accessory',
        //   slot: 'accessory',
        //   align: 'center',
        //   width: 120,
        //   showOverflowTooltip: true
        // },
        {
          label: '状态',
          prop: 'status',
          width: 70,
          align: 'center',
          showOverflowTooltip: true,
          formatter: (row, column, cellValue) => {
            return cellValue == 1 ? '已下发' : '未下发';
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 160,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true,
          fixed: 'right'
        }
      ]
    };
  },
  created() {
    this.requestDict('质检计划类型');
    this.requestDict('取样类型');
  },
  methods: {
    // datasource({ page, limit, where }) {
    //   return getList({
    //     ...where,
    //     pageNum: page,
    //     size: limit
    //   });
    // },
    datasource({ page, limit, where }) {
      return getList({
        pageNum: page,
        size: limit,
        ...where
      });
    },
    search() {
      this.reload();
    },
    openEdit(type, row) {
      this.$refs.edit.open(type, row);
    },
    downloadFile(file) {
      getFile({ objectName: file.storePath }, file.name);
    },
    remove(row) {
      removeItem([row.id])
        .then((message) => {
          this.$message.success(message);
          this.reload();
        })
        .catch((e) => { });
    },
    reload(where) {
      this.$refs.tableRef.reload({ page: 1, where });
    }
  }
};
</script>
