<!-- 生物分析生物样品采集管制备表 -->
<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.instrumentInfo" :columns="yqColumns" :dataSource="yqResource" />

          <LineLabel label="template.dl.dl021.gcqk" />
					<CustomTable
						:ref="`tableRef`"
						:columns="tableColumns"
						:formData="formData"
						operationWidth = "80px"
						:prefixKey = "`table`"
						fieldItemLabel = "template.dl.dl021.gcqk"
						@clickButton="handleClickButton"
						@onCheckboxTagChange="onCheckboxTagChange"
						:showOperation="fillType === 'preFill'"
					>
						<template slot="operation" slot-scope="{ row, rowIndex, columns }">
							<TableOpertaionDelete  :row="row" :rowIndex="rowIndex" :columns="columns"  @deleteRow="deleteRow" ></TableOpertaionDelete>
						</template>
					</CustomTable>

					<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 { EventBus } from "@/utils/eventBus";
import { uniqeResource } from "@/utils/calUnitTools";
import { debounce } from 'lodash-es'
import TableOpertaionDelete from "@/components/Template/operation/TableOpertaionDelete.vue"

export default {
	name: "DL021",
	components: { BaseInfoFormPackage, LineLabel, TableList, Step, CustomTable,TableOpertaionDelete },
	mixins: [templateMixin],
	props: {
		fillType: {
			type: String,
			default: 'preFill',
		},
	},
	computed: {
		// 备注表单配置
		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,
						},
					}
				},
				{
					type: "cellItem",
					label: 'template.dl.dl021.czsj',
					config: {
						startDate: {
							label: 'template.common.startTime',
							type: "input",
						},
						endDate: {
							label: 'template.common.endTime',
							type: "input",
						},
					}
				}
			]
		},
    // 样本信息
		tableColumns() {
			return [{
				label: 'template.dl.dl021.bpbh',
				prop: 'cyd',
				bodyType: 'input',
				bodyFillType: 'actFill',
				width: 100,
				bodyMaxlength: 50,
			},{
				label: 'template.dl.dl021.gcxps',
				prop: 'cyd',
				bodyType: 'input',
				bodyFillType: 'actFill',
				width: 100,
        bodyMaxlength: 10,
			},{
				label: 'template.dl.dl021.zqxxbs',
				label: 'template.dl.dl021.gcxps',
				prop: 'cyd',
				bodyType: 'input',
				bodyFillType: 'actFill',
				width: 80,
        bodyMaxlength: 10,
			}]
		},

	},
	data() {
		return {
			formData: {},
		};
	},
	mounted() {
	},
	methods: {
    //选择table header下拉框也要更新体积
		onHeaderSelectChange(data){
			const {key, headerSelectFields,dataSource=[]} = data;
			const keys = [
				'targetStartSolutionVolumeUnit',
				'targetDiluentVolumeUnit',
				'targetSolutionConcentrationUnit',
				'targetSolutionVolumeUnit',
			]
			if(keys.includes(key)){
				const {targetStartSolution,subTargetStartSolution} = this.$refs.swypyjInfoRef?.getFilledFormData();
				const params = {
					subTargetStartSolution,
					headerSelectFields
				}
				this.batchUpdateTargetStartSolutionVolume(dataSource,targetStartSolution,params)
			}
		},
    beforeSaveRecord(data){
			const formFields = this.$refs.swypyjInfoRef?.getFilledFormData();
			this.onCommonVerifyNdException(formFields,data);
		},
    configComplete(val) {
			const { rowData, headerSelectFields } = val;
			//计算实际目标溶液体积(实际起始溶液体积+实际稀释液体积)
			const { total, unit } = addTj([rowData.actStartSolutionVolume, rowData.actDiluentVolume], [headerSelectFields.actStartSolutionVolumeUnit, headerSelectFields.actDiluentVolumeUnit])
			let postData = {
				mc: null,
				bh: rowData.targetSolutionCode + rowData.subTargetSolutionCode,
				nd: rowData.actSolutionConcentration,//实际目标溶液浓度
				nddw: headerSelectFields.actSolutionConcentrationUnit,
				studySubjectId: this.formData.studySubjectId,
				studyFormId:this.formData.id,
				studyId:this.formData.studyId,
				kc: total,
				kcdw: unit,
			}
			this.configCompleteRequest(postData);
		},
    //分装回调
		subPackageSubmit(data) {
			const { fzsj, rowData, headerSelectFields } = data;
			const { fzList = [], dw = "", mybh } = fzsj;
			if (fzList && fzList.length > 0) {
				const list = fzList.map((item) => {
					return {
						bh: mybh + item.preCode + item.subCode,
						kc: item.num,
						kcdw: dw,
					}
				})
				let postData = {
					studyId: this.formData.studyId,
					studyFormId: this.formData.id,
					bh: mybh,
					nd: rowData.actSolutionConcentration || 0,
					nddw: headerSelectFields.actSolutionConcentrationUnit,
					studySubjectId: this.formData.studySubjectId,
			     	studyFormId:this.formData.id,
					list: list
				}
				this.subPackageRequest(postData);
			}
		},
		//获取已填写的表单数据
		getFilledFormData() {
			return this.getFilledFormDataByRefs(["baseInfoRef",  "swypyjInfoRef", "stepTableRef", "stepRef","remarkRef"])
		},
		//获取填写完成的表单数据
		async getFormData() {
			let content = await this.validFormFields(["baseInfoRef",  "swypyjInfoRef", "stepTableRef", "stepRef","remarkRef"]);
			return content;
		},
		getResource() {
			//使用的试剂、仪器
			const stepResource = this.$refs.stepRef.getStepResource()
			this.resourceTmp = stepResource.sjResource || []
			this.yqResourceTmp = stepResource.yqResource || []
			return this.resourceTmp;
		},
		//保存
		async onSave() {
			let content = await this.$refs.stepRef.getFormData();
			console.log(content);
		},
		// 删除表格行
		// deleteRow(rowIndex) {
		// 	const tableRef = this.$refs['tableRef'];
		// 	if (tableRef) {
		// 		tableRef.deleteRow(rowIndex);
		// 	}
		// },
	}
};
</script>
<style rel="stylesheet/scss" lang="scss">
</style>
