<template>
	<view class="evidence-add">
		<form>
			<view class="form-item">
				<label>会议内容</label>
				<input type="text" v-model="formData.mainContent" placeholder="请输入"
					placeholder-class="input-placeholder" />
			</view>
			<view class="form-item">
				<label>班级</label>
				<picker v-if="classArray.length>0" range-key="class_name" :value="classIndex" :range="classArray"
					@change="classChange">
					<view class="uni-input">
						<text class="val">{{classArray[classIndex].class_name}}</text>
						<uni-icons type="down" size="16" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-item">
				<label>参与人数</label>
				<input v-model="formData.numberOfStudents" type="number" placeholder="请输入" />
			</view>
			<view class="form-item">
				<label>开展时间</label>
				<picker @change="onChangeTime" mode="date" :value="formData.developmentTime">
					<view class="uni-input">
						<text class="val">{{formData.developmentTime}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-item">
				<label>地点</label>
				<input v-model="formData.place" type="text" placeholder="请输入" />
			</view>
			<view class="form-item">
				<label>照片</label>
				<view class="upload-img" v-if="tempImgs.length==0" @tap="uploadImg">
					<view class="add">
						+ 添加照片
					</view>
					<view class="tip">
						图片要求：最多3张，上限5MB
					</view>
					<view class="img-list">

					</view>
				</view>
				<view class="img-list" v-else>
					<view class="imgs">
						<view class="img" v-for="(img,index) in tempImgs">
							<image :src="img.path" @tap="previewImg(tempImgs)" mode="aspectFill"></image>
							<text class="remove" @tap="onRemoveImg(index,img.path)">X</text>
						</view>
					</view>
					<view v-if="tempImgs.length<3" class="btn" @tap="uploadImg">
						+ 添加照片
					</view>
				</view>
			</view>
			<button :disabled="isSubmitting" v-if="commitStatus!=1" type="primary" class="submit-btn"
				@tap="onEdit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import {
		previewImg
	} from "@/utils/uploadImg.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		checkPic
	} from "@/utils/checkPic.js"
	import {
		OwnClass
	} from '@/api/instructor/superintendent.js';
	import {
		updateStuActivityMaterials,
		getStuActivityMaterials
	} from '@/api/instructor/studentActivity.js';
	import uploadFile from "@/plugins/upload.js";
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				isSubmitting: false,
				classArray: [],
				classIndex: 0,
				tempImgs: [],
				commitStatus: "",
				formData: {
					mainContent: "",
					className: "",
					numberOfStudents: "",
					developmentTime: getCurrentDateTime(),
					place: "",
					photo: "",
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: getCurrentDateTime('MM'),
					fillingYear: getCurrentDateTime('yyyy'),
				}
			}
		},
		onLoad(option) {
			this.formData.id = option.id;
			this.commitStatus = option.commitStatus;
			this.getOwnClass();
		},
		methods: {
			uploadImg() {
				uni.chooseImage({
					count: 3,
					success: async (img) => {
						let bool = await checkPic(img.tempFiles[0]);
						if (bool) {
							uploadFile('/common/upload', img.tempFilePaths[0]).then((res) => {
								if (this.formData.photo !== "") {
									this.formData.photo = this.formData.photo + "," + JSON.parse(res)
										.fileName;
								} else {
									this.formData.photo = JSON.parse(res).fileName;
								}
								this.tempImgs.push({
									path: baseUrl + JSON.parse(res).fileName
								});
							})
						}

					}
				})
			},
			onRemoveImg(index, path) {
				this.tempImgs.splice(index, 1);
				let newImgs = this.tempImgs.filter(fileName => fileName.path !== path);
				newImgs = newImgs.map(img => img.path.replace(baseUrl, ''))
				newImgs = newImgs.join(",");
				this.formData.photo = newImgs;
			},
			classChange(e) {
				this.classIndex = e.detail.value;
				this.formData.className = this.classArray[e.detail.value].class_name;
			},
			onChangeTime(e) {
				this.formData.developmentTime = e.detail.value;
			},
			async getDetail() {
				let res = await getStuActivityMaterials(this.formData.id);
				if (res.data.photo != null) {
					let imgs = res.data.photo.split(',');
					this.tempImgs = imgs.map(img => {
						return {
							path: baseUrl + img
						};
					})
				}
				this.formData = res.data;
				//处理班级
				let index = this.classArray.findIndex(item => {
					return item.class_name === this.formData.className;
				});
				this.classIndex = index;

			},
			async onEdit() {
				const requiredFields = [
					'mainContent',
					'className',
					'numberOfStudents',
					'place'
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting = true;
				let res = await updateStuActivityMaterials(this.formData);
				if (res.code == 200) {
					uni.showToast({
						title: "修改成功"
					})
					setTimeout(() => {
						this.isSubmitting = false;
						uni.navigateBack({
							success: () => {
								const pages = getCurrentPages();
								if (pages.length > 0) {
									const prevPage = pages[pages.length - 2]
									if (prevPage && typeof prevPage.getStuActivityMaterials ===
										'function') {
										prevPage.queryParams.pageNum = 1;
										prevPage.getStuActivityMaterials();
									}
								}
							}
						})
					}, 1000)
				} else {
					uni.showToast({
						title: res.msg
					})
				}
			},
			async getOwnClass() {
				let res = await OwnClass(this.formData);
				this.classArray = res.data;
				this.formData.className = this.classArray[0].class_name;
				if (res.code == 200) {
					this.getDetail();
				}
			},
			previewImg(imgs) {
				previewImg(imgs)
			}
		}
	}
</script>

<style scoped lang="scss">
	.evidence-add {
		min-height: 95vh;
		background-color: #F5F5F7;
		padding: 40rpx;

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

			.uni-icons {
				opacity: .5;
				font-weight: bold
			}

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

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

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

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

				.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;
				}
			}
		}

		.submit-btn {
			margin-top: 60rpx;
		}
	}
</style>