<template>
  <div class="pdf-export-generator">
    <TfButton
      :disabled="isDisabled"
      :loading="renderingPdf"
      :size="buttonSize"
      data-test="export pdf"
      type="primary"
      icon="file-download"
      @click="startRender"
    >
      {{ buttonTitle }}
    </TfButton>

    <ExportFile
      v-if="render"
      :filename="filename"
      @save="stopRender"
    >
      <slot />
    </ExportFile>
  </div>
</template>

<script>
  import ExportFile from './ExportFile';

  /**
   * PDF Export
   *
   * @vuedoc
   * @exports TfPdfExport
   * @category components
   */
  export default {
    name: 'TfPdfExport',
    components: { ExportFile },
    props: {
      buttonSize: {
        type: String,
        default: 'text-icon',
      },
      buttonTitle: {
        type: String,
        default: 'export',
      },
      filename: {
        type: String,
        default: 'export.pdf',
      },
      isDisabled: {
        type: Boolean,
        default: false,
      },
    },

    data: () => ({
      render: false,
      renderingPdf: false,
    }),

    methods: {
      /**
       * Starts the render process.
       */
      startRender() {
        this.renderingPdf = true;
        this.render = true;
      },
      /**
       * Stops the render process.
       */
      stopRender() {
        this.render = false;
        this.renderingPdf = false;
      },
    },
  };
</script>
