<?php

namespace Blog\Controller;

use Blog\Model\StandortModel;

class StandortController {

    private $model;
    private $view;

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

    public function showStandorte() {
        $standorte = $this->model->getStandorte();
        $this->view->setVars(['standorte' => $standorte]);
    }

    public function createStandort() {
        $data = [
            'straße' => $_POST['straße'],
            'hausnr' => $_POST['hausnr'],
            'postleitzahl' => $_POST['postleitzahl'],
            'ort' => $_POST['ort'],
            'land' => $_POST['land'],
            'tel' => $_POST['tel'],
            'email' => $_POST['email']
        ];
        $erg = $this->model->createStandort($data);
        $this->view->setVars(['standort' => $erg]);
    }

    public function editStandortForm() {
        $id = $_GET['standortid'];
        $standort = $this->model->getStandort($id);
        $this->view->setVars(['standort' => $standort]);
    }

    public function updateStandort() {
        $id = $_POST['standortid'];
        $data = [
            'straße' => $_POST['straße'],
            'hausnr' => $_POST['hausnr'],
            'postleitzahl' => $_POST['postleitzahl'],
            'ort' => $_POST['ort'],
            'land' => $_POST['land'],
            'tel' => $_POST['tel'],
            'email' => $_POST['email']
            ];
        $erg = $this->model->updateStandort($id, $data);
        $this->view->setVars(['standort' => $erg]);
    }

    public function deleteStandort() {
        $id = $_GET['standortid'] ?? null;
        $this->model->deleteStandort($id);
    }
}