<template>
	<!-- title="添加工单-选择设备" -->
  <ele-modal
    :visible.sync="visible"
    title="选择干燥区域"
    width="65vw"
    append-to-body
    :maxable="true"
  >
    <div class="search-box">
      工位编码:
      <el-input placeholder="请输入" v-model="searchKey"></el-input>
    </div>
    <!-- 数据表格 -->
    <!-- :highlight-current-row="isSingle" -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      @done="handleDone"
      row-key="id"
      :selection.sync="selectionList"
      :datasource="datasource"
      :current.sync="current"
      highlight-current-row
      height="45vh"
      @row-click="choose"
    >
      <template v-slot:select="{ row }">
        <el-radio class="radio" v-model="radio" :label="row.id"
          ><i></i
        ></el-radio>
      </template>
    </ele-pro-table>
    <div class="footer" slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button @click="confirm" type="primary">确定</el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { factoryworkstation } from '@/api/mainData/index';
  export default {
    props: {
      produceVersionId: {
        type: String,
        default: ''
      },
      isPlan: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        visible: false,
        current: null,
        searchKey: '',
        selectionList: [],
        memoList: [],
        isSingle: false,
        callback: null,
        radio: ''
      };
    },
    computed: {
      columns () {
        const list = [
          {
            prop: 'code',
            label: '工位编码'
          },
          {
            label: '工位名称',
            prop: 'name'
          },
          {
            label: '责任人',
            prop: 'leaderName',
            slot: 'factory'
          },
          {
            label: '所属车间',
            prop: 'workshopName'
          },
          {
            label: '所属产线',
            prop: 'productionLineName'
          }
        ];

        if (!this.isSingle) {
          list.push({
            slot: 'select',
            prop: 'select',
            label: '选择',
            align: 'center'
          });
        }
        return list;
      }
    },
    methods: {
      open (list = []) {
        this.memoList = list;
        this.isSingle = false;
        this.visible = true;
      },
      openSingle (list = [], callback) {
        this.memoList = list;
        this.callback = callback;
        this.isSingle = true;
        this.visible = true;
      },
      handleDone ({ data }) {
        if (this.memoList.length) {
          this.$nextTick(() => {
            if (this.isSingle) {
              this.$refs.table.setCurrentRow(
                data.find(
                  (item) =>
                    item.id ==
                    (this.memoList[0].deviceId ||
                      this.memoList[0].sourceInstanceId)
                )
              );
            } else {
              this.memoList.length &&
                this.$refs.table.setSelectedRowKeys(
                  this.memoList.map((i) => i.deviceId)
                );
            }
          });
        }
      },
      datasource ({ page, limit }) {
        return factoryworkstation({
          pageNum: page,
          size: limit,
          code: this.searchKey
        });
      },
      confirm () {
        if (this.isSingle) {
          if (!this.current) return this.$message.error('请选择数据');

          this.callback && this.callback(this.current);
        } else {
          if (!this.current) return this.$message.error('请选择数据');
          this.$emit('success', [this.current]);
        }

        this.cancel();
      },
      cancel () {
        this.$refs.table.setCurrentRow();
        this.$refs.table.clearSelection();
        this.radio = '';
        this.current = null;
        this.visible = false;
      },
      // 单击获取id
      choose (row) {
        this.current = row;
        this.radio = row.id;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .search-box {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 20px;
    .el-input {
      width: 220px;
      margin: 0 32px;
    }
  }
</style>
