<template>
	<view class="">
		<view class="card">
			<up-form labelPosition="left" :model="formData" :rules="rules" ref="uForm" labelWidth="100">
				<up-form-item label="收货人" prop="real_name" borderBottom>
					<up-input v-model="formData.real_name" disabledColor="#ffffff" border="none"
						placeholder="请填写收货人姓名"></up-input>
				</up-form-item>
				<up-form-item label="联系电话" prop="phone" borderBottom>
					<up-input v-model="formData.phone" disabledColor="#ffffff" type="number" placeholder="请填写联系电话"
						border="none"></up-input>
				</up-form-item>
				<!-- <up-form-item label="所在位置" prop="address" borderBottom>
					<up-input v-model="formData.address" disabledColor="#ffffff" type="number"
						@click='showAddressPop=true' placeholder="点击选择位置" border="none"></up-input>
					<template #right>
						<up-icon name="arrow-right"></up-icon>
					</template>
				</up-form-item> -->
				<up-form-item label="详细地址" prop="detail" borderBottom>
					<up-input v-model="formData.detail" disabledColor="#ffffff" placeholder="请填写详细地址"
						border="none"></up-input>
				</up-form-item>
			</up-form>
		</view>
		<view class="card">
			<view class="is-default">
				<view>设置为默认地址</view>
				<up-switch v-model="formData.is_default" :activeValue="1" :inactiveValue="0"
					activeColor="#20B128"></up-switch>
			</view>
		</view>
		<view class="bottom-fixed">
			<up-button color="#20B128" shape="circle" @click="submit">保存</up-button>
		</view>
		<!-- 地址选择器 -->
		<up-popup :show="showAddressPop" @close="showAddressPop=false" @open="showAddressPop=true" :round="10">
			<view class='address-pop'>
				<view class="title">
					收货地址
				</view>
			</view>
		</up-popup>


	</view>
</template>

<script setup>
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import {
		ref
	} from "vue"
	import {
		addressCreateApi,
		addressEditApi,
		addressDetailApi
	} from "@/api/user.js"

	const mode = ref('add');
	const showAddressPop = ref(false)

	const formData = ref({
		real_name: '',
		phone: '',
		detail: '',
		is_default: 0,
		address: ""
	})
	const rules = ref({
		real_name: [{
			required: true,
			message: '请输入姓名',
			trigger: ['blur']
		}],
		phone: [{
			required: true,
			message: '请输入手机号',
			trigger: ['blur']
		}, {
			validator: (rule, value, callback) => {
				return uni.$u.test.mobile(value);
			},
			message: '手机号码不正确',
			trigger: ['change', 'blur'],
		}],
		detail: [{
			required: true,
			message: '请输入地址',
			trigger: ['blur']
		}]
	})
	const uForm = ref(null);
	const submit = () => {
		uForm.value.validate().then(() => {
			console.log('验证通过');
			if (mode.value == 'add') {
				addressCreateApi(formData.value).then(res => {
					uni.$u.toast(res.msg);
					uni.$u.sleep(800).then(res => {
						uni.navigateBack();
					})
				})
			} else {
				addressEditApi(formData.value).then(res => {
					uni.$u.toast(res.msg);
					uni.$u.sleep(800).then(res => {
						uni.navigateBack();
					})
				})
			}
		}).catch(() => {
			console.log('验证失败');
		})
	}

	const getAddressDetil = (address_id) => {
		addressDetailApi({
			address_id
		}).then(res => {
			formData.value = res.data;
		})
	}

	onLoad((options) => {
		if (options.mode == 'edit' && options.address_id) {
			mode.value = 'edit';
			uni.setNavigationBarTitle({
				title: '编辑地址'
			})
			getAddressDetil(options.address_id)
		} else uni.setNavigationBarTitle({
			title: '新增地址'
		})
	})
</script>

<style lang="scss">
	.card {
		margin: 20rpx;
		background-color: #fff;
		border-radius: 14rpx;
		padding: 0 30rpx;

		.is-default {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 0;
		}
	}

	.bottom-fixed {
		position: fixed;
		bottom: 0;
		left: 0;
		height: 120rpx;
		height: calc(constant(safe-area-inset-bottom) + 120rpx);
		/* 适用于iOS设备 */
		height: calc(env(safe-area-inset-bottom) + 120rpx);
		/* 适用于Android设备 */
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
		padding: 20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		/* 适用于iOS设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		/* 适用于Android设备 */
	}

	.address-pop {
		height: 60vh;
		padding: 20rpx;
		overflow-y: auto;

		.title {
			font-size: 32rpx;
			color: black;
			text-align: center;
		}
	}
</style>