<template>
	<view class="zhcx-table entRiskPointCheck-wrap">
		<view class="task-desc">
			<view>
				<view class="uni-title uni-common-pl">措施分类：{{formData.measureTypeTitle}}</view>
				<view class="uni-title uni-common-pl">管控措施：{{formData.measureContent}}</view>
				<view class="uni-title uni-common-pl">法律依据：{{formData.measureReference}}</view>
				<view class="uni-title uni-common-pl">措施描述：{{formData.measureDesc}}</view>
			</view>
			<view>
				<view class="uni-title uni-common-pl">描述：</view>
				<view class="uni-textarea uni-common-pl">
					<textarea @blur="bindTextAreaBlur" auto-height />
				</view>
			</view>
			<view class="uni-title uni-common-pl">附件：
				<p class="tip">支持各类图片，文件不大于10M</p>
				<view class="zhcx-upload" @click="uploadBt">
					<div class="icon"></div>
					<text class="uploadTitle">点击上传</text>
				</view>
				<view class="preview-wrap" v-if="previewImage">
					<image class="preview" :src="previewImage" mode="widthFix"></image>
				</view>
			</view>	
			<view class="handle-wrap">
				<button type="primary" @click="addPass(1)" size="mini">通过</button>
				<button type="default" @click="addPass(2)" size="mini">不通过</button>
				<button type="warn" @click="addDanger" size="mini" :disabled="dangerStatus">有隐患</button>			
			</view>			
		</view>
	</view>
</template>

<script>
	import { upload } from '@/api/index';
	import { updateCheckTaskDoingRecord} from '@/api/checkRecord.js'
	import {manageTaskById} from '@/api/task'
	import {deepClone} from '@/libs'
	export default {
		data() {
			return {
				taskId:undefined,
				riskPointId:undefined,
				previewImage:undefined,
				uploadImage:undefined,	
				fromType:undefined,
				dataList: [],
				dangerStatus:false,
				formData: {
					riskPointId: '',
					measureTypeTitle: '',
					measureContent: '',
					measureReference: '',
					measureDesc: '',
					checklistId: '',
					hazardTitle: '',
					hazardId: '',
					hazardCode: '',
					measureId: '',
					measureTypeId: '',
					riskCode: '',
					riskSource: '',
					riskId: '',
					attachList: []
				}					
			}
		},
		onLoad(options) {
			console.log({
				"检查结果":options
			})
			if(options.taskId)
			{	
				this.taskId = options.taskId
				this.riskPointId = options.riskPointId	
				this.fromType=options.type
			}
			let measure = uni.getStorageSync("entRiskPointMeasure");
				measure.taskId=options.taskId;
				measure.riskPointId=options.riskPointId
			//隐患状态
			this.dangerStatus=measure.checkResult==3;
			if(measure){this.formData = measure}
		},
		methods: {
			bindTextAreaBlur: function(e) {
				this.formData.checkDesc = e.detail.value
			},
			//上传隐患
			addDanger() {
				//跳转到隐患上传页面，传值 风险点ID 管控措施ID 措施内容
				uni.navigateTo({
					url:'/views/danger/taskDanger?riskPointId=' + this.riskPointId + '&taskId=' + this.taskId
				});
			},
			addPass(v) {
				//提交，传值 风险点ID 管控措施ID 措施内容
				this.formData.checkResult = v
				this.create(this.formData);
			},
			//提交保存
			create(data) {
				const taskId=this.taskId;
				const riskPointId=this.riskPointId;
				console.log({
					"检查结果":taskId
				})
				updateCheckTaskDoingRecord(data).then((res) => {
					const { code, data, count } = res
					if (code === 0) {
						//跳转到上一页
						if(this.fromType==='taskdetail')
						{
							uni.reLaunch({
								url: `/views/task/detail/detail?id=${taskId}&riskPointId=${riskPointId}` 
							});	
							return;
						}
						uni.reLaunch({
							url: `/views/entRiskPointCheck/table?taskId=${taskId}&riskPointId=${riskPointId}` 
						});
					}
				})
			},
			//上传入口
			uploadBt() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						this.uploadSubmit(tempFilePaths[0])
						this.previewImage =tempFilePaths[0];
					},
					complete(res) {
						console.log(res)
					}
				});
			},
			uploadSubmit(filePath) {
				upload({ 
					filePath
				}).then((res) => {
					let result=JSON.parse(res);
					if(result.code===0){
						let attachList=[];
						let path=result.data.path;
						let thumbnailsFileUrl=path.split('$')[0];
						let fileUrl=path.split('$')[1];
						let uploadImgInfo={
							thumbnailsFileUrl,
							fileUrl,
							fileSize:result.data.size,
							fileExt:result.data.ext,
							fileTitle:result.data.oriFileName,
							opCode:0
						};
						attachList.push(uploadImgInfo);
						this.$set(this.formData,'attachList',attachList)		
					}else{
						uni.showToast({
							title: result.msg||"上传失败"
						})						
					}
				}).catch((msg) => {
					uni.showToast({
						title: "上传失败"
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.entRiskPointCheck-wrap{
		background-color: #fff;
		min-height: 100vh;
		.handle-wrap{
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20upx 0;
		}
		.tip {
			padding: 8upx 0 16upx 59upx;
			font-size: 24upx;
			color: #3584FF;
		}
		.preview-wrap{
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;
			border: 1px dashed #CCE6FF;
			margin-top: 15upx;
			.preview{
				display: block;
				width: 150upx;
				height: 150upx;
				margin: 10upx;
			}
		}
	}
</style>
