<?php

namespace Bdp\Modules\EventParticipants\Controllers;

use Bdp\Modules\EventParticipants\Models\Event;

class PrintParticipantListKitchenAllergiesPdfController {

	private function get_table_header(string $event_name) : string {
		return '<h1>Zusatz zur Küchenliste 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>Allergien</td>' .
		       '<td>Essgewohnheit</td>' .
		       '<td>Anreise</td>' .
		       '<td>Abreise</td>';
	}

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

		$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 ($participant->allergien == '') {
					continue;
				}

				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="padding-right: 10px; border-style: solid; border-width: 1px;">' . $participant->vorname . '<br /><br /></td>' .
				           '<td style="padding-right: 10px; border-style: solid; border-width: 1px;">' . $participant->nachname . '</td>' .
				           '<td style="padding-right: 10px; border-style: solid; border-width: 1px;">' . $tribe . '</td>' .
				           '<td style="border-style: solid; border-width: 1px;">' .$participant->allergien . '</td>' .
				           '<td style="border-style: solid; border-width: 1px;">' .$participant->essgewohnheit . '</td>' .
				           '<td style="border-style: solid; border-width: 1px;">' .\DateTime::createFromFormat('Y-m-d', $participant->anreise)->format('d.m.Y') . '</td>' .
				           '<td style="border-style: solid; border-width: 1px;">' .\DateTime::createFromFormat('Y-m-d', $participant->abreise)->format('d.m.Y'). '</td></tr>';
				if ( $i == 13 ) {
					$output .= '</table>';
					$i      = 0;
				}
			}
		}
		$output .= '</table></body></html>';
		kompass_create_pdf($output,$event->event_name . ' Zusatz Küchenliste.pdf', 'landscape');
	}
}