<template>
	<view class="page-container">
		<view class="location-card" @click="chooseLocation">
			<view class="left-content">
				<u-icon name="map-fill" color="#1ecbc3" size="24"></u-icon>
				<view class="info">
					<text class="label">我的位置</text>
					<text class="value">{{ currentLocation }}</text>
				</view>
			</view>
			<u-icon name="arrow-right" color="#999" size="16"></u-icon>
		</view>

		<view class="list-title">
			<text>虚拟地址</text>
			<text class="more" @click="goToAdd">新增虚拟地址</text>
		</view>
		
		<view class="address-list">
			<u-radio-group v-model="selected" placement="column" activeColor="#0879FF" @change="onChange">
				<view 
					class="address-item" 
					v-for="(item, index) in addressList" 
					:key="item.id"
				>
					<view class="check-icon">
						<u-radio :name="item.id" />
					</view>

					<view class="address-info">
						<view class="name">{{ item.name }}</view>
						<view class="detail">{{ item.address }}</view>
					</view>

					<view class="delete-btn" @click.stop="onDelete(item)">
						<u-icon name="trash" color="#999" size="22"></u-icon>
					</view>
				</view>
			</u-radio-group>
		</view>

		<u-gap height="100"></u-gap>

		<view class="footer-bar">
			<u-button 
				text="保存" 
				color="#1ecbc3" 
				shape="circle" 
				size="large"
				customStyle="height: 48px; font-size: 16px;"
				@click="onSave"
			></u-button>
		</view>

		<u-modal 
			:show="isShow" 
			title="" 
			content="确定要删除该地址吗？" 
			showCancelButton
			confirmColor="#1ecbc3"
			@confirm="onDelete"
			@cancel="isShow = false"
		></u-modal>
	</view>
</template>

<script>
import {
	getInfo,
	getJsLocation,
} from '@/api/index';
import {
	netVirtualAddrAdd,
	netVirtualAddrDel,
	getVirtualAddrByPhone
} from '@/api/address';
export default {
	data() {
		return {
			query: {},
			selected: '',
			currentLocation: '信达国际金融中心',
			isShow: false,
			// 数据
			addressList: [
				{
					id: 1,
					name: '信达国际金融中心',
					address: '山西省太原市万柏林区迎泽西大街86号',
				},
				{
					id: 2,
					name: '信达国际金融中心',
					address: '山西省太原市万柏林区迎泽西大街86号',
				},
				{
					id: 3,
					name: '信达国际金融中心',
					address: '山西省太原市万柏林区迎泽西大街86号',
				}
			]
		}
	},
	onLoad(query) {
		this.query = query;
	},
	onShow() {
		let sl = uni.getStorageSync('staffLocation');
		if (sl) {
			this.selected = sl.addrId;
			this.currentLocation = sl.address + sl.name;
		}
		this.getData();
	},
	methods: {
		getData() {
			getVirtualAddrByPhone({ phone: this.query.phone }).then(res => {
				if (res.data.code === 200) {
					this.addressList = res.data.data;
				}
			});
		},
		// 选择地址（单选逻辑）
		onChange(e) {
			// console.log(e)
		},
		// 点击我的位置
		chooseLocation() {
			console.log('点击我的位置');
		},			
		// 删除确认
		onDelete(item) {
			uni.showModal({
				title: '提示',
				content: '确定要删除该地址吗？',
				success: (res) => {
					if (res.confirm) {
						netVirtualAddrDel({ id: item.id }).then(res => {
							uni.$u.toast(res.data.msg);
							if (res.data.code === 200) {
								this.getData();
							}
						});
					}
				}
			});
			// this.isShow = true;
		},
		// 点击保存
		onSave() {
			const selected = this.addressList.find(item => item.id === this.selected);
			if(selected) {
				console.log('保存选中的地址:', selected);
				let params = {
					id: this.query.id,
					cPhone: this.query.phone,
					name: selected.name,
					address: selected.address,
					latitude: selected.latitude,
					longitude: selected.longitude,
					cOpenId: uni.getStorageSync('wx_copenid')
				}
				// 更新商户的位置
				getJsLocation(params).then(res => {
                    uni.setStorageSync('staffLocation', {
						addrId: selected.id,
						name: selected.name,
						address: selected.address,
						latitude: selected.latitude,
						longitude: selected.longitude,
					});
					if (res.data.code === 200) {
						uni.navigateBack();
					}
				});
			} else {
				uni.$u.toast('请选择一个地址');
			}
		},
		// 跳转新增
		goToAdd() {
			uni.chooseLocation({
				success: res => {
					console.log(res)
					// address: "融创长风壹号东门北120米"
					// distance: 57
					// errMsg: "chooseLocation:ok"
					// latitude: 37.826281
					// longitude: 112.516064
					// name: "中国电信融创长风壹号营业厅"
					let params = {
						name: res.name,
						address: res.address,
						atlasAdd: res.atlasAdd,
						latitude: res.latitude,
						longitude: res.longitude,
						type: 2,
						userType: 2,
						// user
						openid: uni.getStorageSync('wx_copenid'),
						phone: this.query.phone,
						userName: this.query.name,
					}
					// 更新数据
					netVirtualAddrAdd(params).then(res => {
						this.getData();
					});
				}
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.page-container {
	min-height: 100vh;
	padding: 20rpx;
	padding-bottom: 120rpx; 
	background-color: #F6F7F9;

	.location-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.02);

		.left-content {
			display: flex;
			align-items: center;
			
			.info {
				margin-left: 20rpx;
				display: flex;
				flex-direction: column;
				
				.label {
					font-size: 24rpx;
					color: #999;
					margin-bottom: 8rpx;
				}
				.value {
					font-size: 30rpx;
					font-weight: bold;
					color: #333;
				}
			}
		}
	}

	.list-title {
		font-size: 26rpx;
		color: #999;
		padding: 0 10rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.more {
			color: #1ecbc3;
		}
	}

	.address-item {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.02);
		transition: opacity 0.2s;

		.check-icon {
			margin-right: 20rpx;
		}

		.address-info {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin-right: 20rpx;

			.name {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 10rpx;
			}
			
			.detail {
				font-size: 26rpx;
				color: #666;
				line-height: 1.4;
			}
		}

		.delete-btn {
			padding: 10rpx;
			display: flex;
			align-items: center;
		}
	}
}
</style>