<template>
	<view class="add-complaint">
		<view class="form-item">
			<label><text>*</text> 联系人</label>
			<input type="text" placeholder="请输入" v-model="formData.contact" />
		</view>
		<view class="form-item">
			<label><text>*</text> 联系电话</label>
			<input type="number" maxlength="11" placeholder="请输入" v-model="formData.contactNumber" />
		</view>
		<view class="form-item">
			<label><text>*</text> 关键词</label>
			<input type="text" placeholder="请输入" v-model="formData.keyWords" />
		</view>
		<view class="form-item">
			<label>问题描述</label>
			<textarea type="text" placeholder="请输入" v-model="formData.desc" />
		</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 @tap="previewImg(tempImgs)" :src="img.path" 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 class="btns">
			<button @click="onSubmit" :disabled="isSubmitting">提交</button>
		</view>
	</view>
</template>

<script>
	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 {
		complaintAdd
	} from '@/api/OneStopCommunity/complaint.js'
	export default {
		data() {
			return {
				isSubmitting: false,
				tempImgs: [],
				formData: {
					contact: "",
					contactNumber: "",
					keyWords: "",
					photos: "",
				}
			}
		},
		methods: {
			onSubmit() {
				var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; 
				if(!reg_tel.test(this.formData.contactNumber)){
					uni.showToast({
						title: `请正确填写您的手机号码`,
						icon: "none"
					})
					return;
				}
				const requiredFields = [
					'contact',
					'contactNumber',
					'keyWords',
					//'photos',
				];
				const emptyField = requiredFields.find(field => this.formData[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完 * 必填内容`,
						icon: "none"
					})
					return;
				}
				this.isSubmitting = true; // 设置为正在提交
				console.log(this.formData);
				uni.showLoading({
					title: "正在提交",
					success: async () => {
						let res = await complaintAdd(this.formData);
						if (res.code == 200) {
							uni.showToast({
								title: "提交成功"
							})
							this.isSubmitting = false;
							uni.navigateBack({
								success: () => {
									const pages = getCurrentPages();
									if (pages.length > 0) {
										const prevPage = pages[pages.length - 2]
										if (prevPage && typeof prevPage
											.getlist ===
											'function') {
											prevPage.queryParams.pageNum = 1;
											prevPage.getlist();
										}
									}
								}
							})
						}
					}
				})

			},
			uploadImg() {
				uni.chooseImage({
					count: 3,
					success: async (img) => {
						let bool = await checkPic(img.tempFiles[0]);
						if (bool) {
							uploadFile('/common/upload', img.tempFilePaths[0]).then((res) => {
								if (this.formData.photos !== "") {
									this.formData.photos = this.formData.photos + "," + JSON.parse(res)
										.fileName;
								} else {
									this.formData.photos = 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.photos = newImgs;
			},
			previewImg(imgs) {
				previewImg(imgs);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add-complaint {
		background-color: #F5F5F7;
		padding: 10px;
		padding-bottom: 80px;

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

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

		.btns {
			padding: 10px;
			display: flex;
			background: white;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			z-index: 999;

			button {
				flex: 1;
				background-color: #1890FF;
				color: white;
			}

		}
	}
</style>