<?php 
/*
============================
Administraatori paneel
"Kataloogid" vaade
============================
*/
include "config.php";
include ADMIN_ROOT . "header.php";

?>
<h2 class="decorated"><span>Kataloogide haldamine</span></h2>
<?php

include_once ADMIN_ROOT . "objects/kataloog.class.php";
 
$database = new Database();
$db = $database->dbConnection();
 
$kataloog = new Kataloog($db);
 
// Loeme andmebaasist kõik kataloogid ja kuvame tabelisse
$stmt = $kataloog->read();
$num = $stmt->rowCount();

     echo "<p><a href='kataloogi_lisamine.php' class='btn btn-primary btn-large' style='float:right'><span class='glyphicon glyphicon-plus'></span>&nbsp;Lisa kataloog</a></p>";

if($num>0){
 
    echo "<br/><br/><br/><table class='table table-hover table-responsive table-bordered'>";
        echo "<tr>";
            echo "<th>Nimi</th>";
            echo "<th>Aeg</th>";
            echo "<th>Esemete arv</th>";
            echo "<th style='text-align:center;'>Tegevus</th>";
        echo "</tr>";
 
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
 
            extract($row);
            $kataloog->id = $id;
            $esemete_arv = $kataloog->esemeteArv();
            echo "<tr>";
                echo "<td>{$nimi}</td>";
                
                echo "<td>{$aeg}</td>";
                echo "<td>{$esemete_arv}</td>";
echo "<td width='30%'>";
    echo "<center><a href='kuva_esemed.php?id={$id}' class='btn btn-default left-margin'>Kuva esemed</a>
    <a href='update_kataloog.php?id={$id}' class='btn btn-default left-margin'>Muuda</a> ";
    echo "<a delete-id='{$id}' class='btn btn-default delete-kataloog'>Kustuta</a></center>";
echo "</td>";
            echo "</tr>";
        }
    echo "</table>";
}
 
else{
    echo "<br /> <br /><center>Kataloogid puuduvad.</center>";
}
?>

<!-- Kataloogi kustutamine -->
<script>
$(document).on('click', '.delete-kataloog', function(){

    var id = $(this).attr('delete-id');
    var q = confirm("Kas soovid kataloogi kustutada?");

    if (q == true){

      $.post('delete_kataloog.php', {
        object_id: id
      }, function(data){
        location.reload();
      }).fail(function() {
        alert('Unable to delete.');
      });

    }   
    return false;
  });</script>

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