<!-- 配体结合分析处理记录表 -->
<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" />
					
					<LineLabel label="template.lba.lba006.fxxx" />

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

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

						<div class="template-form-item">
							<div class="config-header-end" v-if="fillType === 'actFill'">
								<el-button type="danger" plain @click="deleteParalleConfig(paralleIndex)">{{ $t('template.lba.lba004.sc')}}</el-button>
							</div>
							<BaseInfoFormPackage @clickable="(e) => handleClickable('paralle', paralleIndex, e)"
								:ref="`paralleStepFormPackageRef_${paralleIndex}`" :formConfig="paralleStepFormConfig"
								:formData="paralleConfig" :prefixKey="'paralle' + paralleConfig.uuid"
								fieldItemLabel="template.lba.lba006.fxxx" />
						</div>
					</div>
					
					<BaseInfoFormPackage fieldItemLabel="template.common.remark" label="template.common.remark"
						ref="remarkRef" :formConfig="remarkConig" :formData="formData" />
				</div>
			</div>
		</div>
		<!-- <button @click="onSave">保存</button> -->
		<EditSign ref="editSignLAB006Ref" @cancel="signCancel" @callback="signCallback" />
	</div>
</template>

<script>
import EditSign from "../../dialog/EditSign.vue";//电子签名弹窗
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,getuuid } from "@/utils/index.js";
import { debounce } from 'lodash-es';
import CustomTable from '@/components/Template/CustomTable.vue';
import TableOpertaionDelete from "@/components/Template/operation/TableOpertaionDelete.vue"
import moment from "moment";

export default {
	name: "LBA006",
	components: { BaseInfoFormPackage, LineLabel, TableList, Step,CustomTable,TableOpertaionDelete,EditSign },
	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,
						},
						methodCode: {
							label: 'template.common.methodCode',
							type: "input",
							fillType: "actFill",
							maxlength: 50
						},
						versionNum: {
							label: 'template.common.versionNumber',
							type: "inputNumber",
							fillType: "actFill",
							prepend: "V",
							maxlength: 50
						},
					}
				},
				{
					type: "conditionItem",
					label: 'template.lba.lba006.fxtj',
					config: {
						act: {
							label: 'template.common.actualFill',
							type: "select",
							fillType: "actFill",
							otherCode: "actOther",
							multiple: true,
							options: this.getDictOptions('business_pztj')
						}
					}
				},
				{
					type: "cellItem",
					label: 'template.lba.lba006.fxsj',
					config: {
						startDate: {
							label: 'template.common.startTime',
							type: "input",
						},
						endDate: {
							label: 'template.common.endTime',
							type: "input",
						},
					}
				}
			]
		},
		paralleStepFormConfig(){
			return [
				{
					type: 'step',
					config: {
						jcb: {
							label: 'template.lba.lba006.jcb',
							type: 'jcb',
							fillType: 'actFill',
						},
						jg: {
							label: 'template.lba.lba006.jg',
							type: "select",
							fillType: "actFill",
							otherLabel: " ",
							otherCode: "jgOther",
							otherMaxlength: 100,
							multiple: false,
							options: this.getDictOptions('business_lba_jg')
						},
						sjwjm: {
							span:1,
							label: 'template.lba.lba006.sjwjm',
							type: 'input',
							fillType: 'actFill',
							maxlength: 100
						}
					}
				}
			]
		},
	},
	data() {
		return {
			formData: {},
			deleteIndex:-1
		};
	},
	mounted() {
		setTimeout(()=>{
			this.handleAddParalle(true);
		},300)
	},
	methods: {
		//获取已填写的表单数据
		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,
						uuid:this.formData.paralleConfigs[i].uuid
					});
				}
			}
			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();
			//使用的试剂、仪器
			this.resourceTmp =  []
			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,
					uuid:getuuid()
				});
			}
		},
		// 删除平行配制配置
		deleteParalleConfig(index) {
			if(this.formData.paralleConfigs.length===1){
				this.$message.error("至少需要1个");
          		return;
			}
			this.checkDelete(index)
			
		},
		checkDelete(index){
			let content = this.getFilledFormData()
			let thisItem = content.paralleConfigs[index]
			if(!thisItem.jcb && !thisItem.jg && !thisItem.sjwjm){
				this.doDeleteParalleConfig(index,null)
			}else{
				this.deleteIndex = index
				this.$refs.editSignLAB006Ref.show();
			}
		},
		// 处理电子签名取消事件
		signCancel() {
		},
		// 处理电子签名确认回调事件
		signCallback(data) {
			let content = this.getFilledFormData()
			let record = [
				{
					"userNameCn": data.qmrMc,
					"userNameEn": data.qmrMcEn,
					"key": "paralle",
					"fieldCn": this.$t('template.lba.lba006.fxxx','zh_CN'),
					"fieldEn": this.$t('template.lba.lba006.fxxx','en_US'),
					// "oldValue": content.paralleConfigs[this.deleteIndex].jcb+";"+content.paralleConfigs[this.deleteIndex].jg+";"+(content.paralleConfigs[this.deleteIndex].jg==='接受'?''(content.paralleConfigs[this.deleteIndex].jgOther+";"))+content.paralleConfigs[this.deleteIndex].sjwjm,
					"oldValue": content.paralleConfigs[this.deleteIndex].jcb+";"+content.paralleConfigs[this.deleteIndex].jg+";"+content.paralleConfigs[this.deleteIndex].jgOther+";"+content.paralleConfigs[this.deleteIndex].sjwjm,
					"value": "",
					"title": "修改",
					"time": moment().format("YYYY-MM-DD HH:mm:ss"),
					"reason": data.remark
				}
			]
			this.doDeleteParalleConfig(this.deleteIndex,record)
		},
		doDeleteParalleConfig(index,newRecord){
			this.formData.paralleConfigs.splice(index, 1);
			const params = {
				type: "fieldChanged",
				newRecord: newRecord,
				resourceList: null,
			}
			setTimeout(() => {
				EventBus.$emit('onModifyRecord', params,)
			}, 10);
		},
		// 删除表格行
		deleteRow(rowIndex, type, configIndex = 0) {
			// 根据类型删除对应的表格行
			if (type === 'ladder') {
				const tableRef = this.$refs[`ladderStepTableRef_${configIndex}`];
				if (tableRef) {
					tableRef.deleteRow(rowIndex);
				}
			} else if (type === 'paralle') {
				const tableRef = this.$refs[`paralleStepTableRef_${configIndex}`];
				if (tableRef) {
					tableRef[0].deleteRow(rowIndex);
				}
			}
		},
	}
};
</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>