<template>
  <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>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="tableRef"
      :columns="columns"
      @done="handleDone"
      row-key="id"
      cache-key="materialDialogtable"
      :datasource="datasource"
      :selection.sync="selection"
  
      height="45vh"
    >
    </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 { getByRootLevelId } from '@/api/classifyManage/itemInformation';
  export default {
    data() {
      return {
        visible: false,
        selection: [],
        callback: null,
        searchKey: ''
      };
    },
    computed: {
      columns() {
        const list = [
          {
            label: '编码',
            prop: 'code'
          },
          {
            label: '名称',
            prop: 'name'
          },
          {
            label: '牌号',
            prop: 'brand'
          },
          {
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          }
        ];
        return list;
      }
    },
    methods: {
      open(memo, callback) {
        this.memo = memo;
        this.callback = callback;
        this.visible = true;
      },
      reload() {
        this.$refs.tableRef.reload();
      },
      handleDone({ data }) {
        if (this.memo?.length) {
          this.$nextTick(() => {
            this.memo.forEach((itm) => {
              this.$refs.tableRef.setCurrentRow(
                data.find((item) => item.id == itm.id)
              );
            });
          });
        }
      },

      datasource() {
        return getByRootLevelId(7).then((res) => ({ list: res }));
      },
      confirm() {
        if (!this.selection.length) return this.$message.error('请选择数据');

        this.callback && this.callback(this.selection);

        this.cancel();
      },
      cancel() {
        this.$refs.tableRef.setCurrentRow();
        this.searchKey = '';
        this.visible = false;
      }
    }
  };
</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>
