<!-- Realisatsiooni aluseks on võetud:
PHP Object Oriented CRUD Example with Bootstrap
Kasutatud 6.04.2016
https://www.codeofaninja.com/2014/06/php-object-oriented-crud-example-oop.html
-->

<?php 
/*
============================
Administraatori paneel
Kataloogi uuendamine
============================
*/
include "config.php";
include ADMIN_ROOT . "header.php";
?>

<!-- DATEPICKER -->
<script type="text/javascript" src="<?php echo HTTP ?>scripts/moment/min/moment.min.js"></script>
<script type="text/javascript" src="<?php echo HTTP ?>js/bootstrap.min.js"></script>
<script type="text/javascript" src="<?php echo HTTP ?>scripts/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<link rel="stylesheet" href="<?php echo HTTP ?>scripts/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" />

<?php

$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Selline ID puudub.');

include_once ROOT . 'db/dbconfig.php';
include_once ADMIN_ROOT . 'objects/kataloog.class.php';

$database = new Database();
$db = $database->dbConnection();

$kataloog = new Kataloog($db);
$kataloog->id = $id;
$kataloog->readInfo();

// Kataloogi uuendamise vormi andmete käsitlemine
if($_POST){

  $kataloog->nimi=$_POST['nimi'];
  $kataloog->aeg=$_POST['aeg'];

  // Uuendame kataloogi
  if($kataloog->update()){
    echo "<div class=\"alert alert-success alert-dismissable\">";
    echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
    echo "Kataloogi uuendamine õnnestus.";
    echo "</div>";
  }

  else{
    echo "<div class=\"alert alert-danger alert-dismissable\">";
    echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
    echo "Kataloogi uuendamine ebaõnnestus.";
    echo "</div>";
  }
}
?>

<h2 class='decorated'><span>Kataloogi muutmine</span></h2>
<br/><br/>
<form action='update_kataloog.php?id=<?php echo $id; ?>' method='post'>

  <table class='table table-hover table-responsive table-bordered'>

    <tr>
      <td width="10%">Nimi</td>
      <td><input type='text' name='nimi' value='<?php echo $kataloog->nimi; ?>' class='form-control' /></td>
    </tr>
    <tr>
      <td>Aeg</td>
      <td>
        <div class="row">
          <div class='col-sm-6'>

            <script type="text/javascript">

              $(function () {
                $('#datetimepicker').datetimepicker({
                  format: 'YYYY-MM-DD HH:mm'
                });
              });
              var today = new Date();
            </script>

            <div class='input-group date' id='datetimepicker'>

              <input name='aeg' type='text' class='form-control' value='<?php echo $kataloog->aeg; ?>' required />
              <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
              </span>
            </div></div>
          </div>
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
          <button type="submit" class="btn btn-primary">Salvesta muudatused</button>
        </td>
      </tr>

    </table>
  </form>

  <button class="btn btn-large" onclick="goBack()"><span class="glyphicon glyphicon-chevron-left"></span>&nbsp;Tagasi</button>
  <script type="text/javascript" src="<?php echo HTTP ?>js/backButton.js"></script> 

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