<template>
  <el-dialog
    :visible.sync="equipmentdialog"
    :before-close="handleClose"
    :close-on-click-modal="true"
    :close-on-press-escape="false"
    append-to-body
    width="60%"
  >
    <div>
      <el-row>
        <el-col :span="24" class="table_col" v-if="equipmentdialog">
          <ele-pro-table
            ref="equiTable"
            :columns="columns"
            :datasource="datasource"
            :selection.sync="selection"
            :current.sync="current"
            highlight-current-row
            cache-key="systemRoleTable3"
            @selection-change="selectionChange"
            @select="select"


            row-key="id"
            height="50vh"
            @done="onDone"
          >
          </ele-pro-table>
        </el-col>
      </el-row>
    </div>
    <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 { getList } from '@/api/inspectionProject';

export default {
  components: {},
  props: {
    selectList: Array,
    type: {
      default: 1 //1多选 2单选
    }
  },
  data() {
    return {
      equipmentdialog: false,
      current: null,
      columns: [
        {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center',
          reserveSelection: true,
          show:this.type==1
        },
        {
          prop: 'inspectionCode',
          label: '编码'
        },
        {
          label: '质检项名称',
          prop: 'inspectionName'
        },

        // {
        //   label: '是否生成过程',
        //   prop: 'isCreateCourse',
        //   formatter: (row, column, cellValue) => {
        //     return cellValue == 1 ? '是' : cellValue === 0 ? '否' : '';
        //   }
        // },
        {
          label: '质检标准',
          prop: 'inspectionStandard',
          formatter: (row, column, cellValue) => {
            return row.symbol+' '+cellValue+' '+row.unit
          }
        },

        // {
        //   label: '质检工具',
        //   prop: 'inspectionTool'
        // },
        {
          label: '状态',
          prop: 'status',
          formatter: (row, column, cellValue) => {
            return cellValue == 1 ? '启用' : cellValue === 0 ? '停用' : '';
          }
        },
        {
          label: '描述',
          prop: 'description'
        },

        {
          label: '备注',
          prop: 'inspectionRemark'
        }
      ],
      categoryLevelId: null,
      code: null,
      selection: [],
      ids:[],
      oldSelection:[],//存储selection
    }
  },

  watch: {},
  methods: {
    datasource({ page, where, limit }) {
      return getList({
        ...where,
        pageNum: page,
        size: limit
      });
    },
    open(ids) {
      this.equipmentdialog = true;
      this.ids=ids
    },
    selectionChange(selection){
      this.oldSelection.push(...selection)
    },
    select(selection, row){
      if(!selection.find(item=>item.id==row.id)){
        this.ids= this.ids.filter(item=>item!=row.id) 
      }else{
        this.ids.push(row.id)
      }
    },
    onDone(){
      this.$nextTick(() => {
        this.$refs.equiTable.setSelectedRowKeys(this.ids);
      });
    },
    handleClose() {
      this.equipmentdialog = false;
      this.$refs.equiTable.clearSelection();
    },
    // 选择
    selected() {
      
      if(this.type==1){
        let ids= [...new Set(this.ids)]
        this.selection=ids.map(item=>this.oldSelection.find(val=>val.id==item))
      }
      this.$emit('choose', this.type==1?JSON.parse(JSON.stringify(this.selection)):JSON.parse(JSON.stringify(this.current)));
      this.handleClose();
    }
  }
};
</script>

<style lang="scss" scoped>
.tree_col {
  border: 1px solid #eee;
  padding: 10px 0;
  box-sizing: border-box;
  max-height: 530px;
  overflow: auto;
}
.table_col {
  padding-left: 10px;
  ::v-deep .el-table th.el-table__cell {
    background: #f2f2f2;
  }
}
.pagination {
  text-align: right;
  padding: 10px 0;
}
.btns {
  text-align: center;
  padding: 10px 0;
}
.topsearch {
  margin-bottom: 15px;
}
</style>
