<template>
	<view class="evidence-add">
		<form>
			<view class="form-item">
				<label>担任职务</label>
				<input type="text" v-model="formData.servingPost" placeholder="请输入" />
			</view>
			<view class="form-item">
				<label>担任时间</label>
				<picker @change="onChangeTime" mode="date" :value="formData.servingTime">
					<view class="uni-input">
						<text class="val">{{formData.servingTime}}</text>
						<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
					</view>
				</picker>
			</view>

			<view class="form-item">
				<label>任命文件</label>
				<view class="upload-file" v-if="tempfiles.length==0" @tap="onUploadFile">
					<view class="add">
						+ 添加文件
					</view>
					<view class="tip">
						<view>文件大小:不超过 5MB</view>
						<view>文件格式:doc/xls/ppt/txt/pdf/xlsx </view>
					</view>
					<view class="file-list">
						<view class="item" v-for="(file,index) in tempfiles" :key="index">
							<image src="../../../../../static/word_file.png" mode="widthFix"></image>
							<view class="right">
								<text>{{file.name}}</text>
								<text>{{file.name}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="file-list" v-else>
					<view class="files">
						<view class="item" v-for="(file,index) in tempfiles" :key="index">
							<image src="../../../../../static/word_file.png" mode="widthFix"></image>
							<view class="right">
								<text>{{file.name}}</text>
								<text v-if="file.size">{{file.size}}k</text>
							</view>
							<view class="del" @tap="onDel(index)">
								删除
							</view>
						</view>
					</view>
					<view v-if="tempfiles.length<3" class="btn" @tap="onUploadFile">
						+ 添加文件
					</view>
				</view>
			</view>
			<button v-if="substatus!=1" :disabled="isSubmitting" type="primary" class="submit-btn" @tap="onEdit">保存修改</button>
		</form>
	</view>
</template>

<script>
	import uploadFile from "@/plugins/upload.js";
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		servingDetail,
		servingUpdate
	} from '@/api/compositive-performance/holdAPost.js';
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				activityIndex: 0,
				substatus:"",
				tempfiles: [],
				formData: {
					servingPost: "",
					servingTime: getCurrentDateTime(),
					fdName: uni.getStorageSync("stuName"),
					years: "",
					servingFile: "",
				}
			}
		},
		onLoad(option) {
			this.formData.years = option.year;
			this.id = option.id;
			this.substatus=option.substatus;
			this.getDetails();
		},
		methods: {
			async getDetails() {
				let result = await servingDetail(this.id);
				console.log(result);
				this.formData = result.data;
				if (result.data.servingFile != "") {
					let files = result.data.servingFile.split(",")
					console.log(files);
					files.forEach(file => {
						const fileExtension = file.split('.').pop(); // 提取后缀名
						this.tempfiles.push({
							name: file,
							extension: fileExtension,
						})
					})
					console.log(this.tempfiles);
				}
			},
			onChangeTime(e) {
				this.formData.servingTime = e.detail.value;
			},
			onUploadFile() {
				uni.chooseFile({
					count: 3,
					extension: ['.doc', '.xls', '.xlsx', '.ppt', '.docx', '.txt', '.pdf'],
					success: (file) => {
						console.log(file);
						if (file.tempFiles.length > 0) {
							const fileExtension = file.tempFiles[0].name.split('.').pop(); // 提取后缀名 
							const fileSizeKB = (file.tempFiles[0].size / 1024).toFixed(2);
							this.tempfiles.push({
								name: file.tempFiles[0].name,
								extension: fileExtension,
								size: fileSizeKB
							})
						}

						uploadFile("/common/upload", file.tempFilePaths[0]).then((res) => {
							let result = JSON.parse(res);
							if (result.code == 200) {
								uni.showToast({
									title: "文件上传成功"
								})
								if (this.formData.servingFile !== "") {
									this.formData.servingFile = this.formData.servingFile +
										"," + result.fileName;
								} else {
									this.formData.servingFile = result.fileName;
								}
							}
						})
					}
				})
			},
			onDel(index) {
				uni.showModal({
					title: "确定删除文件吗?",
					success: (res) => {
						if (res.confirm) {
							this.tempfiles.splice(index, 1);
							let newFiles = this.formData.servingFile.split(",");
							newFiles.splice(index, 1)
							this.formData.servingFile = newFiles.join(",")
						}
					}
				})

			},
			async onEdit() {
				const requiredFields = [
					"servingPost",
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写职务`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting=true;
				let res = await servingUpdate(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-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-file {
				border: 1px solid #E1E1E1;
				padding: 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				text-align: center;

				.tip {
					margin-top: 15rpx;
					color: #202020;
					opacity: 0.25;
					font-size: 24rpx;

					view {
						margin-bottom: 8rpx;
					}
				}
			}

			.file-list {
				border: 1px solid #D8D8D8;
				border-radius: 20rpx;

				.files {
					.item {
						display: flex;
						padding: 40rpx 20rpx;
						align-items: center;

						&:not(:last-child) {
							border-bottom: 1px solid #E1E1E1;
						}

						image {
							width: 60rpx;
							margin-right: 20rpx;
						}

						.right {
							flex: 1;
							margin-right: 30rpx;
							display: flex;
							flex-direction: column;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;

							text:last-child {
								color: #969696;
								margin-top: 10rpx;
							}
						}

						.del {
							color: red;
						}
					}

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