<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search :typeList="typeOptions" @search="reload"/>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        height="calc(100vh - 325px)"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('','add')"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            plain
          >
            删除
          </el-button>
<!--          <el-button-->
<!--            size="small"-->
<!--            type="primary"-->
<!--            icon="el-icon-upload2"-->
<!--            class="ele-btn-icon"-->
<!--            plain-->
<!--          >-->
<!--            导出-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="small"-->
<!--            type="primary"-->
<!--            icon="el-icon-download"-->
<!--            class="ele-btn-icon"-->
<!--            plain-->
<!--          >-->
<!--            导入-->
<!--          </el-button>-->
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            v-if="[0,3].includes(row.processStatus)"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row,'edit')"
          >
            修改
          </el-link>
          <el-link
            v-if="[1,2].includes(row.processStatus)"
            type="primary"
            :underline="false"
            icon="el-icon-setting"
            @click="openEdit(row, 'view')"
          >
            详情
          </el-link>
          <el-link
            v-if="[0,3].includes(row.processStatus)"
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="submit(row)">
            提交
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除当前资质吗？"
            v-if="[0,3].includes(row.processStatus)"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </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>
      </ele-pro-table>
    </el-card>
    <!-- 新增/编辑/详情弹窗 -->
    <add-or-edit-dialog
      :addOrEditDialogFlag.sync="addOrEditDialogFlag"
      v-if="addOrEditDialogFlag"
      ref="addOrEditDialogRef"
      :data="current"
      :controlList="controlList"
      @done="reload"
      :typeList="typeOptions"
    />
  </div>
</template>

<script>
import UserSearch from './components/user-search.vue';
import addOrEditDialog from './components/addOrEditDialog.vue';
import {
  contactQcSubmit,
  deleteProfessionCertificationById,
  getProfessionCertificationPageList
} from '@/api/qualification';
import control from '@/api/technology/control';
import dictMixins from '@/mixins/dictMixins';
import FileUpload from "@/components/upload/fileUpload.vue";
import {getFile} from "@/api/system/file";
import {reviewStatus} from "@/enum/dict";

export default {
  name: 'technologyProduction',
  components: {
    FileUpload,
    UserSearch,
    addOrEditDialog
  },
  mixins: [dictMixins],
  data() {
    return {
      levelOptions: [
        {
          label: '初级',
          value: '1'
        },
        {
          label: '中级',
          value: '2'
        },
        {
          label: '高级',
          value: '3'
        }
      ],
      isView: false,
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center'
        },
        {
          align: 'center',
          prop: 'code',
          label: '编码',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          slot: 'name',
          prop: 'name',
          label: '名称',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110
        },
        {
          slot: 'startTime',
          prop: 'startTime',
          label: '有效开始时间',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110
        },
        {
          slot: 'endTime',
          prop: 'endTime',
          label: '有效结束时间',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110
        },

        {
          label: '状态',
          prop: 'processStatus',
          slot: 'processStatus',
          align: "center",
          width: 120,
          formatter: (_row, _column, cellValue) => {
            return reviewStatus[_row.processStatus];
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 260,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      addOrEditDialogFlag: false,
      // 是否显示参数弹窗
      showSetting: false,
      controlList: [],
      sampleShow: false,
      taskId: null
    };
  },
  created() {
    this.requestDict('工种类型');
  },
  computed: {
    typeOptions() {
      return this.dict[this.dictEnum['工种类型']];
    }
  },
  methods: {
    /*配置工艺参数 */
    openSetting(row) {
      this.current = row;
      this.showSetting = true;
    },
    /* 表格数据源 */
    async datasource({page, limit, where, order}) {
      return await getProfessionCertificationPageList({
        ...where,
        ...order,
        pageNum: page,
        size: limit
      });
    },
    downloadFile(file) {
      getFile({objectName: file.storePath}, file.name);
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({page: 1, where: where});
    },
    /* 打开编辑弹窗 */
    openEdit(row = {}, type) {
      this.addOrEditDialogFlag = true
      this.$nextTick(() => {
        this.$refs.addOrEditDialogRef.init(type,{...row} )
      })
      // this.isView = !!isView;
      // this.getControlList();
      // this.current = row;
      // this.showEdit = true;
      // this.$refs.userEdit.$refs.form &&
      //   this.$refs.userEdit.$refs.form.clearValidate();
    },

    getControlList() {
      const params = {
        pageNum: 1,
        size: -1
      };
      control.list().then((res) => {
        this.controlList = res.list;
      });
    },

    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});

      deleteProfessionCertificationById([row.id])
        .then((msg) => {
          loading.close();
          this.$message.success('删除' + msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          // this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      if (this.selection.length === 0) return;
      let flag = this.selection.some(item => [1, 2].includes(item.processStatus))
      if (flag) return this.$message.warning('抱歉已审核、审核中的数据不能删除,请检查')
      this.$confirm('确定要删除选中的资质吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({lock: true});
          deleteProfessionCertificationById(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success('删除' + msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              // this.$message.error(e.message);
            });
        })
        .catch(() => {
        });
    },
//开启资质提交流程
    async submit(row) {
      await contactQcSubmit({businessId: row.id,certificationType:row.certificationType})
      this.$message.success('操作成功')
      this.reload()
    },
    sampleParam(row) {
      this.taskId = row.id;
      this.sampleShow = true;
    },

    close(done) {
      this.sampleShow = false;
    }
  }
};
</script>
