<template>
  <custom-modal v-model="visible" :width="750" title="QC Results">
    <a-spin :spinning="isLoading">
      <a-table :columns="columns" :dataSource="list" :pagination="false" />
    </a-spin>
    <a-button slot="custom-footer" type="primary" @click="handleExportToExcel">Export to Excel</a-button>
  </custom-modal>
</template>

<script>
import { getAction, getFileAction } from '@/api/manage'
import ModalMixin from '@/mixins/ModalMixin'
import { saveAs } from 'file-saver'
import SampleDataMixin from '../../SampleDataMixin'
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    customCell: (record) => {
      return {
        style: {
          backgroundColor: record.flag == 'FAIL' ? 'red' : '',
        },
      }
    },
  },
  {
    title: 'Pass/Fail',
    dataIndex: 'flag',
    customCell: (record) => {
      return {
        style: {
          backgroundColor: record.flag == 'FAIL' ? 'red' : '',
        },
      }
    },
  },
  {
    title: 'Value',
    dataIndex: 'value',
    customCell: (record) => {
      return {
        style: {
          backgroundColor: record.flag == 'FAIL' ? 'red' : '',
        },
      }
    },
  },
  {
    title: 'Standard',
    dataIndex: 'standard',
    customCell: (record) => {
      return {
        style: {
          backgroundColor: record.flag == 'FAIL' ? 'red' : '',
        },
      }
    },
  },
]
export default {
  mixins: [ModalMixin, SampleDataMixin],
  data() {
    this.columns = columns
    return {
      isLoading: false,
      list: [],
      fileName: '',
    }
  },
  methods: {
    async getData() {
      try {
        this.isLoading = true
        const { sampleId, inputFileName: fileName } = this.sampleData
        const { success, result, message } = await getAction('/gamma/viewQCResult', {
          sampleId,
          fileName,
        })
        if (success) {
          this.list = result
        } else {
          this.$message.error(message)
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.isLoading = false
      }
    },

    beforeModalOpen() {
      this.getData()
    },
    // 导出到Excel
    handleExportToExcel() {
      this.fileName = ''
      const { sampleId, inputFileName: fileName } = this.sampleData
      if (this.list.length > 0) {
        let name = this.newSampleData.inputFileName.split('.')[0]
        let params = {
          sampleId,
          fileName,
        }
        getFileAction('/gamma/exportQCResult', 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, `${name}_QC Result`)
          }
        })
        // 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) {
        //       let params = {
        //         sampleId,
        //         fileName
        //       }
        //       getFileAction('/gamma/exportQCResult', 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></style>
