<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" />
    <EditSign ref="editSignRef" @cancel="handleEditSignCancel" @callback="handleEditSignCallback" />
  </div>
</template>

<script>
import { EventBus } from "@/utils/eventBus";
import { debounce } from 'lodash-es'
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 EditSign from "./dialog/EditSign.vue";//电子签名弹窗

//色谱
import SP001 from "./comps/sp/SP001.vue";
import SP003 from "./comps/sp/SP003.vue";
import SP00456 from "./comps/sp/SP00456.vue";
import SP007 from "./comps/sp/SP007.vue";
//色谱生物样品分析方法学样品制备表-10个类似
import SP008 from "./comps/sp/SWYPFXFFXYPZBB/SP008.vue";
import SP009 from "./comps/sp/SWYPFXFFXYPZBB/SP009.vue";
import SP010 from "./comps/sp/SWYPFXFFXYPZBB/SP010.vue";
import SP011 from "./comps/sp/SWYPFXFFXYPZBB/SP011.vue";
import SP012 from "./comps/sp/SWYPFXFFXYPZBB/SP012.vue";
import SP013 from "./comps/sp/SWYPFXFFXYPZBB/SP013.vue";
import SP014 from "./comps/sp/SWYPFXFFXYPZBB/SP014.vue";
import SP015 from "./comps/sp/SWYPFXFFXYPZBB/SP015.vue";
import SP016 from "./comps/sp/SWYPFXFFXYPZBB/SP016.vue";
import SP017 from "./comps/sp/SWYPFXFFXYPZBB/SP017.vue";
import SP018 from "./comps/sp/SWYPFXFFXYPZBB/SP018.vue";
import Demo from "./comps/sp/Demo.vue";
//供试品
import GSP002 from "./comps/gsp/GSP002.vue";
import GSP003 from "./comps/gsp/GSP003.vue";
import GSP004 from "./comps/gsp/GSP004.vue";
import GSP010 from "./comps/gsp/GSP010.vue";

//试验物质配制计划表/麻精药领取申请单/毒麻药品配制记录表
import SYWZPZJHB from "./comps/gsp/SYWZPZJHB.vue";
import MJYLQSQD from "./comps/gsp/MJYLQSQD.vue";
import DMYPPZJLB from "./comps/gsp/DMYPPZJLB.vue";

// PCR
//PCR001-使用SP001
import PCR002 from "./comps/pcr/PCR002.vue";
import PCR003 from "./comps/pcr/PCR003.vue";
import PCR004 from "./comps/pcr/PCR004.vue";

//LBA
// LBA001-使用SP001
import LBA002 from "./comps/lba/LBA002.vue";
import LBA003 from "./comps/lba/LBA003.vue";
import LBA004 from "./comps/lba/LBA004.vue";
import LBA006 from "./comps/lba/LBA006.vue";

//ADA
// ADA001-使用SP001
// ADA002-使用LBA002
// ADA003-使用LBA003
// ADA004-使用LBA004
// ADA006-使用LBA006

//样品
// YP001-使用SP001

export default {
  name: "TemplateTable",
  components: {
    Demo,
    SubPackageDialog, TagPrintDialog, SelectReagentDialog, SelectInstrumentDialog, SelectMixReagentDialog, EditSign,
    //试验物质配制计划表/麻精药领取申请单/毒麻药品配制记录表
    MJYLQSQD, SYWZPZJHB, DMYPPZJLB,
    //供试品
    GSP002, GSP003, GSP004, GSP010,
    //色谱
    SP001, SP003, SP00456,SP007, SP008, SP009, SP010, SP011, SP012, SP013, SP014, SP015, SP016, SP017, SP018,
    // PCR
    PCR002,
    //LBA
    LBA002,
    LBA003,
    LBA004,
    LBA006
  },
  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': 'SP001',
          'SP002': 'SP001',
          'SP003': 'SP003',
          'SP004': 'SP00456',
          'SP005': 'SP00456',
          'SP006': 'SP00456',
          'SP007': 'SP007',
          'SP008': 'SP008',
          'SP009': 'SP009',
          'SP010': 'SP010',
          'SP011': 'SP011',
          'SP012': 'SP012',
          'SP013': 'SP013',
          'SP014': 'SP014',
          'SP015': 'SP015',
          'SP016': 'SP016',
          'SP017': 'SP017',
          'SP018': 'SP018',
          //供试品
          
          'GSP001': 'DMYPPZJLB',
          'GSP002': 'GSP002',
          'GSP003': 'GSP003',
          'GSP004': 'GSP004',
          'GSP006': 'GSP002',
          'GSP008': 'GSP002',
          'GSP010': 'GSP010',
          'GSP012': 'GSP002',
          'GSP013': 'GSP002',
          'GSP014': 'GSP002',
          //试验物质配制计划表/麻精药领取申请单/毒麻药品配制记录表
          'SYWZPZJHB': 'SYWZPZJHB',
          'MJYLQSQD': 'MJYLQSQD',
          'DMYPPZJLB': 'DMYPPZJLB',
          //PCR
          'PCR001': 'SP001',
          'PCR002': 'PCR002',
          'PCR003': 'PCR003',
          'PCR004': 'PCR004',
          //LBA
          'LBA001': 'SP001',
          'LBA002': 'LBA002',
          'LBA003': 'LBA003',
          'LBA004': 'LBA004',
          'LBA006': 'LBA006',
          //ADA
          'ADA001': 'SP001',
          'ADA002': 'LBA002',
          'ADA003': 'LBA003',
          'ADA004': 'LBA004',
          'ADA006': 'LBA006',
          //样品
          'YP001': 'SP001',
        }
      }
      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: {},
      currentEditSignUuid: null, // 当前请求EditSign的HandleFormItem的uuid
      latestParams: {},//用于存储最新的params
      hasCustomTable: false,//是否有自定义表格
    };
  },
  created() {
    this.debouncedEmit = debounce(this.emitToParent, 100)
  },
  mounted() {
    EventBus.$on('onModifyRecord', this.handleModyfyRecord);
    //分装弹窗
    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)
    })
    EventBus.$on("hideSelectMixReagentDialog", (data) => {
      this.$refs.selectMixReagentDialogRef.hide()
    })
    //显示电子签名弹窗
    EventBus.$on("showEditSignDialog", (data) => {
      this.currentEditSignUuid = data.uuid;
      this.$refs.editSignRef.show();
    })
  },
  beforeDestroy() {
    // 记得移除监听，避免内存泄漏
    EventBus.$off('onModifyRecord');
    EventBus.$off("showSubPackageDialog");
    EventBus.$off("showTagPrintDialog");
    EventBus.$off("showSelectReagentDialog");
    EventBus.$off("showSelectInstrumentDialog");
    EventBus.$off("showSelectMixReagentDialog");
    EventBus.$off("showEditSignDialog");
    EventBus.$off("hideSelectMixReagentDialog");
    this.debouncedEmit.cancel()
  },
  methods: {
    handleModyfyRecord(data) {
      const { source } = data;
      if (source === "customTable") {
        this.latestParams = data;
        // delete this.latestParams.source
        this.hasCustomTable = true
        // 取消之前的等待
        this.debouncedEmit.cancel()
        this.emitToParent()
      }else if(!source  && !this.hasCustomTable){
        this.latestParams = data
        this.debouncedEmit()
      }
    },
    emitToParent() {
      if (this.latestParams) {
        console.log(this.latestParams,"this.latestParams")
        this.$emit(this.emitName, this.latestParams)
      }
    },
    // 处理电子签名取消事件
    handleEditSignCancel() {
      if (this.currentEditSignUuid) {
        EventBus.$emit('onEditSignCancel', { uuid: this.currentEditSignUuid });
        EventBus.$emit('onFormEditSignCancel', { uuid: this.currentEditSignUuid });
        this.currentEditSignUuid = null;
      }
    },
    // 处理电子签名确认回调事件
    handleEditSignCallback(data) {
      if (this.currentEditSignUuid) {
        EventBus.$emit('onEditSignCallback', { uuid: this.currentEditSignUuid, data });
        this.currentEditSignUuid = null;
      }
    },
    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();
    },
    getYqResource() {
      return this.$refs.templateComponent.getYqResource();
    },
    getJcbList() {
      return this.$refs.templateComponent.getJcbList();
    },
    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>