<template>
<view>
<image src="https://cczdsz.cn/app/images/background-from.png" class="background"></image>
<view class="project-content">
	<u-form :model="form" :rules="rules" ref="form">
		<view style="flex-flow: row;display: flex; justify-content: flex-end;">
		<view class="small-title">阀井 </view> 
		<view class="small-title" style="margin-left: auto;" @click="BoxDetails()">详情<u-icon name="arrow-right"></u-icon></view>
		</view>
		<u-form-item label="阀井名称" borderBottom ref="item1" labelWidth="140">
			<u-input v-model="form.valveWellName" disabled borderBottom
				placeholder="请输入阀井名称"></u-input>
		</u-form-item>
		<u-form-item label="位置" borderBottom ref="item1" labelWidth="140">
		<u-input v-model="form.position" disabled borderBottom
			placeholder="请选择位置"></u-input>
		<!-- <u-select v-model="showposition" :list="buildingList" label-name="name" value-name="id"
			@confirm="buildingconfirm()"> </u-select>
		<u-icon slot="right" name="arrow-right"></u-icon> -->
		</u-form-item>
		<u-form-item label="发现问题" labelWidth="140" borderBottom ref="item1">
			<u-radio-group v-model="form.discoverProblems"   >
				<u-radio @change="confirm"
					v-for="(item, index) in typelist.find_problem"
					:key="index" :name="item.dictValue">
					{{item.dictLabel}}
				</u-radio>
			</u-radio-group>
		</u-form-item>
		<!-- <u-form-item label="发现时间" labelWidth="140" borderBottom ref="item1">
				<u-input v-model="form.discoverTime" borderBottom placeholder="请选择接警时间" disabled
					@click="showtime=true&&type!=1"></u-input>
				<u-picker v-model="showtime" mode="time" :params="params" @confirm="time()"></u-picker>
		</u-form-item> -->
		<u-form-item borderBottom ref="item1">
			<view>巡检照片</view>
			<u-input v-model="WarningColumnInformation" disabled placeholder="请上传巡检照片"></u-input>
			<view class="" style="display: flex; flex-wrap: wrap; margin: 0 55rpx;">
				<image src="https://cczdsz.cn/app/images/chooseimg.png" mode=""
					style="width: 190rpx; height: 190rpx; margin: 0 12rpx; " @click="choose()" >
				</image>
				<view v-for="(item,index) in imgymxs" :key="index" style="position: relative;">
					<view v-if="item.type == 'image'">
						<image :src="item.url" 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>
		</u-form-item>
		<u-form-item borderBottom ref="item1">
			<view>备注</view>
			<u-input v-model="form.remark" type="textarea" placeholder="请输入备注" ></u-input>
		</u-form-item>
	</u-form>
	<u-button style="background: #2d95f4;color: #fff;border-radius: 20rpx;" @click="save()" v-if="type!=1"
		type='primary'>确认上传</u-button>
		<u-button style="background: #2d95f4;color: #fff;border-radius: 20rpx;" @click="update()" 
			type='primary' v-else>确认修改</u-button>
</view>
	<view>
			<u-popup v-model="showBoxDetails" height= "400px" border-radius="14" mode=bottom closeable=true   close-icon-color="#000000">
				<image src="https://cczdsz.cn/app/images/background.png" class="background"></image>
				<view>
					<u-form :model="boxform" ref="boxform">
						<view style="text-align:center; font-size: 40rpx;">阀井详情</view>
						<view class="project-content" style="margin-top: 20px;">
	                    <u-form-item label="阀井名称" borderBottom ref="item1" placeholder=" " labelWidth="200" >
		                 <u-input v-model="boxform.valveWellName" disabled></u-input>
	                    </u-form-item>
						<u-form-item label="位置" borderBottom ref="item1" placeholder=" " labelWidth="200" >
						 <u-input v-model="boxform.position" placeholder=" " disabled></u-input>
						</u-form-item>
						<u-form-item label="经度" borderBottom ref="item1" placeholder=" " labelWidth="200" >
						 <u-input v-model="boxform.longitude"  placeholder=" " disabled></u-input>
						</u-form-item>
						<u-form-item label="纬度" borderBottom ref="item1" placeholder=" " labelWidth="200" >
						 <u-input v-model="boxform.latitude"  placeholder=" " disabled></u-input>
						</u-form-item>
						<u-form-item label="控制区域" borderBottom ref="item1" placeholder=" " labelWidth="200" >
						 <u-input v-model="boxform.remark"  placeholder=" " disabled></u-input>
						</u-form-item>
							</view>
							</u-form>
				</view>
			</u-popup>
		</view>
	</view>
</view>
</template>
<script>
import service from '@/api/index.js'
export default {
data() {
	return {
		repairType: [{
			label: '施工问题',
			value: 1
		}, {
			label: '施工问题',
			value: 2
		}, ],
		showrepairType: false,
		action: this.$HTTP.webUrl + `/obs`,
		headers: {
			MAuthorization: "wxBearer " + uni.getStorageSync('token')
		},
		url: [],
		showposition:false,
		params: {
			year: true,
			month: true,
			day: true,
			hour: true,
			minute: true,
			second: false
		},
		uploading: false,
		showtime:false,
		imgArr: [],
		imgymxs: [],
		progress: 0, //图片或视频上传百分比
		form: {discoverProblems:1},
		typelist: [],
		causesOfFamageLabel: null,
		dictlist: ['find_problem'],
		buildingName: null,
		unit: null,
		unitName: null,
		showbuilding: false,
		showunit: false,
		buildingList: [],
		unitList: [],
		type: 2,
		id: null,
		community: null,
		createBy:null,
		valveWellName:null,
		position:null,
		showBoxDetails:false,
		boxform:{},
		managementOfficeId:null,
		
	}
},
onLoad(e) {
	// this.building = e.building
	// this.buildingName = e.buildingName
	// this.community = e.community
	// this.unitName = e.unitName
	// this.unit = e.unit
	this.id = e.id
  this.managementOfficeId=e.managementOfficeId
	this.type = e.type

	console.log(e)
	this.getdictsysinfo()
	uni.setNavigationBarTitle({
		title: '阀井巡查'
	});
	uni.setNavigationBarColor({
		frontColor: '#ffffff',
		backgroundColor: '#2d95f4',
	})
	if (this.type != 1) {
		//this.getBuildingList()
		this.valveWellName=e.valveWellName
		this.position=e.position
		this.form.valveWellName=this.valveWellName
		this.form.position=this.position
	}
	this.getUserName()
},
methods: {
	BoxDetails()
	{
		service.getvalveWellPositionId(this.managementOfficeId).then(res=>{
			this.boxform=res
			this.showBoxDetails=true
		})
		
	},
	time(e) {
		console.log(`${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`)
		this.form.discoverTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`
	},
	checksamplingMeterCount(e) {
		console.log(e)
					//正则表达试
					e = (e.match(/^\d*(\.?\d{0,10})/g)[0]) || null
					//重新赋值给input
					this.$nextTick(() => {
						this.form.samplingMeterCount= e
					})
				},
	getUserName(){
		service.getUserName().then(res=>{
			this.userId=res.id
			this.createBy=res.name
		})
	},
	// showPhoto(index) {
	// 	uni.previewImage({
	// 		current: index,
	// 		urls: this.imgArr,
	// 	})
	// },
	// showPhotos(index) {
	// 	uni.previewImage({
	// 		current: index,
	// 		urls: this.photo,
	// 	})
	// },
	buildingconfirm(e) {
		this.position = e[0].label
		this.building = e[0].value
		console.log(this.building)
		//this.getUnitList(e[0].value)
	},
	// unitconfirm(e) {
	// 	this.unitName = e[0].label
	// 	this.unit = e[0].value
	// },
	// getBuildingList() {
	// 	const _this = this
	// 	service.getBuildingList({
	// 		areaId: this.community
	// 	}).then(res => {
	// 		_this.buildingList = res
	// 	})
	// },
	// getUnitList(building) {
	// 	const _this = this
	// 	service.getUnitList({
	// 		buildingId: building
	// 	}).then(res => {
	// 		_this.unitList = res
	// 	})
	// },
	update()
	{
		let _this = this
		this.form.photoList = this.imgArr
		// if(this.building==null)
		// {
		// 	uni.showToast({
		// 		title: '请选择楼栋',
		// 		icon: 'none'
		// 	})
		// 	return
		// }
		if(this.imgArr.length==0)
		{
			uni.showToast({
				title: '请上传图片或视频！',
				icon: 'none'
			})
			return
		}
		// this.form.buildingId = this.building
		// this.form.communityId=this.community
		this.form.userId=this.userId
		this.form.processStatus = -1
		this.form.createBy=this.userId
		this.form.positionId=this.id
		service.updatevalveWellPosition(this.form).then(res => {
			console.log(res)
			_this.$UTILS.showPrompt('修改成功！')
			// setTimeout(() => {
			// 	console.log('跳')
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	})
			// }, 2000)
			// this.form={discoverProblems:1}
			// this.form.valveWellName=this.valveWellName
			// this.form.position=this.position
			// // this.buildingName=''
			// this.imgArr=[]
			// this.imgymxs=[]
		})
	},
	save() {
		let _this = this
		this.form.photoList = this.imgArr
		// if(this.building==null)
		// {
		// 	uni.showToast({
		// 		title: '请选择楼栋',
		// 		icon: 'none'
		// 	})
		// 	return
		// }
		if(this.imgArr.length==0)
		{
			uni.showToast({
				title: '请上传图片或视频！',
				icon: 'none'
			})
			return
		}
		// this.form.buildingId = this.building
		// this.form.communityId=this.community
		this.form.userId=this.userId
		this.form.createBy=this.userId
		this.form.positionId=this.id
		service.setvalveWellPosition(this.form).then(res => {
			console.log(res)
			_this.$UTILS.showPrompt('上报成功！')
			// setTimeout(() => {
			// 	console.log('跳')
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	})
			// }, 2000)
			this.form={discoverProblems:1}
			this.form.valveWellName=this.valveWellName
			this.form.position=this.position
			// this.buildingName=''
			this.imgArr=[]
			this.imgymxs=[]
		})
	},
	getdictsysinfo() {
		let _this = this
		service.getDictInfoList({
			type: this.dictlist
		}).then(res => {
			if (this.type == 1) {
				let _this = this
				console.log(this.id)
				service.getvalveWellPosition(this.id).then(res => {

					if (null != res.photoList) {
						res.photoList.forEach(item => {
							let url = {}
							url.url = item
							url.type = 'image'
							this.imgymxs.push(url)
							this.imgArr.push(item)
						})
					}
					this.form = res
					res.valveWellName=res.position.valveWellName
					//this.form.valveWellName=res.position.valveWellName
					this.form.position=res.position.position
					this.managementOfficeId=res.positionId
					console.log('123123',res)
				})
			}
			_this.typelist = res
			console.log(res)
		})
	},
	confirm(e) {
		this.form.findProblem =e
	},
	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({
			sourceType: ['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.imgymxs.push({
										url: data.data.url,
										type: 'image'
									})
									_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: '选取中'
						})
					});
				})
			},
		})
	},
	choosevideo() {
		let _this = this;
		console.log('视频')
		uni.chooseVideo({
			sourceType: ['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)
								_this.imgymxs.push({
									url: data.data.url,
									type: 'video'
								})
								_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: '选取中'
					})
				});
			},
		})
	},
	remove(index) {
		uni.showModal({
			title: '提示',
			content: '是否删除该图片或视频？',
			success: (res) => {
				if (res.confirm) {
					this.imgArr.splice(index, 1);
					this.imgymxs.splice(index, 1);
					console.log('this.imgarr', this.imgArr)
				}
			}
		})
	},
}
}
</script>
<style>
.project-content {
padding: 10rpx 20rpx;
border-radius: 20rpx;
background: #fff;
width: 90%;
margin: 0 auto;
}

.background {
z-index: -1;
position: fixed;
width: 100%;
height: 100%;
background-size: 100% 100%;
}
.small-title {
font-size: 38rpx;
color: #2d95f4;
margin: 20rpx 0;
}
</style>
