<template>
	<view>
		<el-popup ref="popup" direction="bottom" backgroundColor="rgb(248, 248, 248)" @close="onClose">
			<view class="title">请选择收货地址</view>
			<view style="width: 100%; height: 70rpx;"></view>
			<scroll-view :style="{ 'height' : (windowHeight / 2) + 'px' }" scroll-y>
				<view class="wrapper">
					<view class="list-wrap">
						<view 
						class="list-box" 
						v-for="(item,index) in list" 
						:key="index"
						@click="onSelected(item)"
						>
							<view class="address">
								<view>
									<text>{{ item.name }}</text>
									<text>{{ item.mobile }}</text>
								</view>
								<view>
									{{ item.address }}
								</view>
							</view>
							<view class="selected">
								<view :class="{ active: !isEmpty(address) && item.id == address.id }">
									<uni-icons class="icon" type="checkmarkempty" color="#fff" size="12"></uni-icons>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			
			<view class="footer">
				<text class="btn" @click="navigateTo('/pages/ucenter/address/editor')">新增地址</text>
			</view>
		</el-popup>
	</view>
</template>

<script setup>
	import { nextTick, onMounted, ref, watch } from "vue";
	import { inArray,isEmpty } from "@/el-ui/libs/check.js";
	import { navigateTo } from "@/el-ui/libs/navigate";
	
	const props = defineProps({
		modelValue: {
			type: Boolean,
			default: false
		}
	});
	
	const emits = defineEmits(["update:modelValue","change"]);
	
	const { windowHeight } = uni.getSystemInfoSync();
	
	const popup = ref();
	onMounted(()=>{
		nextTick(()=>{
			props.modelValue && popup.value?.open();
			getList();
		})
	})
	
	const address = ref({});
	const list 	  = ref([]);
	const getList = ()=>{
		uni.$http.get("checkout/address").then(res=>{
			if(res.code){
				list.value = res.data;
				list.value.forEach((item)=>{
					if(item.is_default){
						address.value = item;
						emits("change",item);
					}
				});
			}
		});
	}
	
	const onSelected = (value)=>{
		address.value = value;
		emits("update:modelValue",false);
		emits("change",value);
	}
	
	const onClose = ()=>{
		emits("update:modelValue",false);
	}
	
	watch(()=>props.modelValue,(value)=>{
		if(value){
			nextTick(()=>{
				popup.value?.open();
			})
		}else{
			nextTick(()=>{
				popup.value?.close();
			})
		}
	});
	
</script>

<style lang="scss" scoped>
	.title {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 32rpx;
		position: absolute;
		left: 0;
		top: 0px;
		background-color: #fff;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}
	.wrapper {
		padding: 0rpx;
		margin-top: 20rpx;
		.list-wrap {
			display: flex;
			flex-direction: column;
			.list-box {
				background-color: #fff;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 30rpx;
				border-radius: 15rpx;
				font-size: 28rpx;
				margin-bottom: 20rpx;
				.address {
					view {
						&:nth-child(1){
							text {
								&:nth-child(1){
									
								}
								&:nth-child(2){
									padding-left: 10rpx;
								}
							}
						}
						&:nth-child(2){
							padding-top: 10rpx;
						}
					}
				}
				.selected {
					view {
						width: 15px;
						height: 15px;
						background-color: #bbb;
						padding: 5rpx;
						border-radius: 50%;
						position: relative;
						&.active {
							background-color: #c21313;
						}
						.icon {
							position: absolute;
							left: 9rpx;
							top: 10rpx;
						}
					}
				}
			}
		}
	}
	.footer {
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		position: absolute;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		.btn {
			display: block;
			width: 95%;
			background: linear-gradient(90deg, var(--ui-bg-color-light), var(--ui-bg-color));
			font-size: 28rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			color: #fff;
			border-radius: 50rpx;
		}
	}
</style>