<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 form-item--class">
				<label>班级</label>
				<picker v-if="classArray.length>0" range-key="class_name" :value="classIndex" :range="classArray"
					@change="multiClassChange">
					<view class="uni-input">
						<view class="val-container">
							<!-- 单选时显示文本 -->
							<text class="val" v-if="selectedIndices.length <= 1 && classIndex > 0">{{ classArray[classIndex].class_name }}</text>
							<!-- 多选时显示 textarea -->
							<textarea 
							v-else 
							:value="selectedClassNames" 
							disabled 
							style="background-color: #f9f9f9; border: none; font-size: 30rpx;" 
							auto-height />
						</view>
						<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 type="primary" :disabled="isSubmitting" class="submit-btn" @tap="onSubmit">提交</button>
		</form>
	</view>
</template>

<script>
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		OwnClass
	} from '@/api/instructor/superintendent.js';
	import {
		addInterviewMaterials
	} from '@/api/instructor/interview.js';
	export default {
		data() {
			return {
				isSubmitting:false,
				classArray: [], // 班级列表
				selectedIndices: [], // 多选索引数组
				classIndex: 0,
				tempImgs: [],
				formData: {
					mainContent: "",
					className: "",
					stuName: "",
					developmentTime: getCurrentDateTime(),
					place: "",
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: "",
					fillingYear: "",
				}
			}
		},
		onLoad(option) {
			this.formData.fillingYear = option.year;
			this.formData.fillingMonth = option.month;
			this.getOwnClass();
		},
		computed: {
			//处理班级多选问题
			selectedClassNames() {
			    if (this.selectedIndices.length === 0) {
			        return '请选择班级'; // 默认提示
			    }
			
			    // 根据索引获取对应的班级名称并拼接
			    return this.selectedIndices.map(index => this.classArray[index].class_name).join('\n');
				//return this.selectedIndices.map(index => this.classArray[index].class_name).join(', ');
			}
		},
		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 onSubmit() {
				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 addInterviewMaterials(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;
			},
			//用于处理班级多选问题的方法
			multiClassChange(e) {
			    const index = e.detail.value; // picker 返回的是单个值
				this.classIndex = e.detail.value;
				this.formData.className = this.classArray[e.detail.value].class_name;
				
			        if (!this.selectedIndices.includes(index)) {
			            this.selectedIndices.push(index);
			        } else {
			            // 如果已经选过，可以取消选择
			            this.selectedIndices = this.selectedIndices.filter(i => i !== index);
			        }
			    
			        const selectedClasses = this.selectedIndices.map(i => this.classArray[i].class_name);
			        this.formData.className = selectedClasses.join(',');
			},
			
		}
	}
</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;
				}
			}
		}
		//班级下拉框样式
		.form-item--class {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		
			picker {
				height: auto;
				line-height: 50rpx;
		
				.uni-input {
					width: 100%;
					min-height: 65rpx;
					height: auto;
					padding: 16rpx 15rpx;
					box-sizing: border-box;
					align-items: flex-start;
		
					.val-container {
						flex: 1;
						overflow: hidden;
						white-space: pre-wrap;
						word-break: break-all;
		
						textarea {
							width: 100%;
							padding: 0;
							margin: 0;
							background-color: transparent;
							font-size: 25rpx;
							line-height: 40rpx;
							height: auto;
							white-space: pre-wrap;
							word-break: break-all;
						}
		
					}
		
		
				}
		
			}
		
		}

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