<?php
/*
======================
Kasutaja paneel
"Andmed" vaade
======================
*/
include 'config.php';
session_start();
include_once ROOT . 'db/dbconfig.php';
require_once ROOT . 'admin/objects/kasutaja.class.php';
$user_home = new Kasutaja();
include ROOT . "header.php";

// Andmete uuendamise vormi käsitlemine
if ($_POST){
    $user_home->id = $_SESSION['userSession'];
    $user_home->telefon = $_POST["telefon"];
    $user_home->email = $_POST["email"];
    $user_home->aadress = $_POST["aadress"];
    $user_home->linn = $_POST["linn"];
    $user_home->maakond = $_POST["maakond"];
    $user_home->postiindeks = $_POST["postiindeks"];
    $user_home->riik = $_POST["riik"];

    // Muudame andmed andmebaasis
    if($user_home->muudaAndmed()){
        $msg = "<div class=\"alert alert-success alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Andmed edukalt muudetud.</div>";
    } else {
      $msg = "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Andmete uuendamisel ilmnes viga. Palun proovige uuesti.</div>";
  }
}


?>

<div class="container">

    <div class="row">

        <?php
        include "kasutaja-nav.php";
        ?>

        <div class="col-md-9">
            <?php
            $user_home->id = $_SESSION['userSession'];
            $stmt = $user_home->loeAndmed();
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            extract($row);
            ?>
            <h3 class="decorated"><span>Andmete muutmine</span></h3><br />

            <form class="form-horizontal" method="post" action="andmed.php">

                <?php
                if(isset($msg)){
                   echo $msg;
               }
               else{
               }
               ?>
               <div class="col-md-8 col-md-offset-2">
                   <div class="form-group">
                       <div class="col-sm-6">
                           <label>Eesnimi:</label>
                           <input type="text" class="form-control" value="<?= $eesnimi ?>" name="eesnimi" disabled  />
                       </div>
                       <div class="col-sm-6">
                           <label>Perekonnanimi:</label>
                           <input type="text" class="form-control" value="<?= $perekonnanimi ?>" name="perekonnanimi" disabled  />
                       </div>
                   </div>

                   <div class="form-group">
                       <div class="col-sm-6">
                           <label>Isikukood:</label>
                           <input type="number" class="form-control" value="<?= $isikukood ?>" name="isikukood" disabled  />
                       </div>
                       <div class="col-sm-6">
                           <label>Telefon:</label>
                           <input type="number" class="form-control" value="<?= $telefon ?>" name="telefon" required  />
                       </div>

                   </div>

                   <div class="form-group">
                       <div class="col-sm-6">
                           <label>E-mail:</label>
                           <input type="email" class="form-control" value="<?= $email ?>" name="email" required  />
                       </div>
                       <div class="col-sm-6">
                           <label>Aadress:</label>
                           <input type="text" class="form-control" value="<?= $aadress ?>" name="aadress" required  />
                       </div>

                   </div>

                   <div class="form-group">
                       <div class="col-sm-6">
                           <label>Linn või asula:</label>
                           <input type="text" class="form-control" value="<?= $linn ?>" name="linn" required  />
                       </div>
                       <div class="col-sm-6">
                           <label>Maakond:</label>
                           <input type="text" class="form-control" value="<?= $maakond ?>" name="maakond" required  />
                       </div>

                   </div>

                   <div class="form-group">
                       <div class="col-sm-6">
                           <label>Postiindeks:</label>
                           <input type="number" class="form-control" value="<?= $postiindeks ?>" name="postiindeks" required  />
                       </div>
                       <div class="col-sm-6">
                           <label>Riik:</label>
                           <input type="text" class="form-control" value="<?= $riik ?>" name="riik" required  />
                       </div>

                   </div>
                   <div class="form-group">
                    <center>
                        <button type="submit" class="btn btn-default" name="btn-signup">
                          <span class="glyphicon glyphicon-log-in"></span> &nbsp; Muuda andmed
                      </button> 
                  </center>
              </div> 

          </form>
          <hr />
</div>
</div>
</div>
</div>

<?php
include ROOT . "footer.php";
?>