<!-- 配体结合分析处理记录表 -->
<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.gspsywzInfo" :columns="gspColumns" :dataSource="resourceGsp" />
					<TableList label="template.common.sjInfo" :columns="sjColumns" :dataSource="resourceSj" />
					<TableList label="template.common.instrumentInfo" :columns="yqColumns" :dataSource="yqResource" />
					
					<LineLabel :label="sn=='GSP009'?'template.gsp.gsp009.xtsyx':'template.gsp.gsp011.cl'" />

					<div v-if="fillType === 'preFill'" class="mt-20">
						<el-button type="primary" @click="handleAddParalle(false)">{{  $t('template.lba.lba004.xz')
							}}</el-button>
					</div>

					<!-- 平行配制区域 -->
					<div :label="$t('template.common.pxpz')"
						v-for="(paralleConfig, paralleIndex) in formData.paralleConfigs"
						:key="'paralle_' + paralleIndex">

						<div class="template-form-item">
							<div class="config-header-end" v-if="fillType === 'preFill'">
								<el-button type="danger" plain @click="deleteParalleConfig(paralleIndex)">{{ $t('template.lba.lba004.sc')}}</el-button>
							</div>
							<BaseInfoFormPackage @clickable="(e) => handleClickable('paralle', paralleIndex, e)"
							@beforeReagentSubmit="(data) => onTableBeforeReagentSubmit(data, paralleIndex, paralleConfig)"
							@onRegentSubmit="(data) => onTableRegentSubmit('paralle', paralleIndex, data)"

								:ref="`paralleStepFormPackageRef_${paralleIndex}`" :formConfig="paralleStepFormConfig"
								:formData="paralleConfig" :prefixKey="'paralle' + paralleIndex"
								:fieldItemLabel="$t('template.lba.lba004.xszj')" />
						</div>
					</div>
					
					<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 { EventBus } from "@/utils/eventBus";
import { uniqeResource,uniqeResourceOne } from "@/utils/calUnitTools";
import { duplicateResource } from "@/utils/index.js";
import { debounce } from 'lodash-es';
import CustomTable from '@/components/Template/CustomTable.vue';
import TableOpertaionDelete from "@/components/Template/operation/TableOpertaionDelete.vue"

export default {
	name: "GSP009",
	components: { BaseInfoFormPackage, LineLabel, TableList, Step,CustomTable,TableOpertaionDelete },
	mixins: [templateMixin],
	props: {
		fillType: {
			type: String,
			default: 'preFill',
		},
		sn: {
			type: String,
			default: '',
		},
	},
	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: "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",
							multiple: true,
							compareTo: "pre",
							options: this.getDictOptions('business_pztj')
						}
					}
				},
				{
					type: "cellItem",
					label: 'template.common.configurationTime',
					config: {
						startDate: {
							label: 'template.common.startTime',
							type: "input",
						},
						endDate: {
							label: 'template.common.endTime',
							type: "input",
						},
					}
				}
			]
		},
		paralleStepFormConfig(){
			return [
				{
					type: 'step',
					config: {
						yp: {
							label: 'template.gsp.gsp009.ypbh',
							type: "input",
							fillType: "preFill",
							subType: "span",
							subKey: "ypBh",
							subFillType: "actFill",
							maxlength: 20,
						},
						xz: {
							label: 'template.gsp.gsp009.xz',
							type: "mix",
							fillType: "actFill",
							subType:"text",
							subText:this.sn=='GSP009'?'template.gsp.gsp009.zw':'template.gsp.gsp011.zw'
						},
					}
				}
			]
		},
	},
	data() {
		return {
			formData: {}
		};
	},
	mounted() {
		this.handleAddParalle(true);
		const formData = this.getFormDataByTemplateData();
		if(this.fillType === "actFill"){
			this.getCode(formData);
		}
	},
	methods: {
		//获取目标溶液编号
		async getCode(content){
			if (content.paralleConfigs && content.paralleConfigs.length > 0 && !content.paralleConfigs[0].ypBh) {
				const codes = content.paralleConfigs.map((item) => ({ pre: item.yp, type: 1 }));
				const snList = await this.getLatestSnArr(codes);
				content.paralleConfigs.forEach((row, idx) => {
					this.$refs[`paralleStepFormPackageRef_${idx}`][0].updateFormData('ypBh',snList[idx])
				});
				const params = {
					type: "fieldChanged",
					newRecord: null,
					resourceList: null,
				}
				setTimeout(() => {
					EventBus.$emit('onModifyRecord', params,)
				}, 10);
			}
		},
		//获取已填写的表单数据
		getFilledFormData() {
			const baseData = this.$refs.baseInfoRef.getFilledFormData();
			const remarkData = this.$refs.remarkRef.getFilledFormData();

			// 获取所有平行配制数据
			const paralleConfigsData = [];
			if (this.formData.paralleConfigs && this.formData.paralleConfigs.length > 0) {
				for (let i = 0; i < this.formData.paralleConfigs.length; i++) {
					const paralleFormData = this.$refs[`paralleStepFormPackageRef_${i}`][0].getFilledFormData();

					paralleConfigsData.push({
						...paralleFormData,
						showParalleConfig: true
					});
				}
			}
			return {
				...baseData,
				...remarkData,
				paralleConfigs: paralleConfigsData,
			}
		},
		//获取填写完成的表单数据
		async getFormData() {
			//先校验再获取值
			const validFlag = await this.validFields();
			if (!validFlag) {
				return false;
			}
			let content = this.getFilledFormData();
			return content
		},
		//只做校验
		async validFields() {
			// 构建需要验证的引用数组
			let refsToValidate = ["baseInfoRef", "remarkRef"];

			// 添加平行配制相关的引用
			if (this.formData.paralleConfigs && this.formData.paralleConfigs.length > 0) {
				for (let i = 0; i < this.formData.paralleConfigs.length; i++) {
					refsToValidate.push(`paralleStepFormPackageRef_${i}`);
				}
			}
			return await this.validFormFields(refsToValidate);
		},
		getResource() {
			let content = this.getFilledFormData();
			//生成resource
			let tmpResource = []
			if (this.fillType === "actFill") {
				//平行配置
				if (content.paralleConfigs && content.paralleConfigs.length > 0) {
					for (let i = 0; i < content.paralleConfigs.length; i++) {
						let paralleConfigs = content.paralleConfigs[i]
						//使用量 
						tmpResource.push({
							mc: null,
							bh: paralleConfigs.xz,
							ph: null,
							nd: null,
							nddw: null,
							ndz: null,
							ly: null,
							sxrq: null,
							kc: null,
							kcdw: null,
							type: paralleConfigs.xzlx,
							syl: null,
							syldw: null,
							yxzq: null,
							yxzqdw: null,
						})
					}
				}
			}
			if (tmpResource.length > 0) {
				tmpResource = uniqeResourceOne(tmpResource)
			}
			tmpResource.forEach((row, idx) => {
				row.syldw = null
				row.syl = null
			});

			//使用的试剂、仪器
			this.resourceTmp = tmpResource
			this.yqResourceTmp = []
			return this.resourceTmp;
		},
		//保存
		async onSave() {
			const formData = this.getStepResource();
			console.log(formData, "formData")
		},
		//新增平行配制
		handleAddParalle(init) {
			let isAdd = !init
			if(init && (!this.formData.paralleConfigs || this.formData.paralleConfigs.length===0)){
				isAdd = true
			}
			if(isAdd){
				// 初始化平行配制配置列表
				if (!this.formData.paralleConfigs) {
					this.$set(this.formData, 'paralleConfigs', []);
				}
				// 添加新的平行配制配置
				this.formData.paralleConfigs.push({
					showParalleConfig: true
				});
			}
			//todo: 需要回调保存？
		},
		// 删除平行配制配置
		deleteParalleConfig(index) {
			if(this.formData.paralleConfigs.length===1){
				this.$message.error("至少需要1个");
          		return;
			}
			this.formData.paralleConfigs.splice(index, 1);
		},
		onTableBeforeReagentSubmit(val1,va2,va3){
			debugger
		},
		onTableRegentSubmit(val1,va2,va3){
			this.$refs[`paralleStepFormPackageRef_${va2}`][0].updateFormData('xzlx',va3.selectInfo.selectType)
		}
	}
};
</script>
<style rel="stylesheet/scss" lang="scss">
.mt-20 {
	margin-top: 20px;
}

.print-btn {
	margin-bottom: 20px;
}
.config-header-end {
	display: flex;
	justify-content: flex-end;
	align-items: center;
	margin-bottom: 15px;
	font-weight: bold;
	font-size: 16px;
	color: #303133;
}
</style>