<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"
      row-key="id"
      toolKit="[ 'size', 'columns', 'fullscreen']"
      cache-key="materialDialogtable"
      :datasource="datasourceShow"
      :current.sync="current"
      :needPage="false"
      :initLoad="false"
      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 { bomSubListByVersionId } from '@/api/mainData';
  export default {
    props: ['params'],
    data () {
      return {
        visible: false,
        current: null,
        callback: null,
        searchKey: '',
        datasource: []
      };
    },
    computed: {
      columns () {
        const list = [
          {
            label: '原料编码',
            prop: 'categoryCode'
          },
          {
            label: '原料名称',
            prop: 'categoryName'
          },
          {
            label: '牌号',
            prop: 'brandNum'
          }
        ];
        return list;
      },
      datasourceShow () {
        return this.datasource.filter((item) => {
          if (this.searchKey) {
            return (
              item.categoryName.indexOf(this.searchKey) !== -1 ||
              item.categoryCode.indexOf(this.searchKey) !== -1
            );
          }
          return true;
        });
      }
    },
    methods: {
      open (memo, callback) {
        this.memo = memo;
        this.callback = callback;
        this.visible = true;

        this._bomSubListByVersionId();
      },
      reload () {
        this.$refs.tableRef.reload();
      },
      handleDone (data) {
        if (this.memo?.id || this.memo?.sourceCategoryId) {
          this.$nextTick(() => {
            this.$refs.tableRef.setCurrentRow(
              data.find(
                (item) =>
                  item.id == (this.memo.id || this.memo.sourceCategoryId)
              )
            );

            console.log(data, this.memo);
          });
        }
      },
      async _bomSubListByVersionId () {
        const res = await bomSubListByVersionId({
          ...this.params
        });

        this.datasource = res;
        this.handleDone(res);
      },
      confirm () {
        if (!this.current) return this.$message.error('请选择数据');

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

        this.cancel();
      },
      cancel () {
        this.$refs.tableRef.clearSelection();
        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>
