<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: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"
            icon="el-icon-edit"
            @click="openEdit('detail', row)"
            v-if="row.status == 1"
          >
            详情
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', row)"
            v-if="row.status == 0"
          >
            修改
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            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="done"></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: [
        {
          prop: 'code',
          label: '编码',
          width: 160
        },
        {
          label: '名称',
          prop: 'name'
        },
        // {
        //   prop: 'batchNo',
        //   label: '批号'
        // },

        {
          label: '类型',
          prop: 'type',
          slot: 'type',
          width: 120

        },
        {
          label: '质检方式',
          prop: 'qualityMode',
          slot: 'qualityMode',
          width: 120

        },
        {
          label: '计划来源编码',
          prop: 'planSourceCode'
        },
        {
          label: '自动派单',
          prop: 'autoOrder',
          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'
        },
        {
          label: '执行人',
          prop: 'executeName',
          width: 80
        },
        {
          label: '计划开始时间',
          prop: 'planStartTime'
        },
        {
          label: '计划结束时间',
          prop: 'planEndTime'
        },
        {
          label: '附件',
          prop: 'accessory',
          slot: 'accessory'
        },
        {
          label: '状态',
          prop: 'status',
          width: 70,
          formatter: (row, column, cellValue) => {
            return cellValue == 1 ? '已下发' : '未下发';
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 220,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ]
    };
  },
  created() {
    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) {
      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.done();
        })
        .catch((e) => {});
    },
    done() {
      this.$refs.search.search();
    }
  }
};
</script>
  