<template>
	<view class="oderPage">
		<view v-if="form.receiveMethodName==='邮寄'" 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">默认</text>
					</view>
					<view class=""> {{form.area +form.address}}</view>
				</view>
			</view>
			<view class="edit">
				<image :src="`${$imgUrl}applyCard/edit.png`" mode=""></image>
			</view>
		</view>
		<u-form :model="form" ref="myForm" :error-type="errorType">
			<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.receiveMethodName">
							<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="radioChange" class="radios">
							<block v-for="(item, index) in radiolist1" :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>
				<!-- 	<u-form-item prop="phone">
					<view class="from_item" style="background-color: #f7f7f7">
						<text><text style="color: red"></text>手机号：</text>
						<u-input v-model="form.mobile" :disabled="true" class="input" />
					</view>
				</u-form-item>
				<u-form-item prop="code">
					<view class="from_item">
						<text><text style="color: red"></text>验证码：</text>
						<u-input v-model="form.code" placeholder="请输入验证码" class="input" />
						<view class="hint2">
							<view class="green">{{ codeDuration === 0 ? "" : codeDuration }}</view>
							<view class="grey" @click="getCode">{{
						    codeDuration === 0 ? "发送验证码" : "秒后可重发"
						  }}</view>
						</view>
					</view>
				</u-form-item> -->
			</view>
		</u-form>
		<button style="margin-top: 50rpx;" class="submit" @click="toPage()">OBU增补申请</button>

	</view>
</template>

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

	// 表单数据
	const form = reactive({
		receiveMethod: "0",
		receiveMethodName: '不邮寄',
		customerName: '',
		customerTel: '',
		mobile: "",
		code: '',
		address: '',
		area: '',
		postCode: ''
	});
	const apply = reactive({
		data: {
			status: undefined,
			id: ''
		}

	});

	//入参
	const params = reactive({
		cardId: '',
		obuId: '',
		orderId: ''

	});

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

	// 单选数据列表
	const radiolist2 = reactive([{
			name: "人为损坏",
			disabled: false,
		},
		{
			name: "自然损坏",
			disabled: false,
		}
	]);

	// 验证规则
	const rules = {
		code: [{
			required: true,
			message: "请输入验证码",
			trigger: ["change", "blur"],
		}],
	};

	// 验证提示类型(toast要版本为1.3.5才支持)
	const errorType = ["toast"];

	// 设置验证规则
	const myForm = ref(null);
	//倒计时时常
	const codeDuration = ref(0);
	let interval = null;
	const address = reactive({
		list: []
	}); //地址list

	/* 验证码倒计时 */
	const codeInterval = () => {
		codeDuration.value = 60;
		interval = setInterval(() => {
			codeDuration.value--;
			if (codeDuration.value === 0) {
				if (interval) {
					clearInterval(interval);
					interval = null;
				}
			}
		}, 1000);
	};

	onReady(() => {
		myForm.value.setRules(rules);
	});

	onLoad((option) => {
		form.mobile = option.mobile
		params.cardId = option.cardId
		params.orderId = option.orderId
		getAddressList()

	});

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

	const getCode = () => {
		if (codeDuration.value !== 0) {
			return;
		}
		const options = {
			type: 2,
			data: {
				mobile: form.mobile
			},
			method: "POST",
			showLoading: true,
		};
		request(sendCode, options)
			.then((res) => {
				codeInterval();

				msg("验证码发送成功！");
			})
			.catch((err) => {
				console.log(err);
			});
	}

// 单选
	const radioChange = (e : any) => {
		console.log(e);
		form.receiveMethod = e.detail.value
		// if (val.name === '是') {
		// 	form.equipmentState = '1';
		// } else {
		// 	form.equipmentState = '2';
		// }
	};



	//申请
	const queryOBUAddAction = () => {
		// if (form.receiveMethodName === '不邮寄') {
		// 	form.receiveMethod = '0'
		// } else {
		// 	form.receiveMethod = '1'
		// }
		var data = {
			orderId: params.orderId,
			openId: getItem(StorageKeys.OpenId),
			configId: wechatPayConfigId,
			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(queryOBUAdd, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}

	//下一步
	const toPage = () => {

		queryOBUAddAction().then(val => {
			navTo(
				`/subpackage/after-sale/additional-equipment/result`
			)
		})
	};
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		background-color: ##eef7f7;
	}
</style>
<style lang="scss" scoped>
	.radios{
		display: flex;
	}
	.radio-box{
		display: flex;
		align-items: center;
		padding-left: 40rpx;
	}
	.hint2 {
		display: flex;

		.green {
			font-size: 28rpx;
			color: #00b38b;
		}

		.grey {
			font-size: 24rpx;
			color: #000000;
			margin-left: 16rpx;
		}
	}

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

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

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

			::v-deep .uni-forms-item {
				border-bottom: 1rpx solid #ccc;
				padding: 15rpx 0;
				margin-bottom: 0;

				.uni-forms-item__label {
					font-size: 28rpx;
					height: 50rpx;
				}

				.uni-forms-item__content {
					display: flex;
				}

				.uni-easyinput__content-input {
					font-size: 28rpx;
					height: 50rpx;
				}
			}

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

		.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: left;
					flex: 1;

					input {
						text-align: left;
					}
				}
			}

			.from_item1 {
				display: flex;
				flex-wrap: nowrap;
				flex-direction: column;
				justify-content: space-between;
				padding: 30rpx;
				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;
				}
			}
		}
	}

	.submit {
		background: linear-gradient(to left, #43a1e0 0%, #13e7c1 100%);
		width: 670rpx;
		height: 80rpx;
		color: #fff;
		border-radius: 100rpx;
		font-size: 32rpx;
	}
</style>
