<!-- 报修 -->
<template>
	<view class="repair bx reserveSpace">
	<!-- 	<view class="repair-title">
			<text>申请报修</text>
		</view> -->

		<uv-form :model="form" :rules="rules" :useBeforeRead="true" ref="form" labelPosition="left" labelWidth="100">

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

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

			<uv-form-item label="项目" prop="form.project" borderBottom>
				<uv-input v-model="form.project" placeholder="请填写项目" :fontSize="30" border="none"></uv-input>
			</uv-form-item>


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

			<uv-form-item label="简介" prop="form.intro" borderBottom>
				<uv-textarea v-model="form.intro" :height="140" :maxlength="200" textStyle="font-size : 30rpx" count
					placeholder="请输入内容"></uv-textarea>
			</uv-form-item>

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

			<uv-form-item label="照片" prop="form.images" labelPosition="top">
				<view class="image-list">
					<view @click="openImageMenu(index)" v-for="(item,index) in form.images" :key="index"
						class="image-item">
						<image :src="item" mode="widthFix"></image>
					</view>
					<view v-if="form.images.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" :itemHeight="100" :round="20" keyName="label" title="选择楼栋"
			@confirm="floorConfirm"></uv-picker>
		<!-- 报修地址选择(室号) -->
		<uv-picker ref="roomPicker" :columns="roomNumberList" :itemHeight="100" :round="20" keyName="label" title="选择室号"
			@confirm="roomConfirm"></uv-picker>

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

<script>
	export default {
		name: 'Repair',
		data() {
			return {
				form: {
					building: '', //楼栋
					room: '', //室号
					project: '', //项目
					phone: '', //手机号
					name: '', //姓名
					intro: '', //简介
					images: []
				},
				rules: { //参数校验
					'form.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					}
				},
				maxUpload: 4, //最大上传图片张数
				list: [ //图片操作菜单操作项
					{
						name: '查看图片',
						id: 0
					},
					{
						name: '删除图片',
						id: 1
					}
				],
				currentIndex: undefined, //当前操作的图片索引
				floorList: [
					[{
							id: 0,
							label: '楼栋1'
						},
						{
							id: 1,
							label: '楼栋2'
						}
					]
				], //楼栋列表
				roomNumberList: [
					[{
							id: 0,
							label: 'A1001'
						},
						{
							id: 1,
							label: 'A1002'
						}
					]
				], //室号列表
			}
		},
		methods: {
			//上传图片
			uploadImage(){
				let self = this
				this.$Oss.ossUploadImage({
					success(res){
						self.form.images.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].label
			},

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

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

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

			//提交报修
			submitRepair() {
				console.log(this.form);
			},
		
		}
	}
</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>