<template>
    <view class="content">
        <form @submit="siteform" class="site-form">
        	<view class="site-input">
        		<label>收货人</label>
        		<input placeholder="请输入收货人姓名" name="name" :value="site.name"></input>
        	</view>
        	<view class="site-input">
        		<label>手机号码</label>
        		<input placeholder="请输入手机号码" maxlength="11" name="mobile" type="number" :value="site.mobile"></input>
        	</view>
        	<view class="site-input">
                <label>所在省份</label>
                <picker @change="areasChange" :value="area.areaIndex" :range="area.areasArr" :range-key="'name'">
                    <view class="picker" v-if="area.areasArr[area.areaIndex]">
                        {{ area.areasArr[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.cityArr" :range-key="'name'" class="conneColor">
                    <view class="picker" v-if="city.cityArr[city.cityIndex]">
                        {{ city.cityArr[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.regiArr" :range-key="'name'" class="conneColor">
                    <view class="picker" v-if="regi.regiArr[regi.regiIndex]">
                        {{ regi.regiArr[regi.regiIndex].name }}
                    </view>
        			<image src="/static/icon/rightsArrow.png"></image>
                </picker>
            </view>
        	<view class="site-input">
        		<label>收货地址</label>
        		<input placeholder="请输入详细地址" name="address" :value="site.address"></input>
        	</view>
        	<view class="site-switch">
                <text>设置默认地址</text>
                <switch style='zoom:.6;' :checked="site.defaultVal" @change="addressDefault" color="#1d37e2"/>
            </view>
        	<view class="site-btn">
        		<button form-type="submit" size="mini" :disabled="disabled">保存</button>
        	</view>
        </form>
    </view>
</template>

<script>
	import { userIndex } from '@/apis/interfaces/user'
    import { siteAdd, siteSee } from '@/apis/interfaces/stock'
    import { create } from '@/apis/interfaces/mall'
    export default {
    	data() {
    		return {
                site: {
                    name      : '',  // 姓名
                    mobile    : '',  // 电话
                    address   : '',  // 地址
                    defaultVal: '',  // 默认地址
                },
                // 省份选择
                area: {
                    areasArr   : [],
                    areaId     : '',
                    areaIndex  : 0,
                },
                        
                // 市级选择
                city: {
                    cityArr    : [],
                    cityId     : 0,
                    cityIndex  : 0,
                },
                        
                // 区域选择
                regi: {
                    regiArr    : [],
                    regiId     : 0,
                    regiIndex  : 0,
                },
                disabled       : false
    		}
    	},
		created() {
			userIndex().then(res => {
				this.site.name = res.nickname
				this.site.mobile = res.username
			}).catch(err => {
				uni.showToast({
					title: err.message,
					icon : 'none'
				})
			})
		},
    	onShow() {
            if(this.$Route.query.experience) {
                this.area.areaIndex = 18
            }
            // 获取省市区列表
    	    this.createInfo();
            //if(this.$Route.query.ediStatet == 'compile') {
                // 获取地址详情
                // this.createSee();
            //}
    	},
    	methods: {
            // 地址详情
            // createSee() {
            //     siteSee(this.$Route.query.id).then(res => {
            //         this.site.name        = res.name
            //         this.site.mobile      = res.mobile
            //         this.site.address     = res.address
            //         this.site.defaultVal  = res.default
            //     }).catch(err => {
            //     	uni.showToast({
            //     		title: err.message,
            //     		icon: "none"
            //     	})
            //     })
            // },
            
            // 省市区列表
            createInfo() {
                create().then(res => {
                    let areas         =  res,
                        areaIndex     =  this.area.areaIndex
                    this.area.areaId  = areas[areaIndex].id
                    this.area.areasArr= areas
                    this.citylist(areas[areaIndex].id)
                }).catch(err => {
                	uni.showToast({
                		title: err.message,
                		icon: "none"
                	})
                })
            },
            
            // 所在省份-下拉
            areasChange(e) {
                let area     = this.area.areasArr,
                    index    = e.detail.value,
                    atcode   = area[index].id
                if (index != this.area.areaIndex) {
                    this.area.areaIndex  = index
                    this.area.areaId     = atcode
                    // 获取市级列表
                    this.citylist(atcode)
                }
            },
            
            // 市级列表
            citylist(cityId) {
                create({
                    parent_id: cityId
                }).then(res=>{
                    let cityArr = res
                    // 从体验官入口进入 默认为深圳
                    if(this.$Route.query.experience) {
                        this.city.cityId    = cityArr[2].id
                        this.city.cityIndex = 2
                        this.city.cityArr   = cityArr
                        this.regilist(cityArr[2].id)
                        return
                    }
                    
                    this.city.cityId    = cityArr[0].id
                    this.city.cityIndex = 0
                    this.city.cityArr   = cityArr
                    this.regilist(cityArr[0].id)
                })
            },
            
            // 市级下拉筛选
            cityDrop(e) {
                let city     = this.city.cityArr,
                    index    = e.detail.value,
                    citycode = city[index].id
                if (index != this.area.areaIndex) {
                    this.city.cityIndex = index
                    this.city.cityId    = citycode
                    
                    // 获取市级列表
                    this.regilist(citycode)
                }
            },
            
            // 区列表
            regilist(areaId) {
                create({
                    parent_id: areaId
                }).then(res=>{
                    this.regi.regiArr   = res
                    this.regi.regiId    = res[0].id
                    this.regi.regiIndex = 0
                })
            },
            
            // 区下拉筛选
            regiDrop(e) {
                let newIndex = e.detail.value
                this.regi.regiIndex  = newIndex
                this.regi.regiId     = this.regi.regiArr[newIndex].id
            },
            
            // 设为默认地址
            addressDefault(e) {
                this.site.defaultVal = e.detail.value
            },
            
            // 提交表单
            siteform(e) {
                let value = e.detail.value
                let data = {
                    name        : value.name,
                    mobile      : value.mobile,
                    address     : value.address,
                    province_id : this.area.areaId,
                    city_id     : this.city.cityId,	
                    district_id : this.regi.regiId,
                    is_default  : this.site.defaultVal
                }
                siteAdd(data).then(res => {
                    this.disabled = true
                    this.$Router.back()
                }).catch(err => {
                	uni.showToast({
                		title: err.message,
                		icon : 'none'
                	})
                })
            }
    	}
    }
</script>

<style lang="scss" scoped>
    .site-form {
        background: white;
        display: block;
        .site-input {
            padding: 0 30rpx 0 200rpx;
            position: relative;
            line-height: 110rpx;
            min-height: 110rpx;
            border-bottom: 2rpx solid #f3f3f3;
            &::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: 110rpx;
            }
            image {
                width: 38rpx;
                height: 38rpx;
                position: absolute;
                right: 20rpx;
                top: calc(50% - 19rpx);
            }
        }
        .site-btn {
            padding: $padding - 10 $padding;
            button[size="mini"] {
                width: 100%;
                background: #1d37e2;
                height: 88rpx;
                line-height: 88rpx;
                font-size: $title-size-lg;
                color: white;
                padding: 0;
                &[disabled] {
                    background: #7789ff !important;
                    color: #fff !important;
                }
            }
        }
        .site-switch {
            font-size: $title-size;
            margin: $margin;
            display: flex;
            line-height: 40rpx;
            text {
                flex: 1;
            }
        }
        .site-switch-active {
            color: #797979;
        }
    }
</style>
