<?php

namespace Blog\Model;

use PDOException;

class StandortModel extends Database {

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

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

    public function createStandort($data) {
        $pdo = $this->linkDB();
        $sql = "INSERT INTO location (street, house_number, postal_code, city, country, phone, email)
                VALUES (:street, :house_number, :postal_code, :city, :country, :phone, :email);";
        $params = [
            ":street" => $data['street'],
            ":house_number" => $data['house_number'],
            ":postal_code" => $data['postal_code'],
            ":city" => $data['city'],
            ":country" => $data['country'],
            ":phone" => $data['phone'],
            ":email" => $data['email']
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
            return $pdo->lastInsertId();
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Anlegen des Standorts.", $e);
            die;
        }
    }

    public function updateStandort($location_id, $data) {
        $pdo = $this->linkDB();
        $sql = "UPDATE location SET
                    street = :street,
                    house_number = :house_number,
                    postal_code = :postal_code,
                    city = :city,
                    country = :country,
                    phone = :phone,
                    email = :email
                WHERE location_id = :location_id;";
        $params = [
            ":street" => $data['street'],
            ":house_number" => $data['house_number'],
            ":postal_code" => $data['postal_code'],
            ":city" => $data['city'],
            ":country" => $data['country'],
            ":phone" => $data['phone'],
            ":email" => $data['email'],
            ":location_id" => $location_id
        ];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
            return $sth->rowCount();
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Aktualisieren des Standorts.", $e);
            die;
        }
    }

    public function deleteStandort($location_id) {
        $pdo = $this->linkDB();
        $sql = "DELETE FROM location WHERE location_id = :location_id;";
        $params = [":location_id" => $location_id];
        try {
            $sth = $pdo->prepare($sql);
            $sth->execute($params);
            return $sth->rowCount();
        } catch (PDOException $e) {
            new \Blog\Library\ErrorMsg("Fehler beim Löschen des Standorts.", $e);
            die;
        }
    }
}