<template>
  <div class="document-tree-container">
    <div class="document-tree-header">
      <span class="document-tree-title">职位</span>
     <!--  <el-button type="primary" size="small" @click="handleAddDocument">
        <el-icon><Plus /></el-icon>添加分类
      </el-button> -->
    </div>
    <el-tree
      ref="treeRef"
      :data="treeData"
      node-key="id"
      :props="defaultProps"
      default-expand-all
      highlight-current
      @node-click="handleNodeClick"
    >
      <template #default="{ node, data }">
        <div class="custom-tree-node">
          <div class="node-label">
            <el-icon><Folder /></el-icon>
            <span class="ml-2">{{ data.title }}</span>
          </div>
          <div class="node-actions" v-if="data.id">
            <el-button type="primary" link size="small" @click.stop="handleEdit(data)">
              <el-icon><Edit /></el-icon>
            </el-button>
            <el-button type="danger" link size="small" @click.stop="handleDelete(node, data)">
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
        </div>
      </template>
    </el-tree>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineEmits } from 'vue';
import { Plus, Folder, Edit, Delete } from '@element-plus/icons-vue';

const props = defineProps({
  treeData: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['treeClick', 'updateDocument', 'deleteDocument']);

const treeRef = ref(null);

const defaultProps = {
  children: 'children',
  label: 'name'
};

// 处理节点点击
const handleNodeClick = (data: any) => {
  emit('treeClick', data);
};

// 添加文档分类
const handleAddDocument = () => {
  emit('updateDocument', 'add');
};

// 编辑文档分类
const handleEdit = (data: any) => {
  emit('updateDocument', 'update', data);
};

// 删除文档分类
const handleDelete = (node: any, data: any) => {
  emit('deleteDocument', data.id, () => {
    // 回调函数，删除成功后可以执行
  });
};

defineExpose({
  treeRef
});
</script>

<style lang="scss" scoped>
.document-tree-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.document-tree-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.document-tree-title {
  font-size: 16px;
  font-weight: bold;
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 8px;
}

.node-label {
  display: flex;
  align-items: center;
}

.node-actions {
  display: none;
}

.custom-tree-node:hover .node-actions {
  display: flex;
}

.ml-2 {
  margin-left: 8px;
}
</style> 