<template>
	<view class="container">
		<!-- 奖项 -->
		<view class="form-item">
			<text class="label">奖项</text>
			<picker :range="options" v-model="selectedOptionIndex" @change="onPickerChange">
				<view class="select">{{ options[selectedOptionIndex] || '请选择奖项' }} </view>
			</picker>
		</view>
		<!-- 政治面貌选择 -->
		<view class="form-item">
			<text class="label">政治面貌：</text>
			<picker :range="politicalOptions" v-model="selectedPoliticalIndex" @change="onPoliticalChange">
				<view class="select">{{ politicalOptions[selectedPoliticalIndex] || '请选择政治面貌' }}</view>
			</picker>
		</view>
		<!-- 职务输入 -->
		<view class="form-item">
			<text class="label">职务：</text>
			<input v-model="position" class="uni-input" focus placeholder="请输入职务" />
		</view>
		<!-- 银行卡号输入 -->
		<view class="form-item">
			<text class="label">银行卡号：</text>
			<input v-model="cardID" class="uni-input" focus placeholder="请输入银行卡号" />
		</view>
		<!-- 开户行 -->
		<view class="form-item">
			<text class="label">开户行：</text>
			<input v-model="bank" class="uni-input" focus placeholder="请输入开户行" />
		</view>
		<!-- 获奖情况 -->
		<view class="form-text">
			<text class="label">获奖情况：</text>
			<view class="txt">
				<uni-forms-item>
					<uni-easyinput type="textarea" v-model="introduction" maxlength="1000" placeholder="请输入获奖情况" />
				</uni-forms-item>
			</view>

		</view>
		<!-- 主要事迹 -->
		<view class="form-text">
			<text class="label">主要事迹：</text>
			<view class="txt">
				<uni-forms-item>
					<uni-easyinput type="textarea" v-model="deeds" maxlength="1000" placeholder="请输入主要事迹" />
				</uni-forms-item>
			</view>

		</view>

		<!-- 按钮 -->
		<button @click="showPopup">提交申请</button>
	</view>
</template>

<script>
	import {
		listOwnScoreClassRank,
		listXyjxjCanType,
		getOwnPassCountByCode,
		getOwnIamCountByCode,
		getOwnRankByCode
	} from "@/api/good/index.js";

	import {
		applyXyjxj
	} from "@/api/good/xyjxj.js";

	import {
		getDict,
		isEmpty
	} from "@/api/helpFunc/index.js";

	import {
		alipayVali
	} from "@/api/helpFunc/bank";

	export default {
		data() {
			return {
				options: [], // 奖项选项
				selectedOptionIndex: 0, // 当前选中的奖项索引
				politicalOptions: ['群众', '共青团员', '中共党员'], // 政治面貌选项
				selectedPoliticalIndex: 0, // 当前选中的政治面貌索引
				position: '', // 职务，
				cardID: "", // 银行卡号，
				bank: '',
				introduction: "",
				deeds: "",


				can_list: []
			};
		},
		async onShow() {

			uni.showLoading();
			await this.listCan();
			uni.hideLoading();
			await this.apply();
		},
		methods: {

			async apply() {
				
				uni.showLoading();

				let cans = [...this.can_list];
				if (cans.length == 0) {

					// 在这里编写申请逻辑
					uni.navigateTo({
						url: "/pages/award/award"
					});

					uni.showToast({
						title: "当前没有可申请的项目",
						icon: "none"
					});
					return;
				} else {
					let code = cans[0].typeCode;

					let res1 = await getOwnPassCountByCode(code);
					if (res1.code == 200) {
						if (isEmpty(res1.data)) {
							uni.navigateTo({
								url: "/pages/award/award"
							});
							uni.showToast({
								title: "您当前学年的课程成绩暂未结算",
								icon: "none"
							});
							return;
						}

						let data1 = res1.data;

						if (data1.unpassCount != 0) {
							uni.navigateTo({
								url: "/pages/award/award"
							});
							uni.showToast({
								title: "您有课程成绩没通过，不能参加评优评先哦",
								icon: "none"
							});
							return;
						}
					}

					let res2 = await getOwnIamCountByCode(code);
					if (res2.code == 200) {
						let data2 = res2.data;
						if (isEmpty(data2)) {
							data2 = {
								minusCount: 0
							}
						}
						if (data2.minusCount != 0) {
							uni.navigateTo({
								url: "/pages/award/award"
							});
							uni.showToast({
								title: "您有思想品德扣分项，不能参加评优评先哦",
								icon: "none"
							});
							return;
						}
					}

					let res3 = await getOwnRankByCode(code);
					if (res3.code == 200) {
						if (!isEmpty(res3.data)) {
							let rankData = res3.data;
							let classCount = rankData.classCount;
							let cphClassRank = rankData.cphClassRank;
							let stuClassRank = rankData.stuClassRank;

							let cphPer = Math.round(cphClassRank / classCount * 10000) / 100;
							let stuPer = Math.round(stuClassRank / classCount * 10000) / 100;

							if (cphPer > 50) {
								uni.navigateTo({
									url: "/pages/award/award"
								});
								uni.showToast({
									title: "你的综合素质成绩排名不达标",
									icon: "none"
								});
								return;
							}

							if (stuPer > 12) {
								uni.navigateTo({
									url: "/pages/award/award"
								});
								uni.showToast({
									title: "你的平均学分绩排名不达标",
									icon: "none"
								});
								return;
							}
						}
					}
				}
				
				uni.hideLoading();



			},

			async listCan() {
				let res = await listXyjxjCanType();
				if (res.code == 200) {
					this.can_list = [...res.data];

					this.options = this.can_list.map(x => x.typeName);

				}
			},
			async showPopup() {

				let sdata = {
					typeCode: this.can_list[this.selectedOptionIndex].typeCode,
					zzmm: this.politicalOptions[this.selectedPoliticalIndex],
					classPost: this.position,
					bankCard: this.cardID,
					bankAddr: this.bank,
					goodHis: this.introduction,
					mainHis: this.deeds
				}

				if (isEmpty(sdata.typeCode)) {
					uni.showToast({
						title: "请选择奖项",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.zzmm)) {
					uni.showToast({
						title: "请填写政治面貌",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.classPost)) {
					uni.showToast({
						title: "请填写职务",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.bankCard)) {
					uni.showToast({
						title: "请填写中国农业银行银行卡号",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.bankAddr)) {
					uni.showToast({
						title: "请填写开户行",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.goodHis)) {
					uni.showToast({
						title: "请填写获奖记录",
						icon: "none"
					});
					return;
				}
				if (isEmpty(sdata.mainHis)) {
					uni.showToast({
						title: "请填写主要事迹",
						icon: "none"
					});
					return;
				}
				if (sdata.mainHis.length < 100) {
					uni.showToast({
						title: "主要事迹字数需要100字以上",
						icon: "none"
					});
					return;
				}

				let card = sdata.bankCard;
				let boolCard = await alipayVali(card);
				if (boolCard.validated == false) {
					uni.showToast({
						title: "请输入正确的银行卡号",
						icon: "none"
					});
					return;
				}

				if (boolCard.bank != "ABC") {
					uni.showToast({
						title: "请输入正确的中国农业银行银行卡号",
						icon: "none"
					});
					return;
				}
				
				uni.showLoading();
				
				let res = await applyXyjxj(sdata);
				
				uni.hideLoading();
				if(res.code == 200){
					uni.showToast({
						title: '申请成功',
						icon: 'success',
						duration: 2000, // 弹窗持续时间，单位为毫秒
						success() {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/award/award' // 替换为您要跳转的页面路径
								});
							}, 1000); // 等待2秒后执行页面跳转，确保用户能看到弹窗内容
						}
					});
				}else{
					uni.showToast({
						title: res.msg,
						icon: "error"
					});
				}



				

			},
			messageToggle(type) {
				this.msgType = type
				this.messageText = `这是一条${type}消息提示`
				this.$refs.message.open()
			},
			// 当选择发生变化时触发
			onPickerChange(event) {
				this.selectedOptionIndex = event.detail.value;
			},
			// 政治面貌选择改变时触发
			onPoliticalChange(event) {
				this.selectedPoliticalIndex = event.detail.value;
			}
		}
	};
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 10rpx;
	}

	.form-item {
		width: 95%;
		height: 80rpx;
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
		justify-content: space-between;

		border-bottom: 1px solid #ededee;
	}

	.form-text {

		width: 95%;

		margin-bottom: 20rpx;
		border-bottom: 1px solid #ededee;
	}

	.txt {
		margin-top: 20rpx;
	}


	.label {

		text-align: right;
		margin-right: 20rpx;
	}

	.select {
		color: #888889;
	}

	.uni-input {
		/*      width: 23%; */
		color: #888889;
		text-align: right;
	}

	button {
		width: 90%;
		background-color: #3388CC;
		color: white;
	}
</style>