<template>
<div class="rebate-setting-pannel">
    <div class="rules">
        <div style="width: 100%;text-align: center;font-size: 16px;margin-bottom: 10px;height: 40px;">
            <el-input v-model="searchValue" style="width: 390px" size="large" >
                <template #append>
                    <el-button size="large" icon="Search" @click="query" ></el-button>
                </template>
            </el-input>
        </div>
        <el-table-v2 :columns="[{key: 'user_code', dataKey: 'id'}]" :data="searchResult" :width="390" :height="440" :header-height="0" :row-event-handlers="handleRow" >
            <template #row="{ rowData }" >
                <div style="padding: 0 10px;width: 100%;cursor: pointer;" >{{ rowData.user_code }}</div>
            </template>
        </el-table-v2>
    </div>
    <div class="hierarchy">
        <div style="width: 100%;height: 100%;padding: 0 20px;">
            <div style="height: 500px;">
                <BaseTree ref="tree" class="mtl-tree" v-model="treeNodes" treeLine style="height: 500px;overflow-y: auto;" >
                    <template #default="{ node, stat }">
                        <svg-icon v-if="stat.open && stat.children.length" class="tree-node-icon" icon-class="jian" @click.native="stat.open = false" />
                        <svg-icon v-else-if="!stat.open && stat.children.length" class="tree-node-icon" icon-class="jia" @click.native="stat.open = true" />
                        <div class="tree-node" :ref="'refN'+node.id" v-if="stat.parent == null" >
                            <div class="label root-label" >
                                <svg-icon icon-class="rootNode" class="label-icon" />
                                {{ node.user_code + '(' + node.id + ')' }}
                            </div>
                        </div>
                        <div class="tree-node" :ref="'refN'+node.id" v-else >
                            <div class="label" :class="{'ib-label': node.user_type == 1}" >
                                <svg-icon v-if="node.user_type == 1" icon-class="ibNode" class="label-icon" />
                                <svg-icon v-if="node.user_type == 2" icon-class="cusNode" class="label-icon" />
                                {{ node.user_code + '(' + node.id + ')' }}
                            </div>
                        </div>
                    </template>
                </BaseTree>
            </div>
        </div>
    </div>
</div>

</template>

<script setup>
import { getIbtree, getAllJunior, saveRebateSet } from "@/api/ib"
import { useLocale } from 'element-plus'
import { isEmpty, returnFloat } from '@/utils/htfx'
import { BaseTree, walkTreeData  } from '@he-tree/vue'
import '@he-tree/vue/style/default.css'
import '@he-tree/vue/style/material-design.css'
import { nextTick } from "vue"

const { t } = useLocale()
const { proxy } = getCurrentInstance();
const allJunior = ref(null)
const treeNodes = ref([])
const selectedRow = ref(null)
const bigAgentId = ref(null)

const searchValue = ref(null)
const searchResult = ref([])

function getTreeData(value) {
    if (value) {
        let tmp = []
        for (let index = 0; index < value.length; index++) {
            const element = value[index];
            if (!element.children) {
                element.children = []
            } else {
                element.children = getTreeData(element.children)
            }
            tmp.push(element)
        }
        return tmp
    } else {
        return []
    }
}

function loadData() {
    getIbtree().then(resp => {
        if (resp.data) {
            treeNodes.value = getTreeData(resp.data)
        }
        console.log(getTreeData(resp.data))
    })
}

function query() {
    let str = searchValue.value
    if (isEmpty(str)) {
        searchResult.value = []
    } else {
        let tmp = []
        walkTreeData(treeNodes.value, (node, index, parent) => {
            if (node.user_code.indexOf(str) >= 0) {
                tmp.push(node)
            }
        }, {
            childrenKey: 'children',
            reverse: false,
            childFirst: false
        })
        searchResult.value = tmp
    }
}

const handleRow = {
    onClick(e) {
        const { rowData } = e
        proxy.$refs.tree.openNodeAndParents(
            rowData
        )
        nextTick(() => {
            proxy.$refs['refN'+rowData.id].scrollIntoView({ behavior: 'smooth' });
        })
    }
}

loadData()
</script>

<style lang="scss" scoped>
.rebate-setting-pannel {
    width: 100%;
    height: 60vh;
    display: flex;

    .rules {
        height: 100%;
        width: 390px;
        flex-shrink: 0;
        flex-grow: 0;
        
        .rule-item {
            cursor: pointer;
        }
    }

    .hierarchy {
        flex-grow: 1;
        flex-shrink: 0;
    }
}

:deep(.el-table) {
    .icon-cell .cell {
        line-height: unset;
    }

    .current-row {
        color: var(--el-menu-active-color);
    }
}

.tree-node-icon {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.tree-node {
    margin: 8px 0;
    height: 34px;
    font-size: 18px;
    // margin-left: 40px;
    display: flex;
    align-items: center;

    .label {
        margin-left: 10px;
        padding: 8px 15px;
        display: flex;
        align-items: center;
        user-select: none;
        color: #fff;
        background-color: rgba(29, 192, 228, 1);

        .label-icon {
            margin-right: 6px;
            width: 20px;
            height: 20px;
        }
    }

    .root-label {
        background-color: #fff;
        color: #000;
    }

    .ib-label {
        background-color: rgba(15, 54, 117, 1);
    }
}

.save-btn {
    min-width: 200px;
    font-size: 18px;
}

</style>