<template>
	<view class='content'>
		<view class="top-content">
			<view class="input">
				<text>车牌号：</text>
				<input placeholder="请输入车牌号" v-model="state.vehiclePlate" />
			</view>
			<view class="last input">
				<text>车牌颜色：</text>
				<uni-data-select v-model="state.vehiclePlateColor" :localdata="state.colorRange" @change="changeColor"
					:clear="false"></uni-data-select>
			</view>
			<view class="example-body">
				<uni-datetime-picker v-model="state.range" type="daterange" />
				<button size="mini" style="color: #ffffff;
				backgroundColor: rgb(118, 200, 77);
				borderColor: rgb(118, 200, 77);
				font-size: 26rpx;
				flex-shrink: 0;margin-left: 20rpx;" @click="search()">搜索</button>
			</view>
		</view>
		<view style="margin-top: 250rpx;">
			<view class='item' v-for="(item,index) in state.newList">
				<view>ETC卡号：{{item.cardId}}<text class="payStatus">{{item.payStatusC}}</text></view>
				<view>充值金额：￥{{item.rechargeMoney/100}}</view>
				<view>车牌号：{{item.vehiclePlate}}</view>
				<view>创建时间：{{item.insertTime}}</view>
				<view>圈存状态：{{item.statusC}}</view>
				<!-- 支付成功和未圈存 -->
				<view class="but-wrap" v-if="item.statusC =='待支付'">
					<text @click="pay(item.rechargeMoney,item.orderNum)">去支付</text>
				</view>
				<view class="but-wrap" v-else-if="item.statusC =='已支付' && item.chargeStatus=='未圈存'">
					<text @click="toTrap(item.rechargeMoney)">修复</text>
					<text @click="queryRefound(item.cardId,item.orderNum)">退款</text>

				</view>
			</view>
			<view style="text-align: center;margin: 20rpx;" v-if="state.flags">我是有底线的~</view>
			<view class="noContent" v-if="!state.newList.length">暂无消费明细</view>
		</view>

	</view>
</template>

<script lang="ts" setup>
	import { reactive } from "vue";
	import { request } from "@/utils/network/request.js";
	import { queryCardRecord, refundQuan, queryRefoundResult } from "@/utils/network/api.js";
	import { stringToJson } from "@/utils/network/encryption.js";
	import { onLoad, onReachBottom, onShow } from "@dcloudio/uni-app";
	import { getItem, StorageKeys } from "@/utils/storage";
	import { getCodeName } from "@/datas/queryKey.js";
	import {
		navTo, msg, timesDiff
	} from "@/utils/utils";
	const state = reactive({
		list: '',  //所有数据
		newList: '', //最终展示的
		cardId: '',
		value: '',//input框里的值
		pageNo: 1,
		pageSize: 16,
		flags: false,
		vehiclePlate: "",
		vehiclePlateColor: "",
		colorRange: [],
		range: ['', ''],
	})
	onLoad((option : any) => {
		console.log("option", option)
		state.cardId = option.cardId;
		let getColor = getItem('key')['VEHICLE_COLOR_TYPE'];
		for (var k = 0; k < getColor.length; k++) {
			let obj = {};
			obj['value'] = getColor[k]['code']
			obj['text'] = getColor[k]['name']
			state.colorRange.push(obj)
		}
	})
	onShow(() => {
		state.pageNo = 1
		state.newList = []
		getList();
	})
	const search = () => {
		state.pageNo = 1
		state.newList = []
		getList();
	}
	const changeColor = (e) => {
		state.vehiclePlateColor = e
		console.log(e)
	}
	const getList = () => {
		const options = {
			type: 2,
			data: {
				'openId': getItem('openId'),
				'pageNo': state.pageNo,
				'pageSize': state.pageSize,
				'vehicleId': state.vehiclePlate ? state.vehiclePlate + "_" + state.vehiclePlateColor : "",
				'insertTimeStart': state.range[0] ? state.range[0] + ' 00:00:00' : state.range[0],
				'insertTimeEnd': state.range[1] ? state.range[1] + ' 00:00:00' : state.range[1],
			},
			method: "POST",
			showLoading: true,
		};
		console.log("data", state.vehiclePlate, state.vehiclePlateColor, state.range)
		request(queryCardRecord, options).then((res) => {
			let data = [...state.newList, ...stringToJson(res.bizContent).data]
			for (var i = 0; i < data.length; i++) {
				console.log("getCodeName", getCodeName('VCR_ORDER_STATUS', data[i]['status']))
				data[i]['payStatusC'] = getCodeName('VCR_ORDER_PAY_STATUS', data[i]['payStatus'])
				// 大于5分钟就不能去支付了
				data[i]['statusC'] = getCodeName('VCR_ORDER_STATUS', data[i]['status'])
			}
			state.newList = data
			console.log("交易记录", state.newList)
		})
	}

	// 触底加载
	onReachBottom(() => {
		if (state.newList.length < state.pageNo * 16) return state.flags = true
		console.log("触底了")
		state.pageNo++
		getList()
	})
	const toTrap = (rechargeMoney) => {
		uni.navigateTo({
			url: `/subpackage/personal-center/trapping-and-repairing/recharge-pay?rechargeMoney=${rechargeMoney}&&payMoney=0`
		})
	}
	// 退款代码
	const queryRefound = (cardId, orderId) => {
		const options = {
			type: 2,
			data: {
				"orderId": orderId,
				"cardId": cardId,
				"openId": getItem(StorageKeys.OpenId),
			},
			method: 'POST',
			showLoading: true,
		}
		console.log("refundQuan", refundQuan, options)
		request(refundQuan, options).then((res) => {
			const data = stringToJson(res.bizContent);
			refoundResult(cardId, orderId);
			console.log("222", data)
		})
	}
	const refoundResult = (cardId, orderId) => {
		const options = {
			type: 2,
			data: {
				"orderId": orderId,
				"cardId": cardId,
			},
			method: 'POST',
			showLoading: true,
		}
		console.log("queryRefoundResult", queryRefoundResult, options)
		request(queryRefoundResult, options).then((res) => {
			const data = stringToJson(res.bizContent);
			console.log("data", data)
			if (data.refundStatus == "SUCCESS") {

			} else if (data.refundStatus == "CLOSED") {
				msg('退款关闭')
			} else if (data.refundStatus == "HANDLING") {
				msg('退款处理中')
			} else if (data.refundStatus == "EXCEPTION") {
				msg('退款异常')
			}
			setTimeout(() => {
				console.log("刷新")
				// 刷新列表
				state.pageNo == 1
				getList();
			}, 1500)

		})
	}
	// 退款代码完成
	// 去支付
	const pay = (rechargeMoney, orderNum) => {
		uni.navigateTo({
			url: `/subpackage/personal-center/trapping-and-repairing/recharge-pay?rechargeMoney=${rechargeMoney}&&payMoney=1&&orderNum=${orderNum}`
		})
	}
</script>

<style scoped>
	.noContent {
		text-align: center;
		margin-top: 100rpx;
	}

	.content {
		background-color: #EEF7F7;
		padding: 0 30rpx;
		overflow: hidden;
		font-size: 32rpx;
		min-height: 100vh;
	}

	.search_wrap {
		display: flex;
		margin: 20rpx 0;
	}

	.search_wrap>input {
		background-color: white;
		width: 76%;
		height: 40rpx;
		line-height: 40rpx;
		padding: 10rpx 10rpx;
		border-radius: 10rpx 0 0 10rpx;
	}

	.item {
		width: 100%;
		border-radius: 10rpx;
		box-sizing: border-box;
		padding: 30rpx 20rpx;
		margin-top: 30rpx;
		background: white;
		color: black;
	}

	.time {
		display: flex;
		margin-bottom: 16rpx;
		justify-content: space-between;
	}

	.payStatus {
		color: red;
		float: right;
	}

	.but-wrap {
		display: flex;
		justify-content: flex-end;
		margin-top: 10rpx;
	}

	.but-wrap>text {
		border: 1px solid #00b38b;
		color: #00b38b;
		height: 50rpx;
		line-height: 50rpx;
		border-radius: 30rpx;
		padding: 0 24rpx;
		font-size: 28rpx;
		box-sizing: border-box;
		margin-left: 12rpx;
	}

	.top-content {
		position: fixed;
		left: 0;
		top: 0;
		background-color: white;
		width: 100%;
		padding: 0 20rpx 20rpx 20rpx;
		box-sizing: border-box;
		z-index: 999999;
	}

	input,
	.uni-input {
		border: 1rpx solid #ccc;
		padding: 0 10rpx;
		height: 28rpx;
		line-height: 28rpx;
	}


	.input {
		display: flex;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.input>text {
		display: inline-block;
		width: 24%;
	}

	.input>input {
		width: 47%;
		margin-top: 10rpx;
		font-size: 30rpx;
	}

	/deep/.last .uni-stat__select {
		width: 360rpx;
	}

	.example-body {
		display: flex;
		align-items: center;
	}



	/deep/.uni-date {
		width: 73% !important;
	}

	/deep/.uni-date__x-input,
	/deep/.uni-select {
		font-size: 13px;
		height: 24px;
		line-height: 24px;
	}

	/deep/.uni-date-x {
		height: 56rpx !important;
	}
</style>