<?php
/*
======================
Administraatori paneel
"Kategooriad" vaade
======================
*/
include "config.php";
include ADMIN_ROOT."header.php";
include_once ROOT.'db/dbconfig.php';
include_once ADMIN_ROOT.'objects/kategooria.class.php';

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

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

$cat = new Kategooria($db);

// Pärime andmebaasist kõik aktegooriad ja kuvame need tabelis
$stmt = $cat->read();
$num = $stmt->rowCount();
echo "<p><a href='kategooria_lisamine.php' class='btn btn-primary btn-large' style='float:right'><span class='glyphicon glyphicon-plus'></span>&nbsp;Lisa kategooria</a></p><br />";

if($num>0){
    echo "<br/><br/><br/><table class='table table-hover table-responsive table-bordered'><col width='50%'>";
    echo "<tr>";
    echo "<th>Nimi</th>";
    echo "<th><center>Esemete arv</center></th>";
    echo "<th style='text-align:center;'>Tegevus</th>";
    echo "</tr>";

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $cat->id = $id;
        $esemete_arv = $cat->esemeteArv();
        echo "<tr>";
        echo "<td>{$nimi}</td>";
        echo "<td><center>{$esemete_arv}</center></td>";
        echo "<td>";
        echo "<center><a href='update_kategooria.php?id={$id}' class='btn btn-default left-margin'>Muuda</a> ";
        echo "<a delete-id='{$id}' class='btn btn-default delete-category'>Kustuta</a></center>";
        echo "</td>";

        echo "</tr>";
    }
    echo "</table>";
}
else{
    echo "<div><center>Kategooriad puuduvad.</center></div>";
}
?>

<!-- Skript kategooria kustutamiseks -->
<script>
    $(document).on('click', '.delete-category', function(){

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

        if (q == true){
          $.post('delete_kategooria.php', {
            object_id: id
        }, function(data){
            location.reload();
        }).fail(function() {
            alert('Kustutamine ebaõnnestus.');
        });
    }   
    return false;
});
</script>

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