<template>
	<view class="content">
		<view class="cell">
			<text>收件人</text>
			<u-input v-model="address.name" :type="type" :border="border" placeholder="请输入收件人姓名" :clearable="false" />
		</view>
		<view class="cell">
			<text>联系电话</text>
			<u-input v-model="address.phone" :type="type" :border="border" placeholder="请输入联系电话" :clearable="false" />
		</view>
		<view class="cell">
			<text>收货地址</text>
			<u-input v-model="address.address" :type="type" :border="border" placeholder="请输入收货地址" :clearable="false" />
		</view>
		<view style="margin: 100upx auto;width: 30%;">
			<u-button type="success" @click="baocun">保存</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isAdd:"",//判断是否为新增
				address: {
					name: "", //姓名
					phone: "", //联系电话
					address: "" //地址
				} //地址信息
			}
		},
		onLoad(opction) { //代替 vue 里面的 created
			this.address = opction
			if(JSON.stringify(opction) === '{}'){
				this.isAdd=true
			}else{
				this.isAdd=false
			}
			console.log(this.isAdd);
		},
		onReady() { //代替 vue 里面的 mounted
		},
		methods: {
			baocun() { //保存收货地址
				if(this.isAdd){
					if (this.$u.test.mobile(this.address.phone)) { //手机号验证
						let data = {
							name: this.address.name,
							phone: this.address.phone,
							address: this.address.address,
							openid: uni.getStorageSync('openid'),
							credentialNumber:"500226199407035525",
							credentialType:1,
							category:0
						}
						this.$request('wechatUser/contact/saveContact', data).then(res => {
							if (res.code == '00') { //保存成功后跳转
								this.$u.route("pages/vlog/orderPay/orderPay")
								uni.showToast({
									title: '保存成功',
									duration: 1000
								});
							}else{
								uni.showModal({
									showCancel: false,
									title: '提示',
									content: res.message,
								});
							}
						})
					} else {
						uni.showModal({
							showCancel: false,
							title: '提示',
							content: "请正确输入手机号!",
						});
					}
				}else{
					if (this.$u.test.mobile(this.address.phone)) { //手机号验证
						let data = {
							id: this.address.id,
							name: this.address.name,
							phone: this.address.phone,
							address: this.address.address,
							openid: uni.getStorageSync('openid')
						}
						this.$request('wechatUser/contact/updateContact', data).then(res => {
							if (res.code == '00') { //保存成功后跳转
								this.$u.route("pages/vlog/orderPay/orderPay")
								uni.showToast({
									title: '保存成功',
									duration: 1000
								});
							}else{
								uni.showModal({
									showCancel: false,
									title: '提示',
									content: res.message,
								});
							}
						})
					} else {
						uni.showModal({
							showCancel: false,
							title: '提示',
							content: "请正确输入手机号!",
						});
					}
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 0 20rpx;
		box-sizing: border-box;
		margin: 0 auto;
	}

	.cell {
		display: flex;
		align-items: center;
		height: 80upx;
		line-height: 80upx;
		border-bottom: 1upx solid #ccc;

		text {
			width: 20%;
			margin-left: 30upx;
		}
	}
</style>
