<template>
  <div class="transfer__body" v-loading="allLoading"
    :element-loading-text="$t('common.loadingText')" :style="{height}">
    <div class="transfer-pane">
      <div class="transfer-pane__tools">
        <el-input placeholder="输入关键词进行搜索" v-model="keyword">
          <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
        </el-input>
      </div>
      <div class="transfer-pane__body">
        <el-tree :data="treeData" :props="props" highlight-current :expand-on-click-node="false"
          check-on-click-node @node-click="handleNodeClick" class="NCC-common-el-tree"
          node-key="id" v-loading="loading" lazy :load="loadNode">
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <i :class="data.icon"></i>
            <span class="text">{{node.label}}</span>
          </span>
        </el-tree>
      </div>
    </div>
    <div class="transfer-pane">
      <div class="transfer-pane__tools">
        <span>已选</span>
        <el-button @click="removeAll" type="text">清空列表</el-button>
      </div>
      <div class="transfer-pane__body shadow right-pane">
        <template>
          <div v-for="(item, index) in selectedData" :key=" index" class="selected-item">
            <span>{{ item.fullName}}</span>
            <i class="el-icon-delete" @click="removeData(index)"></i>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { getImUserSelector, getUserInfoList } from '@/api/permission/user'
export default {
  name: 'NCC-userTransfer',
  data() {
    return {
      allLoading: false,
      loading: false,
      treeData: [],
      selectedData: [],
      props: {
        children: 'children',
        label: 'fullName',
        isLeaf: 'isLeaf'
      },
      keyword: '',
      nodeId: '0',
      ids: []
    }
  },
  props: {
    height: {
      type: String,
      default: "380px"
    },
    // allLoading: {
    //   type: Boolean,
    //   default: false
    // },
    value: {
      type: Array,
      default: () => []
    },
    multiple: {
      type: Boolean,
      default: false
    },
  },
  methods: {
    init() {
      this.selectedData = []
      this.ids = []
      this.keyword = ''
      this.nodeId = '0'
      this.$nextTick(() => {
        this.getList()
        this.getSelectList()
      })
    },
    getSelectList() {
      this.allLoading = true
      if (!this.value.length) return this.allLoading = false
      getUserInfoList(this.value).then(res => {
        this.selectedData = res.data.list
        this.ids = this.selectedData.map(o => o.id)
        this.allLoading = false
      })
    },
    getList() {
      this.loading = true
      if (this.keyword) this.nodeId = '0'
      getImUserSelector(this.nodeId, this.keyword).then(res => {
        this.treeData = res.data.list
        this.loading = false
      })
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        this.nodeId = '0'
        return resolve(this.treeData)
      }
      this.nodeId = node.data.id
      getImUserSelector(this.nodeId).then(res => {
        resolve(res.data.list)
      })
    },
    /**
     * 处理节点点击事件
     * 如果点击的是用户节点，直接添加
     * 如果点击的是组织/部门节点，全选该节点下所有人员
     */
    handleNodeClick(data) {
      // 如果是用户节点，保持原有逻辑
      if (data.type === 'user') {
        const boo = this.selectedData.some(o => o.id === data.id)
        if (boo) return
        const item = {
          id: data.id,
          fullName: data.fullName
        }
        this.multiple ? this.selectedData.push(item) : this.selectedData = [item]
        this.multiple ? this.ids.push(item.id) : this.ids = [item.id]
        this.$emit('input', this.ids)
        this.$emit('getValue', this.ids, this.selectedData)
        return
      }
      
      // 如果是组织/部门节点，全选该节点下所有人员
      this.selectAllUsersInNode(data.id)
    },
    /**
     * 递归获取节点下所有用户并全选
     * @param {String} nodeId - 节点ID
     */
    async selectAllUsersInNode(nodeId) {
      this.allLoading = true
      try {
        const users = await this.getAllUsersRecursive(nodeId)
        if (users.length === 0) {
          this.$message.warning('该节点下没有人员')
          this.allLoading = false
          return
        }
        
        // 去重，避免重复添加
        const newUsers = users.filter(user => !this.ids.includes(user.id))
        if (newUsers.length === 0) {
          this.$message.info('该节点下的人员已全部选中')
          this.allLoading = false
          return
        }
        
        // 添加到已选列表
        if (this.multiple) {
          this.selectedData.push(...newUsers)
          this.ids.push(...newUsers.map(u => u.id))
        } else {
          this.selectedData = newUsers
          this.ids = newUsers.map(u => u.id)
        }
        
        this.$emit('input', this.ids)
        this.$emit('getValue', this.ids, this.selectedData)
        this.$message.success(`已选择 ${newUsers.length} 个人员`)
      } catch (error) {
        console.error('获取用户列表失败:', error)
        this.$message.error('获取用户列表失败')
      } finally {
        this.allLoading = false
      }
    },
    /**
     * 递归获取节点下所有用户
     * @param {String} nodeId - 节点ID
     * @returns {Array} 用户列表
     */
    async getAllUsersRecursive(nodeId) {
      const users = []
      
      try {
        // 获取该节点下的子节点
        const res = await getImUserSelector(nodeId)
        const children = res.data.list || []
        
        for (const child of children) {
          // 如果是用户节点，直接添加
          if (child.type === 'user') {
            users.push({
              id: child.id,
              fullName: child.fullName
            })
          } else {
            // 如果是组织/部门节点，递归获取其下的所有用户
            const subUsers = await this.getAllUsersRecursive(child.id)
            users.push(...subUsers)
          }
        }
      } catch (error) {
        console.error(`获取节点 ${nodeId} 下的用户失败:`, error)
      }
      
      return users
    },
    removeAll() {
      this.selectedData = []
      this.ids = []
      this.$emit('input', this.ids)
      this.$emit('getValue', this.ids, this.selectedData)
    },
    removeData(index) {
      this.selectedData.splice(index, 1)
      this.ids.splice(index, 1)
      this.$emit('input', this.ids)
      this.$emit('getValue', this.ids, this.selectedData)
    },
  }
};
</script>