<script setup>
import { computed, reactive, ref } from "vue";
import Modal from "../../../../Views/Components/Modal.vue";
import ParticipantData from "./ParticipantData.vue";
import {toast} from "vue3-toastify";
import {useAjax} from "../../../../../resources/js/components/ajaxHandler.js";
import {format, getDay, getMonth, getYear} from "date-fns";

const props = defineProps({
    data: {
        type: Object,
        default: () => ({
            localGroups: {},
            participants: {},
            event: {},
        }),
    },
});

const today = format(new Date(), "yyyy-MM-dd");

const { request } = useAjax();

const searchTerms = reactive({});
const selectedStatuses = reactive({});

const event = computed(() => props.data?.event ?? {});
const participantGroups = computed(() => props.data?.participants ?? {});
const showParticipantDetails = ref(false);
const showParticipant = ref(null);
const editMode = ref(false);

const openCancelDialog = ref(false);
const openPartialPaymentDialogSwitch = ref(false);

defineEmits(['showParticipantDetails', 'markCocExisting', 'paymentComplete'])

function openParticipantDetails(input) {
    showParticipantDetails.value = true;
    showParticipant.value = input;
    editMode.value = false;
}

async function saveParticipant(formData) {
    if (!showParticipant.value?.identifier) {
        return;
    }

    const data = await request('/api/v1/event/participant/' + showParticipant.value.identifier + '/update', {
        method: "POST",
        body: JSON.stringify(formData),
    });

    if (data.status === 'success') {
        toast.success(data.message ?? 'Änderungen gespeichert');
        editMode.value = false;
    } else {
        toast.error(data.message ?? 'Speichern fehlgeschlagen');
    }
}


const getGroupEntries = computed(() => {
    return Object.entries(participantGroups.value ?? {});
});

const getAgeCounts = (participants) => {
    const buckets = {
        '0-5': 0,
        '6-11': 0,
        '12-15': 0,
        '16-17': 0,
        '18-27': 0,
        '27+': 0,
    };

    (participants ?? []).forEach((participant) => {
        const age = Number(participant?.age);

        if (!Number.isFinite(age)) {
            return;
        }

        if (age >= 0 && age <= 5) {
            buckets['0-5']++;
        } else if (age <= 11) {
            buckets['6-11']++;
        } else if (age <= 15) {
            buckets['12-15']++;
        } else if (age <= 17) {
            buckets['16-17']++;
        } else if (age <= 27) {
            buckets['18-27']++;
        } else {
            buckets['27+']++;
        }
    });

    return buckets;
};

const getSearchText = (participant) => {
    return [
        participant?.firstname,
        participant?.lastname,
        participant?.nickname,
        participant?.email_1,
        participant?.email_2,
        participant?.phone_1,
        participant?.phone_2,
        participant?.local_group_string,
        participant?.participation_type_string,
        participant?.efz_status_string,
        participant?.amount_string,
    ]
        .filter(Boolean)
        .join(" ")
        .toLowerCase();
};

const getFilteredParticipants = (groupKey, participants) => {
    const searchTerm = (searchTerms[groupKey] ?? "").trim().toLowerCase();

    return (participants ?? []).filter((participant) => {
        const matchesSearch =
            !searchTerm ||
            getSearchText(participant).includes(searchTerm);


        return matchesSearch;
    });
};

const getRowClass = (participant) => {
    if (participant?.unregistered_at) {
        return "bg-gray-50 text-gray-500";
    }

    if (
        Number(participant?.amount?.amount ?? participant?.amount ?? 0) !==
        Number(participant?.amount_paid?.amount ?? participant?.amount_paid ?? 0)
    ) {
        return "bg-red-50";
    }

    return "";
};

const ensureGroupState = (groupKey) => {
    if (searchTerms[groupKey] === undefined) {
        searchTerms[groupKey] = "";
    }

    if (selectedStatuses[groupKey] === undefined) {
        selectedStatuses[groupKey] = "all";
    }
};

async function paymentComplete(participant) {
    const data = await request('/api/v1/event/participant/' + participant.identifier + '/payment-complete', {
        method: "POST",
    });

    if (data.status === 'success') {
        toast.success(data.message);
        document.getElementById('participant-' + participant.identifier + '-payment').removeAttribute('class');
        document.getElementById('participant-' + participant.identifier + '-paid').innerText = participant.amountExpected.readable;
        document.getElementById('participant-' + participant.identifier + '-actions').style.display='none';
    } else {
        toast.error(data.message);

    }
}

async function markCocExisting(participant) {
    const data = await request('/api/v1/event/participant/' + participant.identifier + '/mark-coc-existing', {
        method: "POST",
    });

    if (data.status === 'success') {
        toast.success(data.message);
        document.getElementById('participant-' + participant.identifier + '-coc-status').innerText = 'Gültig';
        document.getElementById('participant-' + participant.identifier + '-coc-action').style.display='none';
        document.getElementById('participant-' + participant.identifier + '-name').removeAttribute('class');
    } else {
        toast.error(data.message);

    }
}

function openCancelParticipationDialog(participant) {
    openCancelDialog.value = true;
    showParticipant = participant;

}

async function execCancelParticipation() {
    openCancelDialog.value = false;
    toast.success('Abmeldung erfolgreich')

}

function openPartialPaymentDialog(participant) {
    openPartialPaymentDialogSwitch.value = true;
    showParticipant = participant;

}

async function execPartialPayment() {
    openPartialPaymentDialogSwitch.value = false;
    toast.success('Teilzahlung erfolgreich')
}

</script>

<template>
    <h2>{{ event?.name ?? "Veranstaltung" }}</h2>
    <div :key="groupKey">
        <div>
            <table style="width: 95%; margin: 20px auto; border-collapse: collapse;" v-for="[groupKey, participants] in getGroupEntries">
                <thead>
                <tr>
                    <th colspan="4" style="background: linear-gradient(to bottom, #fff, #f6f7f7); font-weight: bold">
                        {{ groupKey }} ({{ participants.length }} Personen)
                    </th>
                </tr>
                <tr style="background: linear-gradient(to bottom, #fff, #f6f7f7);">
                    <th>Name</th>
                    <th>Beitrag</th>
                    <th>E-Mail-Adresse</th>
                    <th>Telefon</th>
                </tr>
                </thead>

                <tbody>
                <template
                    v-for="participant in getFilteredParticipants(groupKey, participants)"
                    :key="participant.id"
                >
                    <tr :class="getRowClass(participant)" :id="'participant-' + participant.identifier">
                        <td :id="'participant-' + participant.identifier +'-name'"
                            style="width: 300px;"
                            :class="participant.efz_status === 'checked_invalid' ? 'efz-invalid' :
                                participant.efz_status === 'not_checked' ? 'efz-not-checked' : ''">
                            <div v-html="participant.fullname" /><br />
                            Geburtsdatum: {{ participant.birthday }}<br />
                            Alter: {{ participant.age }} Jahre<br />


                            <span>eFZ-Status: <label :id="'participant-' + participant.identifier +'-coc-status'">{{ participant.efzStatusReadable }}</label></span> &nbsp;
                            <span :id="'participant-' + participant.identifier +'-coc-action'" v-if="participant.efz_status !== 'checked_valid' && participant.efz_status !== 'not_required'" class="link" style="color: #3cb62e; font-size: 11pt;" @click="markCocExisting(participant)">Vorhanden?</span>
                        </td>

                        <td :id="'participant-' + participant.identifier +'-payment'" :class="participant.amount_left_value != 0 && !unregistered_at ? 'not-paid' : ''" style="width: 275px; '">
                            Gezahlt: <label :id="'participant-' + participant.identifier + '-paid'">{{ participant?.amountPaid.readable }}</label> /<br />
                            Gesamt: {{ participant?.amountExpected.readable }}
                            <br /><br />
                            <span v-if="participant.amount_left_value != 0 && !unregistered_at" :id="'participant-' + participant.identifier + '-actions'">
                                <span class="link" style="font-size:10pt;" @click="paymentComplete(participant)">Zahlung buchen</span> &nbsp;
                                <span class="link" style="font-size:10pt;" @click="openPartialPaymentDialog(participant)">Teilzahlung buchen</span>
                            </span>
                        </td>

                        <td>
                            {{ participant?.email_1 ?? "-" }}
                            <div v-if="participant?.email_2 && participant.email_2 !== participant.email_1" class="text-xs text-gray-500">
                                {{ participant.email_2 }}
                            </div>

                        </td>

                        <td>
                            {{ participant?.phone_1 ?? "-" }}
                            <div v-if="participant?.phone_2 && participant.phone_2 !== participant.phone_1" class="text-xs text-gray-500">
                                {{ participant.phone_2 }}
                            </div>
                        </td>


                    </tr>

                    <tr class="participant-meta-row">
                        <td colspan="5" style="height: 15px !important; font-size: 9pt; background-color: #ffffff; border-top-style: none;">
                            {{ participant?.localgroup ?? "-" }} |
                            <strong> Anreise: </strong>{{ participant?.arrival ?? "-" }} |
                            <strong> Abreise: </strong>{{ participant?.departure ?? "-" }} |
                            <strong> Angemeldet am: </strong>{{ participant?.registerDate ?? "-" }} |
                            <span class="link" @click="openParticipantDetails(participant)">Details</span> |
                            <span class="link">E-Mail senden</span> |
                            <span  @click="openCancelParticipationDialog(participant)" v-if="!unregistered_at" class="link" style="color: #da7070;">Abmelden</span>
                            <span v-else class="link" style="color: #3cb62e;">Wieder anmelden</span>
                        </td>
                    </tr>
                </template>

                <tr>
                    <td colspan="3" style="font-weight: normal;">
                        0 - 5 Jahre: <strong>{{ getAgeCounts(participants)['0-5'] ?? 0 }}</strong> |
                        6-11 Jahre: <strong>{{ getAgeCounts(participants)['6-11'] ?? 0 }}</strong> |
                        12-15 Jahre: <strong>{{ getAgeCounts(participants)['12-15'] ?? 0 }}</strong> |
                        16 - 17 Jahre: <strong>{{ getAgeCounts(participants)['16-17'] ?? 0 }}</strong> |
                        18 - 27 Jahre: <strong>{{ getAgeCounts(participants)['18-27'] ?? 0 }}</strong> |
                        27 Jahre und älter: <strong>{{ getAgeCounts(participants)['27+'] ?? 0 }}</strong>
                    </td>
                    <td>
                        E-Mail an Gruppe senden
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

    <Modal
        :show="showParticipantDetails"
        title="Anmeldedetails ansehen"
        @close="showParticipantDetails = false;toast.success('HALLO');"
    >
        <ParticipantData
            @cancelParticipation="openCancelParticipationDialog"
            :participant="showParticipant"
            :editMode="editMode"
            :event="event"
            @editParticipant="editMode = true"
            @saveParticipant="saveParticipant"
            @paymentComplete="paymentComplete"
            @markCocExisting="markCocExisting"
            @closeParticipantDetails="showParticipantDetails = false" />
    </Modal>

    <Modal
        :show="openCancelDialog"
        title="Anmeldung stornieren"
        @close="openCancelDialog = false"
    >
        Datum der Abmeldung:
        <input type="date" style="margin-top: 10px;" id="cancel_date" :value="today" />
        <br /><br />
        <button class="button" @click="execCancelParticipation()">Abmeldung durchführen</button>
    </Modal>

    <Modal
        :show="openPartialPaymentDialogSwitch"
        title="Teilbetragszahlung"
        @close="openPartialPaymentDialogSwitch   = false"
    >
        Gesamtbetrag der Zahlung:
        <input type="text" style="margin-top: 10px; width: 150px !important;" id="partial_payment_amount" /> Euro
        <br /><br />
        <button class="button" @click="execPartialPayment()">Teilbetrag buchen</button>
    </Modal>




</template>

<style scoped>
    table {
        margin-bottom: 60px !important;
    }

    tr {
        vertical-align: top;
    }

    tr td {
        height: 80px;
        padding: 10px;
        padding-top: 20px;
        font-size: 11pt;
        line-height: 1.5;
    }

    tr th {
        height: 40px;
        padding-left: 10px;
        vertical-align: middle;
    }

    tr th:after {
        content: "";

    }

    tr:nth-child(even) {
        background-color: #f9fafb;
        border-style: solid;
        border-width: 0 1px;
        border-color: #e5e7eb;
    }



    tr:nth-child(odd) {
        background-color: #ffffff;
        border-style: solid;
        border-width: 5px 1px 0 1px;
        border-color: #e5e7eb;
    }

    tr:first-child {
        border-width: 1px 1px 0 1px;
    }

    tr:last-child {
        border-width: 0 1px 1px 1px;
    }

    tr:last-child td {
        background: linear-gradient(to bottom, #fff, #f6f7f7); font-weight: bold;
        height: 30px;
    }

    .button {
        display: block;
        font-size: 10pt;
        text-decoration: none;
    }

    .not-paid {
        color: #ff0000; background-color: #ffe6e6;
    }

    .efz-invalid {
        color: #ff0000; background-color: #ffe6e6;
    }

    .efz-not-checked {
        color: #8D914BFF; background-color: #F4E99EFF;
    }
</style>
