<template>
	<view class="evidence-add">
		<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.rwTime">
					<view class="uni-input">
						<text class="val">{{formData.rwTime}}</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 type="primary" :disabled="isSubmitting" class="submit-btn" @tap="onSubmit">提交</button>
		</form>
	</view>
</template>

<script>
	import {
		uploadImg,
		previewImg,
		removeImg
	} from "@/utils/uploadImg.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		undertakeStudentWorkAssignmentsAdd,
	} from '@/api/compositive-performance/undertakeStudentWorkAssignments.js';
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				activityTypes: ["校级", "区级"],
				inputArray: [{
						text: "地点",
						val: "rwPlace"
					},
					{
						text: "参与领导",
						val: "rwParticipate"
					},
					{
						text: "工作人员",
						val: "rwPeople"
					},
					{
						text: "参与学生数",
						val: "rwStu"
					},
					{
						text: "活动主题",
						val: "rwTheme"
					},
					{
						text: "具体工作内容及完成情况 （200字内）",
						val: "rwSituation",
						textarea:true
					},
					{
						text: "分配比例",
						val: "rwProportion",
						textarea:true
					}
				],
				activityIndex: 0,
				tempSceneImgs: [],
				tempNewsImgs: [],
				formData: {
					rwType: "",
					rwTime: getCurrentDateTime(),
					rwPlace: "",
					rwParticipate: "",
					rwPeople: "",
					rwStu: "",
					rwTheme: "",
					rwSituation: "",
					rwProportion: "",
					rwPicture: "",
					rwNews: '',
					fdName: uni.getStorageSync("stuName"),
					years: "",

				}
			}
		},
		onLoad(option) {
			this.formData.years = option.year;
			this.formData.rwType = this.activityTypes[0];
		},
		methods: {
			//上传新闻照片
			uploadNewsImg() {
				uploadImg('/common/upload', this.formData.rwNews, this.tempNewsImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.rwNews=result.photo;
					}
				});

			},
			//移除新闻照片
			onRemoveNewsImg(index, path) {
				removeImg(index, path, this.formData.rwNews, this.tempNewsImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.rwNews=result.photo;
					}
				});
			},
			//上传现场照片
			uploadSceneImg() {
				uploadImg('/common/upload', this.formData.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.rwPicture = result.photo;
					}
				});
			},
			//移除现场照片
			onRemoveSceneImg(index, path) {
				removeImg(index, path, this.formData.rwPicture, this.tempSceneImgs, baseUrl, (error, result) => {
					if (!error) {
						this.formData.rwPicture = result.photo;
					}
				});
			},
			activityChangenge(e) {
				this.activityIndex = e.detail.value;
				this.formData.rwType = this.activityTypes[e.detail.value];
			},
			onChangeTime(e) {
				this.formData.rwTime = e.detail.value;
			},
			async onSubmit() {
				const requiredFields = [
					'rwPlace',
					'rwParticipate',
					'rwPeople',
					'rwStu',
					'rwTheme',
					'rwSituation',
					'rwProportion',
					'rwPicture',
					'rwNews'
				];
				console.log(this.formData);
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true; //表单提交标志位
				let res = await undertakeStudentWorkAssignmentsAdd(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-add {
		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>