<?php
/*
======================
Administraatori paneel
"Hindamine" vaade
======================
*/
include "config.php";
include ADMIN_ROOT . "header.php";
include ADMIN_ROOT . "objects/hinnatav.class.php";

echo '<h2 class="decorated"><span>Esemete hindamine</span></h2>';

// Andmebaasiühenduse loomine
$database = new Database();
$db       = $database->dbConnection();

// Loome uue hinnatava eseme
$hinnatav = new Hinnatav($db);

// Vastuse esitamisel käsitleme sisestatut
if ($_POST) {
    
    // Saadame vastuse
    $hinnatav->send_mail($_POST["email"], $_POST["vastus"], $_POST["pealkiri"]);

    // Märgime hinnatuks
    $hinnatav->margiHinnatuks($_POST["id"]);
    
    // Kuvame kinnitava teate
    echo "<div class=\"alert alert-success alert-dismissable\">";
    echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
    echo "Vastus saadetud.";
    echo "</div>";
}

// Loeme hinnatavad esemed sisse
$stmt = $hinnatav->lugemine();
$num  = $stmt->rowCount();

if ($num > 0) {
    echo "<br/><table class='table table-hover table-responsive table-bordered'>";
    echo "<tr>";
    echo "<th>ID</th>";
    echo "<th>Nimi</th>";
    echo "<th>Aeg</th>";
    echo "<th style='text-align:center;'>Tegevus</th>";
    echo "</tr>";
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        // Kuvame andmed andmebaasist
        extract($row);
        echo "<tr>";
        echo "<td>{$id}</td>";
        echo "<td>{$nimi}</td>";
        echo "<td>{$aeg}</td>";
        echo "<td width='30%'>";
        
        // Kontrollime, kas ese on hinnatud või mitte
        // Kuvame vastavalt kas "Hindama" või "Hinnatud"
        if ($staatus == 0) {
            echo "<center><a href='hindamine.php?id={$id}' class='btn btn-info left-margin'>Hindama</a>";
        } else {
            echo "<center><a href='hindamine.php?id={$id}' class='btn btn-success left-margin'>Hinnatud</a>";
        }
        echo "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
else {
    echo "<br /> <br /><center>Ühtegi eset ei ole hindamisele saadetud.</center>";
}

include ADMIN_ROOT . "footer.php";
?>