<template>
	<view>
		<image src="/static/icon/background-from.png" mode="" class="background"></image>
		<view class="project-content">
			<u-form labelPosition="left" :model="form" :rules="rules" ref="form">
				<u-form-item label="工程名称" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.enginName" borderBottom placeholder="请输入工程名称" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="接警时间" borderBottom ref="item1" labelWidth="150">
					<u-input v-model="form.repairTime" 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 label="报修人" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.repairName" borderBottom placeholder="请输入报修人" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="联系电话" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.repairPhone" borderBottom placeholder="请输入联系电话"
						:disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="报修类型" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="repairTypeLabel" disabled @click=" showrepairType= true;" borderBottom
						disabledColor="#ffffff" placeholder="请选择报修类型" v-if="type!=1"></u-input>
					<u-input v-model="labelList.repair_type" disabled borderBottom disabledColor="#ffffff"
						placeholder="请选择报修类型" v-if="type==1"></u-input>
					<u-select v-model="showrepairType" :list="typelist.repair_type" label-name="dictLabel"
						value-name="dictValue" @confirm="repairType()" :value="form.repairType"></u-select>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item borderBottom ref="item1">
					<view>报修内容</view>
					<u-input v-model="form.repairContent" type="textarea" showWordLimit='255' borderBottom
						:disabled="type==1" placeholder="请输入报修内容"></u-input>
				</u-form-item>
				<u-form-item label="到达时间" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.arrivalTime" borderBottom disabled
						@click="showtime4=true&&type!=1"></u-input>
					<u-picker v-model="showtime4" mode="time" :params="params" @confirm="time4()"></u-picker>
				</u-form-item>
				<u-form-item label="损坏原因" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="causesOfFamageLabel" disabled @click=" causesOfFamageType = true;" v-if="type!=1"
						borderBottom placeholder="请选择损坏原因"></u-input>
					<u-input v-model="labelList.causes_of_damage" disabled v-if="type==1" borderBottom
						placeholder="请选择损坏原因"></u-input>
					<u-select v-model="causesOfFamageType" :list="typelist.causes_of_damage" label-name="dictLabel"
						value-name="dictValue" @confirm="causesOfFamage()" :value="form.causesOfFamage"></u-select>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="施工管径" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.constructionPipeDiameter" borderBottom :disabled="type==1"
						placeholder="请输入施工管径"></u-input>
				</u-form-item>
				
				<u-form-item label="管线压力" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="pipelinePressureLabel" disabled @click=" pipelinePressureType= true;"
						v-if="type!=1" placeholder="请输入管线压力" borderBottom></u-input>
					<u-input v-model="labelList.pipeline_pressure" disabled borderBottom v-if="type==1"></u-input>
					<u-select v-model="pipelinePressureType" :list="typelist.pipeline_pressure" label-name="dictLabel"
						value-name="dictValue" @confirm="pipelinePressure()" :value="form.pipelinePressure"></u-select>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="停气方式" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.stopGasMethod" borderBottom :disabled="type==1"
						placeholder="请输入停气方式"></u-input>
				</u-form-item>
				<u-form-item label="使用材料" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.usingMaterials" borderBottom :disabled="type==1"
						placeholder="请输入使用材料" ></u-input>
				</u-form-item>
				<u-form-item label="维修人" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.maintenanceName" borderBottom disabled
						placeholder="请输入维修人" @click="workerModel=true&&type!=1"></u-input>
					<!-- 	<u-select v-model="workerModel" :list="workerList" label-name="name"
							value-name="id" @confirm="Worker()" :value="form.maintenanceName"></u-select> -->
							<u-popup v-model="workerModel" style= "height: 200px ;padding: 40rpx 36rpx;"mode="bottom">
								<view style="color: #00aaff;margin-left: 10px;">维修人</view>
							<u-search placeholder="请输入维修人名称" v-model="namevalue"  :action-style="{'font-size':'40rpx'}" :show-action="false" @search="getWorker()"></u-search>
							<scroll-view class="scroll-view" scroll-y="true" :scroll-top="scrollTop" style= "padding: 20rpx 36rpx; height: 200px; overflow-y:scroll;"  >
								
											<u-checkbox-group width="200px"  @change="checkboxGroupChange" style="margin: 0 0 40rpx 0;">
			<u-checkbox :customStyle="{marginBottom: '20px'}"
				@change="checkboxChange" 
				v-model="item.checked" 
				v-for="(item, index) in workerList" :key="item.id" 
				:name="item.name"
				:label="item.name"
			>{{item.name}}
			</u-checkbox>
		</u-checkbox-group>
	
		</scroll-view>
		<view style="overflow: hidden;margin: 40rpx 0 0 0; padding: 40rpx 36rpx;">
		<u-button @click="workerCancel()" style="width: 46%; float: left;background: #eae9e9;">取消</u-button>
		<u-button@click="workerButton()" type="primary" style="width: 46%; float: right;background: #0f59ca !important; color: #fff;">确认</u-button>
		</view>
									</u-popup>
						<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="施工时间" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.constructionTime" borderBottom disabled
						@click="showtime3=true&&type!=1"></u-input>
					<u-picker v-model="showtime3" mode="time" :params="params" @confirm="time3()"></u-picker>
				</u-form-item>
				
				<u-form-item label="现场领导" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.onSiteLeadership" borderBottom placeholder="请输入现场领导" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="完成时间" labelWidth="150" borderBottom ref="item1">
					<u-input v-model="form.maintenanceTime" borderBottom disabled
						@click="showtime2=true&&type!=1"></u-input>
					<u-picker v-model="showtime2" mode="time" :params="params" @confirm="time2()"></u-picker>
				</u-form-item>
				<u-form-item label="维修照片" labelWidth="150" borderBottom ref="item1">
					<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()" v-if="type!=1">
						</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="25" v-if="type!=1"></u-icon>
							</view>
						</view>
					</view>
				</u-form-item>
				<u-form-item borderBottom ref="item1">
					<view>备注</view>
					<u-input v-model="form.remarks" borderBottom type="textarea" :disabled="type==1"></u-input>
				</u-form-item>
			</u-form>
			<u-button @click="save()" v-if="type!=1" type='primary'>确认上传</u-button>
		</view>
	</view>
</template>
<script>
	import service from '@/api/index.js'
	export default {
		data() {
			return {
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				},
				showtime: false,
				action: this.$HTTP.webUrl + `/obs`,
				causesOfFamageType: false,
				pipelinePressureType: false,
				repairTypeLabel: null,
				causesOfFamageLabel: null,
				pipelinePressureLabel: null,
				showtime2: false,
				showtime3: false,
				showtime4: false,
				headers: {
					MAuthorization: "wxBearer " + uni.getStorageSync('token')
				},
				url: [],
				uploading: false,
				imgArr: [],
				imgymxs: [],
				causesOfDamage: null,
				progress: 0, //图片或视频上传百分比
				form: {},
				userInfo: {},
				showrepairType: false,
				community: null,
				communityName: null,
				dictlist: ['causes_of_damage', 'pipeline_pressure', 'repair_type'],
				typelist: {},
				typelist2: {},
				labelList: {},
				type: 2,
				id: null,
				createBy:null,
				workerList:[],
				workerModel:false,
				WorkerList:[],
				WorkerName:null,
				userIdList:[],
				maintenanceName:null,
				namevalue:''
				
			};
		},
		onLoad(e) {
			this.getdictsysinfo()
			this.type = e.type
			this.id = e.id
			uni.setNavigationBarTitle({
				title: '抢险维修单'
			});
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#2d95f4',
			})
			this.getUserName()
			this.getWorker()
		},
		onShow() {},
		methods: {
			checkboxGroupChange(val)
			{
			this.WorkerList=val
				// this.maintenanceName=this.WorkerList.join(",")
				// console.log(111,this.maintenanceName)
			},
			workerCancel()
			{
				this.WorkerList=[]
				this.workerModel=false
			},
			workerButton()
			{
				this.form.maintenanceName=this.WorkerList.join(",")
				console.log(111,this.form.maintenanceName)
				this.workerModel=false
			},
			checkboxChange(val)
			{
				console.log(222,val)
			},
			
			getdictsysinfo() {
				let _this = this
				service.getDictInfoList({
					type: this.dictlist
				}).then(res => {
					_this.typelist = res
					if (this.type == 1) {
						let _this = this
						console.log(this.id)
						service.getemergencyRepair(this.id).then(res => {
							console.log(_this.typelist)
							let list = []
							list = _this.typelist.repair_type
							list.forEach((item) => {
								if (item.dictValue == res.repairType) {
									_this.labelList.repair_type = item.dictLabel
								}
							})
							let list2 = []
							list2 = _this.typelist.causes_of_damage
							list2.forEach((item) => {
								if (item.dictValue == res.causesOfDamage) {
									_this.labelList.causes_of_damage = item.dictLabel
								}
							})
							let list3 = []
							list3 = _this.typelist.pipeline_pressure
							list3.forEach((item) => {
								if (item.dictValue == res.pipelinePressure) {
									_this.labelList.pipeline_pressure = item.dictLabel
								}
							})
							if (null != res.photoList) {
								res.photoList.forEach(item => {
									let url = {}
									url.url = item
									url.type = 'image'
									this.imgymxs.push(url)
								})
							}
							this.form = res
						})
					} else {
						// this.getwarningPileInfo(e.value)
					}
				})
			},
			showPhoto(index) {
				uni.previewImage({
					current: index,
					urls: this.imgArr,
				})
			},
			showPhotos(index) {
				uni.previewImage({
					current: index,
					urls: this.photo,
				})
			},
			getdictinfo(data, value) {
				let _this = this
				let name = null
				service.getDictInfo({
					type: data
				}).then(res => {
					let list = []
					list = res
					list.forEach((item) => {
						if (item.dictValue == value) {
							_this.labelList[item.dictType] = item.dictLabel
						}
					})
				})
			},
			Worker(val)
			{
				console.log(val)
				
			},
			getWorker()
			{
				service.getWorker({type:'0',name:this.namevalue}).then(res=>{
					console.log(res)
					this.workerList=res
				})
			},
			time(e) {
				console.log(`${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`)
				this.form.repairTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`
			},
			time2(e) {
				console.log(`${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`)
				this.form.maintenanceTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`
			},
			time3(e) {
				console.log(`${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`)
				this.form.constructionTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`
			},
			time4(e) {
				console.log(`${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`)
				this.form.arrivalTime = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}`
			},
			save() {
				let _this = this
				// this.form.unitId=this.unit
				if(this.imgArr.length==0)
				{
					_this.$UTILS.showPrompt('请上传照片！')
					return
				}
				this.form.photoList = this.imgArr
				this.form.userId=this.userId
				this.form.createBy=this.userId
				service.setemergencyRepair(this.form).then(res => {
					console.log(res)
					_this.$UTILS.showPrompt('上报成功！')
					// setTimeout(() => {
					// 	console.log('跳')
					// 	uni.switchTab({
					// 		url: '/pages/index/index'
					// 	})
					// }, 2000)
					this.form={}
					this.maintenanceName=''
					this.labelList.repair_type=''
					this.repairTypeLabel=''
					this.labelList.causes_of_damage=''
					this.causesOfFamageLabel=''
					this.causesOfFamageType=''
					this.labelList.pipeline_pressure=''
					this.pipelinePressureLabel=''
					this.pipelinePressureType=''
					this.showtime2=''
					this.imgArr=[]
					this.imgymxs=[]
				})
			},
			getUserName(){
				service.getUserName().then(res=>{
					this.userId=res.id
					this.createBy=res.name
				})
			},
			repairType(e) {
				console.log(e[0])
				this.form.repairType = e[0].value
				this.repairTypeLabel = e[0].label
			},
			causesOfFamage(e) {
				this.causesOfDamage = e[0].value
				this.form.causesOfDamage = e[0].value
				console.log(e[0])
				this.causesOfFamageLabel = e[0].label
			},
			pipelinePressure(e) {
				console.log(e[0])
				this.form.pipelinePressure = e[0].value
				this.pipelinePressureLabel = e[0].label
			},
			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','album'],
					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 lang="scss">
	.scroll-view {
		white-space: nowrap;
		height: 200px;
		width: 100%;
	}
		.u-form-right__content__slot{
			display: flex;
		}
	.background {
		z-index: -1;
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
		top: 0;
	}

	.project-content {
		padding: 20rpx;
		border-radius: 20rpx;
		width: 90%;
		margin: 0 auto;
		background: #fff;
	}

	.button {
		background: #2d95f4;
	}
</style>