<?php
namespace App\User;

use App\Core\AbstractController;

class LoginController extends AbstractController
{

  public function __construct(LoginService $loginService) {
    $this->loginService = $loginService;
  }
//  public function index(){
//    $this->loginService->check();
//    $this->render("person/index", []);
//  }
  public function homepages() {
      //$this->loginService->check();
    $this->render("user/homepages", []);
  }

  public function dashboard() {
    $this->loginService->check();
    $this->render("user/dashboard", []);
  }

  public function impressum() {
    $this->loginService->impressum();
    $this->render("user/impressum", []);
  }

  public function privat() {
    $this->loginService->check();
    $this->render("user/privat", []);
  }

  public function logout() {
    $this->loginService->logout();
    $this->render("user/logout", []);
    //header("Location: homepages");
  }

  public function login() {
    $error = false;
    if (!empty($_POST['username']) And !empty($_POST['password'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if ($this->loginService->attempt($username, $password)) {
          $_SESSION['login'] = $username;
          header ("Location: index");
          //header ("Location: dashboard");
          return;
        } else {
          $error = true;
        }
    }
    $this->render("user/login", [
      'error' => $error               // Übergabe an view
    ]);
  }
}
?>
