<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="90%"
  >
    <el-card shadow="never">
      <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"
            :expand-on-click-node="false"
            :default-expanded-keys="categoryId ? [categoryId] : []"
            :highlight-current="true"
            node-key="id"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>

        <!-- 数据表格 -->
        <template v-slot:content>
          <ProductSearch @search="reload" ref="searchRef" />
          <ele-pro-table
            height="400"
            ref="table"
            :columns="columns"
            :datasource="datasource"
            :selection.sync="selection"
            row-key="id"
            :initLoad="false"
          >
            <template v-slot:modelType="{ row }">
              <span>{{ row.category.modelType }}</span>
            </template>
            <template v-slot:specification="{ row }">
              <span>{{ row.category.specification }}</span>
            </template>
            <template v-slot:pathName="{ row }">
              <span>{{
                row.position.length > 0 ? row.position[0].pathName : ''
              }}</span>
            </template>
          </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 { getAssetList } from '@/api/ruleManagement/plan';
  import { getTreeByPid, getTreeByGroup } from '@/api/classifyManage';
  export default {
    data() {
      return {
        ruleIdListIndex: 0,
        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((id) => id == row.id);
            },
            reserveSelection: true,
            fixed: 'left'
          },
          {
            label: '设备名称',
            prop: 'name'
          },
          {
            label: '编号',
            prop: 'codeNumber'
          },
          {
            label: '固资编码',
            prop: 'fixCode'
          },
          {
            label: '使用人',
            prop: 'usePerson'
          },
          {
            label: '使用岗位',
            prop: 'postName'
          },
          {
            label: '负责人',
            prop: 'chargePerson'
          },
          {
            label: '有效开始时间',
            prop: 'startTime'
          },
          {
            label: '有效结束时间',
            prop: 'endTime'
          },
          {
            label: '型号',
            prop: 'modelType',
            slot: 'modelType'
          },
          {
            label: '规格',
            prop: 'specification',
            slot: 'specification'
          },
          {
            label: '设备位置',
            prop: 'pathName',
            slot: 'pathName'
          }
        ],

        // 表格选中数据
        selection: [],
        processData: []
      };
    },
    components: {
      ProductSearch
    },
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        const res = await getAssetList({
          ...where,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryLevelId,
          rootCategoryLevelId: this.rootId
        });
        console.log('res---------', res);
        this.categoryId = res.list[0]?.categoryId;
        return res;
      },
      open(ruleIdList, ruleIdListIndex) {
        let list = ruleIdList
          .map((item) => {
            return item.equipmentList;
          })
          .flat();
        if (list.length > 0) {
          this.processData = list.map((item) => item.id);
        } else {
          this.processData = [];
        }
        this.ruleIdListIndex = ruleIdListIndex;
        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;
            return this.treeList;
          }
        } catch (error) {}
        this.treeLoading = false;
      },

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

      /* 刷新表格 */
      reload(where) {
        if (this.rootId && this.categoryLevelId) {
          this.$refs.table.reload({ page: 1, where: where });
        } else {
          this.$message.error('请选择设备');
        }
      },

      handleClose() {
        this.visible = false;
        this.$refs.table.setSelectedRows([]);
        this.selection = [];
      },
      selected() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        const selectList = this.$refs.treeRef.getCheckedNodes();
        console.log('selectList-----------', selectList);
        this.$emit(
          'chooseEquipment',
          this.selection,
          this.ruleIdListIndex,
          this.categoryId
        );
        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 {
                display: flex;
                flex-direction: column;
                > 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;
        }
      }
    }
  }
  ::v-deep {
    .el-checkbox__input.is-disabled .el-checkbox__inner {
      background-color: #c1c1c1 !important;
    }
  }
  .ml60 {
    margin-left: 60px;
  }
</style>
