<template>
	<view>
		<image src="/static/icon/background.png" mode="" class="background"></image>
		<view style="padding-top: 100rpx;">
			<view class="align-items">
				<back></back>
			</view>
			<view class="project-content">
				<view class="font-forty-eight blue">
					项目名称：{{name}}
				</view>
				<view class="font-forty-eight blue">
					项目地址：{{address}}
				</view>
			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('district')">
					{{districtName ? districtName :'选择行政区区'}}
				</view>
			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('xiaoqu')">
					{{house ? house : '选择小区'}}
				</view>

			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('louyu')">
					{{bulid ? bulid : '选择楼宇'}}
				</view>

			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('danyuan')">
					{{unit ? unit : '选择单元'}}
				</view>

			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('menpai')">
					{{houseNumber ? houseNumber  : '选择门牌'}}
				</view>
			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openQualified()">
					{{isqualified ? isqualified : '是否'+(id=='004'?'合格':'维修')}}
					<u-select v-model="qualifiedShow" :list="qualifiedList" @confirm="qualifiedConfirm()"></u-select>
				</view>
			</view>
			<view class="upload">
				<text class="font-forty SourceHanSansCN">备注：</text>
				<textarea value="" placeholder="请输入备注" v-model="remark" />
			</view>
			<view class="" style="display: flex; flex-wrap: wrap; margin: 0 55rpx;">
				<image src="/static/icon/chooseimg.png" mode="" style="width: 190rpx; height: 190rpx; margin: 0 12rpx; "
					@click="choose()"></image>
				<view v-for="(item,index) in imgArr" :key="index" style="position: relative;">
					<view v-if="item.substring(item.length - 3) == 'png' || item.substring(item.length - 3) == 'jpg' ">
						<image :src="item" mode="" style="width: 190rpx; height: 190rpx; margin: 0 20rpx;"
							@click="showPhoto(index)">
						</image>
					</view>
					<view v-else>
						<video :src="item" style="width: 190rpx; height: 190rpx; margin: 0 20rpx;"></video>
					</view>
					<view @click="remove(index)"
						style="position: absolute; top: 0; right: 14rpx; border-radius: 50%; background-color: #FF0000;">
						<u-icon name="close" color="#FFFFFF" size="35"></u-icon>
					</view>
				</view>
			</view>
			<view class="project background-color1 font-sixty-four white" @click="updatePhoto()">
				确定上传
			</view>
		</view>
		<u-popup v-model="pop" mode="bottom" height="1000rpx">
			<view style="margin: 20rpx;">
				<u-search v-show="inputShow" placeholder="请输入搜索内容" v-model="keyword" :action-style="{'font-size':'40rpx'}"
					@custom="searchinfo(selectType)"></u-search>
			</view>
			<view>
				<scroll-view scroll-y="true" style="height: 600rpx; text-align: center;">
					<view v-for="(item,index) in chooseHouseList" :key="item.value">
						<view :class="current === index ? 'active' : '' " style="color: #000000;font-size: 56rpx;"
							@click="active(item,index)">
							{{item.label}}
						</view>
					</view>
				</scroll-view>
				<view class="">
					<button type="default" class="font-fifty-six" @click="search(selectType)">确定</button>
				</view>
				<view class="">
					<button type="default" @click="pop = false;" class="font-fifty-six">取消</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import service from '@/api/index.js'
	export default {
		data() {
			return {
				action: this.$HTTP.webUrl + `/obs`,
				headers: {
					MAuthorization: "wxBearer " + uni.getStorageSync('token')
				},
				url: [],
				id: 0,
				name: '',
				address: '',
				photo: [],
				uploading: false,
				imgArr: [],
				progress: 0, //图片或视频上传百分比
				inputShow: false,
				districtId: 0,
				districtName: '选择行政区',
				pop: false,
				housename: '',
				houseid: 0,
				buildid: 0,
				unitid: 0,
				numberid: 0,
				houseShow: false,
				current: -1,
				chooseHouseList: [],
				house: '选择小区',
				bulid: '选择楼宇',
				unit: '选择单元',
				houseNumber: '选择门牌',
				serviceType: '',
				housedata: 0,
				remark: '',
				keyword: '',
				loading: false,

				isqualified: null,
				qualifiedShow: false,
				qualifiedList: [{
						value: '是',
						label: '是',
					},
					{
						value: '否',
						label: '否',
					}
				]
			}
		},
		onLoad(e) {
			console.log(e)
			this.id = e.id;
			this.name = e.name;
			this.address = e.address
			// this.getEngineDetail();
		},
		methods: {
			showPhoto(index) {
				uni.previewImage({
					current: index,
					urls: this.imgArr,
				})
			},
			updatePhoto() {
				if (this.housedata == 0 || this.housedata == null) {
					this.$UTILS.showPrompt('请选择门牌号!')
					return
				}
				if (this.isqualified == null || this.isqualified == '') {
					this.$UTILS.showPrompt('请选择是否合格!')
					return
				}
				if (this.imgArr.length <= 0) {
					this.$UTILS.showPrompt('请上传照片!')
					return
				}
				if (this.loading == false) {
					this.$UTILS.showPrompt('照片或视频未上传完毕，无法提交！')
					return
				}
				let param = {
					// photo: this.imgArr,
					picUrl: this.imgArr,
					serviceType: this.id,
					houseId: this.housedata,
					remark: this.remark,
					whether: this.isqualified,
					//小区
					areaId: this.houseid,
					areaName: this.house,
					//楼宇
					buildingId: this.buildid,
					buildingName: this.bulid,
					//单元
					unitId: this.unitid,
					unitName: this.unit,
					//房间
					houseName: this.houseNumber,
				}
				console.log('param', param)
				// this.$UTILS.showPrompt('提交成功！')
				service.submitOrder(param).then(res => {
					this.$UTILS.showPrompt('提交成功！')
					this.houseNumber = null;
					this.imgArr = null;
				}).catch(e => {
					this.$UTILS.showPrompt('提交失败！')
					console.error(e)
				})

			},
			// //获取小区
			// getArea() {
			// 	service.getArea().then(res => {
			// 		this.chooseHouseList = [];
			// 		res.forEach((item, index) => {
			// 			this.chooseHouseList.push({
			// 				value: item.id,
			// 				label: item.name,
			// 				selectType: 'xiaoqu'
			// 			})
			// 		})
			// 	})
			// },
			searchinfo(selectType) {
				if (selectType == 'xiaoqu') {
					console.log('ssss', this.selectType)
					service.getArea({
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'xiaoqu'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'louyu') {
					console.log('ssss', this.selectType)
					service.getBuilding({
						areaId: this.houseid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'danyuan') {
					console.log('ssss', this.selectType)
					service.getUnit({
						buildingId: this.buildid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'menpai') {
					console.log('ssss', this.selectType)
					service.getAllHouse({
						unitId: this.unitid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
					this.keyword = '';
				}

			},
			openHouse(selectType) {
				if (selectType === 'district') {
					this.inputShow = false
					//选择行政区
					this.housename = null;
					this.selectType = 'district'
					service.getDistrict().then(res => {
						// console.log(res);
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.dictValue,
								label: item.dictLabel,
								selectType: 'district'
							})
						})
					})
				} else if (selectType === 'xiaoqu') {
					this.inputShow = true
					//选择小区
					this.selectType = 'xiaoqu'
					service.getArea({
						district: this.districtId
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'xiaoqu'
							})
						})
					})
				} else if (selectType === 'louyu') {
					this.inputShow = true
					//选择楼宇
					this.selectType = 'louyu'
					this.housename = null;
					this.buildid = 0;
					service.getBuilding({
						areaId: this.houseid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})
				} else if (selectType === 'danyuan') {
					this.inputShow = true
					//选择单元
					this.selectType = 'danyuan'
					this.housename = null;
					this.unitid = 0;
					service.getUnit({
						buildingId: this.buildid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})
				} else if (selectType === 'menpai') {
					this.inputShow = true
					//选择门牌
					console.log('menpai', this.numberid)
					this.selectType = 'menpai'
					this.housename = null;
					this.numberid = null;
					service.getAllHouse({
						unitId: this.unitid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
				}
				this.pop = !this.pop;
			},
			active(item, index) {
				this.current = index;
				this.housename = item.label;
				if (item.selectType == 'district') { //行政区
					this.districtId = item.value;
				} else if (item.selectType == 'xiaoqu') {
					this.houseid = item.value;
				} else if (item.selectType == 'louyu') {
					this.buildid = item.value;
				} else if (item.selectType == 'danyuan') {
					this.unitid = item.value;
				} else if (item.selectType == 'menpai') {
					this.numberid = item.value
				}
				this.selectType = item.selectType;
			},
			search(selectType) {
				if (selectType == 'district') {
					this.districtName = this.housename;
					service.getArea({
						district: this.districtId
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.dictValue,
								label: item.dictLabel,
								selectType: 'xiaoqu'
							})
						})
					})
					this.current = -1;
					this.house = null;
					this.bulid = null;
					this.unit = null;
					this.houseNumber = null;

					this.houseid = 0;
					this.buildid = 0;
					this.unitid = 0;
					this.housedata = 0;


				} else if (selectType == 'xiaoqu') {
					this.house = this.housename;
					service.getBuilding({
						areaId: this.houseid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})
					this.current = -1;
					this.bulid = null;
					this.unit = null;
					this.houseNumber = null;

					this.buildid = 0;
					this.unitid = 0;
					this.housedata = 0;
				} else if (selectType == 'louyu') {
					this.bulid = this.housename;
					service.getUnit({
						buildingId: this.buildid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})
					this.unit = null;
					this.houseNumber = null;
					this.current = -1;

					this.unitid = 0;
					this.housedata = 0;
				} else if (selectType == 'danyuan') {
					this.unit = this.housename;
					service.getAllHouse({
						unitId: this.unitid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
					this.houseNumber = null;
					this.current = -1;
					this.housedata = 0;

				} else if (selectType == 'menpai') {
					this.houseNumber = this.housename;
					this.housedata = this.numberid;
					// console.log('this.housedata', this.housedata)
					this.current = -1;
				}
				this.pop = false;
			},
			// getEngineDetail() {
			// 	service.getEngineDetail({
			// 		id: this.id
			// 	}).then(res => {
			// 		this.photo = res.photo
			// 	})
			// },
			choose() {
				let _this = this;
				uni.showActionSheet({
					title: '上传',
					itemList: ['图片', '视频'],
					success: (res) => {
						// console.log(res)
						if (res.tapIndex == 0) {
							this.chooseimage()
						} else {
							this.choosevideo()
						}
					}
				})
			},
			chooseimage() {
				console.log('图片')
				let _this = this;
				uni.chooseImage({
					sizeType: ['album', 'camera'],
					success(resp) {
						console.log('res--uni.chooseMedia', resp);
						resp.tempFiles.forEach((item, index) => {
							const task = uni.uploadFile({
								url: _this.$HTTP.webUrl + `/obs`,
								filePath: item.path,
								name: 'file',
								formData: {},
								header: _this.headers,
								success: res => {
									// 判断是否json字符串，将其转为json格式
									let data = _this.$u.test.jsonString(res
										.data) ? JSON.parse(res.data) : res.data;
									if (![200, 201, 204].includes(res.statusCode)) {
										// this.uploadError(index, data);
										_this.$UTILS.showPrompt('上传失败！')
									} else {
										// 上传成功
										// this.lists[index].response = data;
										// this.lists[index].progress = 100;
										// this.lists[index].error = false;
										// this.$emit('on-success', data, index, this.lists, this
										// 	.index);
										if (_this.progress === 100) {
											// console.log('_this.progress', _this.progress)
											// console.log('data----', data)
											// console.log('res--', res)
											_this.imgArr.push(data.data.url)
											// console.log('imgArr', _this.imgArr)
											_this.$UTILS.showPrompt('上传成功！')
										}
									}
								},
								fail: e => {
									_this.$UTILS.showPrompt('上传失败！')
									this.uploadError(index, e);
								},
								complete: res => {
									_this.uploading = false;
									// _this.uploadFile(index + 1);
									// this.$emit('on-change', res, index, this.lists, this
									// .index);
								}
							});
							task.onProgressUpdate(res => {
								// if (res.progress > 0) {
								// 	this.lists[index].progress = res.progress;
								// 	this.$emit('on-progress', res, index, this.lists, this.index);
								// }
								_this.progress = res.progress;
								console.log('onProgressUpdate', res)
								uni.showLoading({
									title: '上传中'
								})
								if (_this.progress != 100) {
									_this.loading = false
									console.log('_this.loading false', _this.loading)
								} else {
									_this.loading = true
									console.log('_this.loading true', _this.loading)
								}
							});
						})
					},
				})
			},
			choosevideo() {
				let _this = this;
				console.log('视频')
				uni.chooseVideo({
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					success(resp) {
						const task = uni.uploadFile({
							url: _this.$HTTP.webUrl + `/obs`,
							filePath: resp.tempFilePath,
							name: 'file',
							formData: {},
							header: _this.headers,
							success: res => {
								// 判断是否json字符串，将其转为json格式
								let data = _this.$u.test.jsonString(res
									.data) ? JSON.parse(res.data) : res.data;
								if (![200, 201, 204].includes(res.statusCode)) {
									this.uploadError(index, data);
								} else {
									// 上传成功
									// this.lists[index].response = data;
									// this.lists[index].progress = 100;
									// this.lists[index].error = false;
									// this.$emit('on-success', data, index, this.lists, this
									// 	.index);	
									if (_this.progress === 100) {
										console.log('_this.progress', _this.progress)
										console.log('data----', data)
										console.log('res--', res)
										_this.imgArr.push(data.data.url)
										console.log('imgArr', _this.imgArr)
										_this.$UTILS.showPrompt('上传成功！')
									}
								}
							},
							fail: e => {
								_this.$UTILS.showPrompt('上传失败！')
								this.uploadError(index, e);
							},
							complete: res => {
								uni.hideLoading();
								_this.uploading = false;
								// _this.uploadFile(index + 1);
								// this.$emit('on-change', res, index, this.lists, this
								// .index);
							}
						});
						task.onProgressUpdate(res => {
							// if (res.progress > 0) {
							// 	this.lists[index].progress = res.progress;
							// 	this.$emit('on-progress', res, index, this.lists, this.index);
							// }
							_this.progress = res.progress;
							console.log('onProgressUpdate', res)
							uni.showLoading({
								title: '上传中'
							})
							if (_this.progress != 100) {
								_this.loading = false
								console.log('_this.loading false', _this.loading)
							} else {
								_this.loading = true
								console.log('_this.loading true', _this.loading)
							}
						});

					},
				})
			},
			remove(index) {
				uni.showModal({
					title: '提示',
					content: '是否删除该图片或视频？',
					success: (res) => {
						if (res.confirm) {
							this.imgArr.splice(index, 1)
							console.log('this.imgarr', this.imgArr)
						}
					}
				})
			},
			openQualified() {
				this.qualifiedShow = !this.qualifiedShow;
			},
			qualifiedConfirm(e) {
				this.isqualified = e[0].value;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.background {
		z-index: -1;
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
	}

	.choose {
		text-align: center;
		padding: 20rpx;
		margin: 30rpx;
		color: #FFFFFF;
		background: #43CEB1;
		border-radius: 64rpx;
	}

	.upload {
		margin: 30rpx;
		padding: 30rpx 20rpx;
		border: 2rpx solid #3857F3;
		border-radius: 48rpx;
	}

	.active {
		background-color: #1890FF;
	}

	.project-content {
		border: 4rpx solid #3857F3;
		margin: 80rpx 55rpx 30rpx;
		padding: 30rpx 20rpx;
		border-radius: 48rpx;
	}

	.project {
		border-radius: 72rpx;
		padding: 25rpx 0;
		text-align: center;
		margin: 60rpx 55rpx 0;
	}

	.project-img {
		width: 300rpx;
		height: 168rpx;
		margin: 20rpx auto;
	}
</style>
