<template>
	<view class="container clearfix">
		<view class="add-content">
			<view class="text-input b-b">
				<input class="input" type="text" v-model="addressData.shouHuoRen" @input="onShouHuoRen" placeholder="收货人姓名" placeholder-class="placeholder" />
			</view>
			<view class="text-input b-b">
				<input class="input" type="number" maxlength="11" v-model="addressData.mobile"  @input="onMobile" placeholder="手机号码" placeholder-class="placeholder" />
			</view>
			<view class="text-input b-b" @click="showMulLinkageThreePicker">
				<text class="input">
					{{addressData.allAddress}}
				</text>		
				<text class="iconfont icon-xiayibu"></text>
			</view>
			<view class="text-textarea b-b"> 
				<view class="textarea show" v-if="isShowInput">{{addressData.addressDetail ?addressData.addressDetail :'详细地址：如道路、门牌号、小区、楼房号、单元室等'}}</view>
				<textarea 	v-else
							class="textarea" 
							type="text" 
							v-model="addressData.addressDetail" 
							placeholder="详细地址：如道路、门牌号、小区、楼房号、单元室等" 
							placeholder-class="placeholder"
							maxlength="50"
							@input="onTextareaInput"
							:class="isShowInput ? '':''"
				/>
			</view>
			<view class="text-input default-row">
				<text class="tit">设为默认地址</text>
				<switch :checked="switchDefault" color="#5FB00A" @change="switchChange" style="transform:scale(0.8)" />
			</view>
			<button class="add-btn" :disabled="isConfirm" :class="[isConfirm == true ? 'disabled':'']" @click="confirm">{{buttonText}}</button>
		</view>
		<mpvue-city-picker :themeColor="themeColor" 
							ref="mpvueCityPicker" 
						    :pickerValueDefault="cityPickerValueDefault"
							@onCancel="onCancel" 
							@onConfirm="onConfirm">
		</mpvue-city-picker>

	</view>
</template>

<script>	
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	import authorize from '@/common/config/authorize.js'	
	import { addNewAddress,updateAddress } from '@/api/cart.js'
	export default {
		components:{
			mpvueCityPicker
		},
		data() {
			return {
				buttonText:'保存',
				addType:1,				//记录添加地址的类型
				isConfirm:true,
				isShowInput:false,
				switchDefault:false,
				addressData:{
					addressID:'',
					allAddress:'所在地区',
					userID: '',			//用户id ,只在新增收货地址时传
					shouHuoRen: '',		//收货人
					mobile:'',			//收货人手机
					townID:'',			//区ID
					addressDetail: '',	//地址
					defaultFlag:0,		//是否默认收货地址(0 不是默认,1 默认)
				},
				params:{}				//参数
			}
		},
		onLoad(option){
			let title = '添加新地址'
			if(option.type==='edit'){
				title = '编辑收货地址'
				this.isConfirm = false;
				this.addType = 2;			
				let optionData = JSON.parse(option.data)
				this.addressData.allAddress = `${optionData.province}-${optionData.city}-${optionData.town}`
				this.addressData.addressDetail = optionData.address
				this.addressData.defaultFlag = optionData.defaultFlag;
				this.addressData.userID = optionData.userID;
				this.addressData.mobile = optionData.mobile;
				this.addressData.townID = optionData.townID;
				this.addressData.addressID = optionData.addressID;
				this.addressData.shouHuoRen = optionData.shouHuoRen;
				this.addressData.addressID = optionData.addressID;
				if(this.addressData.defaultFlag == 1){
					this.switchDefault = true
				}else{
					this.switchDefault = false
				}
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {			
			switchChange(e){
				if(e.detail.value== true){
					this.addressData.defaultFlag = 1;
				}else{
					this.addressData.defaultFlag = 0;
				}
			},
			// 三级联动选择
			showMulLinkageThreePicker() {
				this.isShowInput = true
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				console.log(e)
				this.addressData.allAddress = e.name;
				this.addressData.townID = e.townCode;	
			},			
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.shouHuoRen){
					this.$util.msg('请填写收货人');
				}
				if(data.address =='所在地区'){
					this.$util.msg('请选择所在地区');
				}
				if(!/(^1[0-9][0-9]{9}$)/.test(data.mobile)){
					this.$util.msg('请输入正确的手机号码');
					return;
				}
				if(this.addType ==1){
					this.params ={
							userID:data.userID  ,		//用户id ,只在新增收货地址时传
							shouHuoRen:data.shouHuoRen ,//收货人
							townID:data.townID,			//区ID
							addressDetail:data.addressDetail,//地址
							mobile:data.mobile,			//手机
							defaultFlag:data.defaultFlag//是否默认收货地址(0 不是默认,1 默认)
						}
					console.log(this.params)
					this.postAddressData(this.params)	
				}else{
					this.params =  this.addressData;
					console.log(this.params)
					this.postAddressData(this.params)
				}
			},
			postAddressData(params){
				this.btnText(true)
				addNewAddress(params).then( response=>{
					this.$util.msg('保存成功',1500,true,'success')
					this.btnText(false)
					setTimeout(()=>{
						uni.navigateBack();
					},2000)
				}).catch(response =>{
					this.$util.msg(response.msg,2000);
					this.btnText(false)
				})
			},
			btnText(flg){
				if(flg){
					this.isConfirm = true;
					this.buttonText = '保存中...'
				}else{
					this.isConfirm = false;
					this.buttonText = '保存'
				}
			},
			onShouHuoRen(e){
			   this.addressData.shouHuoRen = e.detail.value;
			   this.initInput();
			},
			onMobile(e){
			   this.addressData.mobile = e.detail.value;
			   this.initInput();
			},
			onTextareaInput(e){
			   this.addressData.addressDetail = e.detail.value;
			   this.initInput();
			},
			initInput(){
				if(this.addressData.shouHuoRen !== "" && this.addressData.mobile !=="" && this.addressData.addressDetail !==""){
					this.isConfirm =false;
				}else{
					this.isConfirm =true;
				}
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) =>{
				this.addressData.userID = resolve.userID
			})
		}
	}
</script>

<style lang="scss">
	page{
		height: auto;
		background: $bg-color;
		border-top: 1px solid #EBEBEB;
	}
	.add-content{
		width: 702rpx;
		padding:0 24rpx;
	}
	.icon-xiayibu{
		font-size: 36rpx;
		color: $text-color;
	}
	.text-textarea{
		width: 654rpx;
		height: 216rpx;
		padding: 24rpx;
		background: #F7F7F7;
		.textarea{
			width: 100%;
			height: 100%;
			font-size: $font-size-28;
			color: $text-color;
			z-index: 1;
		}
		.textarea.hide{
			opacity: 0;
		}
		.textarea.show{
			color: #999999;
		}
	}
	.default-row{
		background: #FFFFFF;
		margin-top: 16upx;
		.tit{
			font-size: $font-size-28;
			line-height: 40rpx;
			color: #333333;
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		width: 702rpx;
		height: 88rpx;
		font-size: $font-size-28;
		line-height: 88rpx;
		color: #FFFFFF;
		margin: 0 auto;
		text-align: center;
		background: $btn-confirm;
		border-radius: 14rpx;
		margin-top: 80rpx;
	}
	.add-btn.disabled{
		background: #F8F8F8;
		border-radius: 14rpx;
	}
</style>

