<script setup>
import { ref } from 'vue'
import Modal from "../../../../../Views/Components/Modal.vue";
import IbanInput from "../../../../../Views/Components/IbanInput.vue";
import {useAjax} from "../../../../../../resources/js/components/ajaxHandler.js";
import TextResource from "../../../../../Views/Components/TextResource.vue";
import {invoiceCheckContactName} from "../../../../../../resources/js/components/InvoiceUploadChecks.js";
import {toast} from "vue3-toastify";
import ErrorText from "../../../../../Views/Components/ErrorText.vue";

const { request } = useAjax();

const emit = defineEmits(['close'])


const props = defineProps({
    eventId: Number,
    invoiceType: String,
    amount: [String, Number],
    otherText: String,
    receipt: File,
    userName: String,
    userEmail: String,
    userTelephone: String,
    userAccountOwner: String,
    userAccountIban: String,
    havePassengers: Number,
    materialTransportation: Boolean,
    travelReason: String,
})

const finalStep = ref(true)
const userName = ref(props.userName)
const userEmail = ref(props.userEmail)
const userTelephone = ref(props.userTelephone)
const userIban = ref('')
const userAccountOwner = ref('')
const paymentPurpose = ref('')
const sending = ref(false)
const success = ref(false)
const decision = ref('')
const errorMsg = ref('')
const confirmation = ref(null)
const receiptError = ref('')

async function sendData() {
    if (!userName.value) return

    sending.value = true
    errorMsg.value = ''
    success.value = false

    const formData = new FormData()

    formData.append('name', userName.value)
    formData.append('email', userEmail.value)
    formData.append('telephone', userTelephone.value)
    formData.append('amount', props.amount)
    formData.append('otherText', props.otherText)
    formData.append('decision', decision.value)
    formData.append('accountOwner', userAccountOwner.value)
    formData.append('accountIban', userIban.value)
    formData.append('paymentPurpose', paymentPurpose.value)
    formData.append('havePassengers', props.havePassengers ? 1 : 0)
    formData.append('materialTransportation', props.materialTransportation ? 1 : 0)
    formData.append('travelReason', props.travelReason)

    if (props.receipt) {
        formData.append('receipt', props.receipt)
    }


    try {
        const response = await request('/api/v1/invoice/new/' + props.eventId + '/' + props.invoiceType, {
            method: 'POST',
            body: formData
        })

        if (response.status === 'success') {
            window.location.href = '/';
        } else {
            receiptError.value = response.message;
        }
    } catch (err) {
        toast.error(result.message);
    } finally {
        sending.value = false
    }

}

</script>

<template>
    <Modal :show="finalStep" title='Bitte gib deine Daten ein' @close="emit('close')" width="600px">
        <label>
            <strong>Dein Name Name (kein Pfadiname):</strong>
        </label><br />
        <input
            type="text"
            @keyup="invoiceCheckContactName();"
            id="contact_name"
            name="contact_name" v-model="userName"
            style="font-size: 14pt; width: 550px;" /><br /><br />

        <label v-if="userName !== ''">
            <strong>E-Mail-Adresse (Für Rückfragen):</strong>
        </label><br />
        <input
            v-if="userName !== ''"
            type="email"
            name="contact_email"
            v-model="userEmail"
            style="font-size: 14pt; width: 550px;" /><br /><br />

        <label v-if="userName !== ''">
            <strong>Telefonnummer (für Rückfragen):</strong>
        </label><br />
        <input
            v-if="userName !== ''"
            type="text"
            id="contact_telephone"
            name="contact_telephone" v-model="userTelephone"
            style="font-size: 14pt; width: 550px;" /><br /><br />

        <span id="confirm_payment" v-if="userEmail !== '' && userTelephone !== ''">
            <label>
                <strong>Zahlungsempfänger*in:</strong>
            </label><br />
            <input type="text" name="account_owner" id="account_owner" v-model="userAccountOwner" style="font-size: 14pt; width: 550px;" /><br /><br />

            <label>
                <strong>IBAN:</strong>
            </label><br />
            <IbanInput id="account_iban" name="account_iban" v-model="userIban" style="font-size: 14pt; width: 550px;" /><br /><br />

            <label>
                <strong>Überweisungstext (laut Rechnung):</strong>
            </label><br />
            <input
                type="text"
                id="payment_purpose"
                name="payment_purpose"
                v-model="paymentPurpose"
                style="font-size: 14pt; width: 550px;"
            /><br /><br />
            <ErrorText :message="receiptError" />

            <span v-if="paymentPurpose != '' && userAccountOwner != '' && userIban && userIban.length === 27"><br />
                <input type="radio" name="confirmation_radio" value="payment" id="confirmation_radio_payment"  v-model="confirmation">
                 <TextResource belongsTo="confirmation_radio_payment" textName="CONFIRMATION_DIRECT_PAYMENT" /><br /><br />
              <input type="button" v-if="confirmation !== null && !sending" @click="sendData" value="Beleg einreichen" />
            </span>
        </span>
    </Modal>
</template>

<style scoped>
/* optional styling */
</style>

