<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"
        row-key="id"
        :page-size="this.$store.state.tablePageSize"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
            v-if="$hasPermission('main:categoryparam:save')"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="$hasPermission('main:categoryparam:delete')"
            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
            v-if="$hasPermission('main:stepManagement:update')"
            @click="uploadFile()"
          >
            导入
          </el-button>
        </template>
        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit(row, true)"
            >{{ row.code }}</el-link
          >
        </template>
        <template v-slot:name="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit(row, true)"
            >{{ row.name }}</el-link
          >
        </template>
        <!-- <template v-slot:name="{ row }">
          <span> {{ row.name }}</span>
        </template> -->

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="$hasPermission('main:stepManagement:update')"
          >
            修改
          </el-link>
          <!-- <el-link
            type="primary"
            :underline="false"
            icon="el-icon-setting"
            @click="openEdit(row, true)"
          >
            详情
          </el-link> -->

          <el-popconfirm
            class="ele-action"
            title="确定要删除当前工步吗？"
            @confirm="remove(row)"
            v-if="$hasPermission('main:stepManagement:delete')"
          >
            <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"
      :data="current"
      :controlList="controlList"
      :isView="isView"
      @done="reload"
      ref="userEdit"
      :typeList="typeList"
    />
    <SampleParam
      v-if="sampleShow"
      :taskId="taskId"
      @close="close"
    ></SampleParam>

    <importDialog
      ref="importDialogRef"
      apiUrl="/main/producestep/importExcel"
      fileUrl="/main/producestep/downLoadTemplate"
      fileName="工艺_工步导入模板"
      @success="reload"
    />
  </div>
</template>

<script>
  import tabMixins from '@/mixins/tableColumnsMixin';
  import UserSearch from './components/user-search.vue';
  import UserEdit from './components/user-edit.vue';
  import UserSetting from './components/user-setting.vue';
  import SampleParam from './components/sampleParam.vue';
  import stepManagement from '@/api/technology/stepManagement';
  import control from '@/api/technology/control';
  import importDialog from '@/components/upload/comm-dialog.vue';

  export default {
    name: 'technologyProduction',
    mixins: [tabMixins],
    components: {
      UserSearch,
      UserEdit,
      UserSetting,
      SampleParam,
      importDialog
    },
    data() {
      return {
        moduleName: 'mainProduceStep',
        // 表格列配置
        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: 'code'
          },
          {
            slot: 'name',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            label: '内容',
            prop: 'defaultValue',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 330
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 50,
            formatter: (_row) => {
              return ['生效', '失效'][_row.status];
            }
          },

          {
            prop: 'sort',
            label: '排序',
            // sortable: 'custom',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 40
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 260,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示参数弹窗
        showSetting: false,
        controlList: [],
        typeList: [
          {
            value: 1,
            label: '普通工步'
          },
          {
            value: 2,
            label: '抽样质检工步'
          },
          {
            value: 3,
            label: '普通质检工步'
          },
          {
            value: 4,
            label: '包装工步'
          },
          {
            value: 5,
            label: '入库工步'
          },
          {
            value: 6,
            label: '一工一检(常规质检)'
          }
        ],
        isView: false,
        sampleShow: false,
        taskId: null,
        cacheKeyUrl: '8aeb1092-technology-stepManagement'
      };
    },
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        const res = await stepManagement.list({
          ...where,
          ...order,
          pageNum: page,
          size: limit
        });
        return res;
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row, isView) {
        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 });

        stepManagement
          .delete([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;
        }
        this.$confirm('确定要删除选中的工步吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            stepManagement
              .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(() => {});
      },

      sampleParam(row) {
        this.taskId = row.id;
        this.sampleShow = true;
      },

      close(done) {
        this.sampleShow = false;
      },
      uploadFile() {
        this.$refs.importDialogRef.open();
      }
    }
  };
</script>
