<template>
    <ElTree class="hc-tree-node-box" ref="ElTreeRef" :props="ElTreeProps" :data="ElTreeData" :node-key="nodeKey" highlight-current accordion show-checkbox :check-strictly="isStrictly" :expand-on-click-node="false" @check="ElTreeCheckChange">
        <template #default="{ node, data }">
            <div class="custom-tree-node">
                <div class="label" @dblclick="ElTreeDblClick(data)">
                    <el-input v-model="data.title" size="small" @keyup="keyUpEvent($event,data)" v-if="data.isInputName" @blur="ElTreeBtnClick(data)">
                        <template #append>
                            <el-button type="primary" plain size="small" @click="ElTreeBtnClick(data)">
                                <HcIcon name="done"/>
                            </el-button>
                        </template>
                    </el-input>
                    <span v-else>{{data.title}}</span>
                </div>
            </div>
        </template>
    </ElTree>
</template>

<script setup>
import {nextTick, onMounted, ref, watch} from "vue";
import {getArrValue} from "vue-utils-plus"
import wbsApi from '~api/data-fill/wbs';

//参数
const props = defineProps({
    projectId: {
        type: [String,Number],
        default: ''
    },
    nodeKey: {
        type: String,
        default: 'primaryKeyId'
    },
    nodeId: {
        type: [String,Number],
        default: ''
    },
    oldId: {
        type: [String,Number],
        default: ''
    },
    strictly: {
        type: Boolean,
        default: false
    },
})

//变量
const ElTreeRef = ref(null)
const projectId = ref(props.projectId)
const isStrictly = ref(props.strictly)
const ElTreeProps = ref({label: 'title', children: 'children', isLeaf: 'notExsitChild'})

//监听
watch(() => [
    props.projectId,
    props.strictly,
], ([pid,strictly]) => {
    projectId.value = pid
    isStrictly.value = strictly
})

//渲染完成
onMounted(() => {
    ElTreeLoadNode()
})

//事件
const emit = defineEmits(['check-change'])

//树形结构异步加载数据
const ElTreeData = ref([])
const ElTreeLoadNode = async () => {
    let nodeId = props.oldId || props.nodeId || ''
    const {error, code, data} = await wbsApi.queryWbsTreePrivateByProjectIdAndId({
        id: nodeId,
        projectId: projectId.value
    })
    if (!error && code === 200) {
        ElTreeData.value = getArrValue(data)
        await nextTick(() => {
            ElTreeCheckedKeys()
        })
    }
}

//被选择的
const ElTreeCheckChange = (_,nodes) => {
    emit('check-change', nodes)
}

//处理节点
const ElTreeCheckedKeys = () => {
    const Nodes = ElTreeRef.value.getCheckedNodes() || []
    const HalfNodes = ElTreeRef.value.getHalfCheckedNodes() || []
    emit('check-change', {
        checkedNodes: Nodes,
        halfCheckedNodes: HalfNodes
    })
}

//更改节点名称
const ElTreeDblClick = (item) => {
    item.isInputName = true;
}
//回车
const keyUpEvent = (e,item) => {
    if (e.key === "Enter") {
        ElTreeBtnClick(item)
    }
}
//更改节点名称完成
const ElTreeBtnClick = (item) => {
    if (!item?.title) {
        window?.$message?.warning('节点名称不能为空')
    } else {
        item.isInputName = false;
        ElTreeCheckedKeys()
    }
}
</script>

<style lang="scss" scoped>
.custom-tree-node {
    position: relative;
    width: 100%;
}
</style>

<style lang="scss">
.hc-tree-node-box .el-tree-node__content {
    height: 32px !important;
}
</style>
