<template>
  <div class="template-table">
    <component ref="templateComponent" :is="getTemplateComponent()" :templateData="templateData" :fillType="fillType">
    </component>
  </div>
</template>

<script>
import { EventBus } from "@/utils/eventBus"
//色谱
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: {
    //供试品
    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',
          '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) || {};
            console.log(this.fieldCheckObj,v.zdgxjl,"v.zdgxjl")
          }
        }
      }
    }
  },
  provide() {
    return {
      //分发给子组件的fillType
      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)
    })
  },
  beforeDestroy() {
    // 记得移除监听，避免内存泄漏
    EventBus.$off('onModifyRecord')
  },
  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>