<template>
  <custom-modal
    v-model="visible"
    :width="1280"
    title="Mini Radionuclide Laboratory Reports"
    :destroyOnClose="true"
    @cancel="handleCancel"
  >
    <a-spin :spinning="isLoading">
      <div class="rlr">
        <div class="rlr-tabs">
          <div
            class="rlr-tabs-item"
            :class="activeKey == index ? 'active' : ''"
            v-for="(tab, index) in tabs"
            :key="index"
            @click="handleChangeView(index)"
          >
            {{ tab }}
          </div>
        </div>
        <div class="rlr-content">
          <h4>{{ tabs[activeKey] }}</h4>
          <div class="rlr-content-detail">
            <template v-if="activeKey == 0">
              <c-header :allData="allInfo" @valChange="getNewVal_header" />
            </template>
            <template v-if="activeKey == 1">
              <Objective :allData="allInfo" @valChange="getNewVal_obj" />
            </template>
            <template v-if="activeKey == 2">
              <collection :allData="allInfo" @valChange="getNewVal_collect" />
            </template>
            <template v-if="activeKey == 3">
              <sample-receipt :allData="allInfo" @valChange="getNewVal_receipt" />
            </template>
            <template v-if="activeKey == 4">
              <test :allData="allInfo" @valChange="getNewVal_test" />
            </template>
            <template v-if="activeKey == 5">
              <peaks-method :allData="allInfo" @valChange="getNewVal_peakMethod" />
            </template>
            <template v-if="activeKey == 6">
              <peak-fit :tableData="allInfo.peakFit" @valChange="getNewVal_peakFit" />
            </template>
            <template v-if="activeKey == 7">
              <g-analysis-methods :allData="allInfo" @valChange="getNewVal_analyMethod" />
            </template>
            <template v-if="activeKey == 8">
              <peak-association :tableData="allInfo.Association" @valChange="getNewVal_association" />
            </template>
            <template v-if="activeKey == 9">
              <references :allData="allInfo" @valChange="getNewVal_reference" />
            </template>
            <template v-if="activeKey == 10">
              <results :allData="allInfo" @valChange="getNewVal_result" @listChange="getNewList_result" />
            </template>
            <template v-if="activeKey == 11">
              <nuclide-ratios @valChange="getNewVal_coincidence" />
            </template>
            <template v-if="activeKey == 12">
              <g-coincidence-correction @valChange="getNewVal_nuclide" />
            </template>
            <template v-if="activeKey == 13">
              <mda @valChange="getNewVal_mda" />
            </template>
            <template v-if="activeKey == 14">
              <conclusions :allData="allInfo" @valChange="getNewVal_conclusion" />
            </template>
            <template v-if="activeKey == 15">
              <comment :allData="allInfo" @valChange="getNewVal_comment" />
            </template>
          </div>
        </div>
      </div>
    </a-spin>
    <div slot="custom-footer" style="text-align: center">
      <a-space :size="20">
        <a-button type="primary" @click="handleSave">Save</a-button>
        <a-button @click="handleCancel">Cancel</a-button>
      </a-space>
    </div>
  </custom-modal>
</template>

<script>
import ModalMixin from '@/mixins/ModalMixin'
import CHeader from './components/Header.vue'
import Objective from './components/Objective.vue'
import Collection from './components/Collection.vue'
import SampleReceipt from './components/SampleReceipt.vue'
import Test from './components/Test.vue'
import PeaksMethod from './components/PeaksMethod.vue'
import PeakFit from './components/PeakFit.vue'
import GAnalysisMethods from './components/g_AnalysisMethods.vue'
import PeakAssociation from './components/PeakAssociation.vue'
import References from './components/References.vue'
import Results from './components/Results.vue'
import NuclideRatios from './components/NuclideRatios.vue'
import GCoincidenceCorrection from './components/g_CoincidenceCorrection.vue'
import Mda from './components/MDA.vue'
import Conclusions from './components/Conclusions.vue'
import Comment from './components/Comment.vue'
import { getAction, postAction } from '@/api/manage'
import Custom from '@/views/account/settings/Custom.vue'
import SampleDataMixin from '@/views/spectrumAnalysis/SampleDataMixin'

const tabs = [
  'Header',
  'Objective',
  'Collection',
  'SampleReceipt',
  'Test',
  'PeaksMethod',
  'PeakFit',
  'g_AnalysisMethods',
  'PeakAssociation',
  'References',
  'Results',
  'NuclideRatios',
  'g_CoincidenceCorrection',
  'MDA',
  'Conclusions',
  'Comment',
]

export default {
  mixins: [ModalMixin, SampleDataMixin],
  components: {
    CHeader,
    Objective,
    Collection,
    SampleReceipt,
    Test,
    PeaksMethod,
    PeakFit,
    GAnalysisMethods,
    PeakAssociation,
    References,
    Results,
    NuclideRatios,
    GCoincidenceCorrection,
    Mda,
    Conclusions,
    Comment,
    Custom,
  },
  data() {
    this.tabs = tabs
    return {
      isLoading: false,
      activeKey: 0,
      allInfo: {},
      healderVal: {},
      objVal: {},
      collectVal: {},
      receiptVal: {},
      testVal: {},
      peakMethodVal: {},
      peakList: [],
      analyMethodVal: {},
      associationVal: [],
      referenceVal: {},
      resultVal: {},
      resultList: [],
      nuclideList: [],
      coincidenceList: [],
      mdaList: [],
      conclusionVal: {},
      commentVal: {},
      fileName: '',
    }
  },
  mounted() {
    sessionStorage.removeItem('isCellEmpty')
  },
  methods: {
    getNewVal_header(val) {
      this.healderVal = val
    },
    getNewVal_obj(val) {
      this.objVal = val
    },
    getNewVal_collect(val) {
      this.collectVal = val
    },
    getNewVal_receipt(val) {
      this.receiptVal = val
    },
    getNewVal_test(val) {
      this.testVal = val
    },
    getNewVal_peakMethod(val) {
      this.peakMethodVal = val
    },
    getNewVal_peakFit(val) {
      this.peakList = val
    },
    getNewVal_analyMethod(val) {
      this.analyMethodVal = val
    },
    getNewVal_association(val) {
      this.associationVal = val
    },
    getNewVal_reference(val) {
      this.referenceVal = val
    },
    getNewVal_result(val) {
      this.resultVal = val
    },
    getNewList_result(val) {
      this.resultList = val
    },
    getNewVal_nuclide(val) {
      this.nuclideList = val
    },
    getNewVal_coincidence(val) {
      this.coincidenceList = val
    },
    getNewVal_mda(val) {
      this.mdaList = val
    },
    getNewVal_conclusion(val) {
      this.conclusionVal = val
    },
    getNewVal_comment(val) {
      this.commentVal = val
    },
    handleSave() {
      let val = sessionStorage.getItem('isCellEmpty')
      if (!val || val == 'false') {
        // 下载保存文件
        let params = {
          header_msg_id: this.healderVal.msgId || this.allInfo.header_msg_id,
          header_data_type: this.healderVal.dataType || this.allInfo.header_data_type,
          header_priority_level: this.healderVal.priorityLevel || this.allInfo.header_priority_level,
          header_station_code: this.healderVal.stationCode || this.allInfo.header_station_code,
          header_srid: this.healderVal.srid || this.allInfo.header_srid,
          header_lab_code: this.healderVal.labCode || this.allInfo.header_lab_code,
          header_lab_detector: this.healderVal.labDetector || this.allInfo.header_lab_detector,
          header_report_type: this.healderVal.reportType || this.allInfo.header_report_type,
          header_report_number: this.healderVal.reportnumber || this.allInfo.header_report_number,
          header_sample_category: this.healderVal.sampleCategory || this.allInfo.header_sample_category,
          header_transmission: this.healderVal.transmission || this.allInfo.header_transmission,
          Obj_purpose: this.objVal.objPurpose || this.allInfo.Obj_purpose,
          Obj_authorized: this.objVal.objAuthorized || this.allInfo.Obj_authorized,
          Obj_instruction: this.objVal.objInstruction || this.allInfo.Obj_instruction,
          collect_start: this.collectVal.collectStart || this.allInfo.collect_start,
          collect_stop: this.collectVal.collectStop || this.allInfo.collect_stop,
          collect_airVolume: this.collectVal.collectAirVolume || this.allInfo.collect_airVolume,
          Receipt_srid: this.receiptVal.receiptSrid || this.allInfo.Receipt_srid,
          Receipt_sealNum: this.receiptVal.receiptSealNum || this.allInfo.Receipt_sealNum,
          Receipt_sample_dateTime: this.receiptVal.receiptDateTime || this.allInfo.Receipt_sample_dateTime,
          Receipt_package: this.receiptVal.receiptPackage || this.allInfo.Receipt_package,
          Receipt_seal: this.receiptVal.receiptSeal || this.allInfo.Receipt_seal,
          Receipt_sample: this.receiptVal.receiptSample || this.allInfo.Receipt_sample,
          Test_type: this.testVal.testType || this.allInfo.Test_type,
          Test_completion: this.testVal.testCompletion || this.allInfo.Test_completion,
          Test_person: this.testVal.testPerson || this.allInfo.Test_person,
          Test_purpose: this.testVal.testPurpose || this.allInfo.Test_purpose,
          PeakMethod_software: this.peakMethodVal.peakMethodSoftware || this.allInfo.PeakMethod_software,
          PeakMethod_location: this.peakMethodVal.peakMethodLocation || this.allInfo.PeakMethod_location,
          peakFit: this.peakList.length > 0 ? this.peakList : this.allInfo.peakFit,
          AnalyMethod_software: this.analyMethodVal.analyMethodSoftware || this.allInfo.AnalyMethod_software,
          AnalyMethod_nuclide: this.analyMethodVal.analyMethodNuclide || this.allInfo.AnalyMethod_nuclide,
          AnalyMethod_baseline: this.analyMethodVal.analyMethodBaseline || this.allInfo.AnalyMethod_baseline,
          AnalyMethod_lc: this.analyMethodVal.analyMethodLc || this.allInfo.AnalyMethod_lc,
          AnalyMethod_calib: this.analyMethodVal.analyMethodCalib || this.allInfo.AnalyMethod_calib,
          Association: this.associationVal.length > 0 ? this.associationVal : this.allInfo.Association,
          Reference_samplePHD: this.referenceVal.referenceSamplePHD || this.allInfo.Reference_samplePHD,
          Reference_CalibPHD: this.referenceVal.referenceCalibPHD || this.allInfo.Reference_CalibPHD,
          Reference_physical: this.referenceVal.referencePhysical || this.allInfo.Reference_physical,
          Result_act_ref: this.resultVal.activityReferenceTime || this.allInfo.Result_act_ref,
          Result_conc_ref: this.resultVal.Concentration || this.allInfo.Result_conc_ref,
          Result: this.resultList.length > 0 ? this.resultList : this.allInfo.Result,
          NuclideRatios: this.nuclideList,
          g_CoincidenceCorrection: this.coincidenceList,
          mda: this.mdaList,
          conclusion_person: this.conclusionVal.conclusionPerson || this.allInfo.conclusion_person,
          Conclusion_IDC: this.conclusionVal.conclusionIDC || this.allInfo.Conclusion_IDC,
          Conclusion_Lab: this.conclusionVal.conclusionLab || this.allInfo.Conclusion_Lab,
          Conclusion_Res: this.conclusionVal.conclusionRes || this.allInfo.Conclusion_Res,
          Comment: this.commentVal.comment || this.allInfo.Comment,
        }
        let name = this.newSampleData.inputFileName.split('.')[0]
        postAction('/gamma/exportRLR', params).then((res) => {
          const blob = new Blob([res])
          saveAs(blob, `${name}_RLR`)
          // let _this = this
          // this.$confirm({
          //   title: 'Please enter file name',
          //   content: (h) => <a-input v-model={_this.fileName} />,
          //   okText: 'Cancle',
          //   cancelText: 'Save',
          //   okButtonProps: { style: { backgroundColor: '#b98326', color: '#fff', borderColor: 'transparent' } },
          //   cancelButtonProps: { style: { color: '#fff', backgroundColor: '#31aab0', borderColor: 'transparent' } },
          //   onOk() {
          //     console.log('Cancel')
          //   },
          //   onCancel() {
          //     if (_this.fileName) {
          //       saveAs(blob, `${_this.fileName}.RLR`)
          //     }
          //   },
          // })
        })
      } else {
        this.$message.warning('Please finishi ti edit new inserted row first.')
      }
    },
    handleCancel() {
      this.visible = false
      sessionStorage.removeItem('isCellEmpty')
    },
    handleChangeView(index) {
      let val = sessionStorage.getItem('isCellEmpty')
      if (val == 'false' || !val) {
        this.activeKey = index
      } else {
        this.$message.warning('Please finishi ti edit new inserted row first.')
      }
    },
    beforeModalOpen() {
      this.activeKey = 0
      this.getGammaViewRLR()
    },
    getGammaViewRLR() {
      this.isLoading = true
      const { sampleId, inputFileName: fileName } = this.sampleData
      let params = {
        sampleId,
        fileName,
      }
      getAction('/gamma/viewRLR', params).then((res) => {
        this.isLoading = false
        if (res.success) {
          this.allInfo = res.result
        } else {
          this.$message.warning('This operation fails. Contact your system administrator')
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.rlr {
  display: flex;

  &-tabs {
    width: 170px;
    background-color: #275466;
    padding: 3px 0;

    &-item {
      cursor: pointer;
      user-select: none;
      padding: 0 5px;
      height: 32px;
      line-height: 32px;

      &.active {
        background-color: #296d81;
      }
    }
  }

  &-content {
    margin-left: 20px;
    flex: 1;

    h4 {
      color: #fff;
    }

    &-detail {
      height: 600px;
      padding: 20px;
      overflow: auto;
      background-color: #082f3e;
    }
  }
}
/deep/.ant-modal-title {
  letter-spacing: 1px;
}
</style>
