<template>


</template>

<script setup lang="ts">
	import { reactive } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import {
		navTo
	} from "@/utils/utils"
	import {
		orderList,
		workOrderList,
		userInfoQuery,
		searchVehicleInfo,
		silentLoginApi
	} from "@/utils/network/api.js";
	import {
		getItem,
		StorageKeys,
		setItem
	} from "@/utils/storage";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	const state = reactive({
		type: "",
		name: "",
		vehicleId: "",

		customerId: "",
		vehiclePlate: "",
		vehiclePlateColor: "",
		id: "",
		orderId: "",
		deptShortName: "",
		userType: "",
		userIdNum: "",
		mobile: ""
	});
	//type：1用户信息变更，2车辆信息变更3，异常上报，4卡签注销，5挂失，6设备更换，7退货，8解挂 ,
	// 9有两个 1个是解除车牌占用  2个ETC设备更换车牌
	// 20 设备升级  21增补OBU设备  22卡续期 23欠费补缴  24余额补领  25公务车发行

	onLoad((options) => {
		state.type = options.type
		state.name = options.name
		state.vehicleId = options.vehicleId
		state.deptShortName = options.deptShortName
		state.userType = options.userType
		state.userIdNum = options.userIdNum
		state.mobile = options.mobile

		// state.vehicleId = "贵ZXCV32_0"
		silentLogin().then(() => {
			quanCheckActionTrue().then((item : any) => {
				state.id = item.data[0]['id']
				state.orderId = item.data[0]['orderId']
				console.log(item)
				jump()
			})
		})


	})
	const silentLogin = () => {
		if (state.userType == "1") {
			var data = {
				userType: state.userType,
				account: state.mobile,
				loginSource: getItem("loginSource"),
			};
		} else {
			var data = {
				userType: state.userType,
				account: state.userIdNum,
				loginSource: getItem("loginSource"),
			};
		}
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(silentLoginApi, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	const quanCheckActionTrue = () => {
		let source = ""
		// #ifdef MP-ALIPAY
		source = "ALI"
		// #endif
		// #ifdef MP-WEIXIN
		source = "WECHAT"
		// #endif
		var data = {
			opId: getItem(StorageKeys.OpenId),
			source: source,
			tabIndex: '0',
			orderStep: '11',
			isValueCard: '',
			orderStatus: "1",
			isAfter: true,
			vehiclePlate: state.vehicleId.split('_')[0],
			vehiclePlateColor: state.vehicleId.split('_')[1]
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(orderList, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	const jump = () => {
		if (state.type == '4') {
			navTo(`/subpackage/after-sale/ETC-log-off/log-off-confirm?id=${state.id}`)
		} else if (state.type == "6") {
			navTo(`/subpackage/after-sale/replace-equipment/replace-equipment-confirm?id=${state.id}`)
		} else if (state.type == "5" || state.type == "8") {
			navTo(`/subpackage/after-sale/card-loss-reporting/cardloss-confirm?id=${state.id}`)
		} else if (state.type == "9" && state.name == '解除车牌占用') {
			navTo('/subpackage/after-sale/card-deactivation-activation/deactivation-activation-confirm')
		} else if (state.type == "20") {
			searchVehicleInfoQuery().then((item : any) => {
				console.log("====", item.vehicles[0])
				state.customerId = item.vehicles[0]['customerId']
				state.vehiclePlate = item.vehicles[0]['vehiclePlate']
				state.vehiclePlateColor = item.vehicles[0]['vehiclePlateColor']
				isWorkOrder().then((item : any) => {
					console.log("查询是否有工单", item)
					if (item.data) {
						if (item.data[0]['status'] == '0') {
							navTo(`/subpackage/after-sale/equipment-upgrade/upgrade-confirm?id=${state.id}&workId=${item.data[0]['id']}`)
						}
					} else {
						msg('暂无工单')
					}
				})
			})
		} else if (state.type == "21") {
			navTo(`/subpackage/after-sale/additional-equipment/additional-equipment-confirm?id=${state.id}`)
		} else if (state.type == "22") {
			navTo(`/subpackage/after-sale/card-Renewal/renewal-confirm?id=${state.id}&orderId=${state.orderId}`)
		} else if (state.type == "23") {
			navTo(`/subpackage/after-sale/arrears/arrears`)
		} else if (state.type == "24") {
			navTo(`/subpackage/after-sale/refund-order-balance/refund-order-balance?vehicleId=${state.vehicleId}`)
		} else if (state.type == "25") {
			if (!state.deptShortName) {
				navTo("/subpackage/personal-center/query-company-list")
			} else {
				navTo("/subpackage/personal-center/query-company-list?deptShortName=" + state.deptShortName)
			}
		}
	}

	const searchVehicleInfoQuery = () => {
		const options = {
			type: 2,
			data: {
				vehicleId: state.vehicleId
			},
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(searchVehicleInfo, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	const isWorkOrder = () => {
		var data = {
			vehiclePlate: state.vehiclePlate,
			vehiclePlateColor: state.vehiclePlateColor,
			customerId: state.customerId,
		};
		const options = {
			type: 2,
			data: data,
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(workOrderList, options);
			const data = stringToJson(res.bizContent);
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
</script>

<style scoped>

</style>