<?php

namespace Blog\Model;

use PDOException;

class EventModel extends Database {

    public function getEvents() {
        $pdo = $this->linkDB();
        $sql = "SELECT * FROM ausstellung ORDER BY datum_von DESC;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute();
            return $sth->fetchAll(\PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Lesen der Events.", $e);
            die;
        }
    }

    public function getEvent($id) {
        $pdo = $this->linkDB();
        $sql = "SELECT * FROM ausstellung WHERE ausstellungid = :id;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute([":id" => $id]);
            return $sth->fetch(\PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Lesen des Events.", $e);
            die;
        }
    }

    public function updateEvent($id, $data) {
        $pdo = $this->linkDB();
        $sql = "UPDATE ausstellung SET standortid = :standortid, datum_von = :datum_von, datum_bis = :datum_bis, name = :name, beschreibung = :beschreibung, max_tickets = :max_tickets, preis = :preis WHERE ausstellungid = :id;";
        $params = [
            ":standortid" => $data['standortid'],
            ":datum_von" => $data['datum_von'],
            ":datum_bis" => $data['datum_bis'],
            ":name" => $data['name'],
            ":beschreibung" => $data['beschreibung'],
            ":max_tickets" => $data['max_tickets'],
            ":preis" => $data['preis'],
            ":id" => $id
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Aktualisieren des Events.", $e);
            die;
        }
    }

    public function createEvent($data) {
        $pdo = $this->linkDB();
        $sql = "INSERT INTO ausstellung (standortid, datum_von, datum_bis, name, beschreibung, max_tickets, preis) VALUES (:standortid, :datum_von, :datum_bis, :name, :beschreibung, :max_tickets, :preis);";
        $params = [
            ":standortid" => $data['standortid'],
            ":datum_von" => $data['datum_von'],
            ":datum_bis" => $data['datum_bis'],
            ":name" => $data['name'],
            ":beschreibung" => $data['beschreibung'],
            ":max_tickets" => $data['max_tickets'],
            ":preis" => $data['preis']
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
            return $sth;
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Erstellen des Events.", $e);
            die;
        }
    }

    public function deleteEvent($id) {
        $pdo = $this->linkDB();
        $sql = "DELETE FROM ausstellung WHERE ausstellungid = :id;";
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute([":id" => $id]);
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Löschen des Events.", $e);
            die;
        }
    }
}