<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" 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.repairAddress" 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.ownerName" 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" :start="startTime" :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-box">
								<textarea class="textarea" :maxlength="200" placeholder-style="font-size:14px" v-model="formData.repairDesc" auto-height placeholder="请输入问题描述" />
							</view>
						</view>
						<view class="form-item-text">
							<view class="title">问题图片(最多传三张)</view>
							<view class="picker">
								<uni-file-picker :value="formData.repairPhotoList" 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()">{{isPageEdit ? '确认' : '下一步'}}</button>
			</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,
				startTime: Date.now(),
				formData: {
					repairTitle: '',
					repairAddress: '',
					repairArticle: '',
					reservationTime: '',
					ownerName: '',
					ownerPhone: '',
					repairDesc: ''
				},
				filePathsList: [],
				isSubmit: false
			}
		},
		methods: {
			async toAudit() {
				if(!this.formData.repairTitle) {
					uni.showToast({
						title: '请输入标题',
						icon: 'none'
					})
					return
				}
				if(!this.formData.repairAddress) {
					uni.showToast({
						title: '请输入报修位置',
						icon: 'none'
					})
					return
				}
				if(!this.formData.repairArticle) {
					uni.showToast({
						title: '请输入报修物品',
						icon: 'none'
					})
					return
				}
				if(!this.formData.ownerName) {
					uni.showToast({
						title: '请输入上报人员',
						icon: 'none'
					})
					return
				}
				if(!this.formData.ownerPhone) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					})
					return
				}
				this.isSubmit = true
				// let repairPhoto = this.formData.repairPhoto.map(item=>item.url).join(',')
				let repairPhoto = this.filePathsList.map(item=>item.url).join(',')
				if(this.isPageEdit) {	// 编辑
					await editCommunityRepairApi({
						...this.formData,
						repairPhoto,
						repairStatus: 'initiateWaitAudit'
					})
					uni.$emit('change-maintenance');
					setTimeout(()=> {
						uni.showToast({
							title: '编辑成功！',
							icon: 'none'
						});
						setTimeout(()=> {
							this.isSubmit = false
							uni.navigateBack({delta: 2});
						}, 1500)
					}, 30)
				} else {
					await addCommunityRepairApi({
						...this.formData,
						repairPhoto
					})
					getApp().globalData.maintenanceInfo = {
						...this.formData,
						repairPhoto,
						repairStatus: 'initiateWaitAudit',
						reservationTime: Moment.unix(this.formData.reservationTime).format('YYYY-MM-DD HH:mm')
					}
					uni.$emit('change-maintenance');
					setTimeout(()=> {
						uni.showToast({
							title: `${this.isPageEdit ? '编辑' : '提交'}成功！`,
							icon: 'none'
						});
						setTimeout(()=> {
							this.isSubmit = false
							uni.redirectTo({
								url: '/pages/maintenanceAaudit/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.repairPhoto = this.formData.repairPhoto.concat(list)
					// setTimeout(()=> {
					// 	uni.showToast({
					// 		title: '上传成功！',
					// 		icon: 'none'
					// 	});
					// }, 30)
					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(option) {
			if(option.type === 'edit') {
				this.isPageEdit = true
				let formData = getApp().globalData.maintenanceInfo
				let repairPhotoList = formData.repairPhoto ? formData.repairPhoto.split(',') : []
				let repairPhotoLists = []
				repairPhotoList.forEach(item=> {
					let nameList = item.split('/');//分割url
					let name = nameList[nameList.length - 1];//通过最后一个数组下标获取图片名
					let extnameList = name.split('.')
					let extname = extnameList[extnameList.length - 1]
					let url = `${serverConfig.imageURL}${item}`
					repairPhotoLists.push({
						name,
						extname,
						url,
						tempFilePath: item
					})
					this.filePathsList.push({
						url: item,
						tempFilePath: item
					})
				})
				formData['repairPhotoList'] = repairPhotoLists
				this.formData = {...formData}
			} else {
				this.formData.ownerName = uni.getStorageSync('user_name')
				this.formData.ownerPhone = uni.getStorageSync('user_phone')
				this.formData.reservationTime = Date.now() + 24*60*60*1000
			}
		}
	}
</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: 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>