<template>
  <div>
    <q-inner-loading :showing="loading" />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { DocumentConverterApi } from '@openapi/document-converter';
import { notifyError } from '@/utils/notify';

const documentCoverterApi = new DocumentConverterApi();

@Component
export default class ConvertedDocumentDownload extends Vue {
  loading = false;

  async created() {
    const id = this.$route.params.id;
    if (!id) {
      this.backToMainPage();
    }

    this.loading = true
    try {
      const { data } = await documentCoverterApi.getUploadedFile(id, { responseType: 'blob' });
      saveAs(data, `Экспорт_по_услуге_${id}.xlsx`);
    } catch (e) {
      if (e instanceof Error) {
        notifyError(e.message);
      }
    } finally {
      this.loading = false;
      this.$nextTick(() => {
        this.backToMainPage();
      })
    }
  }

  backToMainPage() {
    this.$router.push('/statements/');
  }
}
</script>

<style scoped>

</style>