<!-- 报修 -->
<template>
	<view class="repair bx reserveSpace">
		<uv-form :model="form" :rules="rules" errorType="toast" ref="form" labelPosition="left" labelWidth="140">

			<uv-form-item label="楼栋" prop="building" borderBottom @click="floorPickerOpen">
				<uv-input placeholder="请选择楼层" v-model="form.building" border="none" readonly :fontSize="30"></uv-input>
				<template v-slot:right>
					<uv-icon name="arrow-right"></uv-icon>
				</template>
			</uv-form-item>

			<uv-form-item label="室号" prop="room" borderBottom @click="roomPickerOpen">
				<uv-input placeholder="请选择室号" v-model="form.room" border="none" readonly :fontSize="30"></uv-input>
				<template v-slot:right>
					<uv-icon name="arrow-right"></uv-icon>
				</template>
			</uv-form-item>

			<uv-form-item label="维修物品" prop="project" borderBottom>
				<uv-input v-model="form.project" placeholder="请填写维修物品的名称" :fontSize="30" border="none"></uv-input>
			</uv-form-item>


			<uv-form-item label="你的姓名" prop="name" borderBottom>
				<uv-input v-model="form.name" placeholder="请填写姓名" :fontSize="30" border="none"></uv-input>
			</uv-form-item>

			<uv-form-item label="你的电话" prop="phone" borderBottom>
				<uv-input v-model="form.phone" placeholder="请填写电话号" border="none" :fontSize="30"></uv-input>
			</uv-form-item>

			<uv-form-item label="描述说明" prop="context" borderBottom>
				<uv-textarea v-model="form.context" :height="140" :maxlength="200" textStyle="font-size : 30rpx" count
					placeholder="请输入描述说明"></uv-textarea>
			</uv-form-item>

			<uv-form-item label="照片" prop="image" labelPosition="top">
				<view class="image-list">
					<view @click="openImageMenu(index)" v-for="(item,index) in form.image" :key="index"
						class="image-item">
						<image :src="item" mode="widthFix"></image>
					</view>
					<view v-if="form.image.length < maxUpload" @click="uploadImage" class="upload">
						<uv-icon name="camera-fill" color="#3c9cff" size="50"></uv-icon>
					</view>
				</view>
			</uv-form-item>

			<uv-button type="primary" text="提交" shape="circle" customStyle="margin-top: 10px"
				@click="submitRepair"></uv-button>
		</uv-form>

		<!-- 报修地址选择(楼栋) -->
		<uv-picker ref="floorPicker" :columns="floorList" :round="20" :itemHeight="100" keyName="name" title="选择楼栋"
			@confirm="floorConfirm"></uv-picker>

		<!-- 报修地址选择(室号) -->
		<uv-picker ref="roomPicker" :columns="roomNumberList" :itemHeight="100" :round="20" keyName="name" title="选择室号"
			@confirm="roomConfirm"></uv-picker>

		<!-- 图片操作菜单 -->
		<uv-action-sheet ref="actionSheet" :actions="list" :round="20" cancelText="取消" title="图片操作"
			@select="selectImageSheet"> </uv-action-sheet>

		<!-- 隐私政策 -->
		<PrivacyAgreementPoup ref="showPrivacy"></PrivacyAgreementPoup>
	</view>
</template>

<script>
	import PrivacyAgreementPoup from "@/components/PrivacyAgreementPoup/PrivacyAgreementPoup.vue";
	export default {
		name: 'Repair',
		components: {
			PrivacyAgreementPoup
		},
		data() {
			return {
				form: {
					building: '', //楼栋
					room: '', //室号
					project: '', //项目
					phone: '', //手机号
					name: '', //姓名
					context: '', //简介
					image: []
				},
				rules: { //参数校验
					building: {
						type: 'string',
						required: true,
						message: '请选择楼栋',
						trigger: ['blur', 'change']
					},
					room: {
						type: 'string',
						required: true,
						message: '请选择室号',
						trigger: ['blur', 'change']
					},
					project: {
						type: 'string',
						required: true,
						message: '请填写维修物品',
						trigger: ['blur', 'change']
					},
					name: {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					phone: [{
						required: true,
						message: '请填写手机号',
						trigger: ['blur', 'change']
					}, {
						validator: (rule, value, callback) => {
							return uni.$uv.test.mobile(value);
						},
						message: '手机号格式错误',
						trigger: ['blur']
					}],
					context: {
						type: 'string',
						required: true,
						message: '请填写描述说明',
						trigger: ['blur', 'change']
					},
					image: {
						type: 'array',
						required: true,
						message: '请上传图片',
						trigger: ['blur', 'change']
					},
				},
				maxUpload: 4, //最大上传图片张数
				list: [ //图片操作菜单操作项
					{
						name: '查看图片',
						id: 0
					},
					{
						name: '删除图片',
						id: 1
					}
				],
				currentIndex: undefined, //当前操作的图片索引
				floorList: [], //楼栋列表
				roomNumberList: [], //室号列表
			}
		},
		onShow() {
			if (wx.onNeedPrivacyAuthorization) {
				console.log('onNeedPrivacyAuthorization');
				wx.onNeedPrivacyAuthorization(resolve => {
					console.log('onNeedPrivacyAuthorization');
					this.resolvePrivacyAuthorization = resolve
					this.$refs.showPrivacy.init(resolve)
				})
			}
			if(this.floorList.length < 1){
				this.getActorGoList()
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		methods: {
			//上传图片
			uploadImage() {
				let self = this
				this.$Oss.ossUploadImage({
					success(res) {
						self.form.image.push(res)
					}
				})
			},

			//打开图片操作菜单
			openImageMenu(index) {
				this.currentIndex = index
				this.$refs.actionSheet.open();
			},

			//用户选择了图片操作选项
			selectImageSheet(imageSheet) {
				let {
					id
				} = imageSheet
				if (id) {
					this.deleteImageAsList()
				} else {
					this.viewImageAsList()
				}
			},

			//查看图片
			viewImageAsList() {
				this.$utils.previewImage({
					current: this.currentIndex,
					urls: this.form.images
				})
			},

			//删除图片
			deleteImageAsList() {
				this.form.images = this.form.images.filter((_, index) => {
					return index != this.currentIndex
				})
			},

			//用户选择了楼栋
			floorConfirm(floor) {
				this.form.building = floor.value[0].name
				this.getFloorList(floor.value[0].id)
			},

			//打开选择楼栋
			floorPickerOpen() {
				this.$refs.floorPicker.open();
			},

			//用户选择了室号
			roomConfirm(floor) {
				this.form.room = floor.value[0].name
			},

			//打开选择室号
			roomPickerOpen() {
				this.$refs.roomPicker.open();
			},

			//提交报修
			submitRepair() {
				this.$refs.form.validate().then(res => {
					let images = this.form.image.join()
					this.$api('addSchoolOrder', {
						...this.form,
						image: images
					}, res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '申请报修成功'
							})
							this.cleanfrom()
							this.$refs.form.clearValidate();
							this.toRepairList()
						}
					})
				}).catch(errors => {})
			},

			//跳转到报修记录
			toRepairList() {
				uni.navigateTo({
					url: '/pages/repairList/repairList'
				})
			},

			//清除表单数据
			cleanfrom() {
				this.form = {
					building: '', //楼栋
					room: '', //室号
					project: '', //项目
					phone: '', //手机号
					name: '', //姓名
					context: '', //简介
					image: []
				}
			},

			//获取楼栋列表
			getActorGoList() {
				this.$api('getFloorList', res => {
					this.floorList.push(res.result)
				})
			},

			//获取室号列表 
			getFloorList(id) {
				this.$api('getActorGoList',{id},res => {
					this.roomNumberList = []
					if(res.result.length > 0 && this.form.room) this.form.room = res.result[0].name
					this.roomNumberList.push(res.result)
				})
			}
		}
	}
</script>

<style scoped>
	.bx {
		width: 96%;
		margin: 0rpx auto;
	}

	.repair-title {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100rpx;
		padding-top: 100rpx;
	}

	.repair-title text {
		font-size: 40rpx;
		font-weight: bold;
		height: 70rpx;
		border-bottom: 8rpx solid #3c9cff;
	}

	.image-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding-top: 20rpx;
	}

	.image-item,
	.upload {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 24%;
		height: 174rpx;
		background: #f8f8f8;
		margin-left: 1%;
		border-radius: 15rpx;
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.image-item image {
		width: 100%;
	}
</style>