<template>
	<view class="container">
		<view class="info-box">
			<view class="info-item-box flex">
				<view class="label">
					姓名
				</view>
				<view class="text">
					{{ info.username }}
				</view>
			</view>
			<view class="info-item-box flex">
				<view class="label">
					证件号码
				</view>
				<view class="text">
					{{ info.idNumber }}
				</view>
			</view>
			<view class="info-item-box flex">
				<view class="label">
					业务水平认证证书编号
				</view>
				<view class="text">
					{{ info.licenseNo ?? '无' }}
				</view>
			</view>
			<view class="info-item-box flex">
				<view class="label">
					信用信息卡号
				</view>
				<view class="text">
					{{ info.creditNo ?? '无' }}
				</view>
			</view>
			<view class="info-item-box flex">
				<view class="label">
					缴费年份
				</view>
				<view class="text" @click="showYear = true">
					{{ info.year }}（可拉下选中缴费时间）
				</view>
			</view>
		</view>
		<u-select v-model="showYear" :list="yearList" @confirm="onYearConfirm"></u-select>
		<view class="bottom-box">
			<u-button type="error" shape="circle" @click="onSubmit">点击缴费</u-button>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { person, personPayment } from '@/api/cost.js'
	
	import { useAuthStore } from '@/store/authStore'
	const authStore = useAuthStore()
	
	const openid = ref('')
	
	const form = ref({
		name: '',
		idNumber: ''
	})
	
	const info = ref({
		userId: 1,
		username: 'xxxxxxxxxx', // 姓名
		idNumber: 'xxxxxxxxxx', // 证件号码
		phone: 'xxxxxxxxxx', // 手机号码
		licenseNo: 'xxxxxxxxxx', // 业务水平认证证书编号
		creditNo: 'xxxxxxxxxx', // 信用信息卡号
		year: '', // 缴交年份
		duePrice: 0.00, // 价格
	})
	
	const showYear = ref(false)
	const yearList = ref([
		{
			value: '2025',
			label: '2025'
		}
	])
	function onYearConfirm(val) {
		info.value.year = val[0].value
	}
	
	function onSubmit() {
		const form = {
			title: `${info.value.year}年度个人会费代缴`,
			year: info.value.year,
			amount: info.value.duePrice,
			desc: `${info.value.year}年度个人会费代缴`,
			userId: info.value.userId,
			openid: openid.value
		}
		personPayment(form).then(res => {
			if (res && res.message === 'success') {
				const params = res.data
				wx.requestPayment({
				  timeStamp: params.timeStamp,
				  nonceStr: params.nonceStr,
				  package: params.package,
				  signType: params.signType,
				  paySign: params.paySign,
				  success (res) {
						msgSuccess('支付成功')
						uni.navigateBack()
					},
				  fail (res) {
						const errMsg = res.errMsg
						if (errMsg.indexOf('fail cancel')) {
							msgError('已取消支付')
						}
					}
				})
			}
		})
	}
	
	onLoad((load) => {
		yearList.value = []
		const year = new Date().getFullYear()
		
		openid.value = uni.getStorageSync('openid')
		
		for (let i = 0; i < 10; i++) {
			yearList.value.push({
				value: `${year - i}`,
				label: `${year - i}`
			})
		}
		
		form.value = {
			name: load.name,
			idNumber: load.idNumber
		}
		person(form.value).then(res => {
			if (res && res.message === 'success') {
				info.value = res.data
				info.value.year = year
				info.value.userId = res.data.userId
			}
		})
	})
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: $uni-bg-color;
		padding: 0 20rpx;
		.info-box {
			padding: 30rpx;
			background-color: $uni-bg-color-grey;
			border-radius: $uni-card-border-radius;
			.info-item-box {
				font-size: $uni-title-font-size-2;
				border-bottom: 1rpx solid #E6E6E6;
				height: 82rpx;
				.label {
					font-weight: bold;
				}
			}
			.flex {
				display: flex;
				align-items: center;
				.label {
					width: 40%;
				}
				.text {
					width: 60%;
				}
			}
			.row {
				.label {
					line-height: 62rpx;
				}
				.text {
					line-height: 42rpx;
					font-size: $uni-title-font-size-3;
					font-weight: bold;
				}
			}
		}
	
		.bottom-box {
			padding: 20rpx;
			
			position: absolute;
			bottom: 50rpx;
			width: 95%;
		}
	}
</style>