<template>
	<view class="evidence-detail">
		<form>
			<view class="form-item">
				<label>活动类型</label>
				<picker v-if="activityTypes.length>0" :value="activityIndex" :range="activityTypes"
					@change="activityChangenge">
					<view class="uni-input">
						<text class="val">{{activityTypes[activityIndex]}}</text>
						<uni-icons type="down" size="16" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-item">
				<label>开展时间</label>
				<picker @change="onChangeTime" mode="date" :value="formData.gzsTime">
					<view class="uni-input">
						<text class="val">{{formData.gzsTime}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-item" v-for="(item,index) in inputArray" :key="index">
				<label>{{item.text}}</label>
				<textarea v-if="item.textarea" v-model="formData[item.val]" placeholder="请输入"></textarea>
				<input v-else v-model="formData[item.val]" type="text" placeholder="请输入" />
			</view>
			<view class="form-item">
				<label>现场照片</label>
				<view class="upload-img" v-if="tempSceneImgs.length==0" @tap="uploadSceneImg">
					<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 tempSceneImgs">
							<image @tap="previewImg(tempSceneImgs)" :src="img.path" mode="aspectFill"></image>
							<text class="remove" @tap="onRemoveSceneImg(index,img.path)">X</text>
						</view>
					</view>
					<view v-if="tempSceneImgs.length<3" class="btn" @tap="uploadSceneImg">
						+ 添加照片
					</view>
				</view>
			</view>
			<view class="form-item">
				<label>新闻照片</label>
				<view class="upload-img" v-if="tempNewsImgs.length==0" @tap="uploadNewsImg">
					<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 tempNewsImgs">
							<image @tap="previewImg(tempNewsImgs)" :src="img.path" mode="aspectFill"></image>
							<text class="remove" @tap="onRemoveNewsImg(index,img.path)">X</text>
						</view>
					</view>
					<view v-if="tempNewsImgs.length<3" class="btn" @tap="uploadNewsImg">
						+ 添加照片
					</view>
				</view>
			</view>
			<button v-if="substatus!=1" :disabled="isSubmitting" type="primary" class="submit-btn" @tap="onEdit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import {
		uploadImg,
		previewImg,
		removeImg
	} from "@/utils/uploadImg.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		instructorJobPerformanceDetail,
		instructorJobPerformanceUpdate
	} from '@/api/compositive-performance/instructorJobPerformance.js';
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				activityTypes: ["沙龙", "研讨", "服务", "训练营", "其他"],
				substatus:"",//提交状态
				inputArray: [{
						text: "地点",
						val: "gzsPlace"
					},
					{
						text: "参与领导",
						val: "gzsParticipate"
					},
					{
						text: "参与辅导员",
						val: "gzsFdy"
					},
					{
						text: "参与学生人数",
						val: "gzsStu"
					},
					{
						text: "活动主题",
						val: "gzsTheme"
					},
					{
						text: "活动开展情况介绍（200字内）",
						val: "gzsIntroduce",
						textarea:true
					},
					{
						text: "分配比例",
						val: "gzsProportion",
						textarea:true
					}
				],
				activityIndex: 0,
				tempSceneImgs: [],
				tempNewsImgs: [],
				id: "", //佐证详情id
				details: null,
				formData: {
					activeType: "",
					gzsTime: getCurrentDateTime(),
					gzsPlace: "",
					gzsParticipate:"",
					gzsFdy:"",
					gzsStu:"",
					gzsTheme:"",
					gzsIntroduce:"",
					gzsProportion:"",
					gzsNews:"",
					gzsPicture:"",
					fdName: uni.getStorageSync("stuName"),
					years: "",
				}
			}
		},
		onLoad(option) {
			this.formData.years = option.year;
			this.formData.activeType = this.activityTypes[0];
			this.id = option.id;
			this.substatus=option.substatus;
			this.getDetails();
		},
		methods: {
			//上传新闻照片
			uploadNewsImg() {
				uploadImg('/common/upload', this.formData.gzsNews, this.tempNewsImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.gzsNews=result.photo;
					}
				});
			
			},
			//移除新闻照片
			onRemoveNewsImg(index, path) {
				removeImg(index, path, this.formData.gzsNews, this.tempNewsImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.gzsNews=result.photo;
					}
				});
			},
			//上传现场照片
			uploadSceneImg() {
				uploadImg('/common/upload', this.formData.gzsPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.gzsPicture = result.photo;
					}
				});
			},
			//移除现场照片
			onRemoveSceneImg(index, path) {
				removeImg(index, path, this.formData.gzsPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.gzsPicture = result.photo;
					}
				});
			},
			activityChangenge(e) {
				this.activityIndex = e.detail.value;
				this.formData.activeType = this.activityTypes[e.detail.value];
			},
			onChangeTime(e) {
				this.formData.gzsTime = e.detail.value;
			},
			async getDetails() {
				let result = await instructorJobPerformanceDetail(this.id);
				if (result.code == 200) {
					console.log(result);
					this.formData = result.data;
					this.activityTypes.forEach((item, index) => {
						if (item == this.formData.activeType) {
							this.activityIndex = index;
						}
					})
					if (this.formData.gzsPicture != null) {
						let imgs = this.formData.gzsPicture.split(',');
						this.tempSceneImgs = imgs.map(img => {
							return {
								path: baseUrl + img
							};
						})
					}
					if (this.formData.gzsNews != null) {
						let imgs = this.formData.gzsNews.split(',');
						this.tempNewsImgs = imgs.map(img => {
							return {
								path: baseUrl + img
							};
						})
					}
				}

			},
			async onEdit() {
				console.log(this.formData);
				const requiredFields = [
					'gzsPlace',
					'gzsParticipate',
					'gzsFdy',
					'gzsStu',
					'gzsTheme',
					'gzsIntroduce',
					'gzsProportion',
					'gzsPicture'
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting = true; // 设置为正在提交
				let res = await instructorJobPerformanceUpdate(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
										.getMaterialsList ===
										'function') {
										prevPage.queryParams.pageNum = 1;
										prevPage.getMaterialsList();
									}
								}
							}
						})
					}, 1000)
				} else {
					uni.showToast({
						title: res.msg
					})
				}
			},
			previewImg(imgs) {
				previewImg(imgs);
			}
		}
	}
</script>

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

		.form-item {
			padding: 22rpx 40rpx;
			background-color: white;
			margin-bottom: 20rpx;
			border-radius: 16rpx;
			textarea{
				border: 1px solid #E1E1E1;
				border-radius: 10rpx;
				width: 100%;
				padding:20rpx;
				line-height:40rpx;
			}
			.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>