<template>
	<view class="djhjTask">
		<!-- 任务进度 -->
		<view class="djhjTask_progress">
			<text class="djhjTask_progress_title">任务进度：<text class="djhjTask_progress_text">{{wwcNumber()}}/{{pageData.taskNum}}</text></text>
			<text class="djhjTask_progress_title">不合格数<text class="djhjTask_progress_text">{{bhgNumber()}}</text></text>
		</view>
		<!-- 检查结果 -->
		<uni-card padding="0" spacing="0" v-for="(item,index) in pageData.resList.results" class="djhjList">
			<view class="djhjList_title">
				<view class="djhjList_text">检查项目</view>
				<view class="djhjList_righttext">{{item.item}}</view>
			</view>
			<view class="djhjList_title">
				<view class="djhjList_text">检查方式</view>
				<view class="djhjList_righttext">{{item.assessmentCriteria}}</view>
			</view>
			<view class="djhjList_title">
				<view class="djhjList_text">检查内容</view>
				<view class="djhjList_righttext">{{item.assessmentContent}}</view>
			</view>
			<view class="djhjList_title">
				<view class="djhjList_text">检查结果</view>
				<view :class="item.assessmentResult=='不合格'? 'djhjList_redtext' : 'djhjList_righttext'">{{item.assessmentResult}}</view>
			</view>
			<view class="djhjList_title" v-if="item.resultContent">
				<view class="djhjList_text">原因</view>
				<view class="djhjList_righttext">{{item.resultContent}}</view>
			</view>
			<view class="button_group">
				<view class="bhg_button" @click="bhgCheck(item)">不合格</view>
				<view class="hg_button" @click="hgCheck(item)">合格</view>
			</view>
		</uni-card>
		<!-- 图片上传 -->
		<view class="image_upload">
			<uni-card>
				<uni-section title="图片上传" type="line"></uni-section>
				<view class="img_upload">
					<uni-file-picker limit="4" :modelValue="pageData.imageList" @select="selectImg" @delete="deleteImg"></uni-file-picker>
				</view>
			</uni-card>
		</view>
		<!-- 按钮组 -->
		<view class="operator_button">
			<button class="pass_button" @click="pass">一键通过</button>
			<!-- 如果还有数据按钮显示为“下一个”，数据全部加载完毕显示为“完成” -->
			<view class="finish_button_grop">
				<button class="next_or_finish_button" v-if="pageData.resList.results!=0" @click="next">下一个</button>
				<button class="next_or_finish_button" @click="finish" v-else>完成</button>
			</view>	
		</view>
		<!-- 不合格弹出框 -->
		<uni-popup ref="bhgDialog" type="dialog">
			<uni-popup-dialog type="info" mode="input" confirmText="提交" cancelText="取消" title="不合格原因" value="" placeholder="请填写不合格原因" @confirm="bhgDialogConfirm"
				@close="bhgDialogClose"></uni-popup-dialog>
		</uni-popup>
		<!-- 一键通过弹出框 -->
		<uni-popup ref="passDialog" type="dialog">
			<view class="passDialog">
				<view class="passDialog_title">
					<uni-title type="h2" title="一键通过" align="center"></uni-title>
					<!-- 倒计时 -->
					<smh-countDown second="10" radius="60" lineWidth="3rpx" color="#BFA578" @end="countDownEnd"></smh-countDown>
				</view>
				<view class="passDialog_content">
					<view>10秒后将自动跳转下一个处方</view>
					<view class="passDialog_content_cancel">点击取消可停留本页面</view>
				</view>
				<view class="passDialog_button">
					<view class="passDialog_button_cancel" @click="passCancel">取消</view>
					<view class="passDialog_button_confirm" @click="passConfirm">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref,reactive } from "vue";
import {onLoad,onShow,onUnload,onPullDownRefresh} from "@dcloudio/uni-app";
import http from '@/utils/request';
import {uploadImageModelValue} from '@/pages/controls/image/OssService'

const pageData=reactive({
	bhgNum:0,
	finishNum:'',
	taskNum:'',
	djhjList:[],
	imageList:[],
	resList:[],
	imageIdList:[],
})

const formData=reactive({
	id:''
})

// 临时创建代煎代配企业评估任务内容 入参
const createData=reactive({
	taskId:'',
	pId:'',
	providerType:''
})

// 任务进度（已完成）
const wwcNumber = () =>{
	let number = 0
	let data = pageData.resList.results
	if(data != null){
		for (var i = 0; i < data.length; i++) {
			if (data[i].assessmentResult != null && data[i].assessmentResult != undefined && data[i].assessmentResult != '待检')
				number ++;
		}
	}
	return number
}

// 不合格数
const bhgNumber=()=>{
	let number=0
	if(pageData.resList.results!=null){
		pageData.resList.results.forEach(item=>{
			if(item.assessmentResult=='不合格'){
				number++
			}
		})
	}
	return number
}

// 定义不合格弹出输入框
const bhgDialog=ref()

// 提交不合格原因
const bhgDialogConfirm=(val)=>{
	// 获取输入的不合格原因
	item.value.resultContent = val
	pageData.bhgNum=pageData.bhgNum+1
	pageData.resList.results.forEach((e,idx)=>{
		if(e.id==item.value.id){
			pageData.resList.results[idx]=item.value
		}
	})
	bhgDialog._value.close()
	item.value = {}
}

// 取消不合格弹出输入框
const bhgDialogClose=()=>{
	// 关闭不合格弹出框
	item.value = {}
	bhgDialog._value.close()
}

// 一键通过弹出框
const passDialog=ref()

// 一键通过弹出框确定事件
const passConfirm=()=>{
	countTotal()
	saveHjTask()
}

// 一键通过弹出框取消事件
const passCancel=()=>{
	passDialog._value.close()
}

// 一键通过弹出框倒计时接收事件
const countDownEnd=()=>{
	saveHjTask()
}

// 选中图片，将选中的图片添加列表
const selectImg=(e)=>{
	e.tempFiles.forEach(item=>{
		uni.showLoading({
			title: '图片上传....',
			mask: true
		});
		http.upload("oss/upload",{file:item.file}).then(e => {
			uni.hideLoading()
			item.id = e.id
			console.log(e.id);
			pageData.resList.detail.imageIds=[]
			pageData.imageIdList.push(e.id)
			pageData.resList.detail.imageIds=pageData.imageIdList.toString()
			pageData.imageList.push(item)
		})
	})
}

// 删除图片，并将其从列表中删除
const deleteImg=(e)=>{
	let index=0
	index = pageData.imageList.indexOf(e.tempFilePaths)
	pageData.imageList.splice(index,1)
	pageData.imageIdList.splice(index,1)
	pageData.resList.detail.imageIds=pageData.imageIdList.toString()
}

const item = ref({})
// 不合格
const bhgCheck=(e)=>{
	e.score=0
	if(e.assessmentResult=="合格" || e.assessmentResult=="待检"){
		e.assessmentResult="不合格"
		item.value = e
		// 打开不合格弹出输入框
		bhgDialog._value.open()
	}
}

// 合格
const hgCheck=(e)=>{
	console.log("合格")
	console.log(e.assessmentResult);
	if(e.assessmentResult=='不合格' || e.assessmentResult=='待检'){
		e.assessmentResult='合格'
		e.score=e.totalScore
		e.resultContent=''
	}
}

// 一键通过
const pass=()=>{
	pageData.resList.results.forEach(item=>{
		item.assessmentResult='合格'
		item.resultContent=''
		pageData.finishNum=pageData.taskNum
	})
	passDialog._value.open()
}

// 计算环境质控总分
const countTotal=()=>{
	pageData.resList.resultContent=0
	pageData.resList.results.forEach(item=>{
		pageData.resList.resultContent+=Number(item.score)
	})
}

// 保存环境质控
const saveHjTask=()=>{
	uni.showLoading({
		title: '提交中.......',
		mask: true
	});
	http.post("app-api/zkzy/save",pageData.resList).then(res=>{
		uni.hideLoading()
		console.log(res)
		if(res==true){
			uni.showToast({
				title: '提交成功',
				success: () => {
					uni.navigateBack({
						delta:1
					})
				}
			});
		}else{
			uni.showToast({
				title: '提交失败'
			});
		}
	})
}

// 下一个
const next=()=>{
	pageData.resList.detail.fyfsyf="xcpg"
	console.log("下一个")
	if(wwcNumber()==pageData.taskNum){
		countTotal()
		saveHjTask()
	}else{
		uni.showToast({
			title: '提交失败，请检查任务进度',
			icon:'error'
		});
	}
	
}

// 完成
const finish=()=>{
	console.log("完成")
	pageData.resList.detail.fyfsyf="xcpg"
	console.log("下一个")
	saveHjTask()
}

// 获取taskId
const getTaskId=()=>{
	uni.getStorage({
		key:'saveTaskId',
		success: (res) => {
			console.log(res)
			createData.taskId = res.data.taskId
		}
	})
}

// 临时创建代煎代配企业评估任务内容
const createTask=()=>{
	createData.providerType="xcpg"
	getTaskId()
	http.get("app-api/zkzy/create",createData).then(res=>{
		console.log(res)
		getHJTask(res)
	})
}

// 获取环境质控质控作业
const getHJTask=(data)=>{
	formData.id=data
	http.get("app-api/taskDetail/get",formData).then(res=>{
		console.log(res)
		pageData.resList=res
		pageData.finishNum=0
		pageData.bhgNum=0
		pageData.taskNum = pageData.resList.results.length
		// pageData.resList.results.forEach(item=>{
		// 	// 一开始均为“待检状态”
		// 	item.assessmentResult='待检'
		// })
		pageData.resList.results.forEach(item=>{
			if(item.score!=item.totalScore){
				item.assessmentResult='不合格'
			}else{
				item.assessmentResult='合格'
			}
		})
		if(res.detail.imageIds!=null && res.detail.imageIds!=''){
			pageData.imageList = showImage(res.detail.imageIds)
		}
	})
}

// 显示图片
const showImage=(id)=>{
	return uploadImageModelValue(id)
}

onShow(()=>{
	createTask()
})
</script>

<style lang="scss" scoped>
@import './index.scss'
</style>
