<template>
	<view class="detail" >
		<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?DefaultYear: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?Defaultchild: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" >
					<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"
				>
					<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)">X</text>
						</view>
					</view>
					<view v-if="detail.auditStatus==10" @tap="uploadSceneImg" class="btn">
						+ 添加照片
					</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="form-item" v-if="detail.auditStatus==10||detail.auditStatus==11">
			<label><text class="tip">*</text>未通过原因</label>
			<input type="text" disabled v-model="detail.remarks" placeholder="请输入内容" />
		</view>
		<view class="btns" v-if="detail.auditStatus==10">
			<button @click="resubmitIamForm" :disabled="isSubmitting">重新提交</button>
		</view>
	</view>
</template>

<script>
	import {
		validateFormFields
	} from "@/utils/validateForm.js"
	import {
		listEnableYear,listIamChild,stuAdd,reSub
	} 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: {
					iamId:'',
					stuYearId: '',
					material: '',
					description: '',
					ruleId: '',
				},
				stuYearListIndex: -1, // 当前选中的政治面貌索引
				childListIndex: -1, // 当前选中的学年索引
				stu_year_list:[],
				teststate:false,
				child_list:[],
				iam_add_record:0,
				rwPicture: "",
				tempSceneImgs: [],
				detail:[],
				baseUrl:baseUrl,
				DefaultYear:'',
				Defaultchild:'',
			}
		},
		onLoad(option) {
			var arr = JSON.parse(option.item)
			this.detail=JSON.parse(option.item)
			this.valiFormData.iamId=arr.id
			this.valiFormData.stuYearId=arr.stuYearId
			this.valiFormData.material=arr.material
			this.valiFormData.ruleId=arr.ruleId
			this.valiFormData.description=arr.description
			this.DefaultYear=arr.stuYearName
			this.Defaultchild=arr.ruleName
			this.iam_add_record=arr.operateScore
			this.rwPicture=arr.material
			console.log(this.detail)
			var arr1 =  arr.material.split(",")
			for (var i = 0; i < arr1.length; i++) {
				var obj={}
				obj.path=baseUrl+arr1[i]
				this.tempSceneImgs.push(obj)
			}
			console.log(this.tempSceneImgs)
			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;
			},
			resubmitIamForm() {
				const requiredFields = [
					'iamId',
					'stuYearId',
					'material',
					'description',
					'ruleId',
				];
				this.valiFormData.material=this.rwPicture
				if(this.stuYearListIndex<0){
					this.valiFormData.stuYearId=this.detail.stuYearId
				}
				if(this.childListIndex<0){
					this.valiFormData.ruleId=this.detail.ruleId
				}
				console.log(this.valiFormData)
				if (!validateFormFields(requiredFields, this.valiFormData)) {
					return;
				}
				this.isSubmitting = true; // 设置为正在提交
				uni.showLoading({
					title: "正在提交",
					success: () => {
						reSub(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);
			    }
			  })
			},
			uploadSceneImg() {
				uploadImg('/common/upload', this.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.rwPicture = result.photo
					}
				});
			},
			previewImg(imgs) {
				previewImg(imgs);
			},
			onRemoveSceneImg(index, path) {
				removeImg(index, path, this.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.rwPicture = result.photo
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">
.detail {
		background-color: #F5F5F7;
		padding: 10px;
		padding-bottom: 80px;
		min-height: 98vh;
		.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>