<?php
session_start();
require_once(__DIR__ . "/../inc/config.inc.php");
require_once(__DIR__ . "/../inc/functions.inc.php");


//Überprüfe, dass der User eingeloggt ist
//Der Aufruf von check_user() muss in alle internen Seiten eingebaut sein
$user = check_intern_user();

include("templates/header.inc.php");

if(isset($_GET['save'])) {
	$save = $_GET['save'];
	
	if($save == 'personal_data') {
		$vorname = trim($_POST['vorname']);
		$nachname = trim($_POST['nachname']);
		$tele = trim($_POST['tele']);
		$geburtstag = trim($_POST['geburtstag']);
		$ort = trim($_POST['ort']);
		$strasse = trim($_POST['strasse']);
		$plz = trim($_POST['plz']);
		$kassenart = trim($_POST['kassenart']);
		
		if($vorname == "" || $nachname == "") {
			$error_msg = "Bitte Vor- und Nachname ausfüllen.";
		} else {
			$statement = $pdo->prepare("UPDATE intern_users SET tele = :tele, geburtstag = :geburtstag, ort = :ort, strasse = :strasse, plz = :plz, kassenart = :kassenart, vorname = :vorname, nachname = :nachname, updated_at=NOW() WHERE id = :userid");
			$result = $statement->execute(array('tele' => $tele, 'geburtstag'=> $geburtstag, 'ort' => $ort, 'strasse'=> $strasse, 'plz' => $plz, 'kassenart'=> $kassenart, 'vorname' => $vorname, 'nachname'=> $nachname, 'userid' => $user['id'] ));
			
			$success_msg = "Daten erfolgreich gespeichert.";
		}
		/*
	} 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['passwort'])) {
			$error_msg = "Bitte korrektes Passwort eingeben.";
		} else {
			$statement = $pdo->prepare("UPDATE intern_users SET email = :email WHERE id = :userid");
			$result = $statement->execute(array('email' => $email, 'userid' => $user['id'] ));
				
			$success_msg = "E-Mail-Adresse erfolgreich gespeichert.";
		}
		*/
	} 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['passwort'])) {
			$error_msg = "Bitte korrektes Passwort eingeben.";
		} else {
			$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
				
			$statement = $pdo->prepare("UPDATE intern_users SET passwort = :passwort WHERE id = :userid");
			$result = $statement->execute(array('passwort' => $passwort_hash, 'userid' => $user['id'] ));
				
			$success_msg = "Passwort erfolgreich gespeichert.";
		}
		
	}
}


?>

<div class="container main-container">

<h1>Einstellungen</h1>

<?php 
if(isset($success_msg) && !empty($success_msg)):
?>
	<div class="alert alert-success">
		<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
	  	<?php echo $success_msg; ?>
	</div>
<?php 
endif;
?>

<?php 
if(isset($error_msg) && !empty($error_msg)):
?>
	<div class="alert alert-danger">
		<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
	  	<?php echo $error_msg; ?>
	</div>
<?php 
endif;
?>

<div>

  <!-- Nav tabs (Bootstrap 5) -->
  <ul class="nav nav-tabs" id="settingsTabs" role="tablist">
    <li class="nav-item" role="presentation">
      <button class="nav-link active"
              id="data-tab"
              data-bs-toggle="tab"
              data-bs-target="#data"
              type="button"
              role="tab"
              aria-controls="data"
              aria-selected="true">
        Persönliche Daten
      </button>
    </li>

    <!--
    <li class="nav-item" role="presentation">
      <button class="nav-link"
              id="email-tab"
              data-bs-toggle="tab"
              data-bs-target="#email"
              type="button"
              role="tab"
              aria-controls="email"
              aria-selected="false">
        E-Mail
      </button>
    </li>
    -->

    <li class="nav-item" role="presentation">
      <button class="nav-link"
              id="passwort-tab"
              data-bs-toggle="tab"
              data-bs-target="#passwort"
              type="button"
              role="tab"
              aria-controls="passwort"
              aria-selected="false">
        Passwort
      </button>
    </li>
  </ul>

  <div class="tab-content" id="settingsTabsContent">
    <!-- Persönliche Daten -->
    <div class="tab-pane fade show active" id="data" role="tabpanel" aria-labelledby="data-tab" tabindex="0">
      <br>
      <form action="?save=personal_data" method="post" class="form-horizontal">
        <div class="form-group">
          <label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputVorname" name="vorname" type="text"
                   value="<?php echo htmlentities($user['vorname']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputNachname" name="nachname" type="text"
                   value="<?php echo htmlentities($user['nachname']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputGeburtstag" class="col-sm-2 control-label">Geburtstag</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputGeburtstag" name="geburtstag" type="date"
                   value="<?php echo htmlentities($user['geburtstag']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputTelefon" class="col-sm-2 control-label">Telefon</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputTelefon" name="tele" type="text"
                   value="<?php echo htmlentities($user['tele']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputStrasse" class="col-sm-2 control-label">Straße, Hausnummer</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputStrasse" name="strasse" type="text"
                   value="<?php echo htmlentities($user['strasse']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputPLZ" name="plz" type="text"
                   value="<?php echo htmlentities($user['plz']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputOrt" class="col-sm-2 control-label">Ort</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputOrt" name="ort" type="text"
                   value="<?php echo htmlentities($user['ort']); ?>" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputkassenart" class="col-sm-2 control-label">Kassen-/ Privatpatient</label>
          <div class="col-sm-10">
            <select class="form-control" id="inputkassenart" name="kassenart" required>
              <option value="0" <?php if($user['kassenart'] == '0'){ echo "selected";} ?>>Kassenpatient</option>
              <option value="1" <?php if($user['kassenart'] == '1'){ echo "selected";} ?>>Privat versichert</option>
            </select>
          </div>
        </div>
		<br>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">Speichern</button>
          </div>
        </div>
      </form>
    </div>

    <!-- Änderung der E-Mail-Adresse -->
    <!--
    <div class="tab-pane fade" id="email" role="tabpanel" aria-labelledby="email-tab" tabindex="0">
      ...
    </div>
    -->

    <!-- Änderung des Passworts -->
    <div class="tab-pane fade" id="passwort" role="tabpanel" aria-labelledby="passwort-tab" tabindex="0">
      <br>
      <p>Zum Änderen deines Passworts gib bitte dein aktuelles Passwort sowie das neue Passwort ein.</p>

      <form action="?save=passwort" method="post" class="form-horizontal">
        <div class="form-group">
          <label for="inputPasswortAlt" class="col-sm-2 control-label">Altes Passwort</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputPasswortAlt" name="passwortAlt" type="password" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" required>
          </div>
        </div>

        <div class="form-group">
          <label for="inputPasswortNeu2" class="col-sm-2 control-label">Neues Passwort (wiederholen)</label>
          <div class="col-sm-10">
            <input class="form-control" id="inputPasswortNeu2" name="passwortNeu2" type="password" required>
          </div>
        </div>

        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">Speichern</button>
          </div>
        </div>
      </form>
    </div>
  </div>

</div>




</div>
<?php 
include("templates/footer.inc.php")
?>
