<template>
	<view class="add" v-if="teststate">
		<view class="form-item">
			<label><text class="tip">*</text>学年</label>
			<picker  :value="stuYearListIndex" :range="stu_year_list" :range-key="'stuYearName'" @change="onPoliticsChange">
				<view class="uni-input">
					<text class="val">{{stuYearListIndex<0?'请选择学年':stu_year_list[stuYearListIndex].stuYearName}}</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="child_list" :value="childListIndex" :range-key="'ruleName'" @change="onSecondClassAchievementChange">
				<view class="uni-input">
					<text class="val">{{childListIndex<0?'请选择加分项':child_list[childListIndex].ruleName}}</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 disabled="" type="text" v-model="iam_add_record" placeholder="0" />
		</view>
		<view class="form-item">
			<view class="content">
				<label><text class="tip">*</text>佐证资料</label>
				<view class="upload-img" v-if="tempSceneImgs.length==0" @tap="uploadSceneImg">
					<view class="">
						+ 添加照片
					</view>
					<view class="tip">
						图片要求：大小不超过<text style="color: red;"> 0.5MB </text>格式为<text style="color: red;">png/jpg/jpeg</text> 的文件
					</view>
				</view>
				<view class="img-list" v-else
				>
					<view class="imgs">
						<view class="img" v-for="(img,index) in tempSceneImgs">
							<image :src="img.path" @tap="previewImg(tempSceneImgs)" mode="aspectFill"></image>
							<text class="remove" @tap="onRemoveSceneImg(index,img.path)">X</text>
						</view>
					</view>
					<view  class="btn" @tap="uploadSceneImg">
						+ 添加照片
					</view>
				</view>
			</view>
		</view>
		<view class="form-item">
			<label><text class="tip">*</text>备注</label>
			<input type="text" v-model="valiFormData.description" placeholder="请输入内容" />
		</view>
		<view class="btns">
			<button @click="onSubmit" :disabled="isSubmitting">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		validateFormFields
	} from "@/utils/validateForm.js"
	import {
		listEnableYear,listIamChild,stuAdd
	} from '@/api/comprehensive/student.js';
	import {
		uploadImg,
		previewImg,
		removeImg
	} from "@/utils/uploadImg.js"
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				// 校验表单数据
				valiFormData: {
					stuYearId: '',
					material: '',
					description: '',
					ruleId: '',
				},
				stuYearListIndex: -1, // 当前选中的政治面貌索引
				childListIndex: -1, // 当前选中的学年索引
				stu_year_list:[],
				teststate:false,
				child_list:[],
				iam_add_record:0,
				rwPicture: "",
				tempSceneImgs: [],
			}
		},
		onLoad() {
			this.getStuYear()
			this.listIamChild()
		},
		methods: {
			//选择学年
			onSecondClassAchievementChange(e) {
				this.childListIndex = e.detail.value;
				this.valiFormData.ruleId = this.child_list[this.childListIndex].ruleId;
				this.iam_add_record=this.child_list[this.childListIndex].maxScore;
			},
			//选择加分项
			onPoliticsChange(e) {
				this.stuYearListIndex = e.detail.value;
				this.valiFormData.stuYearId = this.stu_year_list[this.stuYearListIndex].id;
			},
			onSubmit() {
				const requiredFields = [
					'stuYearId',
					'material',
					'description',
					'ruleId',
				];
				this.valiFormData.material=this.rwPicture
				console.log(this.valiFormData)
				if (!validateFormFields(requiredFields, this.valiFormData)) {
					return;
				}
				this.isSubmitting = true; // 设置为正在提交
				uni.showLoading({
					title: "正在提交",
					success: () => {
						stuAdd(this.valiFormData).then(res => {
							if (res.code == 200) {
								uni.showToast({
									title: "申请成功"
								})
								this.isSubmitting = false;
								uni.navigateBack();
							}
						})
					}
				})
			},
			async getStuYear() {
			  let res = await listEnableYear();
			  this.stu_year_list = [...res.rows];
			  console.log( this.stu_year_list)
			  this.teststate=true
			  // this.iamForm.stuYearId = res.rows[0].id;
			},
			async listIamChild() {
			  this.child_list = [];
			  let res = await listIamChild();
			  console.log(res)
			  let temp = [...res.data];
			  temp.map(x => {
			    if (x.maxScore >= 0) {
			      this.child_list.push(x);
			    }
			  })
			  console.log(this.child_list)
			},
			uploadSceneImg() {
				uploadImg('/common/upload', this.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.rwPicture = result.photo
						// console.log(result)
					}
				});
			},
			previewImg(imgs) {
				console.log(imgs)
				previewImg(imgs);
			},
			onRemoveSceneImg(index, path) {
				for (var i = 0; i < this.tempSceneImgs.length; i++) {
					console.log("tempSceneImgs:"+this.tempSceneImgs[i])
				}
				removeImg(index, path, this.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.rwPicture = result.photo
						// this.submitlist.rtFuResourceList.splice(index, 1);
					}
				});
			},
		}
	}
</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: 200rpx;
				padding: 30rpx;

				.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;
					}
				}
			}
			.content {
				background-color: white;
				border-radius: 16rpx;
				width: 90vw;
			
				&>view:nth-child(-n+3) {
					margin: 40rpx 40rpx;
				}
			
				.title {
					padding: 40rpx 0 20rpx;
					display: flex;
					justify-content: center;
					font-size: 38rpx;
					font-weight: 550;
				}
			
				.tip {
					color: red;
				}
			
			
				.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;
					}
				}
			}
			.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>