<template>
	<view class="wrap">
		<uni-section :title="measure.hazardTitle" type="line">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
				<uni-forms-item label="检查内容" required>
				  {{measure.measureContent}}
				</uni-forms-item>				
				<uni-forms-item label="检查结果" name="checkResult" required>
					<view class="uni-data-checkbox-wrap">
						<uni-data-checkbox v-model="formData.checkResult" :localdata="checkResults" />				
					</view>
				</uni-forms-item>	
				<uni-forms-item label="备注" name="remark">
				  <uni-easyinput v-model="formData.remark" type="textarea" :maxlength="-1" autoHeight placeholder="备注" />
				</uni-forms-item>
				<uni-file-picker  v-model="formData.attachList" 
				 fileMediatype="image" 
				 title="请上传附件"
				 limit="1"
				 @select="uploadSuccess"
				 @delete="deleteFile"></uni-file-picker>	
			</uni-forms>
		</uni-section>
		<button type="primary" @click="onSubmit" class="submit-BT">提交</button>		
	</view>
</template>

<script>
import {upload} from '@/api/system/upload.js'	
import { getChecklistHazardRecordById,updateChecklistHazardRecordDoing} from '@/api/aqpt/checklistHazardRecordApi'
import { getChecklistHazardRecordView } from '@/api/aqpt/checklistRecordHazardApi'
import {completeCheckTask} from '@/api/aqpt/task.js'	
	export default {
		data() {
			return {
				checkResults:[
					{text:"通过",value:1},
					{text:"不通过",value:-1},
				],
				formData:{
					checkResult:1,	
					remark:"",
					attachList:[]
				},
				rules:{},
				measure:{},
				handles:[],
				itemList:[]
			}
		},
		created() {
			this.init()
		},
		methods: {
			init(){
				let measure=uni.getStorageSync('measure');
				this.measure=measure;
				this.getLocation()
				this.getchecklist()
			},
			getchecklist(){
				getChecklistHazardRecordView(this.measure.recordId).then((res)=>{
					let itemList=res.data.hazardList
					let measures=[]						
					for(let i=0;i<itemList.length;i++){
						if(itemList[i].riskList){
							itemList[i].riskList.map(child=>{
								child.recordList.map(measure=>{
									measures.push(measure)
								})
							})
						}						
					}
					let index=measures.findIndex(item=>item.measureId===this.measure.measureId)
					if(index>0){
						let temp=measures[0];
						measures[0]=JSON.parse(JSON.stringify(this.measure))
						measures[index]=temp
					}	
					this.itemList=measures									
					this.handles=measures					
				})
			},
			getLocation(){
				const self=this;
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
						self.formData.dutyLongitude=res.longitude
						self.formData.dutyLatitude=res.latitude
					}
				});		
			},
			async onSubmit() {
				let measure=uni.getStorageSync('measure')
				let attachList=[]
				for(let i=0;i<this.formData.attachList.length;i++){
					let filePath=this.formData.attachList[i].url
					let fileresq=await upload({filePath,formData:{
						additions: `经度:${this.formData.dutyLongitude};纬度:${this.formData.dutyLatitude}`
					}}).catch(()=>{
						uni.showToast({
							icon:'none',
							title:"提交失败！"
						})	
					})
					if(fileresq.includes('html')){
						uni.showToast({
							icon:'none',
							title:"图片上传异常"
						})
						return
					}					
					fileresq=JSON.parse(fileresq)
					attachList.push(fileresq.data)		
				}
				await updateChecklistHazardRecordDoing({
					recordId:measure.recordId,
					checklistId:measure.checklistId,
					riskId:measure.riskId,
					hazardId:measure.hazardId,
					measureId:measure.measureId,
					checkResult:this.formData.checkResult,
					remark:this.formData.remark,
					attachList,
				}).catch(()=>{
					uni.showToast({
						icon:'none',
						title:"提交失败！"
					})				
				})
				let idx=this.itemList.findIndex(item=>item.measureId==measure.measureId)
				this.itemList[idx].checkResult=this.formData.checkResult
				this.batchHandle()
			},
			// 批量处理未处理的
			batchHandle(){
				let measure=uni.getStorageSync('measure')
				let handles=JSON.parse(JSON.stringify(this.handles))
				handles.shift()	
				this.handles=handles
				if(handles.length<1){
					const key=this.itemList.find(item=>item.checkResult===-1)
					console.log({
						"this.itemList":this.itemList,
						key
					})
					if(!key){
						completeCheckTask(measure.recordId).then(()=>{
							uni.showToast({
								icon:'none',
								title:"处理完毕",
								duration:1000,
								complete() {
									uni.redirectTo({
										url:`/pages/index/detail/detail?type=task&name=待办任务`
									})
								}
							})	
						}).catch(()=>{
							uni.showToast({
								icon:"none",
								title:"操作失败"
							})
						})						
					}else{
						let cid=measure.checklistId;
						let rid=measure.recordId;
						uni.showToast({
							icon:'none',
							title:"处理完毕",
							duration:1000,
							complete() {
								uni.redirectTo({
									url:`/pages/index/handle/task/hazardRecord/hazardRecord?rid=${rid}&cid=${cid}&title=${measure.hazardTitle}`
								})
							}
						})							
					}
					return					
				}
				this.formData={
					checkResult:1,	
					remark:"",
					attachList:[]
				}
				uni.setStorageSync('measure',handles[0]);
				this.measure=handles[0];								
			},
			uploadSuccess(e){
				let attachList=JSON.parse(JSON.stringify(this.formData.attachList))
				attachList.push(e.tempFiles[0])
				this.formData.attachList=attachList
			},
			deleteFile(e){							
				let attachList=JSON.parse(JSON.stringify(this.formData.attachList))
				attachList.filter(item=>item.uuid!==e.tempFile.uuid)
				this.formData.attachList=attachList
			},			
		},
		onUnload() {			
			uni.removeStorageSync('point')
		}
	}
</script>

<style lang="scss" scoped>
.wrap{
	padding: 20rpx;
	padding-bottom: 200rpx;
	.submit-BT {
		width: 750rpx;
		color: #4D73FF;
		text-align: center;
		font-size: 32rpx;
		padding-bottom: 68rpx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		box-shadow: 0px 0px 12px 0px #0000000A;
		border-radius: 8px 8px 0px 0px
	}
	::v-deep .uni-forms-item{
		.uni-forms-item__content{
			.uni-data-checkbox-wrap{
				height: 100%;
				display: flex;
				align-items: center;					
			}
	
		}		
	}
}
</style>
