import { basePositionApi } from '@/api/ticketMr/ticketMr'
export default {
    data() {
        return {
            selectDeliveryAreaDialogList:[], // 全部数据
            selectDeliveryAreaDialogOpen: false, // 是否显示弹窗
            selectDeliveryAreaDialogTitle: '可售区域',

            selectCityListLoading: false,

            provinceIndex: -1,
            initSelectData: {} // 回显载体
        }
    },
    created() {
        
    },
    methods: {
        handleChangeProvince(){
            
        },
        /**
         * 打开 可售区域配置
         */
        deliverableAreaDialogCancel(data) {
            let obj = {}
            
            this.selectCityListLoading = false

            this.provinceIndex = -1
            this.initSelectData = {}

            if(data && data.length>0) {
                data.forEach((item,index)=>{
                    let list = item.split('-')
                    if(obj[list[0]] && list[1]){
                        obj[list[0]].push([list[1]])
                    }else {
                        obj[list[0]] = []
                        if(list[1]){
                            obj[list[0]].push([list[1]])
                        }
                    }
                })
            }
            console.log('obj====',data,obj)
            this.initSelectData = obj
            this.selectDeliveryAreaDialogOpen = true;
            this.basePositionFun("#")
        },
        /** 关闭弹窗  */
        selectDeliveryAreaDialogCancel(){
            this.selectDeliveryAreaDialogOpen = false;
        },
        /**  确认弹窗  */
        selectDeliveryAreaDialogSubmitForm(){
            let list = []
            this.selectDeliveryAreaDialogList.forEach((item)=>{
                if(item.selected){
                    list.push(item.name)
                }else if(item.selectCityList && item.selectCityList.length>0) {
                    item.selectCityList.forEach((item1,index1)=>{
                        console.log("item1===",item1)
                        list.push(item.name+'-'+item1.join('-'))
                    })
                    
                }
            })
            this.form.saleArea = list
            this.selectDeliveryAreaDialogOpen = false
            console.log("list====",list)
        },
        /**  初次获取行政区划  */
        async basePositionFun(upCode) {
            try {
                this.selectCityListLoading = true
                let res = await basePositionApi({
                    upCode,
                })
                if(res.code == 200) {
                    let list = []
                    let listApi = []
                    res.data.forEach((item,index) => {
                        if(item.code!='#') {
                            list.push({
                                ...item,
                                countyList: [],
                                selected: this.initChekeSelectAll(item),
                                selectCityList: this.initSelectData[item.name] ?this.initSelectData[item.name] : [] ,
                                cityList: []
                            })
                            listApi.push(basePositionApi({
                                upCode: item.code?item.code : '#',
                            }))
                        }
                        
                    });
                    let res1 = await Promise.all(listApi)
                    if(res1) {
                        res1.forEach((item,index)=>{
                            let list1 = []
                            item.data.forEach((item1,index1)=>{
                                list1.push({
                                    ...item1,
                                    //countyList: [],
                                    //leaf: true,
                                })
                            })
                            list[index].cityList = [].concat(list1)
                        })
                    }
                    
                    this.selectDeliveryAreaDialogList = [].concat(list)
                    console.log("res1=====",res1,this.selectDeliveryAreaDialogList)
                }
                this.selectCityListLoading = false
            } catch (error) {
                console.error("error====",error)
                this.selectCityListLoading = false
            }
        },
         /**  懒加载 配置   */
        areaCascaderProps(areaItem, areaIndex) {
            return {
              lazy: false,
              lazyLoad: (node, resolve) => this.areaLazyLoad(node, resolve, areaItem, areaIndex),
              multiple: true,
              children: 'countyList',
              label: 'name',
              value: 'name',
              expandTrigger: 'hover',
              // emitPath: false,
            };
        },
        /**  懒加载 函数   */
        areaLazyLoad(node, resolve, areaItem, provinceIndex) {
            const { level } = node;
            // console.log(node, resolve, areaItem, provinceIndex)
            if (level === 0) {
              resolve([])
            } else if(level == 1 ) {
                console.log(node, resolve, areaItem, provinceIndex)
                if(node.data && node.data.code){
                    if(node.data.countyList) {
                        let list = []
                        node.data.countyList.forEach((item,index)=>{
                            list.push({
                                ...item,
                                leaf: true,
                            })
                        })
                        resolve(list)
                    }
                    // basePositionApi({
                    //     upCode: node.data.code,
                    // }).then((res)=>{
                    //     let list = []
                    //     res.data.forEach((item,index)=>{
                    //         list.push({
                    //             ...item,
                    //             leaf: true,
                    //         })
                    //     })
                    //     resolve(list)
                    // })
                }else {
                    resolve([])
                }   
            }else {
                resolve([])
            }
        },
        // 当选中节点变化时触发
        changeCascader(data){
            console.log("data===",data)
           // console.log('selectDeliveryAreaDialogList====',this.selectDeliveryAreaDialogList)
            this.chekeSelectAll(this.provinceIndex)
        },
        // 下拉框出现/隐藏时触发
        async handleCascaderVisibleChangeCity(isVisible, areaItem, provinceIndex){
            this.provinceIndex = provinceIndex
            if (isVisible) {
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)',
                    customClass: 'custom-class-box'
                });
                try {
                    console.log('this.selectDeliveryAreaDialogList1111====',this.selectDeliveryAreaDialogList)
                    if(false && isVisible && this.selectDeliveryAreaDialogList[provinceIndex].cityList[0].countyList.length==0){
                        
                        console.log(isVisible, areaItem, provinceIndex)
    
                        let listApi = []
                        areaItem.cityList.forEach((item,index)=>{
                            listApi.push(basePositionApi({
                                upCode: item.code?item.code : '#',
                            }))
                        })
                        let res1 = await Promise.all(listApi)
                        console.log("res1====",res1)
                        if(res1) {
                            res1.forEach((item,index)=>{
                                this.$set(this.selectDeliveryAreaDialogList[provinceIndex].cityList[index],'countyList',item.data)
                            })
                        }
                    }
                    loading.close();
                    if(isVisible && areaItem.selected){
                        this.$nextTick(()=>{
                            // let list = [['市辖区']]
                            // this.$set(this.selectDeliveryAreaDialogList[provinceIndex],'selectCityList',list)
                            // console.log('this.selectDeliveryAreaDialogList1111====',this.selectDeliveryAreaDialogList)
                            this.setSelectAll(isVisible, areaItem, provinceIndex)
                        })
                        
                    }
                } catch (error) {
                    loading.close();
                    console.error("error=====",error)
                }
            }
           
            
        },
        /**  全选状态  */
        setSelectAll(isVisible, areaItem, provinceIndex){
            this.$nextTick(()=>{
                let list = []
                this.selectDeliveryAreaDialogList[provinceIndex].cityList.forEach((item,index)=>{
                    if(item.countyList && item.countyList.length > 0) {
                        item.countyList.forEach((item1,index1)=>{
                            list.push([item.name,item1.name])
                        })
                    }else {
                        list.push([item.name])
                    }
                })
                this.$set(this.selectDeliveryAreaDialogList[provinceIndex],'selectCityList',list)
                //console.log('this.selectDeliveryAreaDialogList1111====',this.selectDeliveryAreaDialogList)
            })
        },
        /** 点击选中时 判断是否全选  */
        chekeSelectAll(provinceIndex){
            let flog = true
            this.selectDeliveryAreaDialogList[provinceIndex].cityList.forEach((item,index)=>{
                if(this.selectDeliveryAreaDialogList[provinceIndex].selectCityList.every((currentValue)=> !currentValue.includes(item.name)) ) {
                    flog = false
                }
            })
            this.selectDeliveryAreaDialogList[provinceIndex].selected = flog
            console.log("provinceIndex====",provinceIndex)
            console.log("flog====",flog)
            console.log('this.selectDeliveryAreaDialogList1111====',this.selectDeliveryAreaDialogList)
        },
        /** 点击选中时 判断是否全选  */
        initChekeSelectAll(item){
            if(this.initSelectData[item.name] && this.initSelectData[item.name].length == 0) {
                return true
            }else {
                return false
            }
        },
    }
}