<?php
namespace App\User;
use App\Core\AbstractRepository;
use PDO;

class UsersRepository extends AbstractRepository {
// abstract functions aus dem AbstractRepositery müssen hier definiert werden!
  public function getTableName() {
    return "users";
  }

  public function getModelName() {
    return "App\\User\\UserModel";
  }
// nachstehende function findet den username und gibt array des users zurück
  public function findByUserID($id)  {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE id = :id");
    $stmt->execute(['id' => $id]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, $model);
    $user = $stmt->fetch(PDO::FETCH_CLASS);
//    var_dump($user); //die();
    return $user;
  }

  public function findByUserName($username)  {
      $table = $this->getTableName();
      $model = $this->getModelName();

      $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE username = :username");
      $stmt->execute(['username' => $username]);
      $stmt->setFetchMode(PDO::FETCH_CLASS, $model);
      $user = $stmt->fetch(PDO::FETCH_CLASS);
      //var_dump($user); die();
      return $user;
    }

    public function allUserNames()  {
        $table = $this->getTableName();
        $model = $this->getModelName();

        $strSQL = "SELECT * FROM `$table`";
        $stmt = $this->pdo->query($strSQL);
        $users = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
        return $users;
      }


// *****************************************************************************
    public function saveUserData()  {
      $error_msg = $success_msg = "";

    if (isset($_GET['save'])) {
      $save = $_GET['save'];
      $userid = trim($_POST['id']);
      $id  = trim($_POST['id']);
      $user = $this->findByUserID($id);

      if ($save == 'personal_data') {
        $vorname = trim($_POST['vorname']);
        $nachname = trim($_POST['nachname']);
        if ($vorname == "" || $nachname == "") {
          $error_msg = "Bitte Vor- und Nachname ausfüllen.";
        } else {
          $statement = $this->pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, updated_at=NOW() WHERE id = :id");
          $result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname, 'id' => $id));
          $success_msg = "Daten erfolgreich gespeichert.";
        }
      // emai geändert
      } else if ($save == 'email') {
        $passwort = $_POST['passwort'];
        $email = trim($_POST['email']);
        $email2 = trim($_POST['email2']);
        if ($email != $email2) {
          $error_msg = "Die eingegebenen E-Mail-Adressen stimmten nicht überein.";
        } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $error_msg = "Bitte eine gültige E-Mail-Adresse eingeben.";
        } else
        if (password_verify($passwort, $user['password'])) {
          $error_msg = "Bitte korrektes Passwort eingeben.";
        } else {
          $statement = $this->pdo->prepare("UPDATE users SET email = :email WHERE id = :userid");
          $result = $statement->execute(array('email' => $email, 'userid' => $user['id'] ));
          $success_msg = "E-Mail-Adresse erfolgreich gespeichert!";

        }
      // PW Änderung
      } else if ($save == 'passwort') {
        $passwortAlt = $_POST['passwortAlt'];
        $passwortNeu = trim($_POST['passwortNeu']);
        $passwortNeu2 = trim($_POST['passwortNeu2']);

        if ($passwortNeu != $passwortNeu2) {
          $error_msg = "Die eingegebenen Passwörter stimmten nicht überein.";
        } else if($passwortNeu == "") {
          $error_msg = "Das Passwort darf nicht leer sein.";
        } else if(!password_verify($passwortAlt, $user['password'])) {
          $error_msg = "Bitte korrektes Passwort eingeben";
        } else {
          $passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
          //$oasswort_hash = password_hash($passwordNeu, PASSWORD_BCRYPT);

          $statement = $this->pdo->prepare("UPDATE users SET password = :password WHERE id = :userid");
          $result = $statement->execute(array('password' => $passwort_hash, 'userid' => $user['id'] ));

          $success_msg = "Passwort erfolgreich gespeichert.";
        }
      }
    } // Ende if save = personel data, ...
    return $error_msg." - ".$success_msg;
  } // Ende Funktion

  }
?>
