<template>
    <div>
        <van-cascader
                v-model="formData.presentCode"
                title="请选择现住地址"
                :options="optionsList"
                @close="showPopup = false"
                @change="areaChange"
                @finish="areaFinish"
        />
    </div>
</template>

<script>

import {addReceiveApply, getChildAreaByParentCode} from '@/axios/api'

export default {
    name: "test1",
    data() {
        return {
            // 表单信息  130102001001
            formData: {
                presentCode: '130104002000'
            },
            optionsList: [],
            showPopup: false,
            scrollPosition: 0,
            applyUnitInfo: {unitName: ""},
            birthDateShow: false,
            minDate: new Date(1950, 0, 1),
            maxDate: new Date(),
            indexArr: []
        }
    },
    created() {
        let areaCodeList = [];
        let areaCode = this.formData.presentCode;
        let provinceCode = areaCode.substring(0, 2);
        areaCodeList.push(provinceCode)
        if (areaCode.length > 2) {
            let cityCodeFull = areaCode.substring(0, 4) + "00000000";
            areaCodeList.push(cityCodeFull)
            let countyCodeFull, townshipCodeFull;
            let level = 2;
            if (areaCode.substring(2, 4) != '00') {
                level = 3
                countyCodeFull = areaCode.substring(0, 6) + "000000";
                if (countyCodeFull != cityCodeFull) {
                    areaCodeList.push(countyCodeFull)
                    level = 4;
                    townshipCodeFull = areaCode.substring(0, 9) + "000";
                    if (countyCodeFull != townshipCodeFull) {
                        areaCodeList.push(townshipCodeFull)
                    }
                    if (areaCode != townshipCodeFull) {
                        areaCodeList.push(areaCode)
                    }
                }
            }
        }
        //let areaCodeList = [provinceCode, cityCodeFull, countyCodeFull, townshipCodeFull, villageCode];
        this.initAa(areaCodeList)
        // this.getChildArea(0, 0);
    }
    ,
    methods: {
        areaChange(val) {
            console.log(111, val)
            console.log(222, this.formData.presentCode)
            this.formData.presentName = val.selectedOptions.map((item) => item.text).join('/');
            let indexArr = [];
            val.selectedOptions.forEach(item => {
                indexArr.push(item.index);
            });
            if (indexArr.length == 5) {
                this.showPopup = false;
                return;
            }
            // console.log(this.formData)
            this.indexArr = indexArr;
            this.getChildArea(val.value, val.areaLevel);
        }
        ,
        areaFinish(val) {
            this.formData.presentName = val.selectedOptions.map((item) => item.text).join('/');
        }
        ,
        getChildArea(areaCode, areaLevel) {
            getChildAreaByParentCode(areaCode).then(res => {
                if (res.code === 'SUCCESS') {
                    let optionsList = [];
                    let areaList = res.data;
                    if (areaList != null) {
                        let index = 0;
                        let length = this.indexArr.length;
                        areaList.forEach(area => {
                            let item = {
                                text: area.areaName,
                                value: area.areaCode,
                                index: index,
                                areaLevel: area.areaLevel,
                                children: []
                            };
                            if (length == 4) {
                                delete item.children;
                            }
                            optionsList.push(item)
                            index++;
                        })
                        if (areaLevel == 0) {
                            this.optionsList = optionsList;
                        } else {
                            if (this.indexArr.length == 1) {
                                this.optionsList[this.indexArr[0]].children = optionsList;
                            }
                            if (this.indexArr.length == 2) {
                                this.optionsList[this.indexArr[0]].children[this.indexArr[1]].children = optionsList;
                            }
                            if (this.indexArr.length == 3) {
                                this.optionsList[this.indexArr[0]].children[this.indexArr[1]].children[this.indexArr[2]].children = optionsList;
                            }
                            if (this.indexArr.length == 4) {
                                this.optionsList[this.indexArr[0]].children[this.indexArr[1]].children[this.indexArr[2]].children[this.indexArr[3]].children = optionsList;
                            }
                        }
                    }
                }
            }).catch(res => {
            })
        },
        initAa(areaCodeList) {
            areaCodeList.unshift("0")
            let number = 0
            let code1 = areaCodeList[number++];
            let length = areaCodeList.length;
            let allList = []
            if (!code1) return;
            getChildAreaByParentCode(code1).then(res => {
                let currentList = this.handleData(res.data, allList,length,number)
                this.optionsList = currentList;
                let code2 = areaCodeList[number++];
                if (!code2) return;
                getChildAreaByParentCode(code2).then(res => {
                    let currentList = this.handleData(res.data, allList,length,number)
                    this.findChildList(allList, code2, currentList)
                    let code3 = areaCodeList[number++];
                    if (!code3) return;
                    getChildAreaByParentCode(code3).then(res => {
                        let currentList = this.handleData(res.data, allList,length,number)
                        this.findChildList(allList, code3, currentList)
                        let code4 = areaCodeList[number++];
                        if (!code4) return;
                        getChildAreaByParentCode(code4).then(res => {
                            let currentList = this.handleData(res.data, allList,length,number)
                            this.findChildList(allList, code4, currentList)
                            let code5 = areaCodeList[number++];
                            if (!code5) return;
                            getChildAreaByParentCode(code5).then(res => {
                                let currentList = this.handleData(res.data, allList,length,number)
                                this.findChildList(allList, code5, currentList)
                            })
                        })
                    })
                })
            })
        },
        handleData(areaList, allList,length,number) {
            let optionsList = [];
            if (areaList != null) {
                let index = 0;
                areaList.forEach(area => {
                    let item = {
                        text: area.areaName,
                        value: area.areaCode,
                        index: index,
                        areaLevel: area.areaLevel,
                        children: []
                    };
                    if (length == number+1) {
                        delete item.children;
                    }
                    optionsList.push(item)
                    index++;
                })
                allList.push(optionsList)
            }
            return optionsList;
        },
        findChildList(allList, code, currentList) {
            let lastList = allList[allList.length - 2]
            for (let item of lastList) {
                if (item.value == code) {
                    item.children = currentList
                }
            }
        }
    }
}
</script>

<style scoped>

</style>