
// 联动  childrens ： 只修改params 不修改值；
export const setChildrens = (data:any , controls:any) => {
    for(let key in data){
        let value = data[key]
        let item = controls[key]
        if(!item) continue
        if(item.childrens&& item.childrens.length>0){

            for(let child of item.childrens){
                let targetKey = '', paramKey = key
                if(typeof child == 'string'){
                    targetKey = child
                }else{
                    if(child?.key){
                        targetKey = child.key
                    }
                    if(child?.paramKey){
                        paramKey = child.paramKey
                    }
                }

                if(controls[targetKey]){ // 确定表单中拥有该字段
                    if(controls[targetKey].params==undefined) controls[targetKey].params = {}
                    if(value=='' || (value && typeof value == 'string')){
                        Object.assign(controls[targetKey].params, {
                            [paramKey] : value,
                            [key]: value,
                        })
                    }else if(typeof value == 'object' && value.length>0){
                        Object.assign(controls[targetKey].params, {
                            [paramKey] : value.join(','),
                            [key]: value.join(','),
                        })
                    }
                }
            }
        }
    }
}

// 联动 清空值； 触发在控件内部change时
export const clearChildrensData = (key:string, data:any, controls:any) => {
    // let timer = setTimeout(()=> {
        let item = controls[key]
        // 如果没有childrens 则跳出
        if(!item || !item.childrens || item.childrens.length==0) return
    
        for(let child of item.childrens){
            let targetKey = ''
            if(typeof child == 'string'){
                targetKey = child
            }else{
                if(child?.key){
                    targetKey = child.key
                }
            }
            delete data[targetKey]
        }
    // }, 0)
}