<template>
	<view class="selectCar-box">
		<view v-if="state.list&&state.list.length>0" @click="choose(i,item)" class="item" v-for="(item,i) in state.list"
			:key="i">
			<view class="iten-left">
				<view>新办单号：{{item.orderId}}</view>
				<view>订单车牌号：{{item.vehiclePlate}}</view>
				<view>订单车牌颜色：{{getVehiclePlateColor(item.vehiclePlateColor)}}</view>
			</view>
			<view class="choose-item">
				<view class="active" v-if="flag==i">
				</view>
			</view>
		</view>

		<view v-else>
			<empty title='暂无找到相关订单信息' />
		</view>
	</view>
</template>

<script lang="ts" setup>
	import empty from "@/components/empty/empty.vue";
	import {
		reactive,
		ref
	} from "vue"
	import {
		msg,
		navTo
	} from "@/utils/utils"
	import {
		onLoad,
	} from "@dcloudio/uni-app";
	import {
		orderList, addEquityListApi
	} from "@/utils/network/api.js";
	import {
		request
	} from "@/utils/network/request.js";
	import {
		getItem,
		StorageKeys,
	} from "@/utils/storage";
	import {
		stringToJson
	} from "@/utils/network/encryption";
	import {
		vehiclePlateColor
	} from "@/datas/vehiclePlateColor";

	const state = reactive({
		list: [],//车辆list,
		equtyId: "",//权益id
	});
	const flag = ref('0') //默认选择0
	//获取车牌颜色文字	
	const getVehiclePlateColor = (id : number) => {

		const colors = vehiclePlateColor.filter(item => item.id == id);
		return colors[0].color
	}
	onLoad((option) => {
		state.equtyId = option.equtyId
		quanCheckActionTrue().then((item : any) => {
			console.log("1", item.data)
			state.list = item.data
		})
	});

	const quanCheckActionTrue = () => {
		let source = ""
		// #ifdef MP-ALIPAY
		source = "ALI"
		// #endif
		// #ifdef MP-WEIXIN
		source = "WECHAT"
		// #endif
		var data = {
			"opId": getItem(StorageKeys.OpenId),
			"source": source,
			"vehiclePlate": "",
			"tabIndex": "0",
			"pageNo": 1,
			"pageSize": 1000,
			"promotionModes": 1,
			"isAfter": true,
			"orderStatus": 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 choose = (i, item) => {
		console.log("item", item)
		flag.value = i
		const options = {
			type: 2,
			data: {
				"type": 1,
				"openId": getItem(StorageKeys.OpenId),
				"orderId": item.orderId,
				"equityId": state.equtyId
			},
			method: 'POST',
			showLoading: true,
		}
		// 权益购买入参有个属性是type:1，代表查询是否已经买过了权益，返回的数据中hasData:true表示买过了，如果hasData:false表示没买过。type:2,表示购买
		request(addEquityListApi, options).then((res) => {
			const data = stringToJson(res.bizContent);
			console.log("加购", data.hasData)
			if (data.hasData) {
				uni.showModal({
					title: '提示',
					content: '重复购买此产品',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							chooseBuy(item)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			} else {
				chooseBuy(item)
			}
		})
	}
	const chooseBuy = (item) => {
		console.log("item", item)
		const options = {
			type: 2,
			data: {
				"type": 2,
				"openId": getItem(StorageKeys.OpenId),
				"orderId": item.orderId,
				"equityId": state.equtyId
			},
			method: 'POST',
			showLoading: true,
		}
		// 权益购买入参有个属性是type:1，代表查询是否已经买过了权益，返回的数据中hasData:true表示买过了，如果hasData:false表示没买过。type:2,表示购买
		request(addEquityListApi, options).then((res) => {
			const data = stringToJson(res.bizContent);
			console.log("购买2", data)
			msg("购买成功")
			setTimeout(() => {
				uni.navigateBack({
					delta: 2
				})
			}, 2000)
		})
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #EEF7F7;
	}
</style>
<style lang="scss" scoped>
	.flex {
		display: flex;
		justify-content: center;
	}

	.selectCar-box {
		// width: 100%;
		height: 100%;
		padding: 30rpx;

		.item {
			padding: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 130rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(223, 223, 223, 0.8);
			border-radius: 20rpx;
			margin-bottom: 30rpx;

			.iten-left {
				align-items: center;
				font-size: 28rpx;

				text {
					margin-left: 20rpx;
					font-size: 32rpx;
					font-family: Noto Sans S Chinese;
					font-weight: 400;
					color: #333333;
				}
			}

			.choose-item {
				width: 44rpx;
				height: 44rpx;
				background: #FFFFFF;
				border: 2rpx solid #00B38B;
				border-radius: 50%;
				margin-right: 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				box-sizing: content-box;
			}

			.active {
				width: 34rpx;
				height: 34rpx;
				background: #00B38B;
				border-radius: 50%;
			}
		}
	}
</style>