<template>
	<!-- 搜索框 -->
	<view class="as-layout-horizontal as-gravity-center-start search-layout">
		<view class="search-box">
			<image :src="`${$imgUrl}service/icon-search.png`" class="icon" mode="aspectFill"></image>
			<input class="search" placeholder="请输入车牌号" @blur="onKeyInput" v-model="state.car" />
		</view>
	</view>
	<view class="title">车辆列表</view>
	<view class="selectCar-box">
		<view class="list" v-if="state.list.length > 0" @click="choose(i, item)" v-for="(item, i) in state.list"
			:key="i">
		  <image class="car" :src='`${$imgUrl}issuance/car.png`' mode=""></image>
		  <view class="content">
		  	<view class="content-top">
				<text class="vehiclePlate">{{item.vehiclePlate}}</text>
			</view>
		  	<view  class="content-time">车牌颜色：{{getVehiclePlateColor(item.vehiclePlateColor)}}</view>
		  </view>
		  <image class="arrow" :src='`${$imgUrl}issuance/arrow.png`' mode=""></image>
		</view>
		<view v-else class="flex"> 暂无车辆订单信息 </view>
	</view>
	<view class="action">
		<button type="default" class="ui-btn" @click="add()">
			新增车辆
		</button>
	</view>
</template>

<script lang="ts" setup>
	import { reactive, ref } from "vue";
	import { navTo} from "@/utils/utils";
	import { onLoad,onShow} from "@dcloudio/uni-app";
	import { queryVehicleBind,queryPage } from "@/utils/network/api.js";
	import { requestNew } from "@/utils/network/request.js";
	import { getItem, StorageKeys} from "@/utils/storage";
	import { jump } from "@/datas/9901Jump.js";
import { deviceType } from "@/utils/network/difference";
import {
		getVehiclePlateColor
	} from "@/datas/vehiclePlateColor";
	import {
		getCodeName
	} from "@/datas/queryKey.js";
	import useOrderListItem from "@/composables/order/useOrderListItem";
	const props = defineProps({
		index: {
			type: Number,
			default() {
				return 0
			}
		},
		refresh: { //是否刷新列表
			type: Boolean,
			default: true
		}
	})
	
	// const { onKeyInput, searchKeyWords } = useOrderListItem(props);
	const state = reactive({
		car:"",
		list: [],
		type: "",//1 OBU重新激活   2 卡签注销   3 更换设备  4卡签续期  5挂失解挂  6增补设备（单卡单签补）  7补卡签（设备丢了补卡签） 8解除挂起
		// 30 储值卡转记账卡  31卡pin码解锁  32ETC车牌过户  33 卡签停用/卡签启用   
		// 34月结单查询  35ETC通行流水记录  36 黑名单查询  37恢复签约 
	});
	onLoad((options) => {
		console.log("options", options)
		state.type = options.type
		if (options.type == "2") {
			uni.setNavigationBarTitle({
				title: 'ETC注销-选择车辆'
			});
		} else if (options.type == "3") {
			uni.setNavigationBarTitle({
				title: '更换ETC设备-选择车辆'
			});
		} else if (options.type == "4") {
			uni.setNavigationBarTitle({
				title: '卡签续期-选择车辆'
			});
		}else if (options.type == "5") {
			uni.setNavigationBarTitle({
				title: '卡签挂失/解除挂失-选择车辆'
			});
		}else if (options.type == "6") {
			uni.setNavigationBarTitle({
				title: '增补设备-选择车辆'
			});
		}else if (options.type == "7") {
			uni.setNavigationBarTitle({
				title: '补卡补签-选择车辆'
			});
		}else if (options.type == "8") {
			uni.setNavigationBarTitle({
				title: '解除挂起-选择车辆'
			});
		}else if (options.type == "30") {
			uni.setNavigationBarTitle({
				title: '储值卡转记账卡-选择车辆'
			});
		}else if (options.type == "31") {
			uni.setNavigationBarTitle({
				title: '卡pin码解锁-选择车辆'
			});
		}else if (options.type == "32") {
			uni.setNavigationBarTitle({
				title: 'ETC车牌过户-选择车辆'
			});
		}else if (options.type == "33") {
			uni.setNavigationBarTitle({
				title: '卡签停用/卡签启用-选择车辆'
			});
		}else if (options.type == "34") {
			uni.setNavigationBarTitle({
				title: '月结单查询-选择车辆'
			});
		}else if (options.type == "35") {
			uni.setNavigationBarTitle({
				title: 'ETC通行流水-选择车辆'
			});
		}else if (options.type == "36") {
			uni.setNavigationBarTitle({
				title: '黑名单查询'
			});
		}else if (options.type == "37") {
			uni.setNavigationBarTitle({
				title: '恢复签约-选择车辆'
			});
		}
		
	});
	onShow(()=>{
		if(state.type=='37'){
			// 恢复签约查询订单接口
			list().then((item : any) => {
				console.log("item",item)
				state.list = item.data;
			})
		}else{
			quanCheckActionTrue().then((item : any) => {
				if (state.type == "32") {
					let data = item
					for (var k = 0; k < data.length; k++) {
						if (data[k]['orderStatus'] != "99999") {
							state.list.push(data[k])
						}
					}
				}else{
					state.list = item;
				}
				console.log(item);
			});
		}
		
	})
	const onKeyInput = (event) => {
		// searchKeyWords.value = event.target.value;
		
	}
	const list=()=>{
		let newsource = "WECHAT"
		// #ifdef MP-ALIPAY
		newsource = "ALI"
		// #endif
		const options = {
			type: 2,
			data: {
				"source": newsource,
				"orderStatus": "ACTIVE",
				"tabIndex":"-1"
			},
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await requestNew(queryPage, options);
			const data = res;
		
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	}
	const quanCheckActionTrue = () => {
		const options = {
			type: 2,
			data: {},
			method: "POST",
			showLoading: true,
		};
		return new Promise(async (resolve, reject) => {
			const res = await requestNew(queryVehicleBind, options);
			const data = res;
            console.log("车辆列表",data)
			resolve(data);
		}).catch((error) => {
			reject(error);
		});
	};
	const flag = ref("0");

	const choose = (i, item) => {
		console.log(item.cardId);
			flag.value = i
			// 判断9901
			// 月结单查询 etc通行流水记录  黑名单查询 目前不用跳转到插件
			if (state.type == "34") {
				navTo(`/subpackage/after-sale/month-statement/month-statement-query-list?vehicleId=${item.vehicleId}`)
			}else if (state.type == "35") {
				navTo(`/subpackage/personal-center/search/etcFlowingWater?vehicleId=${item.vehicleId}`)
			}else if (state.type == "36") {
				uni.navigateTo({
					url: `/subpackage/after-sale/blacklist-query/list?vehicleId=${item.vehicleId}`
				})
			}else{
				if (item.deviceType == deviceType) {
					// OBU重新激活跳转
					const params = encodeURIComponent(JSON.stringify(item))
					jump(state.type, params)
				} else {
					if (state.type == "1") {
						// #ifdef MP-WEIXIN
						navTo(
							`/subpackage/after-sale/activation-once-again/activation-once-again?vehicleId=${item.vehicleId}`
						);
						// #endif
						// #ifdef MP-ALIPAY
						navTo(
							`/subpackage/after-sale/activation-once-again/activation-once-again-ali?vehicleId=${item.vehicleId}`
						);
						// #endif
					} else if (state.type == "2") {
						navTo(`/subpackage/after-sale/ETC-log-off/log-off-confirm?vehicleId=${item.vehicleId}`)
					} else if (state.type == "3") {
						navTo(`/subpackage/after-sale/replace-equipment/replace-equipment-confirm?vehicleId=${item.vehicleId}`)
					} else if (state.type == "4") {
						navTo(`/subpackage/after-sale/card-Renewal/renewal-confirm?vehicleId=${item.vehicleId}`)
					} else if (state.type == "5") {
						navTo(`/subpackage/after-sale/card-loss-reporting/cardloss-confirm?vehicleId=${item.vehicleId}`)
					}else if (state.type == "6") {
						navTo(`/subpackage/after-sale/additional-equipment/additional-equipment-confirm?id=${item.id}`)
					}else if (state.type == "7") {
						navTo(`/subpackage/after-sale/replace-cardObu/replace-equipment-confirm?vehicleId=${item.vehicleId}`)
					}else if (state.type == "8") {
						navTo(`/subpackage/after-sale/releaseSuspension/cardloss-confirm?vehicleId=${item.vehicleId}`)
					}else if (state.type == "30") {
						navTo(`/subpackage/after-sale/to-bookkeeping-card/confirm?id=${item.id}`)
					}else if (state.type == "31") {
						navTo(`/subpackage/after-sale/pin-code-deblocking/pin-code-confirm?vehicleId=${item.vehicleId}`);
					}else if (state.type == "32") {
						navTo(`/subpackage/after-sale/transfer-ownership/transfer-confirm?id=${item.id}`)
					}else if (state.type == "33") {
						navTo(`/subpackage/after-sale/card-deactivation-activation/deactivation-activation-confirm?id=${item.id}`)
					}else  if (state.type == "37") {
						// #ifdef MP-ALIPAY
						uni.navigateTo({
							url: `/subpackage/orders/sign-up-ali?orderId=${item.orderId}&&channelSing=1&&vehiclePlate=${item.vehiclePlate}`,
						});
						// #endif
						// #ifdef MP-WEIXIN
						uni.navigateTo({
							url: `/subpackage/orders/sign-up?orderId=${item.orderId}&&channelSing=1&&vehiclePlate=${item.vehiclePlate}`,
						});
						// #endif
					}
				}
			}
	}
	const add=()=>{
		navTo(`/subpackage/after-sale/blacklist-query/addCar`)
	}
</script>

<style lang="scss" scoped>
	.flex {
		display: flex;
		justify-content: center;
	}

	.selectCar-box {
		height: 100%;
		padding: 30rpx;
		padding-top: 0;
	}
	.message{
		font-size: 26rpx;
		margin-left: 6rpx;
	}
	.search-layout {
		.search-box {
			margin: 30rpx 30rpx 0rpx 30rpx;
			height: 80rpx;
			background: #FFFFFF;
			// border: 1px solid #DCDCDC;
			border-radius: 40rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			box-sizing: border-box;
			flex: 1;
		}
	
		.search-box .icon {
			width: 48rpx;
			height: 48rpx;
			margin: 0 20rpx;
		}
	
		.search-box .search {
			flex: 1;
			margin-right: 20rpx;
			height: 100%;
			padding: 0 10rpx;
			font-size: 28rpx;
			color: #00b38b;
		}
	
		.search-btn {
			color: white;
			background-color: #00B38B;
			width: 140rpx;
			height: 75rpx;
			line-height: 75rpx;
			font-size: 32rpx;
			border-radius: 40rpx;
			text-align: center;
			margin-right: 30rpx;
			margin-top: 30rpx;
		}
	}
	.title{
		font-weight: 400;
		font-size: 34rpx;
		color: #01243A;
		margin: 30rpx 30rpx 0rpx 30rpx;
	}
	.list{
		background: #FFFFFF;
		border-radius: 12rpx;
		border: 1px solid #FFFFFF;
		width: 98%;
		margin: 30rpx auto;
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		.content{
			font-weight: 400;
			width: 76%;
			margin-left: 20rpx;
			.content-top{
				margin-bottom: 20rpx;
				.vehiclePlate{
					font-size: 30rpx;
					color: #01243A;
				}
			}
			.content-time{
				font-size: 26rpx;
				color: #999999;
			}
		}
		.car{
			width: 100rpx;
			height: 100rpx;
		}
		.arrow{
			width: 18rpx;
			height: 34rpx;
		}
	}
	.action {
		position: fixed;
		bottom: 0rpx;
		left: 0;
		height: 188rpx;
		background-color: #fff;
		border-radius: 30rpx 30rpx 0 0;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}
</style>