<?php

namespace Blog\Controller;

use Blog\Model\EventModel;

class EventController {

    private $model;
    private $view;

    public function __construct($view) {
        $this->model = new EventModel();
        $this->view = $view;
    }

    public function showEvents() {
        $events = $this->model->getEvents();
        $this->view->setVars([
            'events' => $events
        ]);
    }

    public function createEvent() {
        $data = [
            'name' => $_POST['name'] ?? null,
            'beschreibung' => $_POST['beschreibung'] ?? null,
            'standortid' => $_POST['standortid'] ?? null,
            'datum_von' => $_POST['datum_von'] ?? null,
            'datum_bis' => $_POST['datum_bis'] ?? null,
            'max_tickets' => $_POST['max_tickets'] ?? null,
            'preis' => $_POST['preis'] ?? null
        ];

        $this->model->createEvent($data);
        $this->view->setVars(['event' => $data]);
        exit;
    }

    public function editEventForm() {
        $id = $_GET['ausstellungid'];
        $event = $this->model->getEvent($id);
        $this->view->setVars(['event' => $event]);
    }

    public function updateEvent($id, $data) {
        $id = $_POST['ausstellungid'];
        $data = [
            'standortid' => $_POST['standortid'] ?? null,
            'datum_von' => $_POST['datum_von'] ?? null,
            'datum_bis' => $_POST['datum_bis'] ?? null,
            'name' => $_POST['name'] ?? null,
            'beschreibung' => $_POST['beschreibung'] ?? null,
            'max_tickets' => $_POST['max_tickets'] ?? null,
            'preis' => $_POST['preis'] ?? null
        ];
        $this->model->updateEvent($id, $data);
    }

    public function deleteEvent($id) {
        $this->model->deleteEvent($id);
        $this->view->setVars(['id' => $id]);
    }
}