<template>
  <div class="template-table">
    <component ref="templateComponent" :sn = "sn" :is="getTemplateComponent()" :templateData="templateData" :fillType="fillType">
    </component>
    <SubPackageDialog ref = "subPackageDialogRef"></SubPackageDialog>
    <TagPrintDialog ref = "tagPrintDialogRef"></TagPrintDialog>
    <SelectReagentDialog ref="selectReagentDialogRef"/>
    <SelectInstrumentDialog ref="selectInstrumentDialogRef"/>
    <SelectMixReagentDialog ref="selectMixReagentDialogRef"/>
  </div>
</template>

<script>
import { EventBus } from "@/utils/eventBus";

import SubPackageDialog from "./dialog/SubPackageDialog.vue";//分装弹窗
import TagPrintDialog from "./dialog/PrintTagDialog.vue";//标签打印弹窗
import SelectReagentDialog from "./dialog/SelectReagentDialog.vue";//选择试剂弹窗
import SelectInstrumentDialog from "./dialog/SelectInstrumentDialog.vue";//选择仪器弹窗
import SelectMixReagentDialog from "./dialog/SelectMixReagentDialog.vue";//选择试剂/供试品/给药制剂弹窗

//色谱
import SWYPFXRYPZB from "./comps/sp/SWYPFXRYPZB.vue";
import SWYPFXCBYPZB from "./comps/sp/SWYPFXCBYPZB.vue";
import SWYPBQGZYZBB from "./comps/sp/SWYPBQGZYZBB.vue";
import SWYPNBGZYZBB from "./comps/sp/SWYPNBGZYZBB.vue";
//色谱生物样品分析方法学样品制备表-10个类似
import ZQDYJMD from "./comps/sp/SWYPFXFFXYPZBB/ZQDYJMD.vue";
import QXWDX from "./comps/sp/SWYPFXFFXYPZBB/QXWDX.vue";
import CBYHGZYWDX from "./comps/sp/SWYPFXFFXYPZBB/CBYHGZYWDX.vue";
import Recovery from "./comps/sp/SWYPFXFFXYPZBB/Recovery.vue";
import RXJZXY from "./comps/sp/SWYPFXFFXYPZBB/RXJZXY.vue";
import JZXY from "./comps/sp/SWYPFXFFXYPZBB/JZXY.vue";
import XZXHTYX from "./comps/sp/SWYPFXFFXYPZBB/XZXHTYX.vue";
import ZDYBS from "./comps/sp/SWYPFXFFXYPZBB/ZDYBS.vue";
import XSKKX from "./comps/sp/SWYPFXFFXYPZBB/XSKKX.vue";
import CBYDB from "./comps/sp/SWYPFXFFXYPZBB/CBYDB.vue";
import SWYPFXFFXZKPZB from "./comps/sp/SWYPFXFFXZKPZB.vue";
import Demo from "./comps/sp/Demo.vue";
//供试品
import SYWZPZJHB from "./comps/gsp/SYWZPZJHB.vue";
import MJYLQSQD from "./comps/gsp/MJYLQSQD.vue";



export default {
  name: "TemplateTable",
  components: {
    SubPackageDialog,TagPrintDialog,SelectReagentDialog,SelectInstrumentDialog,SelectMixReagentDialog,
    //供试品
    MJYLQSQD, SYWZPZJHB,
    //色谱
    SWYPFXRYPZB, Demo, SWYPFXCBYPZB, SWYPBQGZYZBB, SWYPNBGZYZBB,ZQDYJMD,QXWDX,CBYHGZYWDX,Recovery,RXJZXY,JZXY,XZXHTYX,ZDYBS,XSKKX,CBYDB,SWYPFXFFXZKPZB
  },
  props: {
    sn: {
      type: String,
      default: '',
    },
    fillType: {
      type: String,
      default: 'preFill',
    },
    templateData: {
      type: Object,
      default: () => { },
    },
    emitName: {
      type: String,
      default: 'onModifyRecord',
    },
  },
  computed: {

    templateComponentMap() {
      if (!this.componentMap) {
        this.componentMap = {
          //色谱
          'SP001': 'SWYPFXRYPZB',
          'SP002': 'SWYPFXCBYPZB',
          'SP003': 'SWYPBQGZYZBB',
          'SP004': 'SWYPNBGZYZBB',
          'SP005': 'SWYPNBGZYZBB',
          'SP006': 'SWYPNBGZYZBB',
          'SP008': 'ZQDYJMD',
          'SP009': 'Recovery',
          'SP010': 'QXWDX',
          'SP011': 'CBYHGZYWDX',
          'SP012': 'RXJZXY',
          'SP013': 'JZXY',
          'SP014': 'XZXHTYX',
          'SP015': 'ZDYBS',
          'SP016': 'XSKKX',
          'SP017': 'CBYDB',
          'SP018': 'SWYPFXFFXZKPZB',
          //供试品
          'SYWZPZJHB': 'SYWZPZJHB',
          'MJYLQSQD': 'MJYLQSQD',
        }
      }
      return this.componentMap || "Demo"
    }
  },
  watch: {
    sn: {
      immediate: true,
      handler(v) {
        console.log(v, "sn")
      }
    },
    templateData: {
      immediate: true,
      deep: true,
      handler(v) {
        if (v) {
          if (v.zdxgjl) {
            this.zdxgjl = JSON.parse(v.zdxgjl) || [];
          }
          if (v.fhyjjl) {
            this.fhyjjl = JSON.parse(v.fhyjjl) || [];
          }
          if (v.zdgxjl) {
            this.fieldCheckObj = JSON.parse(v.zdgxjl) || {};
          }
        }
      }
    }
  },
  provide() {
    return {
      //分发给子组件的fillType
      templateData: this.templateData,
      templateFillType: this.fillType,
      getZdxgjl: () => this.zdxgjl ,
      getFhyjjl: () => this.fhyjjl,
      getFieldCheckObj: () => this.fieldCheckObj,
      //更新提交记录
      updateZdxgjl: (data) => {
        this.zdxgjl.unshift(data);
      },
      //更新复核意见记录
      updateFhyjjl: (data) => {
        this.fhyjjl.unshift(data);
      },
      //替换复核意见记录
      replaceFhyjjl: (data) => {
        this.fhyjjl = data;
      },
      //更新字段检查对象
      updateFieldCheckObj: (data) => {
        this.fieldCheckObj = { ...this.fieldCheckObj, ...data };
      },

    }
  },
  data() {
    return {
      info: {},
      zdxgjl: [],
      fhyjjl: [],
      fieldCheckObj: {},
    };
  },
  mounted() {
    EventBus.$on('onModifyRecord', (data) => {
      this.$emit(this.emitName, data)
    });
    //分装弹窗
    EventBus.$on("showSubPackageDialog",(data)=>{
      this.$refs.subPackageDialogRef.show(data)
    })
    //标签打印弹窗
    EventBus.$on("showTagPrintDialog",(data)=>{
      this.$refs.tagPrintDialogRef.show(data)
    })
    //选择仪器弹窗
    EventBus.$on("showSelectInstrumentDialog",(data)=>{
      this.$refs.selectInstrumentDialogRef.show(data.studyFormId,data)
    })
    //选择试剂弹窗
    EventBus.$on("showSelectReagentDialog",(data)=>{
      this.$refs.selectReagentDialogRef.show(data.studyFormId,data)
    })
    //选择试剂/供试品/给药制剂弹窗
    EventBus.$on("showSelectMixReagentDialog",(data)=>{
      this.$refs.selectMixReagentDialogRef.show(data.studyFormId,data)
    })
  },
  beforeDestroy() {
    // 记得移除监听，避免内存泄漏
    EventBus.$off('onModifyRecord');
    EventBus.$off("showSubPackageDialog");
    EventBus.$off("showTagPrintDialog");
    EventBus.$off("showSelectReagentDialog");
    EventBus.$off("showSelectInstrumentDialog");
    EventBus.$off("showSelectMixReagentDialog");
  },
  methods: {
    async getFormData() {
      if(this.fillType === "actFill"){
        //检查是否有未填写的复核意见
        const flag = this.fhyjjl.every((item)=>!!item.content && !!item.reply)
        console.log(this.fhyjjl,flag,"flag")
        if(!flag){
          this.$message.error("疑问项还未回复，请回复后再提交");
          return;
        }
      }
      return await this.$refs.templateComponent.getFormData();
    },
    getResource() {
      return this.$refs.templateComponent.getResource();
    },
    getFilledFormData() {
      return this.$refs.templateComponent.getFilledFormData();
    },
    getTemplateComponent() {
      return this.templateComponentMap[this.sn]
    },
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.template-table {
  background: #fff;
  padding: 10px 10px;
  width: 100%;
}
</style>