<template>
	<view class="container">

		<!-- 物品信息 -->
		<view class="form-card">

			<view class="form-item">
				<view class="item-label">
					<input class="input-field" placeholder="姓名" placeholder-class="placeholder" maxlength="10"
						v-model="formData.contactName" />
				</view>
				<view class="item-control">
					<input class="input-field" placeholder="电话" placeholder-class="placeholder" maxlength="11"
						v-model="formData.contactPhone" />
				</view>
			</view>

			<picker mode="region" @change="changeAddress">
				<view class="form-item form-items">
					<text
						class="time-value placeholder">{{ formData.provinceName ? formData.provinceName + formData.cityName + formData.countyName : '省市区' }}</text>
					<text class="time-value value">{{ selectedTime }}</text>
					<u-icon class="arrow" name='arrow-right' size="18"></u-icon>
				</view>
			</picker>
			<view class="form-item">
				<view class="item-control">
					<input class="input-field" placeholder="详细地址" placeholder-class="placeholder"
						v-model="formData.detailedAddress" />
				</view>
			</view>


			<view class="form-item">
				<!-- 设置默认和清空 -->
				<view class="form-actions">
					<view class="action-left">
						<switch :checked="formData.defaultFlag == 1" color="#007AFF" @change="onDefaultChange" />
						<text class="action-text">设为默认寄件地址</text>
					</view>
					<view class="action-right" @click="clearForm">
						<text class="clear-text">清空</text>
					</view>
				</view>
			</view>
		</view>


		<view class="section-title" v-if="false">最近使用地址</view>

		<!-- 最近使用地址 -->
		<view class="recent-address" v-if="false">
			<!-- 地址项1 -->
			<view class="address-item" @click="onSelectAddress(addressItem)">

				<AddressInfo v-if="formData.id" :address="addressSend" />
				
			</view>

		
		</view>
		<!-- 下单按钮 -->
		<view class="submit-btn" @click="onConfirm">
			确定
		</view>

		<!-- 安全区域占位 -->
		<view class="safe-area"></view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import AddressInfo from '@/components/AddressInfo.vue'

	import {
		addBook,
		updateBook,
		getBook
	} from '@/api/address.js'

	const addType = ref('') //1 表示从创建订单过来  2 表示编辑  3表示创建

	// 表单数据
	const formData = reactive({
		contactName: '', //姓名
		contactPhone: '', //电话
		provinceName: '', //省
		cityName: '', //市
		countyName: '', //区
		addressId: null, //地址ID
		detailedAddress: '', //详细地址
		defaultFlag: 0 //是否默认 0非默认1默认
	})

	// 最近地址列表
	const recentAddresses = ref([{
			name: '袁添昊',
			phone: '13344642161',
			address: '湖北省荆州市新石南路747号'
		},
		{
			name: '袁添昊',
			phone: '13344642161',
			address: '湖北省荆州市新石南路747号'
		}
	])

	onLoad((option) => {
		if (option.id) {
			formData.addressId = option.id;
			getAddress()
			uni.setNavigationBarTitle({
				title:'编辑地址'
			})
		}
		
		addType.value = option.addType
		
	})
	const getAddress = async () => {
		let res = await getBook(formData.addressId);
		console.log(res.data, formData);
		let {
			addressId,
			contactName,
			contactPhone,
			countyName,
			defaultFlag,
			detailedAddress,
			provinceName,
			cityName
		} = res.data;

		// formData = {addressId,contactName,contactPhone,countyName,defaultFlag,detailedAddress,provinceName}
		Object.assign(formData, {
			addressId,
			contactName,
			contactPhone,
			cityName,
			countyName,
			defaultFlag,
			detailedAddress,
			provinceName
		});
	}
	// 选择地址
	const changeAddress = (e) => {
		formData.provinceName = e.detail.value[0]
		formData.cityName = e.detail.value[1]
		formData.countyName = e.detail.value[2]
	}


	// 省市区选择
	const onRegionChange = (e) => {
		formData.region = e.detail.value
	}

	// 默认地址切换
	const onDefaultChange = (e) => {
		formData.defaultFlag = e.detail.value ? 1 : 0
	}

	// 清空表单
	const clearForm = () => {
		formData.contactName = ''
		formData.contactPhone = ''
		formData.provinceName = ''
		formData.cityName = ''
		formData.countyName = ''
		formData.detailedAddress = ''
		formData.defaultFlag = 0
	}

	// 确定提交
	const onConfirm = async () => {
		// 这里可以添加表单验证
		if (!formData.contactName || !formData.contactPhone || !formData.detailedAddress) {
			uni.showToast({
				title: '请填写完整信息',
				icon: 'none'
			})
			return
		}
		
		if (!formData.provinceName) return uni.showToast({
				title: '请选择省市区',
				icon: 'none'
			})

		// 提交逻辑
		let api = formData.addressId ? updateBook(formData) : addBook(formData)
		let res = await api;

		console.log('提交数据:', formData)
		uni.showToast({
			title: '提交成功',
			icon: 'success'
		})
		
		if(res.code == 200){
			setTimeout(() => {
				// 返回上一页或执行其他操作
				formData.addressId = '-1'
				onSelectAddress(formData)				
				uni.navigateBack() // 返回上一页
			}, 400)
		}

	}
	
	const onSelectAddress = (address) => {
		console.log('-------address---',address)
		if (addType.value != 'sender' && addType.value != 'receiver') {
			return
		}
		uni.$emit('addressSelected', {
			type:addType.value, // 'sender' 或 'receiver'
			address
		})
	}
</script>

<style scoped lang="less">
	.container {
		background-color: #F5F7FA;
		min-height: 100vh;
		padding: 20rpx 20rpx 30rpx 20rpx;
	}

	.form-card {
		background-color: #fff;
		border-radius: 32rpx;
		padding: 0rpx 20rpx;
	}

	.form-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 32rpx;
	}


	.form-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
		border-bottom: 1rpx solid #F1F3F8;

		&:last-child {
			border-bottom: none;
			margin-bottom: 0;
		}

		.item-label {
			font-size: 28rpx;
			color: #666;
			font-weight: 400;

			&.required::before {
				content: '*';
				color: #ff4444;
				margin-right: 8rpx;
			}
		}

		.item-control {
			display: flex;
			align-items: center;

			&.btn {
				background: #fff;
				border: 1rpx solid #dcdfe6;
				border-radius: 5rpx;
				height: 59rpx;
				width: 266rpx;
				box-sizing: border-box;
			}

		}

		.input-field {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			padding: 0 24rpx;
			font-size: 28rpx;
			color: #333;
			text-align: left;


			&::placeholder {
				color: #999;
			}
		}

		.time-value {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333;
			line-height: 88rpx;
			height: 88rpx;
			padding: 0 24rpx;

			&.placeholder {
				color: #999;
			}

			.value {
				margin-right: 16rpx;
			}

			.arrow {
				margin-left: 32rpx;
				color: #999;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
	}

	.form-items {
		&:last-child {
			border-bottom: 1rpx solid #F1F3F8;
		}
	}

	/* 寄件标识 */
	.send-tag {
		padding: 40rpx 30rpx 20rpx;
		background-color: #fff;
	}

	.send-circle {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		background-color: #007AFF;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-weight: bold;
	}

	/* 表单区域 */
	.form-container {
		background-color: #fff;
		padding: 0 30rpx;
		margin-bottom: 20rpx;
	}

	.form-title {
		font-size: 18px;
		font-weight: 500;
		color: #333;
		padding: 30rpx 0 20rpx;
		border-bottom: 1rpx solid #eee;
	}

	.form-item {
		width: 100%;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.form-input {
		font-size: 16px;
		height: 40rpx;
		line-height: 40rpx;
		color: #333;
	}

	.form-input::placeholder {
		color: #999;
	}

	.picker-placeholder {
		font-size: 16px;
		height: 40rpx;
		line-height: 40rpx;
		color: #999;
	}

	.picker-placeholder.has-value {
		color: #333;
	}

	/* 表单操作 */
	.form-actions {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 0;
	}

	.action-left {
		display: flex;
		align-items: center;
	}

	.action-text {
		font-size: 28rpx;
		color: #333;
		margin-left: 10rpx;
	}

	.action-right {
		padding: 10rpx 20rpx;
	}

	.clear-text {
		height: 44rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #1B64F0;
		line-height: 44rpx;
	}

	/* 最近地址 */
	.recent-address {
		background-color: #fff;
		padding: 0 30rpx;
	}

	.section-title {
		height: 48rpx;
		font-size: 32rpx;
		color: #333333;
		line-height: 48rpx;
		margin: 20rpx 0rpx;
		font-weight: bold;
	}

	.address-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.address-info {
		flex: 1;
	}

	.address-name {
		font-size: 16px;
		color: #333;
		margin-bottom: 10rpx;
	}

	.address-detail {
		font-size: 14px;
		color: #666;
		line-height: 1.4;
	}

	.address-select {
		width: 40rpx;
		height: 40rpx;
		border: 1rpx solid #007AFF;
		border-radius: 50%;
		margin-left: 20rpx;
	}


	.safe-area {
		height: 140rpx;
	}

	.submit-btn {
		position: fixed;
		bottom: 40rpx;
		left: 30rpx;
		right: 30rpx;
		width: 686rpx;
		height: 88rpx;
		background: #1B64F0;
		border-radius: 44rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		z-index: 10;
	}
</style>