<template>
    <view>
        <form @submit="siteform" class="site-form">
        	<view class="site-input">
        		<label>收货人</label>
        		<input placeholder="请输入收货人姓名" name="name" :value="fromData.name"></input>
        	</view>
        	<view class="site-input">
        		<label>手机号码</label>
        		<input placeholder="请输入手机号码" name="mobile" type="number" :value="fromData.mobile"></input>
        	</view>
        	<view class="site-input">
                <label>所在省份</label>
                <picker @change="areasChange" :value="area.areaIndex" :range="area.areas" :range-key="'name'" name="province_id">
                    <view class="picker" v-if="area.areas[area.areaIndex]">
                        {{ area.areas[area.areaIndex].name }}
                    </view>
        			<image src="/static/icon/rightsArrow.png"></image>
                </picker>
            </view>
        	<view class="site-input">
                <label>所在城市</label>
                <picker @change="cityDrop" :value="city.cityIndex" :range="city.cityList" :range-key="'name'" class="conneColor" name="city_id">
                    <view class="picker" v-if="city.cityList[city.cityIndex]">
                        {{ city.cityList[city.cityIndex].name }}
                    </view>
        			<image src="/static/icon/rightsArrow.png"></image>
                </picker>
            </view>
        
            <view class="site-input">
                <label>所在区域</label>
                <picker @change="regiDrop" :value="regi.regiIndex" :range="regi.regiList" :range-key="'name'" class="conneColor" name="district_id">
                    <view class="picker" v-if="regi.regiList[regi.regiIndex]">
                        {{ regi.regiList[regi.regiIndex].name }}
                    </view>
        			<image src="/static/icon/rightsArrow.png"></image>
                </picker>
            </view>
        	<view class="site-input">
        		<label>收货地址</label>
        		<input placeholder="请输入详细地址" name="address" :value="fromData.address"></input>
        	</view>
        
        	<view class="site-switch" v-if="fromData.type != 'Compile'">
                <text>设置默认地址</text>
                <switch style='zoom:.6;' @change="addressDefault" color="#f57e32"/>
            </view>
        
        	<view class="site-btn">
        		<button form-type="submit" size="mini">保存</button>
        	</view>
        </form>
    </view>
</template>

<script>
    import { create, children, add } from '@/apis/interfaces/address'
    export default {
    	data() {
    		return {
                fromData: {
                    addressId  : '', // 地址id
                    type       : '', // 类型
                    defaultVal :'',  // 是否为默认地址
                    name       : '', // 姓名
                    mobile     : '', // 手机号
                    address    : '', // 地址内容
                },
                
                // 省份选择
                area: { 
                    areas      : [],
                    areaSn     : '',
                    areaIndex  : 0,
                },
        
                // 市级选择
                city: {
                    cityList   : [],
                    cityId     : 0,
                    cityIndex  : 0,
                },
        
                // 区域选择
                regi: {
                    regiList   : [],
                    regiId     : 0,
                    regiIndex  : 0,
                }
            }
        },
        onLoad(options) {
            // 获取省信息
            this.getProvince()
        },
        methods:{
            // 省信息
            getProvince() {
                create().then(res=>{
                    let areaArr    =  res.provinces,
                        areaIndex  =  this.area.areaIndex
                        
                    this.area.areaSn   = areaArr[areaIndex].code
                    this.area.areas    = areaArr
                    this.citylist(areaArr[areaIndex].code)
                })
            },
            
            // 所在省份
            areasChange(e) {
                let area     = this.area.areas,
                    index    = e.detail.value,
                    atcode   = area[index].code
                if (index != this.area.areaIndex) {
                    this.area.areaIndex  = index
                    this.area.areaSn     = atcode
        
                    // 获取市级列表
                    this.citylist(atcode)
                }
            },
            
            // 市级列表
            citylist(code) {
                children(code).then(res=>{
                    let cityArr = res
                    this.city.cityId    = cityArr[0].code
                    this.city.cityList  = cityArr
                    this.city.cityIndex = 0
                    this.regilist(cityArr[0].code)
                })
            },
            
            // 市级下拉筛选
            cityDrop(e) {
                let city     = this.city.cityList,
                    index    = e.detail.value,
                    citycode = city[index].code
                if (index != this.area.areaIndex) {
                    this.city.cityIndex = index
                    this.city.cityId    = citycode
        
                    // 获取市级列表
                    this.regilist(citycode)
                }
            },
            
            // 区列表
            regilist(areaCode) {
                children(areaCode).then(res=>{
                    this.regi.regiList  = res
                    this.regi.regiId    = res[0].code
                    this.regi.regiIndex = 0
                })
            },
            
            // 区下拉筛选
            regiDrop(e) {
                let newIndex = e.detail.value
                this.regi.regiIndex  = newIndex
                this.regi.regiId     = this.regi.regiList[newIndex].code
            },
            
            // 设为默认地址
            addressDefault(e) {
                this.fromData.defaultVal = e.detail.value
            },
            
            // 保存信息
            siteform(e) {
                add({
                    name        : e.detail.value.name,
                    mobile      : e.detail.value.mobile,
                    province_id : this.area.areaSn,
                    city_id     : this.city.cityId,
                    district_id : this.regi.regiId,
                    address     : e.detail.value.address,
                    def         : this.fromData.defaultVal
                }).then(res=>{
                    uni.navigateBack()
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .site-form {
        background: white;
        display: block;
        .site-input {
            padding: 0 30rpx 0 200rpx;
            position: relative;
            line-height: 90rpx;
            min-height: 90rpx;
            &::before {
                position: absolute;
                bottom: 0;
                left: 30rpx;
                right: 0;
                height: 1rpx;
                content: "";
                background: #e4e6f2;
            }
            &:last-child::before {
                display: none;
            }
            label {
                position: absolute;
                left: 30rpx;
                top: 0;
            }
            input {
                height: 90rpx;
            }
            image {
                width: 38rpx;
                height: 38rpx;
                position: absolute;
                right: 20rpx;
                top: calc(50% - 19rpx);
            }
        }
        .site-btn {
            padding: 20rpx 30rpx;
            button[size="mini"] {
                width: 100%;
                background: #f57e32;
                height: 80rpx;
                line-height: 80rpx;
                font-size: 30rpx;
                color: white;
                padding: 0;
            }
        }
        .site-switch {
            font-size: 32rpx;
            margin: 30rpx;
            display: flex;
            line-height: 40rpx;
            text {
                flex: 1;
            }
        }
        .site-switch-active {
            color: #797979;
        }
    }
</style>
