<template>
	<view class="dpTask">
		<uni-card  class="progress_card">
			<view class="dp_list">
				<image src="/static/time.png" style="height: 20px;width: 20px;"></image>
				<text style="font-size: 17px; color: rgba(51, 51, 51, 1);">任务进度:<text style="color: rgba(190, 163, 117, 1);">{{wwcNumber()}}/{{pageData.totalProgress}}</text></text>
			</view>
			<view class="sub_title">
				<view class="cfbh">
					<text class="cfbh_title">处方编号<text class="cfbh_text">{{pageData.cfbh}}</text></text>
				</view>
				<view class="bhgs">
					<text class="bhgs_title">不合格数<text class="bhgs_text">{{bhgNumber()}}</text></text>
				</view>
			</view>
			<view class="send">
				<uni-forms-item label="配方员" name="id" labelWidth="60px">
					<uni-easyinput v-model="data.reciper" type="" placeholder="请输入配方员姓名" :inputBorder="false" :disabled="pageData.disabledStatus"/>
				</uni-forms-item>
			</view>
			<uni-table class="dp_table">
				<!-- 表头行 -->
				<uni-tr>
					<uni-th align="center" width="150rpx">贴序</uni-th>
					<uni-th align="center" width="150rpx">应重(克)</uni-th>
					<uni-th align="center" width="150rpx">实重(克)</uni-th>
					<uni-th align="center" width="150rpx">误差(%))</uni-th>
				</uni-tr>
				<!-- 表格数据行 -->
				<uni-tr v-for="(item, index) in pageData.dpList" :key="index">
					<uni-td align="center">
						<uni-easyinput v-model="item.item" :clearable="false" :disabled="true"></uni-easyinput>
					</uni-td>
					<uni-td align="center" style="width: 200rpx;">
						<view style="display: flex;align-items: center;">
							<uni-easyinput v-model="item.totalScore" :clearable="false" :disabled="pageData.disabledStatus" type="digit"></uni-easyinput>
							<text>{{"(>"+(item.score!=null?item.score:0)+"g)"}}</text>
						</view>
					</uni-td>
					<uni-td align="center">
						<uni-easyinput v-model="item.score" :clearable="false" :disabled="pageData.disabledStatus" type="digit"></uni-easyinput>
					</uni-td>
					<uni-td align="center">
						<uni-easyinput v-model="item.resultContent" :clearable="false" :disabled="pageData.disabledStatus" type="digit"></uni-easyinput>
					</uni-td>
				</uni-tr>
				<!-- 均值 -->
				<uni-tr>
					<uni-td align="center">均值</uni-td>
					<uni-td align="center">{{avgYz()}}</uni-td>
					<uni-td align="center">{{avgSz()}}</uni-td>
					<uni-td align="center">{{avgWc()}}</uni-td>
				</uni-tr>
			</uni-table>
		</uni-card>

		<view class="dpTask_img">
			<uni-card>
				<uni-section title="图片上传" type="line">
				</uni-section>
				<view class="image_upload" style="border-top: 0.5px solid #ebedf0;margin-bottom: 3px;">
					<uni-file-picker limit="4" :modelValue="pageData.imageList" :readonly="pageData.imageStatus" @select="selectImg" @delete="deleteImg"></uni-file-picker>
				</view>
			</uni-card>
		</view>
		<!-- 下一个 -->
		<view class="finish_button" v-if="pageData.operatorStatus=='add'">
			<button class="button" @click="finishTask()">下一个</button>
		</view>
	</view>
</template>

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

const data = reactive({
	reciper: ''
})

const pageData = reactive({
	operatorStatus:'',
	cfbh:'',
	detailList: [],
	dpList:[],
	// 图片列表
	imageList: [],
	imageIdList:[],
	// 图片状态
	imageStatus:true,
	resData:{},
	id:'',
	// 总任务进度
	totalProgress:0,
	// 输入框状态
	disabledStatus:false
})

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

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

// 均值 应重
const avgYz=()=>{
	let num=0
	pageData.dpList.forEach((item,index)=>{
		num+=Number(item.totalScore)
	})
	return (num/pageData.dpList.length).toFixed(2)
}

// 均值 实重
const avgSz=()=>{
	let num=0
	pageData.dpList.forEach((item,index)=>{
		num+=Number(item.score)
	})
	return (num/pageData.dpList.length).toFixed(2)
}

// 均值 误差
const avgWc=()=>{
	let num=0
	pageData.dpList.forEach((item,index)=>{
		num+=Number(item.resultContent)
	})
	return (num/pageData.dpList.length).toFixed(2)
}

// 选中图片，将选中的图片添加列表
const selectImg=(e)=>{
	console.log(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.resData.detail.imageIds=[]
			pageData.imageIdList.push(e.id)
			pageData.resData.detail.imageIds=pageData.imageIdList.toString()
			pageData.imageList.push(item)
		})
	})
}

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

const countResultContent=()=>{
	pageData.resData.resultContent=0
	pageData.resData.resultContent=avgWc()
	console.log(pageData.resData.resultContent);
	pageData.resData.detail.reciper=data.reciper
}

// 下一个
const finishTask=()=>{
	countResultContent()
	uni.showLoading({
		title: '提交中....',
		mask: true
	});
	http.post("app-api/zkzy/save",pageData.resData).then(res=>{
		uni.hideLoading()
		if(res==true){
			uni.showToast({
				title: '已提交',
				icon:'success',
				success: () => {
					uni.navigateBack({
						delta:1
					})
				}
			});
		}else{
			uni.showToast({
				title: '提交失败',
				icon:'fail'
			});
		}
	})
}

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

// 获取代配质控作业
const getDpTask=(data)=>{
	formData.id=data
	http.get("app-api/taskDetail/get",formData).then(res=>{
		console.log(res)
		pageData.dpList = res.results
		pageData.resData=res
		pageData.totalProgress=res.results.length
	
		if(res.detail.imageIds!=null){
			pageData.imageList=showImage(res.detail.imageIds)
		}
	})
}

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

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

// 不合格数
const bhgNumber=()=>{
	let number=0
	if(pageData.resData.results!=null){
		pageData.resData.results.forEach(item=>{
			if(item.score<item.totalScore){
				number++
			}
		})
	}
	return number
}

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

const getDpTaskDetail=()=>{
	http.get("app-api/taskDetail/get",{id:pageData.id}).then(res=>{
		console.log(res)
		pageData.dpList = res.results
		pageData.resData.results=res.results
		pageData.totalProgress=res.results.length
		data.reciper=res.detail.reciper
		if(res.detail.imageIds!=null){
			pageData.imageList = uploadImageModelValue(res.detail.imageIds)
		}
	})
}

onShow(()=>{
	if(pageData.operatorStatus=='add'){
		pageData.imageStatus=false
		pageData.disabledStatus=false
		createTask()
	}else{
		pageData.imageStatus=true
		pageData.disabledStatus=true
		getDpTaskDetail()
	}
	
})

onLoad((type)=>{
	console.log(type)
	if(type.state!=null){
		pageData.operatorStatus=type.state
	}
	if(type.cfbh!=null){
		pageData.cfbh=type.cfbh
	}
	if(type.ptwybh!=null){
		createData.pId=type.ptwybh
		pageData.cfbh=type.ptwybh
	}
	if(type.fyfslx!=null){
		createData.providerType=type.fyfslx
	}
	if(type.id!=null){
		pageData.id=type.id
	}
})
</script>

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