<script setup>
import { defineProps } from 'vue'
const props = defineProps(['order_id'])
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import LoadButtonWhileAwait from "@/components/request/LoadButtonWhileAwait.vue";
import { handleEconomicError } from "@/components/request/HandleEconomicError.vue";
import { ref } from "vue";
import { useRouter } from "vue-router";
import Swal from "sweetalert2";

const router = useRouter();

const exportOrderToInvoiceStripe = async () => {
  // Get the email to send the invoice to
  await Swal.fire({
    title: 'Indtast email til faktura',
    input: 'email',
    inputAttributes: {
      autocapitalize: 'off'
    },
    showCancelButton: true,
    confirmButtonText: 'Send faktura',
    showLoaderOnConfirm: true,
    preConfirm: (email) => {
      return authenticatedRequest(
          "/modules/stripe/invoice",
          "POST",
          {
            email: email,
            order_id: parseInt(props.order_id)
          }
      ).then((response) => {
        console.log(response);
        Swal.fire({
          title: 'Faktura sendt',
          icon: 'success'
        });
      }).catch((error) => {
        handleEconomicError(error);
      });
    },
    allowOutsideClick: () => !Swal.isLoading()
  });
};
</script>

<template>
  <LoadButtonWhileAwait class="is-success is-inverted" :loadFunction="exportOrderToInvoiceStripe" icon="fas fa-file-invoice"> Gennemfør </LoadButtonWhileAwait>
</template>

<style scoped>

</style>