<template>
  <div class="spectrum-analysis">
    <!-- 顶部操作栏 -->
    <div class="spectrum-analysis-operators">
      <a-dropdown
        class="spectrum-analysis-operators-item"
        overlayClassName="spectrum-analysis-operators-dropdown-overlay"
        :overlay-style="operation.style"
        v-for="operation in operations"
        :key="operation.title"
      >
        <a-button type="primary">{{ operation.title }}</a-button>
        <div slot="overlay">
          <template v-for="(child, index) in operation.children">
            <component :is="child.type" :key="index" v-bind="child.attrs" v-on="child.on">
              <component :is="item.type" v-for="item in child.children" :key="item.title" @click="item.handler">
                {{ item.title }}
              </component>
            </component>
          </template>
        </div>
      </a-dropdown>
    </div>
    <!-- 顶部操作栏结束 -->

    <!-- 频谱分析部分 -->
    <div class="spectrum-analysis-main">
      <gamma-analysis v-if="analysisType == ANALYZE_TYPE.GAMMA" ref="gammaAnalysisRef" />
      <beta-gamma-analysis
        v-if="analysisType == ANALYZE_TYPE.BETA_GAMMA"
        ref="betaGammaAnalysisRef"
        :sample="sampleData"
      />
      <resize-observer @notify="handleResize" />
    </div>
    <!-- 频谱分析部分结束 -->

    <!-- 从数据库加载开始 -->
    <load-from-db-modal v-model="loadFromDbModalVisible" @loadSample="handleLoadSampleFromDB" />
    <!-- 从数据库加载结束 -->

    <!-- 从文件加载开始 -->
    <load-from-file-modal v-model="loadFromFileModalVisible" />
    <!-- 从文件加载结束 -->

    <!-- Peak Infomation 弹窗开始 -->
    <peak-infomation v-model="peakInfomationModalVisible" />
    <!-- Peak Infomation 弹窗结束 -->

    <!-- Nuclide Activity and MDC 弹窗开始 -->
    <nuclide-activity-and-mdc-modal v-model="nuclideActivityAndMDCModalVisible" />
    <!-- Nuclide Activity and MDC 弹窗结束 -->

    <!-- Save Setting 弹窗开始 -->
    <save-setting-modal v-model="saveSettingModalVisible" />
    <!-- Save Setting 弹窗结束 -->

    <!-- 分析-设置弹窗开始 -->
    <analyze-setting-modal v-model="analyzeConfigureModalVisible" />
    <!-- 分析-设置弹窗结束 -->

    <!-- 分析工具弹窗开始 -->
    <analyze-interactive-tool-modal v-model="analyzeInteractiveToolModalVisible" />
    <!-- 分析工具弹窗结束 -->

    <!-- Korsum 弹窗开始 -->
    <korsum-modal v-model="korsumModalShow" />
    <!-- Korsum 弹窗结束 -->

    <!-- ReProcessing 弹窗开始 -->
    <re-processing-modal v-model="reprocessingModalVisible" />
    <!-- ReProcessing 弹窗结束 -->

    <!-- Zero Time 弹窗开始 -->
    <zero-time-modal v-model="zeroTimeModalVisible" />
    <!-- Zero Time 弹窗结束 -->

    <!-- Efficiency Calibration 弹窗开始 -->
    <efficiency-calibration-modal v-model="efficiencyCalibrationModalShow" />
    <!-- Efficiency Calibration 弹窗结束 -->

    <!-- Energy Calibration 弹窗开始 -->
    <energy-calibration-modal v-model="energyCalibrationModalShow" />
    <!-- Energy Calibration 弹窗结束 -->

    <!-- Resolution Calibration 弹窗开始 -->
    <resolution-calibration-modal v-model="resolutionCalibrationModalShow" />
    <!-- Resolution Calibration 弹窗结束 -->

    <!-- SpectrumComments 弹窗开始 -->
    <spectrum-comments-modal v-model="spectrumCommentsModalVisible" :isAdd="isSpectrumCommentsAdd" />
    <!-- SpectrumComments 弹窗结束 -->

    <!-- Color Config 弹窗开始 -->
    <color-config-modal v-model="colorConfigModalVisible" />
    <!-- Color Config 弹窗结束 -->

    <!-- Data Processing Log 弹窗开始 -->
    <data-processing-log-modal v-model="dataProcessingLogModalVisible" />
    <!-- Data Processing Log 弹窗结束 -->

    <!-- Config User Library 弹窗开始 -->
    <config-user-library-modal v-model="configUserLibModalVisible" />
    <!-- Config User Library 弹窗结束 -->

    <!-- Config User Library 弹窗开始 -->
    <nuclide-library-modal v-model="nuclideLibraryModalVisible" />
    <!-- Config User Library 弹窗结束 -->
  </div>
</template>
<script>
import GammaAnalysis from './gamma-analysis.vue'
import BetaGammaAnalysis from './beta-gamma-analysis.vue'
import SpectraListInMenu from './components/SpectraListInMenu.vue'
import LoadFromDbModal from './components/Modals/LoadFromDBModal.vue'
import LoadFromFileModal from './components/Modals/LoadFromFileModal.vue'
import PeakInfomation from './components/PeakInfomation.vue'
import NuclideActivityAndMdcModal from './components/Modals/NuclideActivityAndMDCModal.vue'
import MultiLevelMenu from './components/MultiLevelMenu.vue'
import SaveSettingModal from './components/Modals/SaveSettingModal.vue'
import AnalyzeSettingModal from './components/Modals/AnalyzeSettingModal.vue'
import AnalyzeInteractiveToolModal from './components/Modals/AnalyzeInteractiveToolModal/index.vue'
import KorsumModal from './components/Modals/KorsumModal.vue'
import ReProcessingModal from './components/Modals/ReProcessingModal/index.vue'
import ZeroTimeModal from './components/Modals/ZeroTimeModal.vue'
import EfficiencyCalibrationModal from './components/Modals/EfficiencyCalibrationModal.vue'
import EnergyCalibrationModal from './components/Modals/EnergyCalibrationModal.vue'
import ResolutionCalibrationModal from './components/Modals/ResolutionCalibrationModal.vue'
import SpectrumCommentsModal from './components/Modals/SpectrumCommentsModal.vue'
import ColorConfigModal from './components/Modals/ColorConfigModal.vue'
import DataProcessingLogModal from './components/Modals/DataProcessingLogModal.vue'
import ConfigUserLibraryModal from './components/Modals/ConfigUserLibraryModal.vue'
import NuclideLibraryModal from './components/Modals/NuclideLibraryModal.vue'

// 分析类型
const ANALYZE_TYPE = {
  GAMMA: 'gammaAnalysis',
  BETA_GAMMA: 'betaGammaAnalysis'
}
export default {
  components: {
    BetaGammaAnalysis,
    GammaAnalysis,
    SpectraListInMenu,
    LoadFromDbModal,
    LoadFromFileModal,
    PeakInfomation,
    NuclideActivityAndMdcModal,
    MultiLevelMenu,
    SaveSettingModal,
    AnalyzeSettingModal,
    AnalyzeInteractiveToolModal,
    KorsumModal,
    ReProcessingModal,
    ZeroTimeModal,
    EfficiencyCalibrationModal,
    EnergyCalibrationModal,
    ResolutionCalibrationModal,
    SpectrumCommentsModal,
    ColorConfigModal,
    DataProcessingLogModal,
    ConfigUserLibraryModal,
    NuclideLibraryModal
  },
  data() {
    this.ANALYZE_TYPE = ANALYZE_TYPE

    return {
      analysisType: null, // 分析类型

      sampleList: [],

      loadFromDbModalVisible: false, // 从数据库加载弹窗
      loadFromFileModalVisible: false, // 从文件加载弹窗

      sampleData: {}, // 要分析的谱数据

      peakInfomationModalVisible: false,

      nuclideActivityAndMDCModalVisible: false,

      saveSettingModalVisible: false, // 保存设置弹窗

      analyzeConfigureModalVisible: false, // 分析设置弹窗
      reprocessingModalVisible: false, // 重新分析弹窗
      analyzeInteractiveToolModalVisible: false, // 分析工具弹窗
      zeroTimeModalVisible: false, // Zero Time 弹窗
      korsumModalShow: false, // Korsum 弹窗

      efficiencyCalibrationModalShow: false, // Calibration -> Efficiency 弹窗
      energyCalibrationModalShow: false, // Calibration -> Energy 弹窗
      resolutionCalibrationModalShow: false, // Calibration -> Resolution 弹窗

      spectrumCommentsModalVisible: false, // Comments -> Spectrum Comments 弹窗
      isSpectrumCommentsAdd: true, // Spectrum Comments 是否是新增

      colorConfigModalVisible: false, // Help -> Color Config 弹窗

      dataProcessingLogModalVisible: false, // Log -> Data Processing Log 弹窗

      configUserLibModalVisible: false, // NuclideLibrary -> Config User Library 弹窗
      nuclideLibraryModalVisible: false // NuclideLibrary -> Nuclide Library 弹窗
    }
  },
  created() {
    // 测试
    this.loadSelectedSample({
      dbName: 'auto',
      sampleType: 'B',
      sampleId: '1523651'
    })
  },
  methods: {
    // 从数据库加载-选择完成
    handleLoadSampleFromDB(sampleList) {
      this.sampleList = sampleList
    },

    // 加载选中的样本
    async loadSelectedSample(sample) {
      // B是beta-gamma  P G是gamma
      this.sampleData = sample
      if (sample.sampleType == 'B') {
        this.analysisType = ANALYZE_TYPE.BETA_GAMMA
      } else {
        // gamma
      }
    },

    // 清理全部
    handleCleanAll() {
      this.sampleList = []
    },

    // 保存结果到文件， 服务端生成文件，前端下载
    handleSaveResultsToFile() {
      this.saveSettingModalVisible = true
    },

    /**
     * 保存结果到数据库
     * @param { 'all' | 'current' } type
     */
    handleSaveResultsToDB(type) {
      console.log('%c [ saveResultsToDB ]-157', 'font-size:13px; background:pink; color:#bf2c9f;', type)
    },

    /**
     * 将谱列表中所有谱数据均以IMS2.0格式保存为PHD文件，
     * 服务端生成文件，前端下载
     * @param { 'all' | 'current' } type
     */
    handleSavePHDToFile(type) {
      console.log('%c [ savePHDToFile ]-162', 'font-size:13px; background:pink; color:#bf2c9f;', type)
    },

    handleReprocessAll() {
      console.log('%c [ handleReprocessAll ]-216', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    // 查看自动处理报告
    handleViewARR() {
      console.log('%c [ handleViewARR ]-186', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    // 查看交互分析报告
    handleViewRRR() {
      console.log('%c [ handleViewRRR ]-192', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    // 显示接收到的原始谱文件
    handleViewSpectrum() {
      console.log('%c [ handleViewSpectrum ]-198', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    // 可浏览原始谱中台站操作员对样品采集和测量过程的注释信息；可以浏览分析人员对分析过程的注释信息
    handleViewComments() {
      this.spectrumCommentsModalVisible = true
      this.isSpectrumCommentsAdd = false
    },

    // 可添加能谱分析注释
    handleAddComments() {
      this.spectrumCommentsModalVisible = true
      this.isSpectrumCommentsAdd = true
    },

    // 查看自动处理日志
    handleAutoAnalysisLog() {
      console.log('%c [ handleAutoAnalysisLog ]-211', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    // 查看软件操作帮助文档
    handleHelp() {
      console.log('%c [ handleHelp ]-221', 'font-size:13px; background:pink; color:#bf2c9f;')
    },

    handleResize() {
      this.$refs.gammaAnalysisRef && this.$refs.gammaAnalysisRef.resize()
      this.$refs.betaGammaAnalysisRef && this.$refs.betaGammaAnalysisRef.resize()
    }
  },
  computed: {
    operations() {
      return [
        {
          title: 'SAMPLE',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Load From DB',
                  handler: () => (this.loadFromDbModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Load From File',
                  handler: () => (this.loadFromFileModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Clean All',
                  handler: this.handleCleanAll
                }
              ]
            },
            {
              type: 'a-divider',
              attrs: {
                style: {
                  marginTop: '5px',
                  marginBottom: '5px',
                  display: this.sampleList.length ? '' : 'none'
                }
              }
            },
            {
              type: 'SpectraListInMenu',
              attrs: {
                list: this.sampleList
              },
              on: {
                change: spectra => {
                  if (spectra) {
                    this.loadSelectedSample(spectra)
                  }
                }
              }
            }
          ]
        },
        {
          title: 'SAVE',
          children: [
            {
              type: 'MultiLevelMenu',
              attrs: {
                children: [
                  {
                    title: 'Save Results to File'
                  },
                  {
                    title: 'Save Results to DB',
                    children: [
                      {
                        title: 'Save Current',
                        key: 'current'
                      },
                      {
                        title: 'Save All',
                        key: 'all'
                      }
                    ]
                  },
                  {
                    title: 'Save PHD to File',
                    children: [
                      {
                        title: 'Save Current',
                        key: 'current'
                      },
                      {
                        title: 'Save All',
                        key: 'all'
                      }
                    ]
                  }
                ],
                width: '170px'
              },
              on: {
                menuClick: () => {
                  this.handleSaveResultsToFile()
                },
                submenuClick: ({ item, child }) => {
                  if (item.title == 'Save Results to DB') {
                    this.handleSaveResultsToDB(child.key)
                  } else if (item.title == 'Save PHD to File') {
                    this.handleSavePHDToFile(child.key)
                  }
                }
              }
            }
          ]
        },
        {
          title: 'ANALYZE',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Configure',
                  handler: () => (this.analyzeConfigureModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'ReProcessing',
                  handler: () => (this.reprocessingModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Reprocess All',
                  handler: this.handleReprocessAll
                },
                {
                  type: 'a-menu-item',
                  title: 'Interactive Tool',
                  handler: () => (this.analyzeInteractiveToolModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Zero Time',
                  handler: () => (this.zeroTimeModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Korsum',
                  handler: () => (this.korsumModalShow = true)
                }
              ]
            }
          ]
        },
        {
          title: 'CALIBRATION',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Energy',
                  handler: () => (this.energyCalibrationModalShow = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Resolution',
                  handler: () => (this.resolutionCalibrationModalShow = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Efficiency',
                  handler: () => (this.efficiencyCalibrationModalShow = true)
                }
              ]
            }
          ]
        },
        {
          title: 'NUCLIDELIBRARY',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Nuclide Library',
                  handler: () => (this.nuclideLibraryModalVisible = true)
                },
                {
                  type: 'a-menu-item',
                  title: 'Config User Library',
                  handler: () => (this.configUserLibModalVisible = true)
                }
              ]
            }
          ]
        },
        {
          title: 'COMMENTS',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'View Comments',
                  handler: this.handleViewComments
                },
                {
                  type: 'a-menu-item',
                  title: 'Add Comments',
                  handler: this.handleAddComments
                }
              ]
            }
          ]
        },
        {
          title: 'REPORTS',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'View ARR',
                  handler: this.handleViewARR
                },
                {
                  type: 'a-menu-item',
                  title: 'View RRR',
                  handler: this.handleViewRRR
                },
                {
                  type: 'a-menu-item',
                  title: 'View Spectrum',
                  handler: this.handleViewSpectrum
                }
              ]
            }
          ]
        },
        {
          title: 'LOG',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Automatic Analysis Log',
                  handler: this.handleAutoAnalysisLog
                },
                {
                  type: 'a-menu-item',
                  title: 'GammaViewer Log',
                  handler: () => (this.dataProcessingLogModalVisible = true)
                }
              ]
            }
          ]
        },
        {
          title: 'HELP',
          children: [
            {
              type: 'a-menu',
              children: [
                {
                  type: 'a-menu-item',
                  title: 'Help',
                  handler: this.handleHelp
                },
                {
                  type: 'a-menu-item',
                  title: 'Color Config',
                  handler: () => (this.colorConfigModalVisible = true)
                }
              ]
            }
          ]
        }
      ]
    }
  }
}
</script>
<style lang="less" scoped>
.spectrum-analysis {
  padding-top: 17px;
  height: 100%;
  display: flex;
  flex-direction: column;

  // 顶部操作栏开始
  &-operators {
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    overflow: auto;

    &-item {
      width: 158px;
      border: 1px solid rgba(12, 235, 201, 0.6);
      border-top-width: 3px;
      height: 30px;
      background-color: rgba(51, 202, 217, 0.2);
      color: #ccede8;

      &:not(:last-child) {
        margin-right: 15px;
      }

      ::v-deep {
        span {
          text-shadow: none;
          line-height: 26px;
          letter-spacing: 2px;
        }
      }

      &:nth-child(4) {
        width: 224px;
      }
      &:nth-child(5) {
        width: 268px;
      }
      &:nth-child(6) {
        width: 257px;
      }
      &:nth-child(7) {
        width: 234px;
      }
      &:nth-child(8) {
        width: 125px;
      }
    }
  }
  // 顶部操作栏结束

  ::v-deep {
    // 二级操作栏开始
    .spectrum-analysis-sub-operators {
      flex-shrink: 0;
      margin-bottom: 19px;
      display: flex;
      flex-wrap: nowrap;
      overflow: auto;

      .pop-over-with-icon {
        height: 32px;

        &:not(:last-child) {
          margin-right: 11px;
        }

        &:nth-child(1) {
          width: 256px;
        }
        &:nth-child(2) {
          width: 186px;
        }
        &:nth-child(3) {
          width: 246px;
        }
        &:nth-child(4) {
          width: 246px;
        }
      }

      .peak-info {
        width: 306px;
        height: 32px;
        display: inline-block;
      }
    }
    // 二级操作栏结束
  }

  // 主体部分开始
  &-main {
    margin-top: 15px;
    height: calc(100% - 45px);
    overflow: hidden;
  }
  // 主体部分结束
}
</style>
<style lang="less">
.spectrum-analysis-operators-dropdown-overlay {
  background-color: #03353f;
  .ant-menu {
    background: transparent;
    padding: 0;
    position: relative;
    border-right: 0;

    &-submenu {
      &-active {
        background-color: #055565 !important;
      }

      &-title {
        height: 30px !important;
        line-height: 30px !important;
        color: #fff;
        margin: 0;

        &:active {
          background-color: #055565 !important;
        }

        .ant-menu-submenu-arrow {
          &::before,
          &::after {
            background: #fff !important;
          }
        }
      }
    }

    &-item {
      color: #fff;
      font-family: Arial;
      border: 0;
      background-color: transparent !important;
      padding: 4px 14px;
      height: 30px;
      line-height: 22px;
      margin: 0 !important;

      &:hover {
        background-color: #055565 !important;
      }
      &-selected {
        font-weight: normal;
      }
      &-disabled {
        color: #476d74 !important;
        &:hover {
          background-color: transparent !important;
        }
      }
    }
  }
}
</style>
