<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 @search="reload" />

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        @cell-click="cellClick"
        row-key="code"
      >
        <!-- 表头工具栏 -->
        <template v-slot:action="{ row }">
          <el-radio
            :disabled="row.id == current.stepId"
            class="radio"
            v-model="radio"
            :label="row.id"
            ><i></i
          ></el-radio>
        </template>
        <template v-slot:status="{ row }">
          {{ checkType(row.categoryType) }}
        </template>
        <!-- 状态列 -->

        <template v-slot:textType="{ row }">
          {{
            row.textType == 1
              ? '数值'
              : row.textType == 2
              ? '选择'
              : row.textType == 3
              ? '产品参数'
              : row.textType == 4
              ? '规格'
              : ''
          }}
        </template>
      </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 UserSearch from './step-search.vue';
  import { workingStepUpdate } from '@/api/produce/bom.js';
  import parameter from '@/api/technology/stepManagement';
  export default {
    components: { UserSearch },
    data() {
      return {
        typeList: [
          {
            value: 1,
            label: '普通工步'
          },
          {
            value: 2,
            label: '抽样质检工步'
          },
          {
            value: 3,
            label: '普通质检工步'
          },
          {
            value: 4,
            label: '包装工步'
          },
          {
            value: 5,
            label: '入库工步'
          },
          {
            value: 6,
            label: '一工一检(常规质检)'
          }
        ],
        visible: false,
        title: '工步选择',
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            selectable: (row, index) => {
              return !this.tableData.some((it) => it.stepId == row.id);
            },
            reserveSelection: true,
            fixed: 'left'
          },
          {
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center'
          },
          {
            align: 'center',
            prop: 'code',
            label: '编码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'name',
            label: '名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              return ['生效', '失效'][_row.status];
            }
          }
        ],
        statusList: [
          { label: '工艺', value: 0 },
          { label: '工序', value: 1 },
          { label: '产品', value: 2 },
          { label: '原料', value: 3 },
          { label: '设备', value: 4 },
          { label: '其他', value: 5 }
        ],
        // 表格选中数据
        selection: [],
        type: '新增',
        radio: null,
        currentRow: {},
        tableData: [],
        current: null
      };
    },

    watch: {},
    methods: {
      // 单击获取id
      cellClick(row) {
        this.currentRow = row;
        this.radio = row.id;
      },
      /*回显类型 */
      checkType(id) {
        const obj = this.statusList.find((item) => item.value == id);
        return obj.label;
      },

      /* 表格数据源 */
      async datasource({ page, limit, where, order }) {
        // 过滤失效工步数据
        where.status = 0;
        const res = await parameter.list({
          ...where,
          ...order,
          pageNum: page,
          size: limit
        });
        return res;
      },

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

        this.visible = true;
      },

      handleClose() {
        this.$refs.table.setSelectedRows([]);
        this.selection = [];
        this.visible = false;
      },
      selected() {
        if (this.type === '修改') {
          if (!this.radio) {
            this.$message.error('请至少选择一条数据');
            return;
          }
          const formData = {
            ...this.currentRow,
            id: this.current.id,
            bomCategoryId: this.current.bomCategoryId,
            categoryCode: this.current.categoryCode,
            categoryId: this.current.categoryId,
            stepId: this.currentRow.id,
            paramId: this.current.paramId
          };
          formData.type = this.currentRow.type.code;
          console.log(2333333, formData);
          workingStepUpdate(formData).then(() => {
            this.$message.success('修改成功');
            this.$emit('chooseModal', [], this.current);
          });
        } else {
          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>
