<template>
  <div>
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="list"
      height="calc(100vh - 605px)"
      tool-class="ele-toolbar-form"
      cache-key="inspectionClassify"
      row-key="qualityLevelId"
      :selection.sync="selection"
    >
      <template v-slot:toolbar>
        <el-button @click="handAdd" size="mini" type="primary"
          >新增质检项</el-button
        >
      </template>

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

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

      <template v-slot:defaultValue="{ row }">
        <div style="display: flex">
          <div
            v-if="row.textType == 3"
            style="display: flex; align-items: center"
          >
            <el-input
              style="width: 132px"
              v-model="row.minValue"
            ></el-input>
            <span>&nbsp;&nbsp;-</span>
            <el-input
              style="width: 132px; margin-left: 10px"
              v-model="row.maxValue"
            ></el-input>
          </div>

          <div v-else style="width: 290px">
            <el-input
              v-model="row.defaultValue"
              placeholder="请输入"
            ></el-input>
          </div>

          <el-input
            style="margin-left: 10px; width: 80px"
            v-model="row.unit"
            disabled
          >
          </el-input>
        </div>
      </template>

      <template v-slot:action="{ row, $index }">
        <el-popconfirm
          class="ele-action"
          title="确定要删除当前质检项吗？"
          @confirm="handDel($index)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>

    <termPop ref="termRef" @selectChange="selectChange"></termPop>
  </div>
</template>

<script>
  import termPop from './inspectionClassify/index.vue';

  import dictMixins from '@/mixins/dictMixins';
  import { getByCode } from '@/api/system/dictionary-data';
  export default {
    components: { termPop },
    mixins: [dictMixins],
    props: {
      qualityParam: {
        type: Array
      }
    },

    watch: {
      qualityParam: {
        handler(val) {
          this.list = val;
          this.$forceUpdate();
        },
        deep: true,
        immediate: true
      }
    },

    data() {
      return {
        list: [],
        dictList: [],

        selection: [],

        columns: [
          {
            prop: 'categoryLevelName',
            label: '质检类型',
            align: 'center',
            minWidth: 110
          },

          {
            prop: 'inspectionName',
            label: '质检名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },

          {
            prop: 'defaultValue',
            slot: 'defaultValue',
            label: '工艺参数',
            align: 'center',
            minWidth: 260
          },

          {
            label: '工艺要求',
            prop: 'inspectionStandard',
            formatter: (row, column, cellValue) => {
              return (
                row.symbol + ' ' + cellValue + ' ' + row.unit
              );
            },
            minWidth: 350
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 80,
            align: 'center',
            resizable: false,
            slot: 'action',
            fixed: 'right'
          }
        ]
      };
    },

    created() {
      this.requestDict('质检标准类型');
      // this.getDictList('mathematical_symbol'); // 数学字符
    },

    methods: {
      handDel(index) {
        this.$confirm('是否删除该质检项', '删除', {
          type: 'warning'
        })
          .then(() => {
            this.list.splice(index, 1);
          })
          .catch(() => {});
      },

      handAdd() {
        this.$refs.termRef.open(this.list);
      },

      async getDictList(code) {
        let { data: res } = await getByCode(code);
        this.dictList = res.map((item) => {
          let values = Object.keys(item);
          return {
            value: Number(values[0]),
            label: item[values[0]]
          };
        });
      },

      getDate() {
        return this.list;
      },

      selectChange(list) {
        this.list = list;
      }
    }
  };
</script>

<style lang="scss" scoped>

</style>
