<?php

namespace Bdp\Modules\EventParticipants\Controllers;

use Bdp\Modules\EventParticipants\Models\Event;

class PrintParticipantListDrinkingPdfController {

	private function get_table_header(string $event_name) : string {
		return '<h1>Getränkeliste (alkoholfrei) für ' . $event_name . '</h1><br /><br /><br /><br /><table style="border-spacing: 0; width: 100%;page-break-after: always">' .
		       '<tr>' .
		       '<td>Vorname</td>' .
		       '<td>Nachname</td>' .
		       '<td>Stamm</td>' .
		       '<td>Geburtsdatum</td>' .
               '<td>Getränke</td>';
	}

	public function __construct() {
        global $_POST, $_REQUEST, $dbHandler;

        $group_name = ['participant' => 'Teili', 'volunteer' => 'Teami', 'other' => 'Sonstige'];

		$event = Event::loadById( $_REQUEST['event-id'] );
		$output = '';

		$i = 0;
		foreach ( $event->tribes as $tribe => $participants ) {
			if ( count( $participants ) == 0 ) {
				continue;
			}
			foreach ( $participants as $participant ) {


				if ( $i == 0 ) {
					$output .= $this->get_table_header( $event->event_name );
				}
				$i ++;

				$output .= '<tr style="min-height: 80px; height: 80px; border-style: solid; border-width: 1px;">' .
				           '<td style="width: 150px; border-style: solid; border-width: 1px;">' . $participant->vorname .
                            ('' != $participant->pfadiname ? '<br /> (' . $participant->pfadiname . ')' : '') . '</td>' .
				           '<td style="width: 150px; border-style: solid; border-width: 1px;">' . $participant->nachname . '</td>' .
				           '<td style="padding-right: 100px; border-style: solid; border-width: 1px;">' . $tribe . '</td>' .
				           '<td style="padding-right: 50px; border-style: solid; border-width: 1px;">' .
				           \DateTime::createFromFormat( 'Y-m-d', $participant->geburtsdatum )->format( 'd.m.Y' ) . '<br />' .
				           '(' . $participant->get_age() . ' Jahre)</td>' .
				           '<td style="padding-right: 150px; border-style: solid; border-width: 1px;"></td></tr>';
				if ( $i == 12 ) {
					$output .= '</table>';
					$i      = 0;
				}
			}
		}
		$output .= '</table></body></html>';
		kompass_create_pdf($output,$event->event_name . ' Anmeldeliste.pdf', 'landscape');
	}
}