<div class="wrap">
	<h1 class="wp-heading-inline">
		<?= __('Event-Overview', BDP_LV_PLUGIN_SLUG); ?> - <?= $event->event_name ?></h1>
	<hr class="wp-header-end">
	<?= kompass_print_event_control_header($event_id, $active_tab); ?>

	<div class="tab-content">
		<?php
        $group_name = ['participant' => 'Teili', 'volunteer' => 'Teami', 'other' => 'Sonstige'];
        $admin_link = 'admin.php?page=kompass-events&action=';

        switch ($active_tab) {
			case 'tab1':

				foreach ($event->groups as $group_int_name => $group) {
                    $gruppen_id = $group_int_name;
					if ( count( $group ) === 0 ) {
						continue;
					}

                    $total_amount = 0;
					foreach ($group as $item ) {
                        $total_amount += $item->beitrag - $item->beitrag_bezahlt;
                    }
                    $total_amount = number_format($total_amount,2,',');

                    echo '<h3>' . $group_name[$group_int_name] . ' (' . count( $group ) . ')</h3>';
                    echo '<h4>' . __('Missing amount') . ': ' .$total_amount . ' Euro</h4>';
					echo '<a href="'. $admin_link . 'send-mail-to-group&event-id=' . $event->id . '&group=' . $group_int_name . '" class="button">Rundmail an alle ' . $group_name[$group_int_name]  . '</a>';
					include dirname( __FILE__ ) . '/list-by-groups.php';
				}

				break;
			case 'tab2':
				foreach ($event->tribes as $tribe => $tmp_group) {
                    $gruppen_id = $tribe;
					if ( count( $tmp_group ) === 0 ) {
						continue;
					}

                    $count_underaged = 0;
                    $count_fullaged = 0;
					$total_amount = 0;
					foreach ($tmp_group as $item ) {
						$total_amount += $item->beitrag - $item->beitrag_bezahlt;
                        if (kompass_is_fullaged($item->geburtsdatum)) {
                            $count_fullaged++;
                        } else {
                            $count_underaged++;
                        }
					}

					$total_amount = number_format($total_amount,2,',');

					$group = new stdClass();
                    $group = $tmp_group;
					echo '<h3>' . $tribe . ' (' . $count_underaged . ' WÖs / Pfadi // ' . $count_fullaged . ' RRs)</h3>';
					echo '<h4>' . __('Missing amount') . ': ' .$total_amount . ' Euro</h4>';
                    echo '<a href="'. $admin_link . 'send-mail-to-local-group&event-id=' . $event->id . '&group=' . $tribe . '" class="button">Rundmail an alle vom Stamm ' . $tribe  . '</a>';
					include dirname( __FILE__ ) . '/list-by-groups.php';
				}
				break;
		}
        $admin_link = 'admin.php?page=kompass-events&action=';
		?>

	</div><br /><br />
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_signup_data', 'event-id=<?= $event->id; ?>');" class="button">Teili-Liste als PDF</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_photo_data', 'event-id=<?= $event->id; ?>');" class="button">Foto-Erlaubnis</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_drink_alcoholic_data', 'event-id=<?= $event->id; ?>');" class="button">Getränkeliste-Liste (alkoholisch) als PDF</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_drink_data', 'event-id=<?= $event->id; ?>');" class="button">Getränkeliste-Liste (alkoholfrei) als PDF</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_kitchen_data', 'event-id=<?= $event->id; ?>');" class="button">Küchenliste</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_kitchen_allergies_data', 'event-id=<?= $event->id; ?>');" class="button">Küchenliste (Allergien)</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_medical_data', 'event-id=<?= $event->id; ?>');" class="button">Sani-Liste</a>
    <a href="#" onclick="kompass_load_ajax_nw('EventParticipants', 'print_invoice_data', 'event-id=<?= $event->id; ?>');" class="button">Beitrags-Liste</a>
    <a href="<?= $admin_link . 'send-mail-to-all&event-id=' . $event->id ?>" class="button">Rundmail an alle</a>
</div>

<div class="overlay" id="kompasss_delete_participant" onclick="document.getElementById('kompasss_delete_participant').style.display='none';">
    <div class="overlay-content">
        <h2>Teili abmelden</h2>
        <form method="post" action="<?= $admin_link; ?>delete-participant">
            <p>Bist du sicher, dass du den Teili <span style="font-weight: bold;" id="confirm-name"></span> wirklich abmelden möchtest?</p>
            <b>Die Anmeldedaten werden gelöscht, und der Vorgang kann nicht rückgängig gemacht werden.</b>
            <input type="hidden" id="confirm-participant-id" name="participant-id"><br><br>
            <input type="submit" value="Ja, abmelden" class="button-primary" style="background-color: #d79494 !important;">
            <input type="button" value="Abbrechen" onclick="document.getElementById('kompasss_delete_participant').style.display='none';" class="button">
        </form>
    </div>
</div>

<div class="overlay" id="kompasss_update_amount" onclick="document.getElementById('kompasss_update_amount').style.display='none';">
    <div class="overlay-content">
        <h2>Zahlungseingang buchen</h2>
        <form method="post" action="<?= $admin_link; ?>update-participant-amount">
            <p>Bitte gib den Gesamtbetrag ein, den <span style="font-weight: bold;" id="name"></span> bisher für die Veranstaltung gezahlt hat:</p>
            <input type="text" name="beitrag_bezahlt" id="beitrag_bezahlt" /> Euro
            <input type="hidden" id="payment-participant-id" name="participant-id"><br><br>
            <input type="submit" value="Speichern" class="button-primary" style="background-color: #d79494 !important;">
            <input type="button" value="Abbrechen" onclick="document.getElementById('kompasss_update_amount').style.display='none';" class="button">
        </form>
    </div>
</div>

<div class="overlay" id="kompasss_participant_details" onclick="document.getElementById('kompasss_participant_details').style.display='none';">
    <div class="overlay-content">
         <div id="kompass_participant_data"></div>
    </div>
</div>

<script>
    function kompass_load_participant_data(data_id) {
        kompass_load_ajax_div('EventParticipants', 'kompass_print_participant_summary', 'kompass_participant_data', 'participant-id=' + data_id)
        document.getElementById('kompasss_participant_details').style.display = 'block';
    }

    // JavaScript, um das Overlay zu öffnen und das Formular zu übermitteln
    function kompass_participant_confirm_delete(data_name, data_id) {
        var name = data_name;
        var id = data_id;
        document.getElementById('confirm-name').textContent = name;
        document.getElementById('confirm-participant-id').value = id;
        document.getElementById('kompasss_delete_participant').style.display = 'block';
    }

    function kompass_send_email_to_participant(participant_id) {
        kompass_load_ajax_div('EventParticipants', 'kompass_print_participant_mail_form', 'kompass_participant_data', 'participant-id=' + participant_id)
        document.getElementById('kompasss_participant_details').style.display = 'block';
    }

    function kompass_participant_update_amount(data_amount, data_name, data_id) {
        var name = data_name;
        var id = data_id;
        document.getElementById('beitrag_bezahlt').value = data_amount;
        document.getElementById('name').textContent = name;
        document.getElementById('payment-participant-id').value = id;
        document.getElementById('kompasss_update_amount').style.display = 'block';
    }
</script>





<style>
    .overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }
    .overlay-content {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: white;
        padding: 20px;
        border-radius: 5px;
    }

    @media screen and (max-width: 700px) {
        .participant_group thead th:nth-child(3),
        .participant_group thead th:nth-child(5),
        .participant_group thead th:nth-child(4),
        .participant_group td:nth-child(5),
        .participant_group td:nth-child(3),
        .participant_group td:nth-child(4) {
            display: none !important;
        }
    }

</style>