<template>
	<view class="evidence-detail">
		<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.stuName" type="text" 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>
			<button :disabled="isSubmitting" v-if="commitStatus!=1" type="primary" class="submit-btn" @tap="onEdit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		OwnClass
	} from '@/api/instructor/superintendent.js';
	import {
		updateInterviewMaterials,
		getInterviewMaterials
	} from '@/api/instructor/interview.js';
	export default {
		data() {
			return {
				isSubmitting:false,
				classArray: [],
				classIndex: 0,
				tempImgs: [],
				commitStatus:"",
				formData: {
					mainContent: "",
					className: "",
					stuName: "",
					developmentTime: getCurrentDateTime(),
					place: "",
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: getCurrentDateTime('MM'),
					fillingYear: getCurrentDateTime('yyyy'),
				}
			}
		},
		onLoad(option) {
			this.formData.id = option.id;
			this.commitStatus = option.commitStatus;
			this.getOwnClass();
		},
		methods: {
			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 getInterviewMaterials(this.formData.id);
				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',
					'stuName',
					'place'
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true;
				let res = await updateInterviewMaterials(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
					})
				}
			},
			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();
				}
			}

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

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