<template>
	<view class="evidence-add">
		<form>
			<view class="form-item">
				<label>事件等级</label>
				<picker v-if="levelArray.length>0" range-key="class_name" :value="levelIndex" :range="levelArray"
					@change="levelChange">
					<view class="uni-input">
						<text class="val">{{levelArray[levelIndex]}}</text>
						<uni-icons type="down" size="16" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-item">
				<label>事件描述</label>
				<input type="text" v-model="formData.eventDescription" placeholder="请输入"
					placeholder-class="input-placeholder" />
			</view>
			<view class="form-item">
				<label>起止时间</label>
				<picker mode="date" class="startTime" @change="onChangeStartTime">
					<view class="uni-input">
						<text class="val">{{formData.startTime}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
				<picker mode="date" :start="formData.startTime" class="endTime" @change="onChangeEndTime">
					<view class="uni-input">
						<text class="val">{{formData.endTime}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>
			<button v-if="commitStatus!=1" :disabled="isSubmitting" type="primary" class="submit-btn" @tap="onSubmit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import {
		convertToTimestamp
	} from "@/utils/time-calculation.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		kpiFillingStuEmergencyDetail,
		kpiFillingStuEmergencyUpdate
	} from '@/api/instructor/studentEmergencies.js';
	export default {
		data() {
			return {
				isSubmitting:false,
				levelArray: ["一般事件", "重大事件"],
				levelIndex: 0,
				commitStatus:"",
				formData: {
					eventLevel: 0,
					eventDescription: "",
					startTime: getCurrentDateTime(),
					endTime: getCurrentDateTime(),
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: "",
					fillingYear: "",
				}
			}
		},
		onLoad(option) {
			this.commitStatus=option.commitStatus;
			this.formData.id = option.id;
			this.formData.fillingYear = option.year;
			this.formData.fillingMonth = option.month;
			this.getDetail();
		},
		methods: {
			async getDetail() {
				let res = await kpiFillingStuEmergencyDetail(this.formData.id);
				this.formData = res.data;
				//处理班级
				let index = this.levelArray.findIndex((item, i) => {
					return i === this.formData.eventLevel;
				});
				this.levelIndex = index;
			},
			levelChange(e) {
				this.levelIndex = e.detail.value;
				this.formData.eventLevel = this.levelIndex;
			},
			onChangeStartTime(e) {
				this.formData.startTime = e.detail.value;
			},
			onChangeEndTime(e) {
				this.formData.endTime = e.detail.value;
			},
			async onSubmit() {
				const requiredFields = [
					"eventDescription"
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写事件描述`,
						icon: "none"
					})
					return;
				}
				let start = convertToTimestamp(this.formData.startTime);
				let end = convertToTimestamp(this.formData.endTime);
				if (end < start) {
					uni.showToast({
						title: "开始时间不能晚于结束时间",
						icon: "none"
					});
					return;
				}
				this.isSubmitting=true;
				let res = await kpiFillingStuEmergencyUpdate(this.formData);
				if (res.code == 200) {
					uni.showToast({
						title: "修改成功"
					})
					setTimeout(() => {
						this.isSubmitting=false;
						uni.navigateBack({
							success: () => {
								uni.$emit('refreshStuEmergencyList');
							}
						})
					}, 1000)
				}
			},
		}
	}
</script>

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

		.form-item {
			padding: 22rpx 40rpx 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;

				&.endTime {
					margin-top: 40rpx;
				}

				.uni-input {
					display: flex;
					color: #797979;

					.val {
						flex: 1;
					}
				}
			}
		}

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