<template>
  <el-dialog
    :title="title"
    v-if="visible"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <el-card shadow="never">
      <user-search :typeList="typeOptions" @search="reload" />

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
      >
      </ele-pro-table>
    </el-card>

    <div class="btns">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import UserSearch from '@/views/factoryModel/jobManagement/components/user-search.vue';
  import { getProfessionPageList } from '@/api/factoryModel';
  export default {
    components: { UserSearch },
    mixins: [dictMixins],
    data() {
      return {
        visible: false,
        title: '产品参数',
        levelOptions: [
          {
            label: '初级',
            value: '1'
          },
          {
            label: '中级',
            value: '2'
          },
          {
            label: '高级',
            value: '3'
          }
        ],
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center'
          },
          {
            slot: 'type',
            label: '类型',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110,
            formatter: (_row) => {
              return this.getDictValue('工种类型', _row.type);
            }
          },
          {
            align: 'center',
            prop: 'code',
            label: '编码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            slot: 'name',
            prop: 'name',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            slot: 'level',
            prop: 'level',
            label: '等级',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110,
            formatter: (_row) => {
              return this.levelOptions.filter(
                (item) => _row.level == item.value
              )[0].label;
            }
          },
          {
            slot: 'hourCost',
            prop: 'hourCost',
            label: '标准工时费',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          }
        ],
        statusList: [
          { label: '工艺', value: 0 },
          { label: '工序', value: 1 },
          { label: '产品', value: 2 },
          { label: '原料', value: 3 },
          { label: '设备', value: 4 },
          { label: '其他', value: 5 }
        ],
        // 表格选中数据
        selection: [],
        tableData: [],
        current: null
      };
    },

    created() {
      this.requestDict('工种类型');
    },
    computed: {
      typeOptions() {
        return this.dict[this.dictEnum['工种类型']];
      }
    },
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        const res = await getProfessionPageList({
          ...where,
          ...order,
          pageNum: page,
          size: limit
        });
        return res;
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      open(item, current) {
        if (item) {
          this.tableData = JSON.parse(JSON.stringify(item));
        }
        if (current) {
          this.current = current;
        }
        this.visible = true;
      },

      handleClose() {
        this.visible = false;
        this.$refs.table.setSelectedRows([]);
        this.selection = [];
      },
      selected() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$emit('chooseModal', this.selection, this.current);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .btns {
    text-align: center;
    padding: 10px 0;
  }
</style>
