<template>
	<view>
		<view class="Userinformation">
			<view class="list">
				<view class="sm-list">
					<text>宿舍</text>
					<text>{{dorm}}</text>
				</view>

			</view>
			<view class="form-item">
				<label><text>*</text>打卡图片</label>
				<view class="upload-img" v-if="tempImgs.length==0" @tap="uploadImg">
					<view class="add">
						+ 添加照片
					</view>
					<view class="tip">
						图片要求：最多3张，上限5MB
					</view>
					<view class="img-list">
				
					</view>
				</view>
				<view class="img-list" v-else>
					<view class="imgs">
						<view class="img" v-for="(img,index) in tempImgs">
							<image :src="img.path" @tap="previewImg(tempImgs)" mode="aspectFill"></image>
							<text class="remove" @tap="onRemoveImg(index,img.path)">X</text>
						</view>
					</view>
					<view v-if="tempImgs.length<3" class="btn" @tap="uploadImg">
						+ 添加照片
					</view>
				</view>
			</view>
		</view>
		<button type="primary" @click="doCard">打卡</button>
	</view>
</template>

<script>
	import {
		getOwnDorm,
		doCard
	} from "@/api/dms/daily.js";
	import {
		uploadImg,
		previewImg,
		removeImg
	} from "@/utils/uploadImg.js";
	import uploadFile from "@/plugins/upload.js";
	import {
		checkPic
	} from "@/utils/checkPic.js";
	import {
		baseUrl
	} from "@/config.js";
	import {
		isEmpty
	} from "@/api/helpFunc/index.js";
	export default {
		data() {
			return {
				dorm: "",
				formData: {
					photo: ""
				},
				tempImgs: [],
			}
		},
		onShow() {
			this.getOwnDorm();
		},
		methods: {
			async doCard() {
				let sdata = {
					...this.formData
				};
				if (isEmpty(sdata.photo)) {
					uni.showToast({
						mask: true,
						title: "请上传打卡图片",
						icon: "none"
					});
					return;
				}
				uni.showLoading({
					mask: true,
					title: '加载中',
				});
				let res = await doCard(sdata);
				uni.hideLoading();
				if (res.code == 200) {
					uni.showToast({
						mask: true,
						icon: "success",
						title: res.msg
					});
				}
			},
			async getOwnDorm() {
				uni.showLoading({
					title: '加载中',
					mask: true,
				});
				let res = await getOwnDorm();
				uni.hideLoading();
				if (res.code == 200) {
					let data = {
						...res.data
					};
					this.dorm = data.park_name + data.building_name + data.room_no;
				}
			},
			uploadImg() {
				uni.chooseImage({
					count: 1,
					sourceType: ['camera'],
					success: async (img) => {
						let bool = await checkPic(img.tempFiles[0]);
						if (bool) {
							uploadFile('/common/upload', img.tempFilePaths[0]).then((res) => {
								if (this.formData.photo !== "") {
									this.formData.photo = this.formData.photo + "," +
										JSON.parse(res)
										.fileName;
								} else {
									this.formData.photo = JSON.parse(res).fileName;
								}
								this.tempImgs.push({
									path: baseUrl + JSON.parse(res).fileName
								});
							})
						}

					}
				})
			},
			onRemoveImg(index, path) {
				this.tempImgs.splice(index, 1);
				let newImgs = this.tempImgs.filter(fileName => fileName.path !== path);
				newImgs = newImgs.map(img => img.path.replace(baseUrl, ''))
				newImgs = newImgs.join(",");
				this.formData.photo = newImgs;
			},
			previewImg(imgs) {
				previewImg(imgs);
			}
		},
	}
</script>

<style lang="less" scoped>
	.Userinformation {
		margin-bottom: 10rpx;

		.list {

			display: flex;
			align-items: center;
			flex-direction: column;
			width: 100%;



			.sm-list {
				display: flex;
				justify-content: space-between;
				align-items: center;

				width: 722rpx;
				height: 100rpx;
				border-bottom: 1px solid #EFEFEF;

				text:nth-child(1) {

					color: #0F0F0F;
				}

				text:nth-child(2) {

					color: #575757;
				}

				.password {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					width: 200rpx;
				}
			}
		}


	}

	.form-item {
		display: flex;
		flex-direction: column;
		padding: 22rpx 40rpx;
		background-color: white;
		margin-bottom: 20rpx;
		border-radius: 16rpx;

		&.tip {
			justify-content: center;

			text:first-child {
				color: red;
			}

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

		.tip {
			color: red;
			font-size: 24rpx;
		}

		label {
			margin-bottom: 20rpx;
			display: inline-block;

			text {
				color: red;
			}
		}

		textarea {
			height: 200rpx;
			border: 1px solid #E1E1E1;
			padding: 20rpx;
			width: 100%;
		}

		input {
			border: 1px solid #E1E1E1;
			border-radius: 10rpx;
			height: 70rpx;
			padding-left: 30rpx;
		}

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