<template>
	<view class="evidence-detail">
		<form>
			<view class="form-item">
				<label>主题</label>
				<input type="text" v-model="formData.subject" placeholder="请输入" placeholder-class="input-placeholder" />
			</view>
			<view class="form-item">
				<label>组织单位</label>
				<input type="text" v-model="formData.organizationalUnit" placeholder="请输入" placeholder-class="input-placeholder" />
			</view>
			<view class="form-item">
				<label>开展时间</label>
				<picker @change="onChangeTime" mode="date" :value="formData.startTime">
					<view class="uni-input">
						<text class="val">{{formData.startTime}}</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 {
		addConferenceMeetingMaterials,
		getConferenceMeetingMaterials
	} from '@/api/instructor/instructorAttendanceMaterial.js';
	export default {
		data() {
			return {
				isSubmitting:false,
				classIndex: 0,
				tempImgs: [],
				commitStatus:"",
				formData: {
					subject: "",
					organizationalUnit: "",
					startTime: getCurrentDateTime(),
					place: "",
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: "",
					fillingYear: "",
				}
			}
		},
		onLoad(option) {
			this.formData.id = option.id;
			this.commitStatus= option.commitStatus;
			this.getDetail();
		},
		methods: {
			onChangeTime(e) {
				this.formData.startTime = e.detail.value;
			},
			async getDetail() {
				let res = await getConferenceMeetingMaterials(this.formData.id);
				console.log(this.formData);
				this.formData = res.data;
			},
			async onEdit() {
				console.log(this.formData);
				const requiredFields = [
					'mainContent',
					'organizationalUnit',
					'stuName',
					'place'
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true;
				let res = await addConferenceMeetingMaterials(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
					})
				}
			},
		}
	}
</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>