<template>
  <ele-modal
    :visible.sync="visible"
    :title="categoryLevelId == 5 ? '选择模具' : '选择舟皿'"
    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"
      :key="categoryLevelId"
      @done="handleDone"
      row-key="id"
      cache-key="materialDialogtable"
      :datasource="datasource"
      :current.sync="current"
      highlight-current-row
      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 { getList } from '@/api/classifyManage/itemInformation';
  export default {
    data() {
      return {
        visible: false,
        current: null,
        callback: null,
        categoryLevelId: '',
        searchKey: ''
      };
    },
    computed: {
      columns() {
        const list = [
          {
            label: '编码',
            prop: 'code'
          },
          {
            label: '名称',
            prop: 'name'
          },
          {
            label: '牌号',
            prop: 'brandNum'
          }
        ];
        return list;
      }
    },
    methods: {
      open(categoryLevelId, memo, callback) {
        this.categoryLevelId = categoryLevelId;
        this.memo = memo;
        this.callback = callback;
        this.visible = true;
      },
      reload() {
        this.$refs.tableRef.reload();
      },
      handleDone({ data }) {
        if (this.memo?.id) {
          this.$nextTick(() => {
            this.$refs.tableRef.setCurrentRow(
              data.find((item) => item.id == this.memo.id)
            );
          });
        }
      },

      datasource({ page, limit }) {
        return getList({
          pageNum: page,
          size: limit,
          isProduct: true,
          categoryLevelId: this.categoryLevelId,
          searchKey: this.searchKey
        });
      },
      confirm() {
        if (!this.current) return this.$message.error('请选择数据');

        this.callback && this.callback(this.current);

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