<template>
	<view class="container">
		<view class="page">
			<view class="maintenance">
				<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.repairTitle" 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.repairAddress" 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.repairArticle" 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.name1" 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.ownerPhone" placeholder="请输入手机号码" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>预约上门时间
							</view>
							<view class="form-item-fr">
								<uni-datetime-picker type="datetime" v-model="formData.reservationTime" return-type="timestamp" :hide-second="true" :clear-icon="false" :icon="false" :border="false" />
								<uni-icons type="forward" color="#E4E4EA"></uni-icons>
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题描述</view>
							<view class="textarea">
								<uni-easyinput type="textarea" v-model="formData.repairDesc" placeholder="请输入问题描述" />
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题图片(最多传三张)</view>
							<view class="picker">
								<uni-file-picker v-model="formData.repairPhoto" file-mediatype="image" limit="3" @select="onSelectImage"></uni-file-picker>
							</view>
						</view>
					</uni-forms>
				</view>

				<view class="form-submit" @click="toAudit()">{{isPageEdit ? '确认' : '下一步'}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		uploadFile,
		addCommunityRepairApi,
		editCommunityRepairApi
	} from '@/config/api.js'
	import {Moment} from '@/utils/moment.js'
	import serverConfig from "@/config/server_config.js";
	export default {
		data() {
			return {
				isPageEdit: false,
				formData: {
					repairTitle: '上报小区垃圾桶太少',
					repairAddress: '10栋一单元101',
					repairArticle: '垃圾桶',
					reservationTime: '',
					ownerPhone: '13109890986',
					repairDesc: '啥都好说SDK房价是SDK积分是',
					repairPhoto: []
				},
			}
		},
		methods: {
			async toAudit() {
				let repairPhoto = this.formData.repairPhoto.map(item=>item.url).join(',')
				if(this.isPageEdit) {	// 编辑
					let data = await editCommunityRepairApi({
						...this.formData,
						repairPhoto
					})
					// getApp().globalData.maintenanceInfo = {
					// 	...this.formData,
					// 	repairPhoto,
					// 	repairStatus: 'waitAudit',
					// 	reservationTime: new Moment(this.formData.reservationTime, 'YYYY-MM-DD HH-mm')
					// }
					// setTimeout(()=> {
					// 	uni.showToast({
					// 		title: '编辑成功！',
					// 		icon: 'none'
					// 	});
					// 	setTimeout(()=> {
					// 		uni.navigateTo({
					// 			url: '/pages/maintenanceAaudit/index',
					// 		});
					// 	}, 1500)
					// }, 30)
				} else {
					let data = await addCommunityRepairApi({
						...this.formData,
						repairPhoto
					})
					// getApp().globalData.maintenanceInfo = {
					// 	...this.formData,
					// 	repairPhoto,
					// 	repairStatus: 'waitAudit',
					// 	reservationTime: new Moment(this.formData.reservationTime, 'YYYY-MM-DD HH-mm')
					// }
					// setTimeout(()=> {
					// 	uni.showToast({
					// 		title: '提交成功！',
					// 		icon: 'none'
					// 	});
					// 	setTimeout(()=> {
					// 		uni.navigateTo({
					// 			url: '/pages/maintenanceAaudit/index',
					// 		});
					// 	}, 1500)
					// }, 30)
				}
				getApp().globalData.maintenanceInfo = {
					...this.formData,
					repairPhoto,
					repairStatus: 'waitAudit',
					reservationTime: new Moment(this.formData.reservationTime, 'YYYY-MM-DD HH-mm')
				}
				setTimeout(()=> {
					uni.showToast({
						title: `${this.isPageEdit ? '编辑' : '提交'}成功！`,
						icon: 'none'
					});
					setTimeout(()=> {
						uni.navigateTo({
							url: '/pages/maintenanceAaudit/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.repairPhoto = this.formData.repairPhoto.concat(list)
					setTimeout(()=> {
						uni.showToast({
							title: '上传成功！',
							icon: 'none'
						});
					}, 30)
				}).catch(err=> {
					setTimeout(()=> {
						uni.showToast({
							title: '上传失败，请重试！',
							icon: 'none'
						});
					}, 30)
					console.log(err)
				})
			},
			// 移出图片函数
			async deletephoto(){
				this.form.attachment = ''
			},
		},
		onLoad(option) {
			if(option.type === 'edit') {
				this.isPageEdit = true
				this.formData = getApp().globalData.maintenanceInfo
			}
		},
		onShow() {

		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.maintenance {

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