<template>
  <el-dialog
    :append-to-body="true"
    :title="__('导入第三方券码')"
    :visible.sync="dialogFormVisible"
    width="30%"
    @close="close"
  >
    <el-form label-width="0">
      <el-form-item>
        <el-button type="primary" @click="expPayCardExcel">
          {{ __('导出第三方券码模版') }}
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
          ref="upload"
          :action="action"
          :data="params"
          :limit="1"
          :multiple="false"
          :on-success="handleSuccess"
        >
          <el-button size="small" type="primary">
            {{ __('导入模板') }}
          </el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('关闭') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { URL as configUrl } from '@/config'
  import { getToken } from '@/utils/token'
  import { mapGetters } from 'vuex'
  import { expPayCardTemp } from '@/api/card/type'

  export default {
    name: 'PayCardImport',
    data() {
      return {
        params: {
          authorization: '',
        },
        action: configUrl.pay.card.history.payCardImport,
        dialogFormVisible: false,
      }
    },
    computed: {
      ...mapGetters({
        token: 'user/token',
      }),
    },
    created() {
      this.params.authorization = getToken()
    },

    methods: {
      __,
      expPayCardExcel() {
        expPayCardTemp({}).then((data) => {
          const link = document.createElement('a')
          let blob = new Blob([data], {
            type: 'application/msexcel;charset=utf-8',
          })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.download = '第三方券码模版.xlsx'
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
      },
      showEdit() {
        this.dialogFormVisible = true
      },
      close() {
        this.$refs.upload.clearFiles()
        this.params.topic_type = 0
        this.dialogFormVisible = false
        this.$emit('setPayCardImport')
      },
      handleSuccess(response, file, fileList) {
        let status = response.status
        let msg = response.msg
        if (status === 200) {
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.$emit('setPayCardImport')
          this.close()
        } else {
          this.$refs.upload.clearFiles()
          this.$baseMessage(msg, 'error')
        }
      },
    },
  }
</script>
