<template>


</template>

<script setup lang="ts">
	import { reactive } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import {
		navTo
	} from "@/utils/utils"
	import {
		orderList,
		userInfoQuery,
		silentLoginApi,
		selectCarInfo
	} 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";
	import { msg } from "@/utils/utils";
	import { vehiclePlateColorPai } from "@/datas/vehiclePlateColor.js";
	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公务车发行  26补卡额  27解锁卡  28激活  29通行流水 30黑名单查询
	// 31发票   32月结单查询

	onLoad((options) => {
		if(options.params){
					let params=JSON.parse(decodeURIComponent(options.params))
					// 登录回来
					console.log("options",params)
					state.type = params.type
					state.name = params.name
					state.vehicleId = params.vehicleId
					state.deptShortName = params.deptShortName
					state.userType = params.userType
					state.userIdNum = params.userIdNum
					state.mobile = params.mobile
					judge()
				}else{
					// 不需要登录
					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
					console.log("options", options)
					if((options.userType && state.userIdNum) || (options.userType && state.mobile)){
						// 登录情况
						silentLogin().then(() => {
							judge()
						})
						
					}else{
						let obj={
							type:state.type,
							name:state.name,
							vehicleId:state.vehicleId,
							deptShortName:state.deptShortName,
							userType:state.userType,
							userIdNum:state.userIdNum,
							mobile:state.mobile,
						}
						const params = encodeURIComponent(JSON.stringify(obj))
						// 没登录情况
						uni.navigateTo({
							url: `/login/login?back=2&params=${params}`,
						})
					}
				}
	})
	// 判断哪些需要跳转
	const judge=()=>{
		if(state.type == "9" || state.type == "20" || state.type == "23" || state.type == "25" || state.type == "28" || state.type == "31"){
			jump()
		}else{
			quanCheckActionTrue().then((item : any) => {
				console.log("item====", item, item.data.length)
				if (item.data.length == 1) {
					state.id = item.data[0]['id']
					state.orderId = item.data[0]['orderId']
					console.log(item)
					jump()
				} else {
					msg('暂无车辆')
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 1500)
				}
			
			})
		}
	}
	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);
			setItem("openId", data.openId);
			setItem("token", data.accessToken);
			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/rescind-carId/rescind-carId-select')
		} else if (state.type == "20") {
			queryCarMsg().then((item) => {
				console.log("item", item.vehicleManages)
				const params = encodeURIComponent(JSON.stringify(item.vehicleManages[0]))
				uni.navigateTo({
					url: `/subpackage/personal-center/car-details?params=${params}`
				})
			})
		} 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)
			}
		} else if (state.type == "26") {
			navTo(`/subpackage/orders/flowingWater?vehicleId=${state.vehicleId}`)
		} else if (state.type == "27") {
			navTo(`/subpackage/after-sale/pin-code-deblocking/pin-code-confirm?id=${state.id}`)
		} else if (state.type == "28") {
			navTo(`/subpackage/personal-center/install-activation-order`)
		} else if (state.type == "29") {
			navTo(`/subpackage/personal-center/search/select-card?vehicleId=${state.vehicleId}`)
		} else if (state.type == "30") {
			var vehiclePlate = state.vehicleId.split("_")[0]
			var vehiclePlateColor = state.vehicleId.split("_")[1]
			navTo(`/subpackage/after-sale/blacklist-query/list?vehiclePlate=${vehiclePlate}&vehiclePlateColor=${vehiclePlateColor}`)
		} else if (state.type == "31") {
			if (state.name == "服务发票") {
				navTo(`/subpackage/orders/invoiceApply/invoice-content?type=1`)
			} else {
				navTo(`/subpackage/orders/invoiceApply/invoice-content?type=2`)
			}
		} else if (state.type == "32") {
			navTo(`/subpackage/after-sale/month-statement/month-statement-query-list?vehicleId=${state.vehicleId}`)
		} else if (state.type == "33") {
			var vehiclePlate = state.vehicleId.split("_")[0]
			var vehiclePlateColor = state.vehicleId.split("_")[1]
			navTo(`/subpackage/after-sale/progress-query/progress-query-business?vehiclePlate=${vehiclePlate}&vehiclePlateColor=${vehiclePlateColor}`)
		}
	}


	// 查询车辆信息
	const queryCarMsg = () => {
		const options = {
			type: 2,
			data: {
				"openId": getItem('openId'),
				"vehicleId": state.vehicleId
			},
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await request(selectCarInfo, options);
			const data = stringToJson(res.bizContent);
			for (var i = 0; i < data.length; i++) {
				for (var j = 0; j < vehiclePlateColorPai.length; j++) {
					if (data[i].vehiclePlateColor == vehiclePlateColorPai[j]['id']) {
						data[i].color = vehiclePlateColorPai[j]['color']
						data[i].showColor = vehiclePlateColorPai[j]['showColor']
					}
				}
			}
			resolve(data);
		}).catch((error) => {
			reject(error);
		});

	}
</script>

<style scoped>

</style>
