<template>
	<view class="page">
		<view class="row b-b">
			<text class="tit">收货人</text>
			<input class="input" type="text" v-model="name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">联系电话</text>
			<input class="input" type="number" v-model="mobile" placeholder="联系电话" placeholder-class="placeholder" />
		</view>
		<view class="row b-b" v-on:click="chooseLoc()">
			<text class="tit">收货地址</text>
			<view class="input clamp2" :class="{placeholder: !addr}">
				{{addr ? addr :'定位地址'}}
			</view>
			<image src="/static/r-arrow.png" mode="aspectFill" class="ic"></image>
		</view>
		<view class="row b-b">
			<text class="tit">详细地址</text>
			<input class="input clamp2" type="text" v-model="desc" placeholder="详细地址,楼号" placeholder-class="placeholder" />
		</view>

		<view class="row default-row">
			<div class="l flex-col justify-center">
				<text class="tit">设为默认</text>
				<text class="tips">每次下单会默认推荐使用该地址</text>
			</div>
			
			<switch :checked="is_default" color="#92B99C" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
		
	
	</view>
</template>

<script>
	import {chooseLoc} from '@/common/loc.js';
	export default {
		data() {
			return {
				name: '',
				mobile: '',
				addr:'',
				desc:'',
				is_default:'',
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			if (option.type === 'edit') {
				// this.getInfo(option.id);
				title = '编辑收货地址'
			} else {
				// this.$refs.mpvueCityPicker.creat(this.pickerValueDefault);
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({ 
				title
			})
		},
		methods: {
			// 获取地址详情
			async getInfo(id){
				let  res= await this.$api.request(`/addons/unishop/address/info?id=${id}`);
				let addressData = res.data;
				if (addressData) {
					console.log(addressData);
					
					this.addressData = addressData;
					let pickerValueDefault = [];
					pickerValueDefault.push(addressData.province_id);
					pickerValueDefault.push(addressData.city_id);
					pickerValueDefault.push(addressData.area_id);
					this.pickerValueDefault = pickerValueDefault;
					
					this.$refs.mpvueCityPicker.creat(pickerValueDefault);
				}
			},
			// 城市选择器
			async chooseLoc() {
				let  {
				   province,
				   city, 
				   area,
				   address,
				   latitude,
				   longitude,
				} =await chooseLoc();
				this.addr = `${province}${city} ${area}`;
				this.desc = address;
			},
			//默认地址
			switchChange(e) {
				this.is_default = e.detail.value;
			},
			//提交
			async confirm() {
				//Deep Clone
				if(this.$api.inAction('confirm',this)){
					return;
				}
				let data = JSON.parse(JSON.stringify(this.addressData));
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.address) {
					this.$api.msg('请填详细地址信息');
					return;
				}
				console.log(data.is_default);
				data.is_default = data.is_default == true ? 1 : 0;
				let action = this.manageType == 'edit' ? 'edit' : 'add';
				let result = await this.$api.request('/addons/unishop/address/' + action, 'POST', data);
				if (result) {
					this.$api.prePage().refreshList(data, this.manageType);
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}
	.page{
		padding: 16rpx 30rpx 0;
		min-height: 100vh;
		padding-bottom: calc(var(--safe-area-inset-bottom) / 2);
	}
	.row {
		display: flex;
		align-items: center;
		position: relative;
		height: 110upx;
		background: #fff;
		border-bottom: 2rpx solid #eee;
		.tit {
			flex-shrink: 0;
			margin-right: 30rpx;
			font-size: 28upx;
			color: #333;
		}
		.tips{
			margin-top: 20rpx;
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}
		.input {
			flex: 1;
			font-size: 28upx;
			color: #333;
			line-height: 36rpx;
		}
        .ic{
			flex-shrink: 0;
			width: 24rpx;
			height: 24rpx;
			margin-left: auto;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			margin-left: auto;
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 120upx auto 10rpx;
		font-size: 30rpx;
		color: #fff;
		background-color: $base-color;
		border-radius: 38upx;
	}
</style>
