<template>
	<view class="content-wrap">
		<!-- 收货地址 -->
		<view v-if="!state.data.address" class="address-lane">
			<view class="title">请选择或新增收货地址</view>
			<view style="margin-top: 30rpx" class="action">
				<button type="default" class="button" @click="state.show = true">
					添加收货地址
				</button>
			</view>
		</view>
		<view v-if="state.data.address" class="address-lane">
			<view class="title">收货地址</view>
			<view @click="state.show = true" class="address-content" style="margin-top: 30rpx">
				<view class="flex">
					<image :showLoading="true" :src="`${$imgUrl}applyCard/location.png`"
						style="width: 48rpx; height: 48rpx">
					</image>
					<view style="margin-left: 18rpx" class="">
						<view class="address">
							{{ state.data.region + state.data.address }}
						</view>
						<view style="margin-top: 15rpx" class="flex">
							<view class="name">
								{{ state.data.consignee }}
							</view>
							<view class="phone">
								{{ state.data.consigneeTel }}
							</view>
						</view>
					</view>
				</view>
				<view class="arror">
					<image :src="`${$imgUrl}common/arror-right.png`" class="icon"></image>
				</view>
			</view>

			<view style="margin-top: 30rpx" class="action">
				<button type="default" class="button" @click="nextAction()">
					下一步
				</button>
			</view>
		</view>

	</view>
	<u-popup mode="bottom" v-model="state.show">
		<view class="address-line">
			<view v-if="state.addressArray" v-for="(item, index) in state.addressArray" :key="index"
				@click="addressSelected(item)" style="margin-bottom: 60rpx" class="flex-bettwen">
				<view class="flex">
					<view class="xing">
						{{ item.consignee.charAt(0) }}
					</view>
					<view class="content">
						<view class="flex">
							<view class="name">
								{{ item.consignee }}
							</view>
							<view class="phone">
								{{ item.consigneeTel }}
							</view>
						</view>
						<view class="address">
							{{ item.region + item.address }}
						</view>
					</view>
				</view>
				<view @click.stop="editAddress(item)" style="margin-left: 80rpx" class="picture">
					<image :showLoading="true" :src="`${$imgUrl}applyCard/edit.png`"
						style="width: 48rpx; height: 48rpx"></image>
				</view>
			</view>
			<view :style="state.addressArray ? 'margin-top: 60rpx;' : ''" class="action-bottom">
				<button type="default" class="button" @click="goToAddAddress()">
					添加收货地址
				</button>
			</view>
		</view>
	</u-popup>
</template>

<script setup lang="ts">
	import {
		reactive
	} from "vue";
	import {
		getItem,
		StorageKeys
	} from "@/utils/storage";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import {
		addressQuery,
		gongWuSupplementObu
	} from "@/utils/network/api.js";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		navTo,
		confirm,
		msg
	} from "../utils/utils";
	onLoad((option) => {
		state.data.openId = getItem(StorageKeys.OpenId);
		state.data.vehicleId = option.vehicleId;
		state.data.deptId = option.deptId;
	});
	onShow(() => {
		var data = {
			openId: getItem(StorageKeys.OpenId),
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		request(addressQuery, options).then((res) => {
			console.log(res);
			const data = stringToJson(res.bizContent);
			state.addressArray = data.data;
		});
	});
	const state = reactive({
		data: {
			openId: "",
			orderSource: "WECHAT",
			receiveMethod: 0,
			deptId: 0,
			vehicleId: "",
			consignee: "", //收货人
			consigneeTel: "", //收货电话
			region: "",
			address: "",
			postalCode: "",
			postCode: "",
			area: "",
		},
		show: false,
		addressArray: undefined,
	});
	const addressSelected = (val: any) => {
		console.log(val);
		state.data = {
			...state.data,
			...val,
		};
		state.data.postCode = val.postalCode;
		state.data.area = val.region;
		state.show = false;
	};
	const goToAddAddress = () => {
		uni.navigateTo({
			url: "/applyCard/addAddress",
		});
	};
	const editAddress = (val) => {
		uni.navigateTo({
			url: `/applyCard/editAddress?content=` + JSON.stringify(val),
		});
	};

	const nextAction = () => {
		console.log("下一步");
		console.log(state.data);

		const options = {
			type: 2,
			data: state.data,
			method: "POST",
			showLoading: true,
		};
		request(gongWuSupplementObu, options).then((res) => {
			console.log(res);
			const result = stringToJson(res.bizContent);
			if (result?.orderId) {
				// console.log(888888888);
				navTo("/orders/order-details-obu?id=" + result.orderId)
			} else if (res.statusCode == 600) {
				msg(res.errorMsg)
				setTimeout(()=>{
					uni.switchTab({
						url: "/pages/order/order"
					})
				},2000)	
							

			} else {
				msg("信息提交错误")
			}
		});
	}
</script>

<style lang="scss">
	page {
		background: #EEF7F7;
	}

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

	.content-wrap {
		position: relative;
		padding: 50rpx 30rpx;

		.title {
			font-size: 30rpx;
			font-family: Noto Sans S Chinese;
			font-weight: bold;
			color: #000000;
			line-height: 30rpx;
			margin-bottom: 30rpx;
		}

		.car-input {}

		.chepai-lane {
			margin-top: 60rpx;
			margin-bottom: 20rpx;
		}

		.address-lane {
			margin-bottom: 30rpx;

			.title {
				font-size: 30rpx;
				font-weight: bold;
				color: #000000;
				line-height: 30rpx;
			}
		}
	}

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

		.name {
			font-size: 26rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #999999;
			font-size: 26rpx;
		}

		.phone {
			font-size: 26rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #999999;
			line-height: 26rpx;
			margin-left: 20rpx;
		}

		.arror .icon {
			width: 36rpx;
			height: 36rpx;
		}

		.address {
			font-size: 32rpx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #333333;
			line-height: 32rpx;
		}
	}

	.address-line {
		padding: 20px;

		.flex-bettwen {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.xing {
			width: 68rpx;
			height: 68rpx;
			background: rgba(0, 179, 139, 0.2);
			border-radius: 50%;
			font-size: 26rpx;
			font-weight: 400;
			color: #00b38b;
			line-height: 68rpx;
			text-align: center;
		}

		.content {
			margin-left: 20px;

			.name {
				font-size: 30rpx;
				font-family: Microsoft YaHei;
				font-weight: 500;
				color: #333333;
			}

			.phone {
				margin-left: 10rpx;
				font-size: 24rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #999999;
				line-height: 36rpx;
			}

			.address {
				margin-top: 10rpx;
				font-size: 26rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #333333;
				line-height: 36rpx;
			}

			.picture {
				width: 48rpx;
				height: 48rpx;
			}

			.editIcon {
				width: 48rpx;
				height: 48rpx;
			}
		}
	}

	.action {
		padding-bottom: 100rpx;

		.button {
			height: 80rpx;
			background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			font-weight: 400;
			color: #ffffff;
			line-height: 80rpx;
		}
	}

	.action-bottom {
		padding-bottom: 30rpx;

		.button {
			height: 80rpx;
			background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
			border-radius: 40rpx;
			font-size: 32rpx;
			font-weight: 400;
			color: #ffffff;
			line-height: 80rpx;
		}
	}
</style>
