<template>
	<view>
		<image src="/static/icon/background.png" mode="" class="background"></image>
		<view class="project-content">
			<u-form labelPosition="left" :model="form" :rules="rules" ref="form">
				<u-form-item label="接警时间" borderBottom ref="item1" labelWidth="120">
					<u-input v-model="form.repairTime" border="none" 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="120" borderBottom ref="item1">
					<u-input v-model="form.repairName" border="none" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="联系电话" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.repairPhone" border="none"  :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="报修类型" labelWidth="120" 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 label="报修内容" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.repairContent" type="textarea" showWordLimit='255' border="none" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="损坏原因" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="causesOfFamageLabel" disabled @click=" causesOfFamageType = true;" v-if="type!=1"
						borderBottom></u-input>
						<u-input v-model="labelList.causes_of_damage" disabled v-if="type==1"
							borderBottom></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="120" borderBottom ref="item1">
					<u-input v-model="pipelinePressureLabel" disabled @click=" pipelinePressureType= true;" v-if="type!=1"
						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="120" borderBottom ref="item1">
					<u-input v-model="form.stopGasMethod" border="none" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="使用材料" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.usingMaterials" border="none" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="维修人" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.maintenanceName" border="none" :disabled="type==1"></u-input>
				</u-form-item>
				<u-form-item label="完成时间" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.maintenanceTime" border="none" disabled @click="showtime2=true&&type!=1" :disabled="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="120" 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="35"></u-icon>
							</view>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="备注" labelWidth="120" borderBottom ref="item1">
					<u-input v-model="form.remarks" border="none" type="textarea" :disabled="type==1"></u-input>
				</u-form-item>


			</u-form>
			<u-button @click="save()" v-if="type!=1">确认上传</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,
				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
			};
		},
		onLoad(e) {
			this.getdictsysinfo()
			this.type=e.type
			this.id=e.id
			
			uni.setNavigationBarTitle({
				title: '抢险维修单'
			});
		},
		onShow()
		{
			
			
		},
		methods: {
			
			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.causesOfDamage=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
						}
						
					})
					
				})
				
			},
			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}`
			},
			save() {
				let _this=this
				// this.form.unitId=this.unit
			    this.form.photoList=this.imgArr 
				service.setemergencyRepair(this.form
				).then(res => {
					console.log(res)
					_this.$UTILS.showPrompt('上报成功！')
					setTimeout(()=>{
						console.log('跳')
						uni.switchTab({
							url:'/pages/index/index'
						})
					},2000)
				})
				
			},
			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({
					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.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: ['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)
										_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">
	.background {
		z-index: -1;
		position: fixed;
		width: 100%;
		height: 100%;
		background-size: 100% 100%;
	}

	.project-content {
		border: 4rpx solid #3857F3;
		margin: 40rpx 55rpx 0;
		padding: 30rpx 20rpx;
		border-radius: 48rpx;
	}
</style>