<template>
	<view>
		<view style="margin: 30rpx;">
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('xiaoqu')">
				{{house ? house :'选择小区'}}
			<u-popup v-model="pop" mode="bottom" height="1000rpx">
				<view style="margin: 20rpx;">
					<u-search 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;" @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>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('louyu')">
				{{bulid ? bulid : '选择楼宇'}}
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('danyuan')">
				{{unit ? unit : '选择单元'}}
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('menpai')">
				{{houseNumber ? houseNumber  : '选择门牌'}}
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('fuwu')">
				{{serviceType ? serviceType  : '服务类别'}} 
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('leibie')">
				{{tubeType ? tubeType  : '管子类别'}}
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('changdu')">
				{{tubeLong ? tubeLong  : '管子长度'}}
			</view>
			<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('zibifa')">
				{{valueType ? valueType  : '自闭阀类型'}}
			</view>
			<view class="upload">
				<text class="font-forty SourceHanSansCN">备注：</text>
				<textarea value="" placeholder="请输入备注" v-model="remark"/>
			</view>
			<view class="font-forty-eight align-items" style="margin: 30rpx 0;">
				图片与视频：
			</view>
			<view class="" style="display: flex; flex-wrap: wrap;">
				<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>
		<button type="default" class="btn" @click="updata()">确认更改</button>
	</view>
</template>

<script>
	import service from '@/api/index.js'
	export default {
		data() {
			return {
				id:0,
				imgArr:[],
				progress:0 ,//上传图片进度百分比
				headers: {
					MAuthorization: "wxBearer " + uni.getStorageSync('token')
				},
				house:'',
				bulid:'',
				unit:'',
				houseNumber:'',
				tubeType: '',
				tubeLong: '',
				typeValue: '',
				valueType:'',
				selectType:'',
				pop:false,
				housename:'',
				houseid:0,
				buildid:0,
				unitid:0,
				numberid:0,
				houseShow: false,
				current:-1,
				chooseHouseList: [],
				serviceType:'',
				serviceTypeId:'',
				tubetypeid:'',
				tubelength:'',
				valuetypeid:'',
				shows:false,
				remark:'',
				index:0,
				keyword:'',
			}
		},
		onLoad(e) {
			this.id = parseInt(e.id);
			this.photo();
			this.getById();
		},
		methods:{
			photo(){
				service.getOrderPhoto({orderId:this.id}).then(res=>{
					this.imgArr = res;
				})
			},
			showPhoto(index){
				uni.previewImage({
					current:index,
					urls:this.imgArr,
				})
				console.log('sss')
			},
			getById(){ 
				service.getById({id:this.id}).then(res=>{
					this.house = res.areaName;
					this.bulid = res.buildingName;
					this.unit = res.unitName; 
					this.houseNumber = res.houseName;
					this.serviceType = res.serviceTypeName;
					this.houseid = res.areaId;
					this.buildid = res.buildingId;
					this.unitid = res.unitId;
					this.numberid = res.houseId;
					this.remark = res.remark;
					this.tubeType = res.pipeType;
					this.tubeLong = res.pipeLength;
					this.valueType = res.valveType;
					this.serviceTypeId = res.serviceType;
				})
			},
			//获取小区
			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.getHouse({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 === 'xiaoqu'){
					//选择小区
					this.selectType = 'xiaoqu'
					this.getArea();
				}else if(selectType === 'louyu'){
					//选择楼宇
					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'){
					//选择单元
					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'){
					//选择门牌
					service.getHouse({unitId: this.unitid}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType:'menpai'
							})
						})
					})
				}
				else if(selectType === 'fuwu'){
					//选择服务类别
					// console.log('fuwu',this.selectType)
					this.chooseHouseList = [];
					service.getDic({dicType: 'service_type'}).then(res => {
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.value,
								label: item.name,
								selectType:'fuwu'
							})
						})
					})
				}else if(selectType === 'leibie'){
					//选择管子类别
					this.chooseHouseList = [];
					service.getPipeType().then(res => {
						res.forEach(item => {
							// this.chooseHouseList = [];
							this.chooseHouseList.push({
								value: item.id,
								label: item.pipeType,
								selectType:'leibie'
							})
						})
					})
				}else if(selectType === 'changdu'){
					//选择管子长度
					this.chooseHouseList = [];
					service.getPipeLength().then(res => {
						res.forEach(item => {
							// this.chooseHouseList = [];
							this.chooseHouseList.push({
								value: item.id,
								label: item.pipeLength,
								selectType:'changdu'
							})
						})
					})
				}else if(selectType === 'zibifa'){
					//选择自闭阀类别
					this.chooseHouseList = [];
					service.getProject().then(res => {
						// this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.valveType,
								selectType:'zibifa'
							})
						})
					
					})
				}
				this.pop = !this.pop;
			},
			search(selectType){
				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;

				}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.current = -1;
				}else if(selectType == 'danyuan'){
					this.unit = this.housename;
					service.getHouse({unitId: this.unitid}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType:'menpai'
							})
						})
					})

					this.current = -1;
					
				}else if(selectType == 'menpai'){
					this.houseNumber = this.housename;
					this.numberid = this.numberid;
					this.current = -1;
				}else if(selectType == 'fuwu'){
					this.serviceType = this.housename;
					this.current = -1;
				}else if(selectType == 'leibie'){
					this.tubeType = this.housename;
					this.current = -1;
				}else if(selectType == 'changdu'){
					this.tubeLong = this.housename;
					this.current = -1;
				}else if(selectType == 'zibifa'){
					this.valueType = this.housename;
					this.current = -1;
				}
				this.pop = false;
			},
			active(item,index){
				console.log('active')
				this.current = index;
				this.housename = item.label;
				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
				}else if(item.selectType == 'fuwu'){
					this.serviceTypeId = item.value
				}else if(item.selectType == 'leibie'){
					this.tubetypeid = item.value
				}else if(item.selectType == 'changdu'){
					this.tubelength = item.value
				}else if(item.selectType == 'zibifa'){
					this.valuetypeid = item.value
				}
				this.selectType = item.selectType;
			},
			choose() {
				let _this = this;
				uni.showActionSheet({
					title: '上传',
					itemList: ['图片', '视频'],
					success: (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 {
										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('上传成功！')
											_this.photo = false;
										} 
									}
								},
								fail: e => {
									_this.$UTILS.showPrompt('上传失败！')
									this.uploadError(index, e);
								},
								complete: res => {
									uni.hideLoading();
									_this.uploading = false;
								
								}
							});
							task.onProgressUpdate(res => {
								_this.progress = res.progress;
								console.log('onProgressUpdate', res)
								console.log('task.onProgressUpdate', _this.progress)
								uni.showLoading({
									title: '上传中'
								})
							});
						})
					},
				})
			},
			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 {	
									//上传成功
									if (_this.progress === 100) {
										console.log('_this.progress', _this.progress)
										_this.imgArr.push(data.data.url)
										console.log('imgArr', _this.imgArr)
										_this.$UTILS.showPrompt('上传成功！')
										_this.photo = false;
									} 
								}
							},
							fail: e => {
								_this.$UTILS.showPrompt('上传失败！')
								this.uploadError(index, e);
							},
							complete: res => {
								uni.hideLoading();
								_this.uploading = false;
							}
						});
						task.onProgressUpdate(res => {
							_this.progress = res.progress;
							console.log('onProgressUpdate', res)
							uni.showLoading({
								title: '上传中'
							})
						});
			
					},
				})
			},
			remove(index) {
				uni.showModal({
					title: '提示',
					content: '是否删除该图片或视频？',
					success: (res) => {
						if (res.confirm) {
							this.imgArr.splice(index, 1)
							console.log('this.imgarr',this.imgArr)
						}
					}
				})
			},
			updata(){
				let param = {
					id:this.id,
					serviceType: this.serviceTypeId,
					pipeType: this.tubeType,
					pipeLength: this.tubeLong,
					houseId: this.numberid,
					valveType: this.valueType,
					picUrl: this.imgArr,
					remark: this.remark,
				};
				console.log('param', param)
				service.getUpdateOrder(param).then(res=>{
					this.$UTILS.showPrompt('提交成功！')
					// setTimeout(()=>{
					// 	uni.switchTab({
					// 		url:'/pages/historyConstruction/historyConstruction'
					// 	})
					// },1000)
				}).catch(e => {
					this.$UTILS.showPrompt('提交失败！')
					console.error(e)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input {
		// width: 300rpx;
		height: 20%;
		border: 2rpx solid #000000;
	}
	.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;
	}
	.btn {
		background-color: #007AFF;
		color: #FFFFFF;
		margin: 30rpx;
	}
	.image{
		margin: 50rpx auto;
		width: 330rpx;
		height: 230rpx;
	}
</style>
