<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="检验项目" @clickLeft="back">
		</uni-nav-bar>

		<view class="wrapper">
			<view class="herder_item">
				<view class="herder_text"></view>
				<view class="herder_view" style="font-size: 32rpx;">
					检验信息（{{form.status==1?'已检':form.status==2?'已派单':form.status==3?'已请托':'待检'}}）

				</view>
			</view>
			<text class="marginTop20 tag">
				{{form.inspectionName}}
			</text>
			<view class="marginTop20 workValue">
				{{workData.productName}}/{{workData.code}}/{{workData.productCode}}
			</view>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					执行标准
				</view>
			</view>
			<u--input suffixIcon="arrow-right" style="background-color:#fff ;" :disabledColor="'#ffffff'"
				:disabled="true" class="marginTop20" placeholder=" " border="surround"
				:value="getDictValue('质检标准类型', form.qualityStandardType+'')"></u--input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					工艺参数
				</view>
			</view>
			<view class="marginTop20 defValue">
				{{form.defaultValue}}
			</view>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					工单剩余样品数
				</view>
			</view>
			<view class="marginTop20 ">
				<u-input disabled class="marginTop20" placeholder=" " border="surround"
					v-model="workData.remainingSampleCount">
					<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}
					</template>
				</u-input>
			</view>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					质检内容
				</view>
			</view>
			<u-input :disabled='form.status==1' @change="handleInput(form, form.qualityResultContent)"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.qualityResultContent">
				<template slot="suffix" v-if="form.unitName">{{form.unitName}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					样品数
				</view>
			</view>
			<u-input :disabled='form.status==1' @change="inputValue('sampleQuantity')" type="number" class="marginTop20"
				placeholder="请输入内容" border="surround" v-model="form.sampleQuantity">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					合格数
				</view>
			</view>
			<u-input :disabled='form.status==1' type="number" @change="inputValue('qualifiedQuantity')"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.qualifiedQuantity">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					不合格数
				</view>
			</view>
			<u-input :disabled='form.status==1' type="number" @change="inputValue('noQualifiedQuantity')"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.noQualifiedQuantity">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					损耗数（合格品）
				</view>
			</view>
			<u-input :disabled='form.status==1' @change="inputValue('lossNumber')" type="number" class="marginTop20"
				placeholder="请输入内容" border="surround" v-model="form.lossNumber">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					损耗数（不合格品）
				</view>
			</view>
			<u-input :disabled='form.status==1' @change="inputValue('lossNumberUnqualified')" type="number"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.lossNumberUnqualified">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					留样数（合格品）
				</view>
			</view>
			<u-input :disabled='form.status==1' @change="inputValue('retainedSampleQuantity')" type="number"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.retainedSampleQuantity">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					留样数（不合格品）
				</view>
			</view>
			<u-input :disabled='form.status==1' type="number" @change="inputValue('retainedSampleUnqualified')"
				class="marginTop20" placeholder="请输入内容" border="surround" v-model="form.retainedSampleUnqualified">
				<template slot="suffix" v-if="form.measureUnit">{{form.measureUnit}}</template>
			</u-input>
			<view class="herder_item marginTop20">
				<view class="herder_text"></view>
				<view class="herder_view">
					质检结果
				</view>
			</view>
			<uni-data-picker v-if='form.status!=1' class="marginTop20" v-model="form.qualityResults" placeholder="请选择"
				:localdata="acceptUnpackList">
			</uni-data-picker>
			<u-input disabled v-else class="marginTop20" placeholder="请输入内容" border="surround"
				:value="acceptUnpackList.find(item=>item.value==form.qualityResults).text">
			</u-input>

		</view>

		<view class="footerButton">
			<u-button type="default" text="返回" @click="back"></u-button>
			<u-button v-if="form.status!=1" type="primary" @click="exeReportWorkBySingleTemplate" text="保存"></u-button>
		</view>
		<view style="width:100%;height: 100rpx;"></view>
		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
	import dictMixns from '@/mixins/dictMixins'
	import {
		getById,
		exeReportWorkBySingleTemplate
	} from '@/api/inspectionWork/index.js'
	import {
		handleInput
	} from './unit.js';

	export default {
		components: {},
		mixins: [dictMixns],
		data() {
			return {
				handleInput,
				show: false,
				form: {
					lossNumber: '',
					lossNumberUnqualified: '',
					retainedSampleQuantity: '',
					retainedSampleUnqualified: '',
					qualifiedQuantity:'',
					noQualifiedQuantity:'',
					sampleQuantity:'',
				},
				acceptUnpackList: [{
						text: "合格",
						value: 1
					},
					{
						text: "不合格",
						value: 2
					}, {
						text: "让步接收",
						value: 3
					},
				],

				workData: {}
			}
		},
		computed: {

		},
		onLoad(data) {
			this.requestDict('质检计划类型');
			this.requestDict('取样类型');
			this.requestDict('质检标准类型');
			if (data) {
				getById(data.workId).then(res => {
					this.workData = res
					this.$set(this,'form',res.templateList.find(item => item.id == data.projectId))
					this.form.measureUnit = res.qualitySampleList[0]?.measureUnit

				})
			}

		},

		onShow() {

		},
		methods: {

			exeReportWorkBySingleTemplate() {
				// let params = JSON.parse(JSON.stringify(this.workData))
				// let index = params.templateList.findIndex(item => item.id == this.form.id)
				// params.templateList[index] = this.form
				// params.
				if (!this.form.qualityResultContent) {
					this.$refs.uToast.show({
						type: "error",
						icon: false,
						message: "请填写质检内容！",
					}, )
					return
				}
				if (!this.form.sampleQuantity) {
					this.$refs.uToast.show({
						type: "error",
						icon: false,
						message: "请填写样品数！",
					}, )
					return
				}
				this.form.status = 1
				exeReportWorkBySingleTemplate({
						id: this.workData.id,
						planTemplateList: [this.form]
					})
					.then((msg) => {
						this.back()
					})
					.catch((e) => {
						this.back()
					});
			},
			valueInit(data) {
				['lossNumber', 'lossNumberUnqualified', 'retainedSampleQuantity', 'retainedSampleUnqualified'].forEach(
					key => {
						data[key] = ''
					})
			},

			inputValue(type) {
				let data = JSON.parse(JSON.stringify((this.form)))
				if (data[type] < 0) {
					data[type] = '';
				}
				//样品数
				if (type == 'sampleQuantity') {
					if (+data[type] > this.workData.remainingSampleCount) {
						data[type] = 0
						this.$refs.uToast.show({
							type: "error",
							icon: false,
							message: "样品数不能大于工单剩余样品数",
						}, )
					}
					data.qualifiedQuantity = data[type]
					data.noQualifiedQuantity = 0
					this.valueInit(data)
				}
				//样品合格数、不合格数
				if (type == 'qualifiedQuantity' || type == 'noQualifiedQuantity') {
					if (+data[type] > data.sampleQuantity) {
						data[type] = 0
						this.$refs.uToast.show({
							type: "error",
							icon: false,
							message: (type == 'qualifiedQuantity' ? '合格' : '不合格') + "数不能大于样品数",
						}, )
					}
					let key = type == 'qualifiedQuantity' ? 'noQualifiedQuantity' : 'qualifiedQuantity'
					data[key] = data.sampleQuantity - data[type]
					this.valueInit(data)
				}
				//损耗数 留样数
				if (['lossNumber', 'lossNumberUnqualified', 'retainedSampleQuantity', 'retainedSampleUnqualified']
					.includes(type)) {
					if (type == 'lossNumber' || type == 'retainedSampleQuantity') {
						if (Number(data.lossNumber) + Number(data.retainedSampleQuantity) > data.qualifiedQuantity) {
							this.$refs.uToast.show({
								type: "error",
								icon: false,
								message: "损耗合格数与留样合格数之和不能大于样品合格数",
							}, )
							data[type] = ''
						}

					} else {
						if (Number(data.lossNumberUnqualified) + Number(data.retainedSampleUnqualified) > data
							.noQualifiedQuantity) {
							this.$refs.uToast.show({
								type: "error",
								icon: false,
								message: "损耗不合格数与留样不合格数之和不能大于样品不合格数",
							}, )
							data[type] = ''
						}
					}
				}
				this.$nextTick(() => {
					this.$set(this, 'form', JSON.parse(JSON.stringify((data))))
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.mainBox {
		background-color: #f3f8fb;
		height: 100vh;
		font-size: 27rpx;
		overflow-y: auto;
	}

	.wrapper {
		width: 720rpx;
		background: #fff;
		margin: 20rpx auto 0;
		border-radius: 30rpx;
		padding: 26rpx;
	}

	.footerButton {
		width: 100%;
		height: 84rpx;
		display: flex;
		position: fixed;
		bottom: 0;
		z-index: 10;
		background-color: #fff;

		/deep/.u-button {
			height: 100%;
		}

		>view {
			flex: 1;
		}
	}

	.workValue {
		word-break: break-all;
		font-weight: bold;

	}

	.tag {
		// font-size: 24rpx;
		color: #fff;
		padding: 12rpx;
		border-radius: 10rpx;
		background: #979797;
		display: inline-block;

	}

	/deep/.input-value {
		font-size: 28rpx !important;
	}

	.defValue {
		color: #c1353c;
		text-indent: 2ch;
	}

	.marginTop20 {
		margin-top: 30rpx;
	}


	.herder_item {
		display: flex;

		font-weight: bold;
		// font-size: 26rpx;

		.herder_text {

			min-width: 10rpx;
			height: 32rpx;
			border-radius: 10rpx;
			background: #00c0a1;
			margin-right: 12rpx;
			margin-top: 5rpx;
		}
	}
</style>