<template>
  <custom-modal v-model="visible" title="Spectrum Comments" :okHandler="handleOk" :footer="isAdd ? undefined : null">
    <a-spin :spinning="isLoading">
      <a-textarea v-model="comments" :rows="8" :disabled="!isAdd"></a-textarea>
    </a-spin>
  </custom-modal>
</template>

<script>
import { getAction, putAction } from '@/api/manage'
import ModalMixin from '@/mixins/ModalMixin'
import SampleDataMixin from '../../SampleDataMixin'

export default {
  mixins: [ModalMixin, SampleDataMixin],
  props: {
    isAdd: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      isLoading: false,
      comments: '',
    }
  },
  methods: {
    // ModalMixin 中的生命周期方法
    beforeModalOpen() {
      if (this.isAdd) {
        this.comments = ''
        this.getTotalComment()
      } else {
        this.getInfo()
      }
    },

    // View接口
    async getInfo() {
      try {
        this.isLoading = true
        const { sampleId, inputFileName: fileName } = this.sampleData
        const { success, result, message } = await getAction('/gamma/viewComment', {
          sampleId,
          fileName,
        })
        this.isLoading = false
        if (success) {
          this.comments = result
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      }
    },

    // Add 获取 Comment 接口
    async getTotalComment() {
      try {
        this.isLoading = true
        const { inputFileName: fileName } = this.sampleData
        const { success, result, message } = await getAction('/gamma/viewGeneralComment', {
          fileName,
        })
        this.isLoading = false
        if (success) {
          this.comments = result
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      }
    },

    async handleOk() {
      const { inputFileName: fileName } = this.sampleData
      const { success, message } = await putAction(`/gamma/addComment?fileName=${fileName}&comment=${this.comments}`)
      if (!success) {
        this.$message.error(message)
        throw new Error(message)
      }
    },
  },
}
</script>

<style></style>
