<template>
	<view class="contain">
		<view class="main">
			<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1">
				<!-- <u-form-item labelWidth='180' label="任务名称:" prop="userInfo.contactPerson" borderBottom ref="item1">
                    <u--input v-model="model1.userInfo.contactPerson" border="none"></u--input>
                </u-form-item> -->
				<u-form-item labelWidth='180' label="报修区域:" prop="userInfo.repairRegionName" borderBottom
					@click="showSex = true;" ref="item1" :required=true>
					<u--input v-model="model1.userInfo.repairRegionName" disabled disabledColor="#ffffff"
						placeholder="请选择报修区域" border="none"></u--input>
					<u-icon name="arrow-right" class="ml5"></u-icon>
				</u-form-item>
				<u-form-item labelWidth='180' label="报修位置:" prop="userInfo.repairPositionName" borderBottom
					@click="showfloors();" ref="item1" :required=true>

					<u--input v-model="model1.userInfo.repairPositionName" disabled disabledColor="#ffffff"
						placeholder="请选择报修位置" border="none"></u--input>
					<u-icon name="arrow-right" class="ml5"></u-icon>
				</u-form-item>
				{{model1.userInfo.repairType}}
				<u-form-item labelWidth='180' label="报修类型:" prop="userInfo.repairTypeName" borderBottom
					@click="showtaskdegree = true;" ref="item1" :required=true>
					<u--input v-model="model1.userInfo.repairTypeName" disabled disabledColor="#ffffff"
						placeholder="请选择报修类型" border="none"></u--input>
					<u-icon name="arrow-right" class="ml5"></u-icon>
				</u-form-item>
				<u-form-item labelWidth='180' label="联系人:" prop="userInfo.contactPerson" borderBottom ref="item1"
					:required=true>
					<u--input v-model="model1.userInfo.contactPerson" border="none" placeholder="请输入"></u--input>
				</u-form-item>
				<u-form-item labelWidth='180' label="联系电话:" prop="userInfo.contactPhone" borderBottom ref="item1"
					:required=true>
					<u--input v-model="model1.userInfo.contactPhone" border="none" placeholder="请输入"></u--input>
				</u-form-item>
				<u-form-item labelWidth='180' class="ms" label="故障描述:" prop="userInfo.faultDes" ref="item1"
					:required=true>
				</u-form-item>
				<u-form-item labelWidth='180' borderBottom style="padding: 0;">
					<u-textarea v-model="model1.userInfo.faultDes" border="none" placeholder="请输入" height="140"
						style="padding: 0 20rpx;"></u-textarea>
				</u-form-item>
				<u-form-item class="ms" labelWidth='180' label="上传照片:" prop="userInfo.faultPics" ref="item1"
					:required=true>
				</u-form-item>
				<u-form-item labelWidth='180' borderBottom>
					<view class="uploadPart">
						<view class="allimg" v-for="img in fileList1">
							<image :src="img" mode="aspectFill" class="newimg"></image>
							<u-icon @click="deletePic(img)" class="mytrash" name="close-circle" color="#999"
								size="30"></u-icon>
						</view>

						<span @click="uploadImg" class="uploadBtn" v-if="fileList1.length<3"><u-icon color="#5c8fff"
								size="30px" name="camera-fill"></u-icon></span>
					</view>
				</u-form-item>
			</u--form>



		</view>
		<view class="tijiao">
			<u-button type="primary" text="提交" @click="editdata"></u-button>
		</view>
		<u-picker @cancel='showtaskdegree = false' :itemHeight="80" @confirm='degreeconfirm' :show="showtaskdegree"
			:columns="columnshowtaskdegree" keyName="label">
		</u-picker>
		<u-picker @cancel='showSex = false' :itemHeight="80" @confirm='typeconfirm' :show="showSex"
			:columns="columnsquyu" keyName="label">
		</u-picker>
		<u-picker :show="showfloor" ref="uPicker" :itemHeight="80" :loading="loading" @confirm="confirmfloor"
			@cancel='cancelfloor' keyName="orgName" :columns="columns" @change="changeHandler"></u-picker>
		<u-picker :show="showfloor2" ref="uPicker" :itemHeight="80" :loading="loading" @confirm="confirmfloor"
			@cancel='cancelfloor' keyName="orgName" :columns="columns2" @change="changeHandler"></u-picker>
	</view>
</template>

<script>
	const BaseApi = require("@/http/baseApi.js");
	export default {
		components: {},
		data() {
			return {
				uploadUrl: BaseApi.BaseApi + '/uploadFile',
				loading: false,
				fileList1: [],
				xiangji: require('@/static/index/xj.png'),
				columns: [
					[],
					[],
					[]
				],
				columns2: [
					[],
					[]
				],
				columnData: [],
				columnDatalist: [],
				showfloor: false,
				showfloor2: false,
				myposition: '',
				showSex: false,
				showday: false,
				showPicker: false,
				showtaskdegree: false,
				showtaskpeople: false,

				model1: {
					userInfo: {
						id: "",
						buildingId: "", //  楼栋ID
						storeyId: "", //  层数ID
						houseId: "", //  房间ID
						repairPositionName: "", //报修位置名称
						repairRegion: '', //报修修区域
						repairRegionName: "", //报修区域名称
						repairType: '', //报修类型
						repairTypeName: "", //报修类型
						contactPerson: "", //联系人
						contactPhone: "", //电话
						faultDes: "", // 故障描述
						faultPics: "", //图片
					},
				},
				columnsquyu: [
					[{
						label: '公共区域',
						id: 1

					}, {
						label: '室内',
						id: 2
					}]
				],

				columnshowtaskdegree: [
					[{
							label: '水',
							id: 1

						}, {
							label: '电',
							id: 2
						},
						{
							label: '其他',
							id: 3
						},
					]
				],

				rules: {
					'userInfo.contactPerson': {
						type: 'string',
						required: true,
						message: '请填写联系人',
						trigger: ['blur', 'change']
					},
					'userInfo.faultDes': {
						type: 'string',
						required: true,
						message: '请填写故障描述',
						trigger: ['blur', 'change']
					},
					"userInfo.faultPics": {
						type: 'string',
						required: true,
						message: '请上传照片',
						trigger: ['blur', 'change']
					},
					'userInfo.contactPhone': [{
							required: true,
							message: '请填写联系电话',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],
					'userInfo.repairRegionName': {
						type: 'string',
						required: true,
						message: '请选择报修区域',
						trigger: ['blur', 'change']
					},
					'userInfo.repairRegionName': {
						type: 'string',
						required: true,
						message: '请选择报修区域',
						trigger: ['blur', 'change']
					},
					'userInfo.repairPositionName': {
						type: 'string',
						required: true,
						message: '请选择报修位置',
						trigger: ['blur', 'change']
					},
					'userInfo.repairTypeName': {
						type: 'string',
						required: true,
						message: '请选择报修类型',
						trigger: ['blur', 'change']
					},


				},

			};
		},
		watch:{
			"model1.userInfo.repairRegionName"(newval,oldval){
				if(newval!=oldval){
					this.model1.userInfo.repairPositionName = '';
					this.model1.userInfo.buildingId = '';
					this.model1.userInfo.storeyId = '';
					this.model1.userInfo.houseId = '';
				}
			}
		},
		onLoad() {
			this.getfloor();
			//	this.loadmore();
		},
	
		methods: {
			//显示区域选择
			showfloors() {
				if (this.model1.userInfo.repairRegionName == '公共区域') {
					this.showfloor2 = true;
				} else {
					this.showfloor = true;
				}
			},
			// 删除图片
			deletePic(event) {
				this.fileList1.splice(event.index, 1)
			},

			// 新增图片
			uploadImg() {
				uni.chooseImage({
					count: 3,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: BaseApi.BaseApi + '/uploadFile',
								filePath: tempFilePaths[i],
								name: 'file',
								formData: {
									'user': 'test'
								},
								success: (uploadFileRes) => {
									//console.log(uploadFileRes);
									this.fileList1.push(JSON.parse(uploadFileRes.data).data)
									console.log(this.fileList1);
									this.model1.userInfo.faultPics = this.fileList1.join(',')
								}
							});
						}

					}
				});


			},
			changeHandler(e) {
				console.log(e)
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					indexs,
					picker = this.$refs.uPicker
				} = e
				if (columnIndex == 0) {
					picker.setColumnValues(1, this.alldata[index].childrenList)
					if (this.model1.userInfo.repairRegionName == '室内') {
						if (this.alldata[indexs[0]].childrenList[0].childrenList == null) {
							picker.setColumnValues(2, [])
						} else {
							picker.setColumnValues(2, this.alldata[indexs[0]].childrenList[0].childrenList)
						}
					}

				} else if (this.model1.userInfo.repairRegionName == '室内' && columnIndex == 1) {
					// picker为选择器this实例，变化第三列对应的选项
					if (this.alldata[indexs[0]].childrenList[indexs[1]].childrenList == null) {
						picker.setColumnValues(2, [])
					} else {
						picker.setColumnValues(2, this.alldata[indexs[0]].childrenList[indexs[1]].childrenList)
					}

				}


			},
			confirmfloor(e) {
				if(this.model1.userInfo.repairRegionName == '室内'){
					if (e.value[2] == undefined) {
						this.model1.userInfo.repairPositionName = e.value[0].orgName + '/' + e.value[1].orgName;
						this.model1.userInfo.buildingId = e.value[0].orgId;
						this.model1.userInfo.storeyId = e.value[1].orgId;
						this.model1.userInfo.houseId = '';
					} else {
						this.model1.userInfo.repairPositionName = e.value[0].orgName + '/' + e.value[1].orgName + '/' + e
							.value[2].orgName;
						this.model1.userInfo.buildingId = e.value[0].orgId;
						this.model1.userInfo.storeyId = e.value[1].orgId;
						this.model1.userInfo.houseId = e.value[2].orgId;
					}
					this.showfloor = false;
				}else{
					this.model1.userInfo.repairPositionName = e.value[0].orgName + '/' + e.value[1].orgName;
					this.model1.userInfo.buildingId = e.value[0].orgId;
					this.model1.userInfo.storeyId = e.value[1].orgId;
					this.model1.userInfo.houseId = '';
					this.showfloor2 = false;
				}
			},
			cancelfloor() {
				if(this.model1.userInfo.repairRegionName == '室内'){
				this.showfloor = false;
				}else{
					this.showfloor2 = false;
				}
			},
			getfloor() {
				this.$api.get('/control/getOrgStructureTree', {})
					.then(res => {
						if (res.data.code == 0) {
							this.alldata = res.data.data[0].childrenList;
							this.columns = [
								this.alldata,
								this.alldata[0].childrenList,
								[]
							]
							this.columns2 = [
								this.alldata,
								this.alldata[0].childrenList,
							]

						} else {
							this.showdct = true
						}
					})
			},
			onShowDatePicker() { //显示
				this.showPicker = true;
			},

			returndata() {
				uni.navigateBack({
					delta: 1
				});
			},
			editdata() {
				this.$refs.form1.validate().then(res => {
					this.$api.post('/repairorder', this.model1.userInfo)
						.then(res => {
							uni.navigateBack({
								delta: 1
							});
						})


				}).catch(errors => {
					//		uni.$u.toast('校验失败')
				})


			},

			typeconfirm(e) {
				this.model1.userInfo.repairRegion = e.value[0].id;
				this.model1.userInfo.repairRegionName = e.value[0].label;

				this.showSex = false;
				this.$refs.form1.validateField('userInfo.repairRegionName')
			},

			degreeconfirm(e) {
				this.model1.userInfo.repairType = e.value[0].id;
				this.model1.userInfo.repairTypeName = e.value[0].label;

				this.showtaskdegree = false;
				this.$refs.form1.validateField('userInfo.repairTypeName')
			},



		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.form1.setRules(this.rules)
		},
	};
</script>

<style lang="scss" scoped>
	.uploadPart {
		display: flex;
		justify-content: center;
		width: 100%;

		.uploadBtn {
			width: 160rpx;
			height: 160rpx;
			background-color: #ECF0FF;
			text-align: center;
			border-radius: 4rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.allimg {
			position: relative;
		}

		.newimg {
			width: 160rpx;
			height: 160rpx;
			margin-right: 10rpx;
		}

		.mytrash {
			position: absolute;
			top: 2px;
			right: 10px;
		}
	}
</style>