<template>
  <el-dialog
    title="选择产品"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="80%"
  >
    <el-card shadow="never">
      <seekPage :seekList="seekList" @search="reload" />
      <ele-split-layout
        width="244px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
      >
        <div class="ele-border-lighter split-layout-right-content">
          <AssetTree
            ref="assetTreeRef"
            id="9"
            @handleNodeClick="handleNodeClick"
          />
        </div>
        <!-- 表格 -->
        <template v-slot:content>
          <ele-pro-table
            ref="table"
            :columns="columns"
            :datasource="datasource"
            height="calc(100vh - 450px)"
            class="dict-table"
            :selection.sync="selection"
            @cell-click="cellClick"
          >
          </ele-pro-table>
        </template>
      </ele-split-layout>
    </el-card>
    <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 AssetTree from '@/components/AssetTree';

  import { getListAndInProduceTask } from '@/api/classifyManage/itemInformation';
  export default {
    components: { AssetTree },
    props: {
      // 是否 多选
      multiple: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      seekList() {
        return [
          {
            label: '产品编码',
            value: 'code',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '产品名称',
            value: 'name',
            type: 'input',
            placeholder: '请输入'
          },
          {
            label: '型号',
            value: 'modelType',
            type: 'input',
            placeholder: '请输入'
          }
        ];
      }
    },
    data() {
      return {
        visible: false,
        // 表格列配置
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            reserveSelection: true
          },
          {
            prop: 'code',
            label: '产品编码'
          },
          {
            prop: 'name',
            label: '产品名称',
            showOverflowTooltip: true
          },
          {
            prop: 'brandNum',
            label: '牌号'
          },
          {
            prop: 'modelType',
            label: '型号'
          },
          {
            prop: 'measuringUnit',
            label: '计量单位'
          },
          {
            prop: 'packingUnit',
            label: '包装单位'
          }
        ],
        categoryLevelId: '9',
        // 表格选中数据
        selection: []
      };
    },
    watch: {},
    methods: {
      /* 表格数据源 */
      datasource({ page, where, limit }) {
        return getListAndInProduceTask({
          ...where,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryLevelId,
          isProduct: true,
          produceTaskCode: this.produceTaskCode,
          bomTypes: [1, 2, 3]
        });
      },
      handleNodeClick(data) {
        this.categoryLevelId = data.id;
        this.reload();
      },
      /* 刷新表格 */
      reload(where = {}) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      open(rows, produceTaskCode) {
        this.produceTaskCode = produceTaskCode;
        console.log('rows', rows, this.produceTaskCode);
        this.visible = true;
        if (rows) {
          this.selection = rows;
          this.$nextTick(() => {
            this.$refs.table.setSelectedRows(rows);
          });
        } else {
          this.selection = [];
          this.$nextTick(() => {
            this.$refs.table.setSelectedRows([]);
          });
        }
        // 刷新表格
        this.reload();
        console.log('this.selection', this.selection);
      },
      handleClose() {
        this.visible = false;
        this.selection = [];
      },
      selected() {
        console.log('this.selection', this.selection);
        if (!this.selection.length) {
          return this.$message.warning('请选择产品');
        }

        if (!this.multiple && this.selection.length > 1) {
          return this.$message.warning('只能选择一个产品');
        }

        this.$emit(
          'changeProduct',
          this.multiple ? this.selection : this.selection[0]
        );
        this.handleClose();
      },
      cellClick(row) {
        console.log('cellClick', row);
        this.selection.push(row);
        this.$refs.table.setSelectedRows(this.selection);
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree_col {
    border: 1px solid #eee;
    padding: 10px 0;
    box-sizing: border-box;
    height: 500px;
    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>
