<template>
	<view class="oderPage">
		<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>
						<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.applyType == 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="cardState">
					<view class="from_item">
						<text><text style="color: red">*</text>设备损坏类型：</text>
						<radio-group @change="typeChange" class="radios">
							<block v-for="(item, index) in radiolist2" :key="item.val">
								<view class="radio-box">
									<radio :value="item.val" :checked="form.damageMode == 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 style="margin: 30rpx;" v-if="state.isPicture">
			<view class="please"><text style="color: red">*</text>请上传图片：</view>
			<uni-file-picker fileMediatype="image" mode="grid" :limit="9" @select="select"
				:image-styles="state.imageStyles" />
		</view>

		<view class="attention">
			<view>注意事项：</view>
			<view>1.设备人为损坏时需要支付设备费用;</view>
			<view>2.设备自然损坏时：</view>
			<view>&nbsp;(1)更换的设备中有设备不在质保期内，需要支付对应的设备费用;</view>
			<view>&nbsp;(2)选择自然损坏需要将损坏的设备寄回，不寄回则视为人为损坏，需要支付设备费用;</view>
			<view>&nbsp;(3)寄回的设备由专业人员进行检测，检测结果为自然损坏且在质保期内，则免费更换，检测结果为人为损坏则需要支付设备费用;</view>
			<view>3.设备费用，卡20元，obu130元。</view>
		</view>
		<view class="btn-wrapper">
			<button class="submit button" v-if="apply.data.status === 'APPLY'||!apply.data.status" @click="toPage()"
				type="default">下一步填写资料</button>
		</view>

	</view>
</template>

<script setup lang="ts">
	import { jump } from "@/datas/9901Jump.js";
	import {
		ref,
		reactive
	} from "vue";
	import {
		navTo, uploadFile
	} from "@/utils/utils";
	import {
		onReady,
		onLoad,
		onShow,
	} from "@dcloudio/uni-app";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		sendCode,
		changeCardApply,
		changeCardQuery,
	} from "@/utils/network/api.js";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import {
		msg
	} from "@/utils/utils";
	const state = reactive({
		imageList: [],  //图片数据
		imageStyles: {
			width: 80,
			height: 80,
		},
		isPicture: false,  //是否上传图片
	})
	// 表单数据
	const form = reactive({
		damageMode: "0",
		damageModeName: '人为损坏',
		applyType: 'EXCHANGE_CARD',
		applyTypeName: '卡',
		mobile: "",
		code: '',
		applyRes: ''
	});
	const apply = reactive({
		data: {
			status: undefined,
			id: ''
		}

	});

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

	});

	// 单选数据列表
	const radiolist1 = reactive([{
		name: "卡",
		val: "EXCHANGE_CARD",
	},
	{
		name: "OBU",
		val: "EXCHANGE_OBU",
	}, {
		name: "卡和OBU",
		val: "EXCHANGE_ALL",
	},
	]);

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

	// 验证规则
	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 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

	});

	onShow(() => {
		changeCardQueryAction()
	})
	const select = (e) => {
		console.log('选择文件：', e)
		uploadFile(e.tempFilePaths[0], "", "").then((data) => {
			const imgArr = state.imageList
			imgArr.push(data)
			state.imageList = imgArr;
			console.log("data.data.url", state.imageList)
		})
	}
	// 单选
	const radioChange = (e : any) => {
		console.log(e);
		form.applyType = e.detail.value

	};
	// 单选
	const typeChange = (e : any) => {
		console.log(e);
		form.damageMode = e.detail.value
		if (e.detail.value == 1) {
			state.isPicture = true
		} else {
			state.isPicture = false
		}
	};

	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 changeCardQueryAction = () => {
		var data = {
			orderId: params.orderId
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		request(changeCardQuery, options).then((res) => {
			const data = stringToJson(res.bizContent);
			if (data) {
				apply.data = data;
				apply.data.status = data.status;
			}

		})
	}

	//申请
	const queryCckChangejzCardAction = () => {
		if (form.damageMode == "1") {
			if (state.imageList.length == 0) {
				msg('请上传图片');
				return;
			}
		}
		var data = {
			orderId: params.orderId,
			applyType: form.applyType,
			damageMode: form.damageMode,//0 --- 人为损坏 1 --- 自然损坏 当为换卡签的时候必传
			fileUrl: state.imageList.toString()
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(changeCardApply, options);
			var resultData = res.bizContent
			console.log("data88888", JSON.parse(resultData))
			resolve(JSON.parse(resultData));
		}).catch((error) => {
			reject(error);
		});
	}

	//下一步
	const toPage = () => {
		console.log("sdkchkdscn", apply.data)
		console.log("form.damageMode", form.damageMode)
		// if(state.aa){
		// 	let obj={
		// 		applyType:form.applyType,
		// 		vehicleId:form.vehicleId,
		// 		cardId:params.cardId,
		// 	}
		// 	const params = encodeURIComponent(JSON.stringify(obj))
		// 	jump("5",params)
		// }else{
			queryCckChangejzCardAction().then(val => {
				console.log("val888888", val)
				navTo(
					`/subpackage/after-sale/replace-equipment/form?orderId=${params.orderId}&&id=${val.id}&&damageMode=${form.damageMode}&&msg=${val.msg}&&cardExpire=${val.cardExpire}&&obuExpire=${val.obuExpire}`
				)
			})
		// }
	};
</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: 20rpx;
	}

	.hint2 {
		display: flex;

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

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

	.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;
					background: transparent;

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

	.btn-wrapper {
		display: flex;
		padding-top: 60rpx;
	}

	.button::after {
		border: none;
	}

	.default {
		height: 80rpx;
		background: rgba(0, 179, 139, .1);
		border-radius: 40rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #00B38B;
		line-height: 80rpx;
		width: 45%;
		border: 1px solid #00B38B;
	}

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

	.upload-img {
		margin-top: 10rpx;
	}

	.please {
		font-size: 28rpx;
		margin-bottom: 20rpx;
	}

	.attention {
		padding: 20rpx;
		font-size: 28rpx;
	}
</style>