import { http } from '@/common/utils/request'
// 联动  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') || (typeof value == 'object' && value.length>0)){
                        Object.assign(controls[targetKey].params, {
                            [paramKey] : value,
                            [key]: value,
                        })
                    }
                }
            }
        }
    }
}

// 联动 清空值； 触发在控件内部change时
export const clearChildrensData = (key:string, data:any, controls:any) => {
    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]
    }
}



// 联动 带参； 
export const getCarryData = ({ formData, key, carry, Id}:any) => {
    if(!carry?.url) return
    return new Promise<void>((resolve, reject) => {
        // 请求地址
        let url = carry['url'];
        
        // 参数组织
        let params = {}
        if(carry['inKey']){
            let arr = carry['inKey'].split(',')
            let filter = ''
            for(let i of arr){
                if(i==key) continue
                filter = filter + `${i}=${formData[i] || ''};`
            }
            Object.assign(params, { filter, })
        }else{
            Object.assign(params, { ...formData, })
        }
        if(carry['params']){
            Object.assign(params, { ...carry['params'], })
        }
        Object.assign(params, { carry_field:key, })
        Object.assign(params, { carry_value:formData[key], })
        Object.assign(params, { carry_to: carry['outKey']})
        Object.assign(params, {...formData})
        http.get(`${url}`, params).then((result:any) => {
            let res = result.result
            let data = {}
            if(Array.isArray(res)){
                data = res[0]
            }else{
                data = {...res}
            }
            Object.assign(formData, data)
        }).catch(()=>{
        })

    })
}
export const clearCarryData = ({ formData, carry,}:any) => {
    if(!carry?.url) return
    if(carry['outKey']){
        let keys = carry['outKey'].split(',')
        for(let key of keys){
            delete formData[key]
        }
    }
}