<template>
  <el-dialog
    title="关联设备"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="90%"
    :maxable="true"
  >
    <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"
            :pageSizes="tablePageSizes"
            ref="table"
            :columns="columns"
            :datasource="datasource"
            row-key="id"
            @cell-click="cellClick"
            :initLoad="false"
          >
            <template v-slot:action="{ row }">
              <el-radio
                class="radio"
                v-model="radio"
                :disabled="row.isBindMainDevice"
                :label="row.id"
              >
                <i></i>
              </el-radio>
            </template>
            <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[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 modalTitle from '@/components/modalTitle.vue';
  import ProductSearch from './product-search.vue';
  import { getAssetList } from '@/api/ruleManagement/plan';
  import { getTreeByPid } from '@/api/classifyManage';

  export default {
    components: {
      ProductSearch,
      modalTitle
    },
    data() {
      return {
        visible: false,
        radio: '',
        current: null,
        title: '选择设备',
        categoryLevelId: null,
        categoryId: 1,
        treeList: [],
        treeLoading: false,
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        // 表格列配置
        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择',
            reserveSelection: true,
            fixed: 'left'
          },
          {
            label: '设备名称',
            prop: 'name'
          },
          {
            label: '编码',
            prop: 'code'
          },
          {
            label: '固资编码',
            prop: 'fixCode'
          },
          {
            label: '使用人',
            prop: 'usePerson'
          },
          {
            label: '使用岗位',
            prop: 'postName'
          },
          {
            label: '片区负责人负责人',
            prop: 'areaPersonInChargeUserName'
          },
          {
            label: '有效开始时间',
            prop: 'startTime'
          },
          {
            label: '有效结束时间',
            prop: 'endTime'
          },
          {
            prop: 'category.modelType',
            label: '型号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'category.specification',
            label: '规格',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            label: '设备位置',
            prop: 'pathName',
            formatter: (_row) => {
              const positionDetail =
                _row.position &&
                _row.position.length != 0 &&
                _row.position[0].detailPosition
                  ? _row.position[0].detailPosition
                  : '-';
              return _row.deviceLocationName
                ? _row.deviceLocationName + '-' + positionDetail
                : '';
            }
          }
        ]
      };
    },
    methods: {
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        const res = await getAssetList({
          ...where,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryLevelId,
          rootCategoryLevelId: this.rootId || this.categoryLevelId
        });
        console.log('res---------', res);
        this.categoryId = res.list[0]?.categoryId;
        return res;
      },
      open() {
        this.visible = true;
        this.getTreeData();
      },

      async getTreeData() {
        try {
          this.treeLoading = true;
          const res = await getTreeByPid(29);
          console.log('res---treeList----------', res);
          this.treeLoading = false;
          if (res?.code === '0') {
            this.treeList = res.data;
            return this.treeList;
          }
        } catch (error) {
          console.log(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.current = null;
        this.radio = '';
      },

      // 单击获取id
      cellClick(row) {
        if (row.isBindMainDevice) {
          return;
        }
        this.current = row;
        this.radio = row.id;
      },
      selected() {
        if (!this.current?.id) {
          this.$message.error('请选择一条数据');
          return;
        }

        this.$emit('chooseEquipment', this.current);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  ::v-deep {
    .el-checkbox__input.is-disabled .el-checkbox__inner {
      background-color: #c1c1c1 !important;
    }
  }
  .ml60 {
    margin-left: 60px;
  }
  .rx-sc {
    flex: 0 0 50px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
