<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    class="productModal_dialog"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <el-card shadow="never">
      <ProductSearch @search="reload" ref="searchRef" />

      <ele-split-layout
        width="244px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
      >
        <div class="ele-border-lighter split-layout-right-content">
          <el-tree
            :data="treeList"
            :props="defaultProps"
            ref="treeRef"
            :default-expanded-keys="categoryId ? [categoryId] : []"
            :highlight-current="true"
            node-key="id"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>

        <!-- 数据表格 -->
        <template v-slot:content>
          <ele-pro-table
            style="min-height: 400px"
            ref="table"
            :columns="columns"
            :datasource="datasource"
            :selection.sync="selection"
            row-key="id"
          >
          </ele-pro-table>
        </template>
      </ele-split-layout>
    </el-card>

    <div class="rx-sc">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import ProductSearch from './product-search.vue';
  import { getTreeByGroup } from '@/api/classifyManage';
  import { getList } from '@/api/classifyManage/itemInformation.js';
  export default {
    components: {
      ProductSearch
    },
    data() {
      return {
        visible: false,
        title: '选择工具',

        categoryLevelId: null,
        categoryId: 1,
        treeList: [],
        treeLoading: false,

        defaultProps: {
          children: 'children',
          label: 'name'
        },
        type: null,

        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            selectable: (row, index) => {
              return !this.processData.some((it) => row.id == it.categoryId);
            },
            reserveSelection: true,
            fixed: 'left'
          },

          {
            label: '物料名称',
            prop: 'name'
          },

          {
            label: '物料编码',
            prop: 'code'
          },
          {
            label: '牌号',
            prop: 'brandNum'
          },
          {
            label: '型号',
            prop: 'modelType'
          },

          {
            prop: 'availableCountBase',
            label: '包装库存',
            sortable: 'custom'
          },
          {
            label: '单位',
            prop: 'weightUnit'
          },

          {
            prop: 'packingCountBase',
            label: '计量库存',
            sortable: 'custom'
          },

          {
            label: '计量单位',
            prop: 'unit'
          },

          {
            label: '数量',
            prop: 'count'
          }
        ],

        // 表格选中数据
        selection: [],

        processData: [],
        current: null
      };
    },

    watch: {},
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        const res = await getList({
          ...where,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryLevelId
        });
        return res;
      },

      open(item) {
        this.processData = item || [];
        this.visible = true;

        this.getTreeData();
      },

      async getTreeData() {
        try {
          this.treeLoading = true;

          const res = await getTreeByGroup({ type: 4 });
          this.treeLoading = false;
          if (res?.code === '0') {
            this.treeList = res.data;

            this.$nextTick(() => {
              // 默认高亮第一级树节点
              if (this.treeList[0]) {
                this.rootTreeId = this.treeList[0].id;
                this.$nextTick(() => {
                  this.$refs.treeRef.setCurrentKey(this.treeList[0].id);
                });
              }
            });
            return this.treeList;
          }
        } catch (error) {}
        this.treeLoading = false;
      },

      handleNodeClick(data) {
        this.categoryLevelId = data.id;
        this.$refs.table.reload({ pageNum: 1, where: {} });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },

      handleClose() {
        this.visible = false;
        this.$refs.table.setSelectedRows([]);
        this.selection = [];
      },
      selected() {
        let _arr = [];
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }

        _arr = this.selection.map((m) => {
          m.categoryId = m.id;
          delete m.id;
          return {
            ...m
          };
        });

        this.$emit('chooseModal', _arr);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .productModal_dialog {
    overflow: hidden;
    ::v-deep .el-dialog {
      margin: 50px auto !important;
      height: 90%;
      overflow: hidden;

      .el-dialog__body {
        position: absolute;
        left: 0;
        top: 54px;
        bottom: 0;
        right: 0;
        padding: 0;
        z-index: 1;
        overflow: hidden;
        overflow-y: auto;
        // 下边设置字体，我的需求是黑底白字
        color: #ffffff;
        line-height: 30px;
        padding: 0 15px;
        display: flex;
        flex-direction: column;
        > div {
          flex: 1;
          .el-card__body {
            height: 100%;
            display: flex;
            flex-direction: column;
            > div {
              flex: 1;
              .ele-split-panel-body {
                > div {
                  height: 100%;
                  display: flex;
                  flex-direction: column;
                  .el-table {
                    flex: 1 0 auto;
                    height: 0;
                    overflow: auto;
                  }
                }
              }
            }
          }
        }
        .rx-sc {
          flex: 0 0 50px;
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }
  }

  .ml60 {
    margin-left: 60px;
  }
</style>
