<template>
	<view class="container">
		<view class="page">
			<view class="complaints">
				<view class="form">
					<uni-forms>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>投诉主题
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.complaintTheme" focus placeholder="请输入投诉主题" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>投诉人姓名
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.userName" placeholder="请输入投诉人姓名" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>手机号码
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.userPhone" placeholder="请输入手机号码" />
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题描述</view>
							<view class="textarea-box">
								<textarea class="textarea" placeholder-style="font-size:14px" :maxlength="200" v-model="formData.problemDesc" auto-height placeholder="请输入问题描述" />
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题图片(最多传三张)</view>
							<view class="picker">
								<uni-file-picker file-mediatype="image" limit="3" @select="onSelectImage" @delete="onDeleteImage"></uni-file-picker>
							</view>
						</view>
					</uni-forms>
				</view>

				<button class="form-submit" :loading="isSubmit" :disabled="isSubmit" @click="toAudit()">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		uploadFile,
		addCommunityComplaintApi
	} from '@/config/api.js'
	import serverConfig from "@/config/server_config.js";
	export default {
		data() {
			return {
				formData: {
					complaintTheme: '',
					userName: '',
					userPhone: '',
					problemDesc: ''
				},
				filePathsList: [],
				isSubmit: false
			}
		},
		methods: {
			async toAudit() {
				if(!this.formData.complaintTheme) {
					uni.showToast({
						title: '请输入投诉主题',
						icon: 'none'
					})
					return
				}
				if(!this.formData.userName) {
					uni.showToast({
						title: '请输入投诉人姓名',
						icon: 'none'
					})
					return
				}
				if(!this.formData.userPhone) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					})
					return
				}
				let reg = /^1(3|4|5|7|8)\d{9}$/
				if(!reg.test(this.formData.userPhone)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						icon: 'none'
					})
					return
				}
				this.isSubmit = true
				let complaintImg = this.filePathsList.map(item=>item.url).join(',')
				let data = await addCommunityComplaintApi({
					...this.formData,
					complaintImg
				})
				uni.$emit('update-list');
				getApp().globalData.complaintInfo = {
					...this.formData,
					complaintImg,
					repairStatus: 'waitAudit'
				}
				setTimeout(()=> {
					uni.showToast({
						title: '提交成功！',
						icon: 'none'
					});
					setTimeout(()=> {
						this.isSubmit = false
						uni.redirectTo({
							url: '/pages/complaintsDetail/index',
						});
					}, 1500)
				}, 30)
			},
			uploadImage(path) {
				return new Promise((resolve, reject)=> {
					uploadFile({
						filePath: path,
						name: 'file',
						formData: {                                  //其他需要携带的参数
							'biz': 'scott/pic'
						}
					}).then(({message})=> {
						let res = {
							url: message,
							tempFilePath: path
						}
						resolve(res)
					}).catch(err=> {
						reject(err)
					})
				})
			},
			onSelectImage(e) {
				let serveList = []
				e.tempFilePaths.forEach(url=> {
					serveList.push(this.uploadImage(url))
				})
				Promise.all(serveList).then(data=> {
					// let list = data.map(url=> {
					// 	let chunk = url.split('.')
					// 	return {
					// 		url: serverConfig.baseURL + '/sys/common/static/' + url,
					// 		name: chunk[0],
					// 		extname: chunk[1]
					// 	}
					// })
					// this.formData.complaintImg = this.formData.complaintImg.concat(list)
					this.filePathsList = this.filePathsList.concat(data)
				}).catch(err=> {
					setTimeout(()=> {
						uni.showToast({
							title: '上传失败，请重试！',
							icon: 'none'
						});
					}, 30)
					console.log(err)
				})
			},
			onDeleteImage(err) {
				const num = this.filePathsList.findIndex(v => v.tempFilePath === err.tempFilePath);
				this.filePathsList.splice(num, 1);
			}
		},
		onLoad() {
			this.formData.userName = uni.getStorageSync('user_name')
			this.formData.userPhone = uni.getStorageSync('user_phone')
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.complaints {

		.form {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 100rpx;
				line-height: 100rpx;
				border-bottom: 1px solid #F8F6F9;
				overflow: hidden;
			}

			.form-item-fl {
				font-size: 30rpx;
				color: #373737;
				display: flex;

				.asterisk {
					color: #F42E2E;
					width: 10rpx;
					margin-right: 6rpx;
				}

			}

			.form-item-fr {
				display: flex;
				flex: 2;
				align-items: center;
				justify-content: flex-end;
				margin-left: 20rpx;
				text-align: right;
			}

			.form-item-tit {
				font-size: 30rpx;
				color: #666;
				margin-right: 20rpx;
				text-align: right;
			}
			.form-item-text{
				.title{
					height: 100rpx;
					line-height: 100rpx;
					font-size: 30rpx;
					color: #373737;
					margin-left: 10rpx;
				}
				.textarea-box {
					background-color: #F8F8F8;
					border-radius: 5px;
					.textarea {
						font-size: 30rpx;
						width: 100%;
						min-height: 200rpx;
						padding: 5px 10px;
						box-sizing: border-box;
					}
				}
			}
		}
	}

	.form-submit {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 30rpx;
		color: #FFFFFF;
	}
</style>