<template>
	<view class="add">
		<view class="form-item">
			<label><text class="tip">*</text>政治面貌</label>
			<picker :range="politicsOptions" @change="onPoliticsChange">
				<view class="uni-input">
					<text class="val">{{ politicsOptions[selectedPoliticsOptionIndex] || '请选择政治面貌' }}</text>
					<uni-icons type="down" size="16" color="#202020"></uni-icons>
				</view>
			</picker>
		</view>
		<view class="form-item">
			<label><text class="tip">*</text>第二课堂学分认定</label>
			<picker :range="secondClassAchievement" @change="onSecondClassAchievementChange">
				<view class="uni-input">
					<text class="val">{{ secondClassAchievement[secondClassAchievementIndex] || '请选择成绩认定' }}</text>
					<uni-icons type="down" size="16" color="#202020"></uni-icons>
				</view>
			</picker>
		</view>
		<view class="form-item">
			<label><text class="tip">*</text>青年大学习学习率</label>
			<input type="text" v-model="valiFormData.youthPercent" placeholder="单位为%" />
		</view>

		<view class="form-item">
			<label><text class="tip">*</text>职务</label>
			<input type="text" v-model="valiFormData.classPost" placeholder="(例如：班长，没有填 无)" />
		</view>
		<view class="form-item">
			<label><text class="tip">*</text>获奖情况</label>
			<textarea placeholder="请输入" v-model="valiFormData.goodHis"></textarea>
		</view>
		<view class="form-item">
			<label><text class="tip">*</text>主要事迹</label>
			<textarea class="main-his" placeholder="字数100字以上" v-model="valiFormData.mainHis"></textarea>
		</view>
		<view class="btns">
			<button @click="onReSubmit" :disabled="isSubmitting" v-if="detail.applyStatus==10">重新提交</button>
		</view>
	</view>
</template>

<script>
	import {
		toBackPage
	} from "@/utils/toBack.js"
	import {
		validateFormFields
	} from "@/utils/validateForm.js"
	import {
		goodStuApply,
		verifyApplySHXS,
		reApply
	} from "@/api/good/shxs.js";
	export default {
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				detail: null,
				// 校验表单数据
				valiFormData: {
					zzmm: '',
					classtwoSure: '',
					youthPercent: '',
					classPost: '',
					goodHis: '',
					mainHis: '',
				},
				politicsOptions: ['群众', '共青团员', '党员'], // 政治面貌
				selectedPoliticsOptionIndex: -1, // 当前选中的政治面貌索引
				secondClassAchievement: ["合格", "不合格"], // 学年
				secondClassAchievementIndex: -1, // 当前选中的学年索引
			}
		},
		onLoad(option) {
			this.detail = JSON.parse(option.detail);
			this.valiFormData = this.detail;
			this.politicsOptions.forEach((item, index) => {
				if (item == this.valiFormData.zzmm) {
					this.selectedPoliticsOptionIndex = index;
				}
			})
			this.secondClassAchievement.forEach((item, index) => {
				if (item == this.valiFormData.classtwoSure) {
					this.secondClassAchievementIndex = index;
				}
			})
			console.log(this.valiFormData);
		},
		methods: {
			//选择成绩认定
			onSecondClassAchievementChange(e) {
				this.secondClassAchievementIndex = e.detail.value;
				this.valiFormData.classtwoSure = this.secondClassAchievement[this.secondClassAchievementIndex];
			},
			//选择政治面貌
			onPoliticsChange(e) {
				this.selectedPoliticsOptionIndex = e.detail.value;
				this.valiFormData.zzmm = this.politicsOptions[this.selectedPoliticsOptionIndex];
			},
			onReSubmit() {
				const requiredFields = [
					'zzmm',
					'classtwoSure',
					'youthPercent',
					'classPost',
					'goodHis',
					'mainHis',
				];
				if (!validateFormFields(requiredFields, this.valiFormData)) {
					return;
				}
				if (this.valiFormData.mainHis.length < 100) {
					uni.showToast({
						title: "主要事迹字数需要100字以上",
						icon: "none"
					});
					return;
				}
				console.log(this.valiFormData);
				this.isSubmitting = true; // 设置为正在提交
				uni.showLoading({
					title: "正在提交",
					success: () => {
						reApply(this.valiFormData).then(res => {
							if (res.code == 200) {
								uni.showToast({
									title: "重新提交成功"
								})
								toBackPage(500, (page) => {
									page.getList();
									this.isSubmitting = false;
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		background-color: #F5F5F7;
		padding: 10px;
		padding-bottom: 80px;

		.form-item {
			display: flex;
			flex-direction: column;
			padding: 22rpx 40rpx;
			background-color: white;
			margin-bottom: 20rpx;
			border-radius: 16rpx;

			.tip {
				color: red;
				font-size: 24rpx;
			}

			label {
				margin-bottom: 20rpx;
				display: inline-block;
			}

			input {
				border: 1px solid #E1E1E1;
				border-radius: 10rpx;
				height: 70rpx;
				padding-left: 30rpx;

				.input-placeholder {
					color: #b6b6b6;
				}
			}

			textarea {
				border: 1px solid #E1E1E1;
				border-radius: 10rpx;
				width: 100%;
				height:250rpx;
				padding: 30rpx;
				&.main-his{
					height:500rpx;
				}
				.input-placeholder {
					color: #b6b6b6;
				}
			}

			picker {
				border: 1px solid #E1E1E1;
				height: 70rpx;
				line-height: 70rpx;
				padding: 0 30rpx;

				text {
					color: #b6b6b6;
				}

				.uni-input {
					display: flex;
					color: #797979;

					.val {
						flex: 1;
					}
				}
			}

			.upload-img {
				border: 1px solid #E1E1E1;
				padding: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				text-align: center;

				.tip {
					margin-top: 15rpx;
					color: #7a7a7a;
					font-size: 24rpx;
				}
			}

			.img-list {
				border: 1px solid #D8D8D8;
				border-radius: 20rpx;

				.imgs {
					padding: 22rpx;
					display: flex;
					flex-wrap: wrap;

					.img {
						position: relative;
						width: 160rpx;
						height: 170rpx;
						margin-bottom: 15rpx;
						margin-right: 15rpx;

						image {
							width: 100%;
							height: 100%;
						}

						.remove {
							position: absolute;
							right: 0;
							top: 0;
							color: white;
							background-color: rgba(0, 0, 0, 0.4);
							width: 52rpx;
							height: 52rpx;
							display: flex;
							justify-content: center;
							align-items: center;
							border-radius: 50%;

						}
					}

				}

				.btn {
					border-top: 1px solid #D8D8D8;
					height: 100rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 32rpx;
				}
			}
		}

		.btns {
			padding: 10px;
			display: flex;
			background: white;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			z-index: 999;

			button {
				flex: 1;
				background-color: #1890FF;
				color: white;

				&[disabled] {
					opacity: 0.5;
				}
			}

		}
	}
</style>