<template>
	<!-- 	<navBar title="补办ETC卡"></navBar>
 -->
	<view class="oderPage">
		<view class="addr-box">
			<view class="addr-box-left">
				<view class="text-image">{{form.customerName.charAt(0)}} </view>
				<view class="content">
					<view class="">
						<text class="item1">{{form.customerName}}</text>
						<text class="item2">{{form.customerTel}}</text>
						<text class="item3" v-if="form.defaultAddress ==1">默认</text>
					</view>
					<view class=""> {{form.area +form.address}}</view>
				</view>
			</view>
			<view class="edit" @click="address.show = true">
				<image :src="`${$imgUrl}applyCard/edit.png`" mode=""></image>
			</view>
		</view>

		<u-form ref="myForm">
			<view class="from">
				<u-form-item prop="cardState">
					<view class="from_item">
						<text><text style="color: red">*</text>是否寄回设备：</text>
						<!-- <u-radio-group v-model="form.sendStatusName">
							<u-radio :customStyle="{ marginBottom: '8px' }" activeColor="#2CE242"
								v-for="(item, index) in radiolist1" :key="index" :label="item.disabled"
								:name="item.name">
								{{ item.name }}
							</u-radio>
						</u-radio-group> -->
						<radio-group @change="sendChange" class="radios">
							<block v-for="(item, index) in radiolist1" :key="item.val">
								<view class="radio-box">
									<radio :value="item.val" :checked="form.sendStatus == item.val" color="#13e7c1"
										style="transform:scale(0.75)" />
									<view>{{item.name}}</view>
								</view>
							</block>
						</radio-group>
					</view>
				</u-form-item>
				<!-- <u-form-item prop="logistics" v-if="form.sendStatusName == '寄回'"> -->
				<view class="from_item" v-if="form.sendStatusName == '寄回'" @click="showPicker">
					<text style="font-size: 28rpx;"><text style="color: red">*</text>退货物流公司：</text>
					<view style="display: flex;align-items: center;font-size: 28rpx;">
						<u-input v-model="form.courierCompany" class="input" @click="showPicker"
							:placeholder="form.courierCompany?form.courierCompany:'请选择'" style="text-align: right;" />
						<!-- <text v-if="form.courierCompany">{{form.courierCompany}}</text> -->
						<u-icon name="arrow-right" style="margin-left: 10px"></u-icon>
					</view>
				</view>
				<!-- </u-form-item> -->
				<u-form-item prop="returnNum" v-if="form.sendStatusName == '寄回'">
					<view class="from_item">
						<text><text style="color: red"></text>退货单号：</text>
						<u-input v-model="form.courierOrderId" class="input" />
					</view>
				</u-form-item>
				<u-form-item prop="cardState">
					<view class="from_item">
						<text><text style="color: red">*</text>收货方式：</text>
						<!-- <u-radio-group v-model="form.receiveMethodName">
							<u-radio :customStyle="{ marginBottom: '8px' }" activeColor="#2CE242"
								v-for="(item, index) in radiolist2" :key="index" :label="item.disabled"
								:name="item.name">
								{{ item.name }}
							</u-radio>
						</u-radio-group> -->
						<radio-group @change="receiveChange" class="radios">
							<block v-for="(item, index) in radiolist2" :key="item.val">
								<view class="radio-box">
									<radio :value="item.val" :checked="form.receiveMethod == item.val" color="#13e7c1"
										style="transform:scale(0.75)" />
									<view>{{item.name}}</view>
								</view>
							</block>
						</radio-group>
					</view>
				</u-form-item>
			</view>
		</u-form>

		<!-- 物流弹窗 -->
		<view>
			<!-- 物流 -->
			<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
		</view>

		<button class="submit" @click="submit">下一步</button>
	</view>
	<!-- 地址弹窗 -->
	<u-popup mode="bottom" v-model="address.show">
		<view class="address-line">
			<view v-if="address.list" v-for="(item, index) in address.list" :key="index" @click="addressSelected(item)"
				style="margin-bottom: 40rpx" 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="address.list ? 'margin-top: 60rpx;' : ''" class="action-bottom">
				<button type="default" class="button" @click="goToAddAddress()">
					添加收货地址
				</button>
			</view>
		</view>
	</u-popup>
</template>

<script setup lang="ts">
	// import navBar from "../ETCcancellation/components/nav-bar.vue";
	import {
		reactive,
		ref
	} from "vue";
	import {
		onLoad,
		onShow,
		onReady
	} from "@dcloudio/uni-app";
	import {
		navTo,
		checkStr, msg
	} from "@/utils/utils";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		getLogistics,
		addressQuery,
		cckChangejzCardInfo
	} from "@/utils/network/api.js";
	import {
		stringToJson
	} from "@/utils/network/encryption.js";
	import {
		getItem,
		StorageKeys
	} from "@/utils/storage";
	import {
		wechatPayConfigId,

	} from "@/utils/network/api";
	const list = reactive([]); //物流数据

	const address = reactive({
		list: [],
		show: false
	}); //地址list

	const form = reactive({
		customerName: '',
		customerTel: '',
		address: '',
		area: '',
		sendStatusName: '寄回',
		sendStatus: '1', //0 --- 不邮寄 1 --- 邮寄
		courierCompany: '',
		courierOrderId: '',
		receiveMethod: '1',
		postCode: '',
		receiveMethodName: '邮寄',//0 --- 不邮寄 1 --- 邮寄
		defaultAddress: 2
	});

	//入参
	const params = reactive({
		/* cardId: '',
		obuId: '', */
		orderId: '',
		applyId: '',
		productId: ""
	});


	// 单选数据列表
	const radiolist1 = reactive([{
		name: "寄回",
		val: "1",
	},
		// {
		// 	name: "不寄回",
		// 	val: "0",
		// },
	]);

	const radiolist2 = reactive([{
		name: "邮寄",
		val: "1",
	},
		// {
		// 	name: "不邮寄",
		// 	val: "0",
		// },
	]);

	let show = ref(false);

	const goToAddAddress = () => {
		uni.navigateTo({
			url: "/subpackage/orders/addAddress",
		});
	};
	const editAddress = (val) => {
		uni.navigateTo({
			url: `/subpackage/orders/editAddress?content=` + JSON.stringify(val),
		});
	};

	const addressSelected = (val : any) => {
		console.log(val);
		// state.data = {
		// 	...state.data,
		// 	...val,
		// };
		// state.show = false;
		form.customerName = val.consignee
		form.customerTel = val.consigneeTel
		form.address = val.address
		form.area = val.region
		form.postCode = val.postalCode
		form.defaultAddress = val.defaultAddress
		address.show = false
	};

	// 寄回单选
	const sendChange = (e : any) => {
		console.log(e);
		form.sendStatus = e.detail.value
	};
	// 邮寄单选
	const receiveChange = (e : any) => {
		console.log(e);
		form.receiveMethod = e.detail.value
	};

	// 打开物流先择器
	const showPicker = function () {
		show.value = true;
	};

	const confirm = (e) => {
		form.courierCompany = e[0].value;
		console.log(e[0].value);
	};

	onLoad((option) => {
		console.log("option", option)
		params.orderId = option.orderId
		params.applyId = option.applyId
		params.productId = option.productId
	});

	onShow(() => {
		getAddressList();
		getexpresslist();
	})

	const getexpresslist = () => {
		//参数说明
		let options = {
			type: 2, //type: 2,JSON格式提交数据(默认表单形式提交)
			data: {
				// opId: getItem("openId"),
				// source: "WECHAT",
				// tabIndex: "0",
			}, //请求参数
			method: "POST", //提交方式(默认POST)
			showLoading: true, //是否显示加载中(默认显示)
		};

		//调用方式
		request(getLogistics, options)
			.then((res) => {
				// stringToJson(res.bizContent)
				stringToJson(res.bizContent).forEach((item) => {
					list.push({
						value: item,
						label: item,
					});
				});
			})
			.catch((err) => {
				console.log(err);
			});
	};
	const getAddressList = () => {
		var data = {
			openId: getItem(StorageKeys.OpenId),
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		request(addressQuery, options).then((res) => {
			const data = stringToJson(res.bizContent);
			address.list = data.data;
			if (address.list.length > 0) {
				var defaultAddress = address.list[0]
				form.customerName = defaultAddress.consignee
				form.customerTel = defaultAddress.consigneeTel
				form.address = defaultAddress.address
				form.area = defaultAddress.region
				form.postCode = defaultAddress.postalCode
				form.defaultAddress = defaultAddress.defaultAddress
			}
		});
	};
	const submit = () => {
		// if (form.sendStatusName === '寄回') {
		// 	form.sendStatus = '1'
		// } else {
		// 	form.sendStatus = '0'
		// }
		// if (form.receiveMethodName === '邮寄') {
		// 	form.receiveMethod = '1'
		// } else {
		// 	form.receiveMethod = '0'
		// }
		queryCckChangejzCardInfo().then(val => {
			console.log(val)
			navTo(
				`/subpackage/after-sale/to-bookkeeping-card/result`
			)
		})

	};
	const queryCckChangejzCardInfo = () => {
		if (!form.address) {
			msg("请编辑收货详细地址");
			return;
		}
		var data = {
			applyId: params.applyId,
			orderId: params.orderId,
			productId: params.productId,
			sendStatus: form.sendStatus,
			configId: wechatPayConfigId,
			courierCompany: form.courierCompany,
			courierOrderId: form.courierOrderId,
			receiveMethod: form.receiveMethod,
			customerName: form.customerName,
			customerTel: form.customerTel,
			address: form.address,
			area: form.area,
			postCode: form.postCode
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};

		return new Promise(async (resolve, reject) => {
			const res = await request(cckChangejzCardInfo, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		background-color: #f3f3f3;
	}
</style>
<style lang="scss" scoped>
	.radios {
		display: flex;
	}

	.radio-box {
		display: flex;
		align-items: center;
		padding-left: 20rpx;
	}

	.oderPage {
		flex: 1;
		width: 100%;

		.addr-box {
			margin-top: 20rpx;
			display: flex;
			background: #fff;
			padding: 30rpx;
			justify-content: space-between;
			align-items: center;

			.addr-box-left {
				display: flex;

				.text-image {
					width: 68rpx;
					height: 68rpx;
					background: #00b38b;
					border-radius: 50%;
					text-align: center;
					font-size: 26rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #ffffff;
					line-height: 68rpx;
					margin-right: 30rpx;
				}

				.content {
					view {
						font-size: 26rpx;
						font-family: Microsoft YaHei;
						font-weight: 400;
						color: #333333;

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

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

						.item3 {
							width: 71rpx;
							height: 37rpx;
							background: #ffffff;
							border: 1px solid #ff8000;
							border-radius: 6rpx;
							font-size: 22rpx;
							font-family: Microsoft YaHei;
							font-weight: 400;
							color: #ff8000;
							padding: 3rpx 6rpx;
							margin-left: 10rpx;
						}
					}
				}
			}

			.edit {
				image {
					width: 50rpx;
					height: 50rpx;
				}
			}
		}

		.from {
			background-color: #fff;
			margin-top: 30rpx;
			padding: 0 30rpx;

			::v-deep .u-form-item {
				padding: 0;
				line-height: normal;

				.u-form-item__message {
					margin-bottom: 12rpx;
				}
			}

			.from_item {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				align-items: center;
				height: 80rpx;
				border-bottom: 1rpx solid #dcdcdc;

				.btn {
					font-size: 24rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #ffffff;
					background: #00b38b;
					border-radius: 10rpx;
					padding: 10rpx 15rpx;
				}

				::v-deep .input {
					text-align: right;
					flex: 1;

					input {
						text-align: right;
					}
				}
			}

			.from_item1 {
				display: flex;
				flex-wrap: nowrap;
				flex-direction: column;
				justify-content: space-between;
				padding: 30rpx 0;
				border-bottom: #dcdcdc 1px solid;

				input {
					text-align: right;
				}

				.textarea {
					background-color: #f1f1f1;
					width: 100%;
					border-radius: 20rpx;
					margin-top: 10rpx;
					text-indent: 1rem;
					height: 180rpx;
					padding: 20rpx;
					box-sizing: border-box;
				}
			}
		}
	}

	.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;
		}
	}

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

	.submit {
		width: 670rpx;
		height: 80rpx;
		background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
		box-shadow: 0rpx 4rpx 11rpx 1rpx rgba(223, 223, 223, 0.5);
		border-radius: 40rpx;
		font-size: 32rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #ffffff;
		line-height: 80rpx;
		margin-top: 60rpx;
	}
</style>