<template>
  <custom-modal v-model="visible" :width="1200" :title="type == 1 || type == 3 ? 'ARR' : 'RRR'">
    <a-spin :spinning="isLoading">
      <a-textarea v-model="content" :readonly="type == 1 || type == 2"></a-textarea>
    </a-spin>
    <div slot="custom-footer" style="text-align: center">
      <a-space :size="20">
        <a-button type="primary" @click="handleOk">Export</a-button>
        <a-button @click="visible = false">Cancel</a-button>
      </a-space>
    </div>
  </custom-modal>
</template>

<script>
import ModalMixin from '@/mixins/ModalMixin'
import { getAction, postAction } from '../../../../api/manage'
import { saveAs } from 'file-saver'
import SampleDataMixin from '../../SampleDataMixin'
export default {
  mixins: [ModalMixin, SampleDataMixin],
  props: {
    type: {
      type: Number,
    },
    extraData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      content: '',
      isLoading: true,
      fileName: '',
    }
  },
  methods: {
    async getContent() {
      let url = ''
      switch (this.type) {
        case 1:
          url = '/gamma/viewARR'
          break
        case 2:
          url = '/gamma/viewRRR'
          break
        case 3:
          url = '/spectrumAnalysis/viewARR'
          break
        case 4:
          url = '/spectrumAnalysis/viewRRR'
          break
      }
      console.log(this.extraData)
      try {
        this.content = ''
        this.isLoading = true
        const { sampleId, inputFileName: fileName, dbName, detFileName, gasFileName, qcFileName } = this.newSampleData
        let res = null
        if (this.type == 4) {
          let params = {
            dbName,
            sampleId,
            sampleData: this.extraData.sampleData,
            gasBgData: this.extraData.GasBgData,
            detBgData: this.extraData.DetBgData,
            qcData: this.extraData.QCData,
            sampleFileName: fileName,
            gasFileName,
            detFileName,
            qcFileName,
          }
          res = await postAction(url, params)
        } else if (this.type == 2) {
          res = await getAction(url, {
            sampleId,
            fileName,
            // ...this.extraData,
          })
        } else {
          res = await getAction(url, {
            sampleId,
          })
        }

        if (typeof res == 'string') {
          this.content = res
        } else {
          const { success, result, message } = res
          if (success) {
            this.content = result
          } else {
            this.$message.error(message)
          }
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.isLoading = false
      }
    },

    beforeModalOpen() {
      this.getContent()
    },
    handleOk() {
      this.fileName = ''
      if (this.content) {
        let name = this.newSampleData.inputFileName.split('.')[0]
        let strData = new Blob([this.content], { type: 'text/plain;charset=utf-8' })
        // if (this.type == 1 || this.type == 3) {
        //   saveAs(strData, `${this.type == 1 ?'Gamma-':'Beta-'} ARR.txt`)
        // } else {
        //   saveAs(strData, `${this.type == 2 ?'Gamma-':'Beta-'} RRR.txt`)
        // }
        if (this.type == 1 || this.type == 3) {
          saveAs(strData, `${name}_ARR.txt`)
        } else {
          saveAs(strData, `${name}_RRR.txt`)
        }
        // 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(strData, `${_this.fileName}.txt`)
        //     }
        //   },
        // })
      } else {
        this.$message.warning('No data can be saved!')
      }
    },
  },
}
</script>

<style lang="less" scoped>
textarea {
  height: calc(100vh - 350px);
  padding: 5px;
  overflow: auto;
  background-color: #285367;
}
</style>
