<!-- 生物样品分析方法学样品制备表-Recovery -->
<template>
	<div>
		<div class="detail-container">
			<div class="detail-title"><img src="@/assets/images/detail-title.png">{{ formData.bdmc }}<img src="@/assets/images/detail-title.png" /></div>
			<div class="detail-content">
				<div class="content">
					<BaseInfoFormPackage fieldItemLabel="template.common.baseInfo" label="template.common.baseInfo"
						ref="baseInfoRef" :formConfig="baseInfoFormConfig" :formData="formData" />
					<TableList label="template.common.reagentInfo" :columns="sysjColumns" :dataSource="resource" />
					<TableList label="template.common.instrumentInfo" :columns="yqColumns" :dataSource="yqResource" />
					<BaseInfoFormPackage fieldItemLabel="template.common.storageCondition"
						label="template.common.storageCondition" ref="storageConditionRef"
						:formConfig="storageFormConfig" :formData="formData" />
					<LineLabel label="template.common.operationSteps" />
					<div class="template-form-item">
						<span>基质</span>
						<BaseInfoFormPackage fieldItemLabel="template.common.operationSteps" ref="stepFormPackageRef"
							:formConfig="stepFormConfig" @blur="onHandleBlur" :formData="formData" />

						<CustomTable @blur="onHandleTableBlur" :ref="`tableRef`" :columns="tableStepColumns" :showOperation="fillType === 'preFill'"
							:formData="formData" :prefixKey="`table`"
							fieldItemLabel="template.common.operationSteps">
							<template slot="operation" slot-scope="{ row, rowIndex, columns }">
								<TableOpertaion :fillType="fillType" :row="row" :rowIndex="rowIndex" :columns="columns"
									@deleteRow="(rowIndex) => deleteRow(rowIndex)"
									@subPackageSubmit="subPackageSubmit"
									@startConfig="startConfig"
									@configComplete="configComplete"
								></TableOpertaion>
							</template>
						</CustomTable>
					</div>
					<div class="template-form-item">
						<span>溶液</span>

						<BaseInfoFormPackage fieldItemLabel="template.common.operationSteps"
							ref="rongyeStepFormPackageRef" :formConfig="rongyeStepFormConfig" @blur="onHandleBlur"
							:formData="formData" />
						<CustomTable @blur="onHandleTableBlur" :ref="`rongyeTableRef`" :columns="rongyeTableStepColumns" :showOperation="fillType === 'preFill'"
							:formData="{stepTableFormData:formData.stepTableFormData_1,headerSelectFields:formData.headerSelectFields_1}" :prefixKey="`rongyeTable`"
							fieldItemLabel="template.common.operationSteps">
							<template slot="operation" slot-scope="{ row, rowIndex, columns }">
								<TableOpertaion :fillType="fillType" :row="row" :rowIndex="rowIndex" :columns="columns"
									@deleteRow="(rowIndex) => deleteRow(rowIndex)"
									@subPackageSubmit="subPackageSubmit"
									@startConfig="startConfig"
									@configComplete="configComplete"
								></TableOpertaion>
							</template>
						</CustomTable>
					</div>
					<Step ref="stepRef" :formData="formData.stepData"></Step>
					<BaseInfoFormPackage fieldItemLabel="template.common.remark" label="template.common.remark"
						ref="remarkRef" :formConfig="remarkConig" :formData="formData" />

				</div>
			</div>
		</div>
		<!-- <button @click = "onSave">保存</button> -->
	</div>
</template>

<script>
import BaseInfoFormPackage from "@/components/Template/BaseInfoFormPackage";
import LineLabel from "@/components/Template/LineLabel";
import TableList from "@/components/Template/Table";
import Step from "@/components/Template/Step";
import templateMixin from "../../mixins/templateMixin";
import CustomTable from '@/components/Template/CustomTable.vue';
import { getLatestSnArr } from '@/api/template';
import { getSWYPFXFFXYPZBBTableConfig, getSWYPFXFFXYPZBBTQHSLTableConfig } from "../../formConfig/SWYPFXFFXYPZBBTableConfig.js";
import TableOpertaion from "@/components/Template/operation/TableOpertaion.vue"
import { EventBus } from "@/utils/eventBus";
import { addTj,uniqeResource,uniqeResourceOne,addDecimals } from "@/utils/calUnitTools";

export default {
	name: "SWYPFXRYPZB",
	components: { BaseInfoFormPackage, LineLabel, TableList, Step, CustomTable, TableOpertaion },
	mixins: [templateMixin],
	props: {
		fillType: {
			type: String,
			default: 'preFill',
		},
	},
	computed: {
		storageFormConfig() {
			return [
				{
					type: "conditionItem",
					config: {
						storageCondition: {
							label: 'template.common.storageConditionLabel',
							type: "select",
							fillType: "preFill",
							options: this.getDictOptions("business_cctj"),
							otherCode: "storageConditionOther",
						},
					}
				}
			];
		},
		remarkConig() {
			return [
				{
					type: "cellItem",
					config: {
						remark: {
							label: "",
							type: "textarea",
							fillType: "actFill",
							span: 1,
							placeholder: 'template.common.remarkPlaceholder',
							maxlength: 1000,
							rows: 5
						}
					}
				}
			]
		},

		baseInfoFormConfig() {
			return [
				{
					type: "cardItem",
					config: {
						studyMc: {
							label: 'template.common.testName',
							type: "input",
							disabled: true,
						},
						studySn: {
							label: 'template.common.testNumber',
							type: "input",
							disabled: true,
						},
						methodCode: {
							label: 'template.common.methodCode',
							type: "input",
							fillType: "preFill",
							maxlength: 50
						},
						versionNum: {
							label: 'template.common.versionNumber',
							type: "inputNumber",
							fillType: "actFill",
							prepend: "V",
							maxlength: 50
						},
						//处理批编号
						clpbg: {
							label: 'template.sp.sp009.clpbh',
							type: "input",
							fillType: "actFill",
							maxlength: 50
						},

					}
				},
				{
					type: "conditionItem",
					label: 'template.common.testConfigurationConditions',
					config: {
						pre: {
							label: 'template.common.preFill',
							type: "select",
							multiple: true,
							fillType: "preFill",
							options: this.getDictOptions('business_pztj'),
							otherCode: "preOther",
						},
						act: {
							label: 'template.common.actualFill',
							type: "select",
							fillType: "actFill",
							otherCode: "actOther",
							compareTo: "pre",
							multiple: true,
							options: this.getDictOptions('business_pztj')
						}
					}
				},
				{
					type: "cellItem",
					label: 'template.sp.sp009.rqcz',
					config: {
						rqcz: {
							type: "select",
							multiple: true,
							fillType: "actFill",
							options: this.getDictOptions('business_rqcz'),
							otherCode: "rqczOther",
						},
					}
				},
				{
					type: "cellItem",
					label: 'template.sp.sp009.clsj',
					config: {
						startDate: {
							label: 'template.common.startTime',
							type: "input",
						},
						endDate: {
							label: 'template.common.endTime',
							type: "input",
						},
					}
				}
			]
		},
		stepFormConfig() {
			return [
				{
					type: "step",
					config: {
						jz: {
							label: 'template.sp.sp008.xzjz',
							type: "input",
							fillType: "preFill",
							subType: "sj",
							subKey: "subJz",
							subFillType: "actFill",
							maxlength: 20,
							labelWidth: 80,
						},
					}
				}
			]
		},
		rongyeStepFormConfig() {
			return [
				{
					type: "step",
					config: {
						gzy: {
							label: 'template.sp.sp009.xzgzy',
							type: "input",
							fillType: "preFill",
							subType: "sj",
							subKey: "subGzy",
							subFillType: "actFill",
							maxlength: 20,
							labelWidth: 80,
						},
					}
				},
				{
					type: "step",
					config: {
						xsy: {
							label: 'template.sp.sp009.xzxsy',
							type: "input",
							fillType: "preFill",
							subType: "sj",
							subKey: "subXsy",
							subFillType: "actFill",
							maxlength: 20,
							labelWidth: 80,
						},
					}
				},
			]
		},
		tableStepColumns() {
			return getSWYPFXFFXYPZBBTableConfig(this, 'business_sp_tqhsl', false);
		},
		rongyeTableStepColumns() {
			return getSWYPFXFFXYPZBBTQHSLTableConfig(this, 'business_sp_tqhsl');
		},
	},
	data() {
		return {
			reagentType: '',
			resourceData1: [],
			formData: {}
		};
	},
	mounted() {
		const formData = this.getFormDataByTemplateData();
		if (this.fillType === "actFill") {
			this.getCode(formData);
		}
	},
	methods: {
		//获取目标溶液编号
		async getCode(v) {
			// 基质表格
			const {stepTableFormData = []} = v;
			if(stepTableFormData && stepTableFormData.length>0 && !stepTableFormData[0].bhCode){
				let postSn = []
				for(let i=0;i<stepTableFormData.length;i++){
					postSn.push({
						pre:stepTableFormData[i].bh,
						type:1
					})
				}
				const result = await getLatestSnArr(postSn)
				if(result.code == 200){
					for(let i=0;i<stepTableFormData.length;i++){
						this.$refs.tableRef.updateDataSourceByRowIndex(i,{
							bhCode: result.data[i],
						})
					}

					const params = {
						type: "fieldChanged",
						newRecord: null,
						resourceList: null,
					}
					setTimeout(() => {
						EventBus.$emit('onModifyRecord', params,)
					}, 10);
				}
			}
			// 溶液表格
			const {stepTableFormData_1 = []} = v;
			if(stepTableFormData_1 && stepTableFormData_1.length>0 && !stepTableFormData_1[0].bhCode){
				let postSn = []
				for(let i=0;i<stepTableFormData_1.length;i++){
					postSn.push({
						pre:stepTableFormData_1[i].bh,
						type:1
					})
				}
				const result = await getLatestSnArr(postSn)
				if(result.code == 200){
					for(let i=0;i<stepTableFormData_1.length;i++){
						this.$refs.rongyeTableRef.updateDataSourceByRowIndex(i,{
							bhCode: result.data[i],
						})
					}

					const params = {
						type: "fieldChanged",
						newRecord: null,
						resourceList: null,
					}
					setTimeout(() => {
						EventBus.$emit('onModifyRecord', params,)
					}, 10);
				}
			}
		},
		//获取已填写的表单数据
		getFilledFormData() {
			return this.getFilledFormDataByRefs(["baseInfoRef", "storageConditionRef", "stepFormPackageRef", "tableRef", "stepRef", "rongyeTableRef", "rongyeStepFormPackageRef", "remarkRef"])
		},
		async getFormData() {
			let content = await this.validFormFields(["baseInfoRef", "storageConditionRef", "stepFormPackageRef", "tableRef", "stepRef",
			 "rongyeTableRef", "rongyeStepFormPackageRef", "remarkRef"]);
			return content;
		},
		getResource() {
			let content = this.getFilledFormData();
			//生成resource
			let tmpResource = []
			if(this.fillType === "actFill"){
				let tableList = content.stepTableFormData
				if(tableList && tableList.length>0){
					for(let i=0;i<tableList.length;i++){
						//配置的试剂
						let item = tableList[i]
						console.log('表格item:'+JSON.stringify(item))

						//使用的  实际溶液
						tmpResource.push({
							mc: null,
							bh: item.sjry,
							ph: null,
							ndz: null,
							nd: null,
							nddw: null,
							ly: 'ELN配制',
							sxrq: null,
							kc: null,
							kcdw: null,
							syl: item.sjryxql,
							type: 1,
							elnType: this.product,
							syldw:content.headerSelectFields.sjryxqlUnit,
							yxzq:null,
							yxzqdw:null,
						})
						// 基质吸取量
						tmpResource.push({
							mc: null,
							bh: content.subJz,
							ph: null,
							ndz: null,
							nd: null,
							nddw: null,
							ly: 'ELN配制',
							sxrq: null,
							kc: null,
							kcdw: null,
							syl: item.sjjzxql,
							type: 1,
							elnType: this.product,
							syldw:content.headerSelectFields.sjjzxqlUnit,
							yxzq:null,
							yxzqdw:null,
						})
						//分装的
						let fzTotal = 0//分装总量
						if(item.fzsj && item.fzsj.fzList && item.fzsj.fzList.length>0){
							for(let j=0;j<item.fzsj.fzList.length;j++){
								let fzItem = item.fzsj.fzList[j]
								tmpResource.push({
									mc: null,
									bh: item.fzsj.mybh+fzItem.preCode+ fzItem.subCode,
									ph: null,
									ndz: item.hhwznd + content.headerSelectFields.hhwzndUnit,
									nd: item.hhwznd,
									nddw: content.headerSelectFields.hhwzndUnit,
									ly: 'ELN配制',
									sxrq: null,
									kc: fzItem.num,
									kcdw: item.fzsj.dw,
									syl: null,
									type: null,
									elnType: this.product,
									syldw:item.fzsj.dw,
									yxzq:null,
									yxzqdw:null,
								})
								fzTotal=addDecimals(fzTotal,fzItem.num)
							}
						}
 						//生成的
						const { total, unit } = addTj([item.sjjzxql, item.sjryxql], [content.headerSelectFields.sjjzxqlUnit, content.headerSelectFields.sjryxqlUnit])

						tmpResource.push({
							mc: null,
							bh: item.bh+item.bhCode,
							ph: null,
							ndz: item.hhwznd + content.headerSelectFields.hhwzndUnit,
							nd: item.hhwznd,
							nddw: content.headerSelectFields.hhwzndUnit,
							ly: 'ELN配制',
							sxrq: null,
							kc: total,
							kcdw: unit,
							syl: fzTotal>0?fzTotal:null,
							type: null,
							elnType: this.product,
							syldw:fzTotal>0?item.fzsj.dw:unit,
							yxzq:null,
							yxzqdw:null,
						})
					}
				}

				tableList = content.stepTableFormData_1
				if(tableList && tableList.length>0){
					for(let i=0;i<tableList.length;i++){
						//配置的试剂
						let item = tableList[i]
						console.log('表格item:'+JSON.stringify(item))

						//使用的  实际溶液
						tmpResource.push({
							mc: null,
							bh: item.sjry,
							ph: null,
							ndz: null,
							nd: null,
							nddw: null,
							ly: 'ELN配制',
							sxrq: null,
							kc: null,
							kcdw: null,
							syl: item.sjryxql,
							type: 1,
							elnType: this.product,
							syldw:content.headerSelectFields.sjryxqlUnit,
							yxzq:null,
							yxzqdw:null,
						})
						// 工作液
						tmpResource.push({
							mc: null,
							bh: content.subGzy,
							ph: null,
							ndz: null,
							nd: null,
							nddw: null,
							ly: 'ELN配制',
							sxrq: null,
							kc: null,
							kcdw: null,
							syl: item.sjgzyxql,
							type: 1,
							elnType: this.product,
							syldw:content.headerSelectFields.sjgzyxqlUnit,
							yxzq:null,
							yxzqdw:null,
						})
						// 稀释液
						tmpResource.push({
							mc: null,
							bh: content.subXsy,
							ph: null,
							ndz: null,
							nd: null,
							nddw: null,
							ly: 'ELN配制',
							sxrq: null,
							kc: null,
							kcdw: null,
							syl: item.sjxsyxql,
							type: 1,
							elnType: this.product,
							syldw:content.headerSelectFields.sjxsyxqlUnit,
							yxzq:null,
							yxzqdw:null,
						})
						//分装的
						let fzTotal = 0//分装总量
						if(item.fzsj && item.fzsj.fzList && item.fzsj.fzList.length>0){
							for(let j=0;j<item.fzsj.fzList.length;j++){
								let fzItem = item.fzsj.fzList[j]
								tmpResource.push({
									mc: null,
									bh: item.fzsj.mybh+fzItem.preCode + fzItem.subCode,
									ph: null,
									ndz: item.hhwznd + content.headerSelectFields.hhwzndUnit,
									nd: item.hhwznd,
									nddw: content.headerSelectFields.hhwzndUnit,
									ly: 'ELN配制',
									sxrq: null,
									kc: fzItem.num,
									kcdw: item.fzsj.dw,
									syl: null,
									type: null,
									elnType: this.product,
									syldw:item.fzsj.dw,
									yxzq:null,
									yxzqdw:null,
								})
								fzTotal=addDecimals(fzTotal,fzItem.num)
							}
						}
						// 生成的
						const { total, unit } = addTj([item.sjryxql, item.sjgzyxql,item.sjxsyxql], [content.headerSelectFields_1.sjryxqlUnit, content.headerSelectFields_1.sjgzyxqlUnit,content.headerSelectFields_1.sjxsyxqlUnit])
						tmpResource.push({
							mc: null,
							bh: item.bh+item.bhCode,
							ph: null,
							ndz: item.hhwznd + content.headerSelectFields_1.hhwzndUnit,
							nd: item.hhwznd,
							nddw: content.headerSelectFields_1.hhwzndUnit,
							ly: 'ELN配制',
							sxrq: null,
							kc: total,
							kcdw: unit,
							syl: fzTotal>0?fzTotal:null,
							type: null,
							elnType: this.product,
							syldw:fzTotal>0?item.fzsj.dw:unit,
							yxzq:null,
							yxzqdw:null,
						})
					}
				}
			}
			if(tmpResource.length>0){
				tmpResource = uniqeResourceOne(tmpResource)
			}
			//使用的试剂、仪器
			const stepResource = this.$refs.stepRef.getStepResource()
			this.resourceTmp =uniqeResource(tmpResource,stepResource.sjResource||[])
			this.yqResourceTmp = stepResource.yqResource||[]
			return this.resourceTmp;
		},
		async onSave() {
			const formData = this.getFilledFormData();
			// const formData = await this.validFormFields(["rongyeTableRef", "tableRef"])

			console.log(formData, "formData")
		},
		// 删除表格行
		deleteRow(rowIndex) {
			const tableRef = this.$refs['tableRef'];
			if (tableRef) {
				tableRef.deleteRow(rowIndex);
			}
		},
		subPackageSubmit(val){
			console.log("分装数据："+JSON.stringify(val))
			if(val.fzsj.fzList && val.fzsj.fzList.length>0){
				let list = []
				for(let i=0;i<val.fzsj.fzList.length;i++){
					let item = val.fzsj.fzList[i]
					list.push({
						bh:val.fzsj.mybh+item.preCode+ item.subCode,
						kc:item.num,
						kcdw:val.fzsj.dw,
					})
				}
				let postData = {
					studyId:this.formData.studyId,
					studyFormId:this.formData.id,
					studySubjectId: this.formData.studySubjectId,
					bh:val.fzsj.mybh,
					nd:val.rowData.hhwznd,
					nddw:val.headerSelectFields.hhwzndUnit,
					list:list
				}
				console.log("分装提交数据："+JSON.stringify(postData))
				this.subPackageRequest(postData)
			}
		},
		startConfig(val){
			console.log("开始配置数据："+JSON.stringify(val))
			let postData = {
				bh:val.rowData.bh+val.rowData.bhCode,
				studyId:this.formData.studyId,
				studyFormId:this.formData.id,
				studySubjectId: this.formData.studySubjectId,
			}
			console.log("开始配置提交数据："+JSON.stringify(postData))
			this.startConfigRequest(postData);
		},
		configComplete(val){
			console.log("完成配置数据："+JSON.stringify(val))
			let amount = {
				total:0,
				unit:''
			}
			if(val.headerSelectFields.sjgzyxqlUnit === undefined){
				if(val.rowData.sjjzxql==='' || val.rowData.sjjzxql===undefined || val.rowData.sjryxql==='' || val.rowData.sjryxql===undefined){
					this.$message.error("表格内容未填完")
					return
				}
				amount = addTj([val.rowData.sjjzxql, val.rowData.sjryxql], [val.headerSelectFields.sjjzxqlUnit, val.headerSelectFields.sjryxqlUnit])
			}else{
				if(val.rowData.sjryxql==='' || val.rowData.sjryxql===undefined || val.rowData.sjgzyxql==='' || val.rowData.sjgzyxql===undefined || val.rowData.sjxsyxql==='' || val.rowData.sjxsyxql===undefined){
					this.$message.error("表格内容未填完")
					return
				}
				amount = addTj([val.rowData.sjryxql, val.rowData.sjgzyxql,val.rowData.sjxsyxql], [val.headerSelectFields.sjryxqlUnit, val.headerSelectFields.sjgzyxqlUnit,val.headerSelectFields.sjxsyxqlUnit])
			}
			let postData = {
				mc:null,
				studyId:this.formData.studyId,
				studyFormId:this.formData.id,
				studySubjectId: this.formData.studySubjectId,
				bh:val.rowData.bh+val.rowData.bhCode,
				nd:val.rowData.hhwznd,
				nddw:val.headerSelectFields.hhwzndUnit,
				kc: amount.total,
				kcdw: amount.unit
			}
			this.configCompleteRequest(postData);
		},

	}
};
</script>
<style rel="stylesheet/scss" lang="scss">
.mt-20 {
	margin-top: 20px;
}
</style>