<template>
	<view class="page">
		<view class="row b-b">
			<text class="tit">收货人</text>
			<input class="input" type="text"  placeholder="收货人姓名" placeholder-class="placeholder" v-model="consignee" />
		</view>
		<view class="row b-b">
			<text class="tit">联系电话</text>
			<input class="input" type="number"  placeholder="联系电话" placeholder-class="placeholder" v-model="mobile" />
		</view>
		<view class="row b-b" v-on:click="chooseLoc()">
			<text class="tit">收货地址</text>
			<view class="input clamp2" :class="{placeholder: !addrTop}">
				{{addrTop ? addrTop :'定位地址'}}
			</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"  placeholder="详细地址,楼号" placeholder-class="placeholder" v-model="address" />
		</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=='1' ? true :false" color="#92B99C" @change="switchChange" />
		</view>
		
		
		<submit-btn @submit="submit" ref="submitRef">
			<template v-slot:btns="{isSubmitted}">
				<view class="add-btn" :class="{'disabled':isSubmitted}" >提交</view>
			</template>
		</submit-btn>
	
	</view>
</template>

<script>
	import {chooseLoc} from '@/common/loc.js';
	export default {
		data() {
			return {
				sourcePage: '', //mang
				consignee: '',
				mobile: '',
				addrTop:'',
				is_default:1,
				
				province:'',
				city:'',
				district:'',
				address:'',
				address_lng:'',
				address_lat:'',
			}
		},
		onLoad(option) {
			this.sourcePage = option.sourcePage;
			let title = '新增收货地址';
			if (+option.id) {
				this.id = option.id;
				this.getInfo(option.id);
				title = '编辑收货地址'
			} else {
				// this.$refs.mpvueCityPicker.creat(this.pickerValueDefault);
			}
			uni.setNavigationBarTitle({ 
				title
			})
		},
		methods: {
			// 获取地址详情
			async getInfo(id){
				let  res= await this.$request(`addr.info`,{id});
				let data = res.data || {};
				this.consignee = data.consignee;
				this.mobile = data.mobile;
				this.province = data.province;
				this.city = data.city;
				this.district = data.district;
				this.address = data.address;
				this.is_default = data.is_default;
				this.address_lng = data.address_lng;
				this.address_lat = data.address_lat;
				this.addrTop = `${data.province}${data.city}${data.district}`;
			},
			// 城市选择器
			async chooseLoc() {
				let  {
				  province,
				  city, 
				  district,
				  desc,
				  latitude,
				  longitude,   
				} =await chooseLoc();
				this.addrTop = `${province}${city}${district}`;
				
				this.province = province;
				this.city = city;
				this.district = district;
				this.address = desc;
				this.address_lng = longitude;
				this.address_lat = latitude;
			},
			//默认地址
			switchChange(e) {
				this.is_default = e.detail.value ? 1 : 0;
			},
			//提交
			async submit() {
				if (!this.consignee) {
					this.$tools.msg('请填写收货人姓名');
					return;
				}
				if (!this.$tools.match(this.mobile,'mobile')) {
					return;
				}
				if (!this.addrTop) {
					this.$tools.msg('请选择地址');
					return;
				}
				if (!this.address) {
					this.$tools.msg('请填详细地址信息');
					return;
				}
				let sendData = {
					consignee :this.consignee,
					mobile :this.mobile,
					province :this.province,
					city :this.city,
					district :this.district,
					address :this.address,
					is_default :this.is_default,
					address_lng :this.address_lng,
					address_lat :this.address_lat,
				};
				if(this.id){
					sendData = {...sendData,id:this.id}
				}
				let res = await this.$request('addr.save' , sendData);
				if(!this.id){
					sendData = {...sendData,id:res.data.id}
				}
				this.$refs.submitRef.chgeStatus();
				// 无默认地址直接添加情况
				// if(this.$tools.prePage().setDefaultAddr){
				// 	this.$tools.prePage().setDefaultAddr(sendData)
				// }
				//盲盒页进
				if(this.sourcePage === 'mang'){
					this.$store.commit('userInfo/ADDR_INFO',sendData)
				}
				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;
		&.disabled{
			background-color: #eee;
		}
	}
</style>
