<template>
	<view class="wrap">
		<uni-section :title="hazard.measureContent" type="line">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" :label-width="300" label-position="top">
				<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>
		<view v-if="handles.length>0" class="tip">还剩<text class="word">{{handles.length}}</text>条</view>		
		<button type="primary" @click="onSubmit" class="submit-BT"> {{handles.length>0?'下一步':'提交'}}</button>		
	</view>
</template>

<script>
	import {upload} from '@/api/system/upload.js'	
	import {updateChecklistHazardRecordDoing,completeChecklistDoing} from '@/api/aqpt/checklistHazardRecordApi.js'
	import { getChecklistHazardRecordView } from '@/api/aqpt/checklistRecordHazardApi'
	export default {
		data() {
			return {
				checkResults:[
					{text:"通过",value:1},
					{text:"不通过",value:-1},
				],
				formData:{
					checkResult:1,	
					remark:"",
					attachList:[]
				},
				rules:{},
				hazard:{},
				itemList:[],
				handles:[]
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init(){
				let hazard=uni.getStorageSync('hazard');
				this.hazard=hazard;
				this.getchecklist()
			},
			async onSubmit() {
				let hazard=uni.getStorageSync('hazard')
				let attachList=[]
				for(let i=0;i<this.formData.attachList.length;i++){
					let filePath=this.formData.attachList[i].url
					let fileresq=await upload({filePath})
					fileresq=JSON.parse(fileresq)
					attachList.push(fileresq.data)
				}
				await updateChecklistHazardRecordDoing({
					recordId:hazard.recordId,
					checklistId:hazard.checklistId,				
					hazardId:hazard.hazardId,
					riskId:hazard.riskId,						
					measureId:hazard.measureId,					
					checkResult:this.formData.checkResult,
					remark:this.formData.remark,
					attachList,
				}).catch(()=>{
					uni.showToast({
						icon:'none',
						title:"提交失败！"
					})				
				})
				// uni.showToast({
				// 	icon:'none',
				// 	title:"提交成功！",
				// 	complete() {
				// 		uni.redirectTo({
				// 			url:`/pages/app_views/hazard/index/index?type=form&id=${hazard.checklistId}&recordId=${hazard.recordId}`
				// 		})
				// 	}
				// })
				let idx=this.itemList.findIndex(item=>item.measureId===hazard.measureId)
				this.itemList[idx].checkResult=this.formData.checkResult
				this.batchHandle()

			},
			getchecklist(){
				getChecklistHazardRecordView(this.hazard.recordId).then((res)=>{
					let itemList=res.data.hazardList
					let measures=[]	
					let measureId=this.hazard.measureId
					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===measureId)
					if(index>0){
						let temp=measures[0];
						measures[0]=JSON.parse(JSON.stringify(this.hazard))
						measures[index]=temp
					}	
					this.itemList=measures									
					this.handles=measures					
				})
			},
			// 批量处理未处理的
			batchHandle(){
				let measure=uni.getStorageSync('hazard')
				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);
					if(!key){
						completeChecklistDoing(measure.recordId,measure.checklistId).then(()=>{	
							uni.showToast({
								icon:'none',
								title:"处理完毕",
								duration:1000,
								complete() {
									uni.$emit('type',4)
									uni.switchTab({
										url:'/pages/history/history'
									})
								}
							})	
						}).catch(()=>{
							uni.showToast({
								icon:"none",
								title:"操作失败"
							})
						})						
					}else{
						let cid=measure.checklistId;
						let rid=measure.recordId;
						completeChecklistDoing(measure.recordId,measure.checklistId).then(()=>{
							uni.showModal({
								title: '提示',
								content: '本次检查有未通过项，是否需要登记隐患？',
								success: function (res) {
									if (res.confirm) {
										uni.reLaunch({
											url:`/pages/app_views/danger/submit/submit?rid=${rid}&cid=${cid}&type=hazard`
										})	
									} else if (res.cancel) {
										uni.$emit('type',4)
										uni.switchTab({
											url:'/pages/history/history'
										})
									}
								}
							});		
						}).catch(()=>{
							uni.showToast({
								icon:"none",
								title:"操作失败"
							})
						})	
						
					}
					return					
				}
				this.formData={
					checkResult:1,	
					remark:"",
					attachList:[]
				}
				uni.setStorageSync('hazard',handles[0]);
				this.hazard=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('hazard')
		}
	}
</script>

<style lang="scss" scoped>
.wrap{
	padding: 20rpx;
	.tip{
		padding: 20rpx 10rpx 10rpx;
		text-align: right;
		color: #666;
		.word{
			font-weight: bold;
			color: #222;
			padding: 0 10rpx;
		}
	}
	.submit-BT {
		height: 72rpx;
		line-height: 72rpx;	
		text-align: center;							
		background:#3D90F4;
		border-radius: 42rpx;	
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		margin: 50rpx auto;
	}
	::v-deep .uni-forms-item{
		.uni-forms-item__content{
			.uni-data-checkbox-wrap{
				height: 100%;
				display: flex;
				align-items: center;					
			}
	
		}		
	}
}
</style>
