<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        v-loading="loading"
        row-key="code"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>

        <template v-slot:checkMethod="{ row }">
          {{ checkMethod(row.checkMethod) }}
        </template>
        <template v-slot:submitMethod="{ row }">
          {{ submitMethod(row.submitMethod) }}
        </template>
        <template v-slot:produceMethod="{ row }">
          {{ produceMethod(row.produceMethod) }}
        </template>
        <!-- 状态列 -->

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>

          <el-popconfirm
            class="ele-action"
            title="确定要删除当前工序吗？"
            @confirm="remove(row)"
          >
            <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>
    <!-- 编辑弹窗 -->
    <user-edit
      :visible.sync="showEdit"
      :checkMethodList="checkMethodList"
      :data="current"
      :produceMethodList="produceMethodList"
      :submitMethodList="submitMethodList"
      @done="reload"
      ref="userEdit"
    />
    <!-- 导入弹窗 -->
  </div>
</template>

<script>
  import UserSearch from './components/user-search.vue';
  import UserEdit from './components/user-edit.vue';

  import control from '@/api/technology/control';

  export default {
    name: 'technologyWork',
    components: {
      UserSearch,
      UserEdit
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            prop: 'code',
            label: '编码',

            showOverflowTooltip: true,
            align: 'center',

            minWidth: 110
          },
          {
            prop: 'name',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'checkMethod',
            label: '检验方式',
            slot: 'checkMethod',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'submitMethod',
            label: '报工方式',
            slot: 'submitMethod',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'produceMethod',
            label: '加工方式',
            slot: 'produceMethod',
            showOverflowTooltip: true,
            align: 'center'
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],

        // 检验枚举
        checkMethodList: [
          { label: '专检', value: 1 },
          { label: '非专检', value: 2 }
        ],
        //报工枚举
        submitMethodList: [
          { label: '必须报工', value: 1 },
          { label: '可选报工', value: 2 },
          { label: '不允许报工', value: 3 }
        ],
        //加工枚举
        produceMethodList: [
          { label: '不限制', value: 1 },
          { label: '厂内加工', value: 2 },
          { label: '委外加工', value: 3 }
        ],

        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false,
        loading: false
      };
    },

    methods: {
      /*回显工厂 */
      checkMethod(value) {
        let obj = this.checkMethodList.find((f) => f.value == value);
        return obj?.label;
      },
      /*回显类别 */
      submitMethod(value) {
        return this.submitMethodList.find((f) => f.value == value).label;
      },
      /*回显负责人 */
      produceMethod(value) {
        let obj = this.produceMethodList.find((f) => f.value == value);
        return obj?.label;
      },

      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        const res = await control.list({
          ...where,
          ...order,
          pageNum: page,
          size: limit
        });
        return res;
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
        this.$refs.userEdit.$refs.form &&
          this.$refs.userEdit.$refs.form.clearValidate();
      },
      /* 打开导入弹窗 */
      openImport() {
        this.showImport = true;
      },
      /* 删除 */
      remove(row) {
        // const loading = this.$loading({ lock: true });
        this.loading = true;

        control
          .delete([row.id])
          .then((msg) => {
            // loading.close();
            this.loading = false;
            this.$message.success('删除' + msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            // this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的工序吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            producetask
              .delete(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(() => {});
      }
    }
  };
</script>
