<template>
	<view class="content">
		<view @click="confirmPay()" class="ul-item">
			<image style="width: 100%; height: 146rpx;" :src="`${$imgUrl}issuance/equity-bg.png`"
				mode='scaleToFill'>
			</image>
			<view class="item-value">
				<view class="money">
					<text class='unit'>￥</text>
					<text>{{ state.deviceLogOutState.cancelAmount }}</text>
				</view>
				<view class="content">
					<view class="title">
						违约金
					</view>
				</view>
				<view class="r-btn">支付</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { reactive, ref, computed } from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	import { breachContractPaySearch, deviceLogOutStateQuery, breachContractPay, confirmBreachContract } from '@/utils/network/api.js'
	import { requestNew } from "@/utils/network/request.js";
	import { number } from "echarts";
	import { confirm } from '@/utils/utils'
	import { logOffConfirm } from './js/log-off.js'
	import { deviceStore } from '@/stores/device.js'
	
	const deviceStores = deviceStore()
	const state = reactive({
		orderNo: '',
		vehicleId: '',
		isConnectDevice: false, // 是否连接设备
		deviceLogOutState: {
			cancelAgeLimit: 0, // 注销年限
			cancelAmount: 0, // 注销违约金 单位：分
			isBlack: false, // 是否存在黑名单
			isCancel: false, // 是否需要支付违约金
			productName: "" // 产品名称
		},
		flag: true
	})
	
	onLoad(({orderNo, vehicleId, isConnectDevice}) => {
		if (orderNo) {
			state.orderNo = orderNo
			state.vehicleId = vehicleId
			state.isConnectDevice = isConnectDevice === '1'
			getDeviceLogOutStateQuery()
		}
	})
	
	const getDeviceLogOutStateQuery = () => {
		let option = {
			data: {
				vehicleId: state.vehicleId,
				cardId: deviceStores.deviceInfo.cardId,
				obuId: deviceStores.deviceInfo.obuId
			},
			method: "POST",
			showLoading: true,
		}
		requestNew(deviceLogOutStateQuery, option).then(res => {
			state.deviceLogOutState = res
			console.log(state.deviceLogOutState)
		})
	}
	
	const getPayInfo = () => {
		let option = {
			type: 2,
			data: {orderNo: state.orderNo},
			method: "POST",
			showLoading: true,
		}
		requestNew(breachContractPaySearch, option).then(res => {
			
		})
	}
	
	const confirmPay = () => {
		if (!state.flag) return false
		let option = {
			type: 2,
			data: {orderNo: state.orderNo},
			method: "POST",
			showLoading: true,
		}
		state.flag = false
		requestNew(breachContractPay, option).then(res => {
			const data = res;
			uni.requestPayment({
				provider: "wxpay",
				orderInfo: "",
				timeStamp: data.timeStamp,
				nonceStr: data.nonceStr,
				package: data.wxPackage ? data.wxPackage : "",
				signType: data.signType,
				paySign: data.paySign,
				success: (e) => {
					console.log("支付成功", res);
					let params = {
						cardExpireTime: deviceStores.deviceInfo.cardExpireTime,
						hasDevice: '',
						obuExpireTime: deviceStores.deviceInfo.obuExpireTime,
						orderNo: state.orderNo
					}
					logOffConfirm(params)
				},
				fail: function (err) {
					confirm(err, () => { }, "支付失败", false);
				},
			});
			state.flag = true
		}).catch(() => {
			state.flag = true
		})
		
	}
	
	const confirmLogOut = () => {
		
	}
</script>

<style lang="scss" scoped>
	.content{
		padding: 32rpx;
	}
	.ul-item {
		display: flex;
		align-items: center;
	
		&:nth-child(n+2) {
			margin-top: 30rpx;
		}
	
		.item-value {
			padding: 20rpx;
			position: absolute;
			display: flex;
			align-items: center;
			width: 81%;
			box-sizing: border-box;
			height: 146rpx;
	
			.money {
				color: #fff;
				font-size: 46rpx;
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				flex: 1;
	
				.unit {
					font-size: 29rpx;
					margin-right: 2rpx;
				}
			}
	
			.content {
				margin-left: 60rpx;
				flex: 2;
	
				.title {
					font-weight: 400;
					font-family: SourceHanSansCN, SourceHanSansCN;
					font-size: 36rpx;
					color: #2C3246;
				}
			}
	
			.r-btn {
				width: 139rpx;
				height: 65rpx;
				background: radial-gradient(at 0% 0%, #C6B077 0%, #DFCC96 100%);
				border-radius: 33rpx;
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				font-size: 26rpx;
				color: #FFFFFF;
				line-height: 65rpx;
				text-align: center;
				flex: 1;
			}
		}
	
		.icon-tip {
			width: 100rpx;
			height: 100rpx;
		}
	}
</style>