<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" 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.userPhone" focus placeholder="请输入手机号码" />
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题描述</view>
							<view class="textarea">
								<uni-easyinput type="textarea" v-model="formData.problemDesc" 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"></uni-file-picker>
							</view>
						</view>
					</uni-forms>
				</view>

				<view class="form-submit" @click="toAudit()">提交</view>
			</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: '13119098978',
					problemDesc: '邻居每天两三点装修，太不厚道',
					complaintImg: []
				}
			}
		},
		methods: {
			async toAudit() {
				let complaintImg = this.formData.complaintImg.map(item=>item.url).join(',')
				let data = await addCommunityComplaintApi({
					...this.formData,
					complaintImg
				})
				getApp().globalData.complaintInfo = {
					...this.formData,
					complaintImg,
					repairStatus: 'waitAudit'
				}
				setTimeout(()=> {
					uni.showToast({
						title: '提交成功！',
						icon: 'none'
					});
					setTimeout(()=> {
						uni.navigateTo({
							url: '/pages/complaintsDetail/index',
						});
					}, 1500)
				}, 30)
			},
			uploadImage(path) {
				return new Promise((resolve, reject)=> {
					uploadFile({
						filePath: path,
						name: 'file',
						formData: {                                  //其他需要携带的参数
							'biz': 'scott/pic'
						}
					}).then(({message})=> {
						resolve(message)
					}).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)
				}).catch(err=> {
					setTimeout(()=> {
						uni.showToast({
							title: '上传失败，请重试！',
							icon: 'none'
						});
					}, 30)
					console.log(err)
				})
			}
		}
	}
</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: 84rpx;
				line-height: 84rpx;
				border-bottom: 1px solid #F8F6F9;
				overflow: hidden;
			}

			.form-item-fl {
				font-size: 28rpx;
				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: 28rpx;
				color: #666;
				margin-right: 20rpx;
				text-align: right;
			}
			.form-item-text{
				.title{
					height: 84rpx;
					line-height: 84rpx;
					font-size: 28rpx;
					color: #373737;
					margin-left: 10rpx;
				}
				.textarea{
					background-color: #F8F8F8;
				}
			}
		}
	}

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