<script setup>
import {ref, watch} from 'vue'
import apiDataEDB from '@/api/dataEDB'
import {deleteClassifyItemEmpty,findItemWithIdAndParents} from '../util/util'
const props=defineProps({
    show:{
        type:Boolean,
        default:false
    },
    defaultId:{//初始选中的分类
        type:[Number,String],
        default:0
    }
})

const emits=defineEmits(['update:show','select'])

function handleClose(){
    emits('update:show',false)
}

watch(
    ()=>props.defaultId,
    (n)=>{
        // if(n){
            classify.value=n
        // }
    }
)

// 获取指标库分类
const classify=ref('')
const edbClassifyList=ref([])
function getEdbClassifyList(){
    apiDataEDB.edbClassifyList().then(res=>{
        if(res.Ret===200){
            deleteClassifyItemEmpty(res.Data.AllNodes||[])
            edbClassifyList.value=res.Data.AllNodes||[]
        }
    })
}
getEdbClassifyList()

let activeData=null
function handleChange(data){
    activeData=data
}

function handleConfirmMove(){
    emits('select',{value:activeData.value,selectedOptions:activeData.selectedOptions})
    handleClose()
}

function getSelectClassifyOpt(id){
    let arr=[]
    arr=findItemWithIdAndParents(edbClassifyList.value,id)
    if(arr?.length===0){
        classify.value=''
    }

    console.log(arr);

    emits('select',{value:id,selectedOptions:arr})
    return arr
}

defineExpose({getSelectClassifyOpt})


</script>

<template>
    <van-popup 
        :show="props.show" 
        round
        position="bottom"
    >
        <van-cascader
            v-model="classify"
            title="选择目录"
            :options="edbClassifyList"
            :field-names="{text:'ClassifyName',value:'ClassifyId',children:'Children'}"
            @close="handleClose"
            @change="handleChange"
        />
        <div style="width:300px;margin:0 auto;padding:20px 0">
            <van-button type="primary" round block @click="handleConfirmMove">确定</van-button>
        </div>
    </van-popup>
</template>