<?php

use Bdp\Modules\EventParticipants\Controllers\MainController;

function kompass_print_events_overview() {
	global $dbHandler;
	?>
    <h2>Veranstaltungsübersicht</h2>
	<table class="wp-list-table widefat fixed striped table-view-list" id="myTable">
		<thead>
		<tr>
			<th scope="col" class="manage-column column-name"><?= __('Event Name', BDP_LV_PLUGIN_SLUG); ?></th>
			<th scope="col" class="manage-column column-name"><?= __('Count participants', BDP_LV_PLUGIN_SLUG); ?></th>
			<th class="manage-column column-name"><?= __('Actions', BDP_LV_PLUGIN_SLUG); ?></th>
		</tr>
		</thead>
		<tbody>
		<?php

		foreach ($dbHandler->readFromDb( MainController::KOMPASS_EVENTS_EVENTS, ['archived' => false]) as $event_data) {
            $event = \Bdp\Modules\EventParticipants\Models\Event::loadById($event_data->id);
			$participant_count = 0;
            $amount = 0;
            foreach ($event->groups as $group => $participants) {
                $participant_count += count($participants);
                foreach ($participants as $group_participant) {
                    $amount += $group_participant->beitrag;
                }
            }


			echo '<tr>';
			echo '<td><a href="' . admin_url('admin.php?page=kompass-events&action=show-event&event-id=' . $event->id ) . '">' . $event->event_name  .'</a></td>';
			echo '<td>' . $participant_count . '<br />' . number_format($amount, 2, ',') . ' Euro<br />';
            echo  number_format($participant_count > 0 ? $amount / $participant_count : 0, 2, ',') . ' Euro / Teili  </td>';
			echo '<td> ' .
                 '<a href="' . admin_url('admin.php?page=kompass-events&action=edit-event&event-id=' . $event->id ) . '">' . __('Edit', BDP_LV_PLUGIN_SLUG) . '</a><br />' .
			     ($event->signup_allowed ?
                 '<a style="color: #ff0000;" href="' . admin_url('admin.php?page=kompass-events&action=close-event&event-id=' . $event->id ) . '">' . __('Deny registration', BDP_LV_PLUGIN_SLUG) . '</a>' :
			     '<a style="color: #72b752;" href="' . admin_url('admin.php?page=kompass-events&action=open-event&event-id=' . $event->id ) . '">' . __('Allow registration', BDP_LV_PLUGIN_SLUG) . '</a>') .
                 '<br />' .
			     '<a style="color: #ff0000;" href="' . admin_url('admin.php?page=kompass-events&action=archive-event&event-id=' . $event->id ) . '">' . __('Move to Archive', BDP_LV_PLUGIN_SLUG) . '</a></td>';
			echo '</tr>';
		}
		?>
		</tbody>
	</table><br /><br />


    <a class="button" href=" <?= admin_url('admin.php?page=kompass-events') . '&action=new-event'; ?>"><?= __( 'New Event', BDP_LV_PLUGIN_SLUG ); ?></a>

<?php
}
