<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="code"
        :page-size="20"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>

          <el-button
            type="primary"
            size="mini"
            icon="el-icon-upload2"
            plain
            @click="uploadFile"
            >导入</el-button
          >
        </template>

        <template v-slot:maxValue="{ row }">
          {{ row.maxValue }} <span v-if="row.maxValue">{{ row.unitName }}</span>
        </template>

        <template v-slot:minValue="{ row }">
          {{ row.minValue }} <span v-if="row.minValue">{{ row.unitName }}</span>
        </template>

        <template v-slot:defaultValue="{ row }">
          <span v-if="row.defaultValue">{{ row.symbol }}</span>
          {{ row.defaultValue }}
        </template>

        <template v-slot:textType="{ row }">
          {{
            row.textType == 1
              ? '数值'
              : row.textType == 2
              ? '选择'
              : row.textType == 3
              ? '上下限'
              : row.textType == 4
              ? '规格'
              : row.textType == 5
              ? '时间'
              : row.textType == 6
              ? '范围'
              : row.textType == 7
              ? '文本'
              : ''
          }}
        </template>

        <template v-slot:type="{ row }">
          {{ getDictValue('质检标准类型', row.qualityStandardType) }}
        </template>

        <template v-slot:toolList="{ row }">
          <div
            style="display: inline-block"
            v-for="(item, idx) in row.toolList"
            :key="idx"
            >{{ item.name }}
            <span v-if="row.toolList && idx != row.toolList.length - 1"
              >,
            </span>
          </div>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-popconfirm
            class="ele-action"
            title="确定要复制当前质检吗？"
            @confirm="copy(row)"
          >
            <template v-slot:reference>
              <el-link type="primary" :underline="false" icon="el-icon-tickets">
                复制
              </el-link>
            </template>
          </el-popconfirm>
          <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"
      :data="current"
      @done="reload"
      ref="userEdit"
    />
    <!-- 导入弹窗 -->

    <importDialog
      ref="importDialogRef"
      @success="reload"
      :fileUrl="'/qms/static/质检项导入模板.xlsx'"
      fileName="质检项导入模板"
      apiUrl="/qms/inspectionProject/importFile"
    ></importDialog>
  </div>
</template>

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

  import { getList, removeItem, copyItem } from '@/api/inspectionProject';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    name: 'inspectionProject',
    components: {
      UserSearch,
      UserEdit,
      importDialog
    },
    mixins: [dictMixins],
    data() {
      return {
        // 表格列配置
        columns: [
          {
            prop: 'inspectionCode',
            label: '参数编码',
            align: 'center',
            minWidth: 130
          },
          {
            prop: 'inspectionName',
            label: '参数名称',

            align: 'center',
            minWidth: 110
          },

          {
            prop: 'textType',
            label: '参数类型',

            align: 'center',
            slot: 'textType',
            minWidth: 110
          },

          {
            prop: 'defaultValue',
            slot: 'defaultValue',
            label: '默认值',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'maxValue',
            slot: 'maxValue',
            label: '参数上限',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'minValue',
            slot: 'minValue',
            label: '参数下限',
            align: 'center',
            showOverflowTooltip: true
          },
          // {
          //   label: '工艺要求',
          //   prop: 'inspectionStandard',
          //   formatter: (row, column, cellValue) => {
          //     return row.symbol + ' ' + cellValue + ' ' + row.unit;
          //   },
          //   minWidth: 150
          // },

          {
            label: '标准类型',
            prop: 'type',
            slot: 'type'
          },
          {
            prop: 'unitName',
            label: '参数单位',
            align: 'center'
          },
          {
            prop: 'qualityStandardName',
            label: '标准名称',
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'standardCode',
            label: '标准代码',
            align: 'center',
            minWidth: 110
          },

          {
            prop: 'toolList',
            slot: 'toolList',
            label: '工具名称',
            align: 'center',
            minWidth: 150
          },

          {
            label: '状态',
            prop: 'status',
            formatter: (row, column, cellValue) => {
              return cellValue == 1 ? '启用' : cellValue === 0 ? '停用' : '';
            }
          },

          {
            label: '备注',
            prop: 'inspectionRemark'
          },

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

        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false,
        moudleName: ''
      };
    },
    methods: {
      /*回显类型 */

      /* 表格数据源 */
      datasource({ page, where, limit }) {
        return getList({
          ...where,
          pageNum: page,
          size: limit
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },

      uploadFile() {
        this.$refs.importDialogRef.open();
      },

      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
        this.$refs.userEdit.$refs.form &&
          this.$refs.userEdit.$refs.form.clearValidate();
      },

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

        removeItem([row.id])
          .then((msg) => {
            loading.close();
            this.$message.success('删除' + msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
          });
      },
      // 复制
      copy(row) {
        console.log(row, '33333');
        const loading = this.$loading({ lock: true });
        copyItem(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success('复制成功');
            this.reload();
          })
          .catch((e) => {
            loading.close();
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的质检吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeItem(this.selection.map((d) => d.id))
              .then((msg) => {
                loading.close();
                this.$message.success('删除' + msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
              });
          })
          .catch(() => {});
      }
    }
  };
</script>
