<?php

namespace Bdp\Modules\EventParticipants\Controllers;

use Bdp\Modules\EventParticipants\Models\Event;
use function Bdp\Modules\EventParticipants\Views\registrationForm;

class ParticipationSignupController
{
	public static function saveRegistration(array $postedData) : string {
		global $dbHandler;

        $event = Event::loadById($postedData['eventId']);

        $participation_types = ['other', 'participant', 'volunteer'];
		if (isset($postedData['foto']) && is_array($postedData['foto'])) {
			foreach ($postedData['foto'] as $fotoData) {
				$postedData['foto_' . $fotoData] = true;
			}
		}

		if ($postedData['badeerlaubnis'] == '') {
			$postedData['badeerlaubnis'] = 'complete';
		}
      	if (isset($postedData['zusatzbeitrag'])) {
			(int)$postedData['beitrag'] += (int)$postedData['zusatzbeitrag'];
          	unset($postedData['zusatzbeitrag']);
        }
        
		unset($postedData['foto']);
		unset($postedData['_dsgvo_accept']);
        $postedData['teilnahme'] = $participation_types[$postedData['gruppe_id']];
        unset($postedData['gruppe_id']);
		if (null == $dbHandler->insertRows(MainController::KOMPASS_EVENTS_PARTICIPANTS, $postedData)) {
            $errorPage = file_get_contents(dirname(__FILE__) . '/../Templates/Partials/Registration/error.webseite.php');
            $errorPage = sprintf($errorPage,
                $postedData['vorname'],
                $event->event_email,
                $event->event_name,
                $event->event_email
            );

            wp_mail($event->event_email, 'Anmeldung für "' . $event->event_name . '" fehlerhaft', print_r($postedData, true));

            return $errorPage;
        }

		$successMail = file_get_contents(dirname(__FILE__) . '/../Templates/Partials/Registration/done.email.php');
		$successMail = sprintf($successMail,
			$postedData['vorname'],
            $event->event_name,
			\DateTime::createFromFormat('Y-m-d', $postedData['anreise'])->format('d.m.Y'),
			\DateTime::createFromFormat('Y-m-d', $postedData['abreise'])->format('d.m.Y'),
		    ucfirst($postedData['essgewohnheit']),
            $event->event_email,
            $event->event_name,
            $event->event_email
		);

		$successPage = file_get_contents(dirname(__FILE__) . '/../Templates/Partials/Registration/done.webseite.php');
		$successPage = sprintf($successPage,
			$postedData['vorname'],
            $event->event_name,
			\DateTime::createFromFormat('Y-m-d', $postedData['anreise'])->format('d.m.Y'),
			\DateTime::createFromFormat('Y-m-d', $postedData['abreise'])->format('d.m.Y'),
			ucfirst($postedData['essgewohnheit']),
            $event->event_email,
            $event->event_name,
            $event->event_email
		);

		$sent = wp_mail(
			$postedData['email_1'],
            'Anmeldung zu "' . $event->event_name . '"',
			$successMail,
			['Reply-To: ' . $event->event_email, 'Content-Type: text/html; charset=UTF-8']);


		if ('' !== $postedData['email_2']) {
			$sent = wp_mail(
				$postedData['email_2'],
				'Anmeldung zu "' . $event->event_name . '"',
				$successMail,
				['Reply-To: ' . $event->event_email, 'Content-Type: text/html; charset=UTF-8']);
		}

		return $successPage;
	}
}