<template>
<div>
    <el-image :src="require('../../assets/wanshan.png')"/>
    <div class="home">

        <div class="content">
            <el-form hide-required-asterisk :model="ruleForm" :rules="rules" ref="ruleForm" label="top"
                     class="demo-ruleForm">
                <el-form-item prop="dept1">
                    <div class="flex-box" slot="label">
                        <div class="shu"></div>
                        <div class="small-box">省</div>
                    </div>
                    <div class="box">
                        <el-select v-model="ruleForm.dept1" placeholder="请选择" @change="provincesChange">
                            <el-option
                                    v-for="item in provinces"
                                    :key="item._id"
                                    :label="item.name"
                                    :value="item._id">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item prop="dept2">
                    <div class="flex-box" slot="label">
                        <div class="shu"></div>
                        <div class="small-box">地市</div>
                    </div>
                    <div class="box">
                        <el-select v-model="ruleForm.dept2" placeholder="请选择" @change="citysChange">
                            <el-option
                                    v-for="item in citys"
                                    :key="item._id"
                                    :label="item.name"
                                    :value="item._id">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item prop="dept3">
                    <div class="flex-box" slot="label">
                        <div class="shu"></div>
                        <div class="small-box">县（市、区）</div>
                    </div>
                    <div class="box">
                        <el-select v-model="ruleForm.dept3" placeholder="请选择"  @change="areasChange">
                            <el-option
                                    v-for="item in areas"
                                    :key="item._id"
                                    :label="item.name"
                                    :value="item._id">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item prop="dept4">
                    <div class="flex-box" slot="label">
                        <div class="shu"></div>
                        <div class="small-box">乡镇（街道）</div>
                    </div>
                    <div class="box">
                        <el-select v-model="ruleForm.dept4" placeholder="请选择" @change="countysChange">
                            <el-option
                                    v-for="item in countys"
                                    :key="item._id"
                                    :label="item.name"
                                    :value="item._id">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
                <el-form-item prop="dept5">
                    <div class="flex-box" slot="label">
                        <div class="shu"></div>
                        <div class="small-box">村（居）民委员会</div>
                    </div>
                    <div class="box">
                        <el-select v-model="ruleForm.dept5" placeholder="请选择">
                            <el-option
                                    v-for="item in villages"
                                    :key="item._id"
                                    :label="item.name"
                                    :value="item._id">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
            </el-form>
        </div>
        <div style="width: 100%;margin-top: 50px">
            <el-button size="small" type="primary" round @click="finishInfo">下一步</el-button>
            <el-button style="margin-top: 20px" size="small" type="primary" round @click="goBack">返回上一页</el-button>
        </div>

    </div>

</div>
</template>

<script>
    import {statistics} from '../../api'
    export default {
        name: "AddDept",
        data(){
            return{
                ruleForm:{
                    dept1:'',
                    dept2:'',
                    dept3:'',
                    dept4:'',
                    dept5:'',
                },
                rules:{
                    dept1: [
                        { required: true, message: '请选择省', trigger: 'change' }
                    ],
                    dept2: [
                        { required: true, message: '请选择地市', trigger: 'change' }
                    ],
                    dept3: [
                        { required: true, message: '请选择县（市、区）', trigger: 'change' }
                    ],
                    dept4: [
                        { required: true, message: '请选择乡镇（街道）', trigger: 'change' }
                    ],
                    dept5: [
                        { required: true, message: '请选择村（居）民委员会', trigger: 'change' }
                    ],
                },
                provinces:[],
                citys:[],
                areas:[],
                countys:[],
                villages:[],
                queryData:{
                    fid:'',
                }
            }
        },
        methods:{
            finishInfo(){
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        this.$router.push({
                            name: 'independentInfo',
                            // 只是把query改了，其他都没变
                            query: {
                                ruleForm:this.ruleForm
                            }
                        });
                    } else {
                        return false;
                    }
                });

            },
            goBack(){
                this.$router.replace("/independent/main");
            },
           async provincesChange(){
                this.ruleForm.dept2='';
                this.ruleForm.dept3='';
                this.ruleForm.dept4='';
                this.ruleForm.dept5='';
                this.areas=[];
                this.countys=[];
                this.villages=[];
                console.log(this.queryData,'this.queryData');
                this.queryData.fid=this.ruleForm.dept1;
                this.citys= await this.getData();
            },
            async citysChange(){
                this.ruleForm.dept3='';
                this.ruleForm.dept4='';
                this.ruleForm.dept5='';
                this.queryData.fid=this.ruleForm.dept2;
                this.areas=await this.getData();
                this.countys=[];
                this.villages=[];
            },
           async areasChange(){
                this.ruleForm.dept4='';
                this.ruleForm.dept5='';
                this.queryData.fid=this.ruleForm.dept3;
                this.countys=await this.getData();
                this.villages=[];
            },
           async countysChange(){
                this.queryData.fid=this.ruleForm.dept4;
                this.ruleForm.dept5='';
                this.villages=await this.getData();
            },
            async getData(){
                let  data=await statistics(this.queryData);
                if(data.code=='0')
                {
                    return data.data;
                }
            }
        },
         async mounted() {
             this.provinces= await this.getData();
        }
    }
</script>

<style scoped>

    .home {
        border: 1px solid #E4F0FE;
        box-shadow: 0px 3px 20px 0px rgba(32, 123, 242, 0.08);
        border-radius: 20px;
        position: absolute;
        left: 4%;
        top: 30px;
        background: #FFFFFF;
        padding-bottom: 30px;
        width: 92%;
        margin-bottom: 30px;
    }


    .flex-box {
        display: flex;
        margin-top: 20px;
        margin-bottom: 10px;
        width: 100%;
    }

    .small-box {
        margin-left: 10px;
    }

    .shu {
        height: 18px;
        width: 7px;
        background: #2087F7;
        border-radius: 4px;
    }

    /deep/ .el-form-item__label {
        line-height: 17px;
    }

    /deep/ .el-form-item {
        margin-bottom: 0;
    }

    /deep/ .el-select {
        width: 100%;
    }

    .content {
        width: 90%;
        margin: 0 auto;
    }

    .box {
        position: relative;
        font-size: 14px;
        display: inline-block;
        width: 100%;
    }
    /deep/ .el-button {
        width: 90%;
        margin-left: 5%;
    }
    /deep/ .el-radio__inner {
        width: 20px;
        height: 20px;
    }
</style>
