<template>
  <custom-modal
    v-model="visible"
    :width="1231"
    title="Nuclide Activity and MDC"
    class="nuclide-activity-and-mdc-modal"
    :footer="null"
  >
    <!-- 顶部搜索栏 -->
    <a-form-model class="search-form">
      <div class="time-pickers">
        <a-form-model-item label="Activity reference time">
          <custom-date-picker show-time v-model="queryParam.activityReferenceTime" />
        </a-form-model-item>
        <a-form-model-item label="Concentration reference time">
          <custom-date-picker show-time v-model="queryParam.concentrationReferenceTime" />
        </a-form-model-item>
      </div>
      <a-space class="operators" :size="20">
        <a-button :type="compareVisible ? 'grey' : 'primary'" @click="handleComparision">
          <img src="@/assets/images/spectrum/comparation.png" />
          {{ compareVisible ? 'Cancel comparison' : 'Comparison' }}
        </a-button>
        <a-button type="primary" @click="handleExportToExcel">
          <img src="@/assets/images/spectrum/download.png" />
          Export to Excel
        </a-button>
      </a-space>
    </a-form-model>
    <!-- 顶部搜索栏结束 -->
    <a-spin :spinning="isLoading">
      <!-- 主体部分 -->
      <div v-if="compareVisible" class="comparison-list">
        <div class="comparison-list-item">
          <div class="comparison-list-item-title">ARMD</div>
          <custom-table :list="list" :scroll="{ y: 230 }" :columns="columns"></custom-table>
        </div>
        <div class="comparison-list-item">
          <div class="comparison-list-item-title">IDC</div>
          <custom-table :list="compareList" :scroll="{ y: 230 }" :columns="columns"></custom-table>
        </div>
      </div>
      <custom-table v-else :columns="columns" :list="list" :scroll="{ y: 460 }" :canSelect="false"></custom-table>
    </a-spin>
  </custom-modal>
</template>

<script>
import { getAction, getFileAction } from '@/api/manage'
import ModalMixin from '@/mixins/ModalMixin'
import SampleDataMixin from '@/views/spectrumAnalysis/SampleDataMixin'
import { saveAs } from 'file-saver';

const columns = [
  {
    title: 'Nuclide',
    dataIndex: 'nuclide'
  },
  {
    title: 'HalfLife',
    dataIndex: 'halfLife'
  },
  {
    title: 'Energy (keV)',
    dataIndex: 'energy',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'Yield (%)',
    dataIndex: 'yield'
  },
  {
    title: 'Efficiency',
    dataIndex: 'efficiency',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'Activity (Bq)',
    dataIndex: 'activity',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'Act Err (%)',
    dataIndex: 'actErr',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'MDA (Bq)',
    dataIndex: 'mda',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'Conc (uBq/m3)',
    dataIndex: 'conc',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(6) : text
    }
  },
  {
    title: 'MDC (uBq/m3)',
    dataIndex: 'mdc',
    customRender: text => {
      return text && text !== 'null' ? Number(text).toPrecision(5) : text
    }
  }
]
export default {
  mixins: [ModalMixin, SampleDataMixin],
  props: {
    sampleId: {
      type: Number
    }
  },
  data() {
    this.columns = columns
    return {
      queryParam: {
        activityReferenceTime: undefined,
        concentrationReferenceTime: undefined
      },
      compareVisible: false,
      isLoading: false,

      list: [],
      compareList: [],
      fileName: ''
    }
  },
  methods: {
    // 切换比较
    handleComparision() {
      this.compareVisible = !this.compareVisible
    },

    async getInfo() {
      try {
        this.isLoading = true
        const { success, result, message } = await getAction('/gamma/radionuclideActivity', {
          sampleId: this.sampleId
        })
        if (success) {
          const { dateTime_act_ref, dateTime_con_ref, table } = result
          this.queryParam.activityReferenceTime = dateTime_act_ref
          this.queryParam.concentrationReferenceTime = dateTime_con_ref
          this.list = table
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.isLoading = false
      }
    },

    beforeModalOpen() {
      this.compareVisible = false
      this.getInfo()
    },

    // 导出到Excel
    handleExportToExcel() {
      if (this.list.length > 0) {
        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) {
              _this.visible = false
              let params = {
                // sampleId: "426530",
                // fileName: "CAX05_001-20230731_1528_S_FULL_37563.6.PHD"
                sampleId: this.sampleId,
                fileName: this.sampleData.fileName
              }
              getFileAction('/gamma/exportRadionuclideActivity', params).then(res => {
                if (res.code && res.code == 500) {
                  this.$message.warning("This operation fails. Contact your system administrator")
                } else {
                  const blob = new Blob([res], { type: "application/vnd.ms-excel" })
                  saveAs(blob, `${_this.fileName}`)
                }
              })
            }
          },
        });
      } else {
        this.$message.warning("No downloadable data")
      }
    }
  }
}
</script>

<style lang="less" scoped>
.nuclide-activity-and-mdc-modal {
  ::v-deep {
    .ant-modal-body {
      padding: 16px;
    }
  }
}

.search-form {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;

  .time-pickers {
    display: flex;
    .ant-form-item {
      margin-bottom: 0;

      &:nth-child(2) {
        margin-left: 20px;
      }

      ::v-deep {
        .ant-form-item {
          &-label {
            > label {
              color: #ade6ee;
            }
          }

          &-control-wrapper {
            width: 210px;
          }
        }
      }
    }
  }
  .operators {
    .ant-btn {
      display: flex;
      align-items: center;

      img {
        margin-right: 10px;
      }
    }
  }
}

.comparison-list {
  padding-top: 15px;
  border-top: 1px solid rgba(13, 109, 118, 0.5);

  &-item {
    &-title {
      color: #0cebc9;
    }

    &:last-child {
      margin-top: 12px;
    }
  }
}
</style>
