/*
 * @Description:: 请补充填写
 * @Version: 请配置版本号
 * @Author: 舟舟
 * @Date: 2022-07-10 15:16:58
 * @LastEditors: 舟舟
 * @LastEditTime: 2022-07-10 16:06:04
 * @FilePath: /ouicai_web_framework_vite/src/utils/tree.ts
 */
import { reactive } from 'vue'
import createAxios from '/@/utils/axios'

export default class tree {

    public api

    public treeObj: treeData = reactive({data:[{
        id: 0,
        pid: 0,
        label: '',
        children: [],
    }]})

    public defaultProps = {
        children: 'children',
        label: 'label',
    }

    constructor(api:string,defaultProps:{children:string,label:string}){

        this.api=api
        this.defaultProps=defaultProps

    }

    //远程请求并绑定树元素集合
   public BindTrees=()=>{
        createAxios({
            url: this.api,
            method: 'GET',
        }).then((res) => {
            this.treeObj.data = res.data.trees
        })

    }

    //获取树元素父级集合
   public FindTreePids = (treePid: number) => {
        const pidarr: [number] = [treePid]
        this.FindParentTreeId(treePid, this.treeObj.data, pidarr)
        // 数据转换成eg:0,1,22,5
        return pidarr.reverse().join(',')
    }

    //递归查找所属树元素父级PID
    FindParentTreeId = (rootId: number, treeData: treeStruct[], pidArr: [number]) => {
        treeData.map((item) => {
            {
                if (item.id == rootId) {
                    pidArr.push(item.pid)
                    this.FindParentTreeId(item.pid, this.treeObj.data, pidArr)
                } else {
                    if (item.children) {
                        this.FindParentTreeId(rootId, item.children, pidArr)
                    }
                }
            }
        })
        return pidArr
    }





}
