<template>
	<view class="content">
		<image class="bg-image" mode="widthFix"
			src="https://qtzl.etcjz.cn/default-bucket/20240324/3eba2095f5204798a1f8101b_beijing.jpg"></image>
		<view class="content-re">
			<custom-header title="订单信息修改" :back="config.hasReturn === 'true'"></custom-header>
			<u-toast ref="uToastRef" />
			<view style="padding: 15rpx" v-if="isShow">
				<view class="container">
					<view class="ceil-content">
						<view class="item">
							<view class="item-l">订单状态</view>
							<view class="item-r">
								{{ formData.stepName }}
							</view>
						</view>
						<view class="item" v-if='formData.rejectReasonName'>
							<view class="item-l">审核原因</view>
							<view class="item-r">
								{{ formData.rejectReasonName }}
							</view>
						</view>
						<view class="item">
							<view class="item-l">车牌号码</view>
							<view class="item-r">
								{{ formData.vehiclePlate }}
							</view>
						</view>
					</view>

					<view class="ceil-content">
						<view class="item">
							<view class="item-l">用户信息修改</view>
							<view class="item-r link-nav" @click="handleToNav(1)">
								<text v-if='isXiuKey.userKey' class='txt'>重新修改</text>
								<u-icon name="arrow-right" color="#999" size="24"></u-icon>
							</view>
						</view>
						<view class="item">
							<view class="item-l">行驶证信息修改</view>
							<view class="item-r link-nav" @click="handleToNav(2)">
								<text v-if='isXiuKey.drKey' class='txt'>重新修改</text>
								<u-icon name="arrow-right" color="#999" size="24"></u-icon>
							</view>
						</view>
					</view>
					<button type="success" class="btn-txt btn" @click="submit">
						确认修改
					</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script setup lang="ts">
	import { unifyTemplate } from '@/hooks/unifyTemplate';
	import { reactive, ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { request } from '@/utils/network/request';
	import { setToken, getToken } from '@/utils/storage';
	import handleAmount from '@/utils/handleAmount.js';
	import {
		useAuditOrderStore
	} from '@/stores/auditOrder.js';
	const {
		CustomHeader, //头部组件
		initData, //初始化数据
		isShow, //是否展示formBuilderVue组件
	} = unifyTemplate(); //初始化数据
	// import {
	// 	request
	// } from '../../static/js/network/request'

	let formData = ref<any>({});
	let config = ref<any>({
		submitName: '提交',
		titleWidth: 160
	});
	let code = ref('');
	const auditOrderStore = useAuditOrderStore();
	onLoad((opin) => {
		// 初始化数据
		initData(opin, 7).then(async (data) => {
			console.log('新版1', data);
			handleData(data)
			isShow.value = true
			delete data.config.tableConfig;
			config.value = Object.assign(data.config, config.value);
			// await login(data); //无感登录
			handleSendParams(data.order)
		});
	});
	const reasonOptions = [
		{ val: "1", label: "开户证件不合规" },
		{ val: "2", label: "车辆证件不合规" },
		{ val: "3", label: "车主证件不合规" },
		{ val: "8", label: "其他" },
		{ val: "5", label: "车辆图片不合格" },
		{ val: "6", label: "不符合售后条件" },
		{ val: "7", label: "原设备未回收" },
		{ val: "4", label: "车型计算错误" },
	]
	const orderStepOptions = [
		{ val: 0, label: "未创建订单" },
		{ val: 1, label: "完成填写订单初始化信息" },
		{ val: 2, label: "完成个人/单位信息上传" },
		{ val: 3, label: "待签约" },
		{ val: 4, label: "完成车辆信息上传,待支付" },
		{ val: 5, label: "完成订单支付。已支付" },
		{ val: 6, label: "待审核" },
		{ val: 7, label: "审核不通过" },
		{ val: 8, label: "待发货" },
		{ val: 9, label: "待收货" },
		{ val: 10, label: "待激活" },
	]
	const handleData = (data) => {
		formData.value = data.order
		formData.value.stepName = orderStepOptions.find(item => item.val === data.order.orderStep)?.label || '其它'
		formData.value.rejectReasonName = reasonOptions.find(rejItem => rejItem.val === data.order.rejectReason)?.label || data.order.rejectReason
	}

	// 无感登录,如果没有登录调用登录
	const loginData = ref({
		token: '',
		openId: ''
	});
	async function login(orderData) {
		let data = {
			userType: '',
			account: ''
		};
		if (orderData.order.userType === 'PERSONAL_USER') {
			// 个人办理
			data.userType = 'PERSONAL';
			data.account = orderData.order.customerTel;
		} else {
			// 单位办理
			data.userType = 'ENTERPRISE';
			data.account = orderData.order.customerIdnum;
		}
		await request('abaf0013caa24dafad12b0f571e8ee40', {
			data
		}).then((res) => {
			if (res.statusCode === 0) {
				let bizContent = JSON.parse(res.bizContent);
				// 保存token
				setToken(bizContent.accessToken);
				loginData.value.token = bizContent.accessToken;
				loginData.value.openId = bizContent.openId;
			}
		});
	}
	function handleToNav(navKey) {
		if (navKey === 1) {
			uni.navigateTo({
				url: '/pages/userInfoUploading/userInfoUploadingAudit',
				animationType: 'pop-in',
				animationDuration: 500
			});
		} else {
			uni.navigateTo({
				url: '/pages/drivingLicense/drivingLicenseAudit',
				animationType: 'pop-in',
				animationDuration: 500
			});
		}
	}
	const isXiuKey = reactive({
		userKey: false,
		drKey: false,
	})
	let sendParams = {}
	function handleSendParams(data) {
		sendParams = {}
		console.log(auditOrderStore, data.orderId);
		if (auditOrderStore.userData.orderId === data.orderId) {
			sendParams = {
				...sendParams,
				...auditOrderStore.userData,
			}
			isXiuKey.userKey = true
		} else {
			isXiuKey.userKey = false
			auditOrderStore.setUserData({})

		}
		if (auditOrderStore.drivingLicenseData.orderId === data.orderId) {
			sendParams = {
				...sendParams,
				...auditOrderStore.drivingLicenseData,
			}
			isXiuKey.drKey = true
		} else {
			isXiuKey.drKey = false
			auditOrderStore.setDrivingData({})
		}

		console.log(sendParams, 'sendParams');
	}
	//确认办理
	async function submit() {
		if (!isXiuKey.userKey) {
			uni.showModal({
				title: '提示',
				content: '请确认用户信息是否正确',
				success: function (res) {
					if (res.confirm) {
						uni.navigateTo({
							url: '/pages/userInfoUploading/userInfoUploadingAudit',
							animationType: 'pop-in',
							animationDuration: 500
						});
					} else if (res.cancel) {

					}
				}
			});
			return
		}
		if (!isXiuKey.drKey) {
			uni.showModal({
				title: '提示',
				content: '请确认行驶证相关信息是否正确',
				success: function (res) {
					if (res.confirm) {
						uni.navigateTo({
							url: '/pages/drivingLicense/drivingLicenseAudit',
							animationType: 'pop-in',
							animationDuration: 500
						});
					} else if (res.cancel) {

					}
				}
			});
			return
		}
		uni.showModal({
			title: '提示',
			content: '请确认所有信息是否正确',
			success: (res) => {
				if (res.confirm) {
					sendRequ()
				} else if (res.cancel) {

				}
			}
		});


	}
	function sendRequ() {
		console.log(sendParams, '请求参数');
		return request('83aa6ab9c8f441dda9ad75b46f00c805', {
			data: sendParams
		}).then((res) => {
			if (res.statusCode === 0) {
				console.log('修改成功');
				uni.navigateTo({
					url: '/pages/audit/auditSucess',
					animationType: 'pop-in',
					animationDuration: 500
				});
			} else {
			}
		})
	}
</script>
<style lang="scss" scoped>
	.content {
		height: 100%;
		// background-color: #eee;
	}

	.title {
		font-size: 30rpx;
		padding-bottom: 10rpx;
		color: #999;
	}

	.container {
		padding: 20rpx;
		font-size: 28rpx;

		.ceil-content {
			background-color: #fff;
			margin: 20rpx 0;
			border-radius: 14rpx;
			box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);

			.item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx 20rpx;
				border-bottom: 2rpx solid #eee;

				.link-nav {
					padding: 0 10rpx 0 30rpx;
					height: 100%;
					cursor: pointer;

					.txt {
						font-size: 26rpx;
						color: #999;
					}
				}
			}
		}
	}

	.payment-total {
		text-align: right;
		padding: 0 10px 20px;
	}


	.status {
		margin: 0;
	}

	.amount {
		color: #1aac1b;
		max-width: 450rpx;
	}
	.btn{
		margin-top: 40rpx;
	}
</style>