<!-- 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
Saalioksjoni eseme uuendamine
=============================
*/
include "config.php";
include ADMIN_ROOT . "header.php";

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

include_once ROOT . 'db/dbconfig.php';
$database = new Database();
$db = $database->dbConnection();

include_once ADMIN_ROOT . 'objects/pilt.class.php';
$image = new Pilt($db);

include_once ADMIN_ROOT . 'objects/ese.class.php';
$ese = new Ese($db);

$ese->id = $id;
$ese->readOne();

// Kui esitati eseme kirjelduse vorm
if(isset($_POST['submitText'])){
   
    $ese->nimi=$_POST['nimi'];
    $ese->kirjeldus=$_POST['kirjeldus'];
    
    if($ese->update()){
        echo "<div class=\"alert alert-success alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Eseme 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 "Eseme uuendamine ebaõnnestus.";
        echo "</div>";
    }
}

// Kui esitati uue kaanefoto valimise vorm
if(isset($_POST['submitKaanefoto'])){
   
    $pildi_id = $_POST['pildi_id'];
    $image->tyhjendaKaanefotoSaal($ese->id);
    
    if($image->valiKaanefotoSaal($pildi_id)){
        echo "<div class=\"alert alert-success alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Kaanefoto uuendatud.";
        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 "Kaanefoto uuendamisel ilmnes viga. Palun proovi uuesti.";
        echo "</div>";
    }
}

// Kui soovitakse pilti kustutada
if(isset($_POST['submitKustuta'])){
   
    $pildi_id = $_POST['pildi_id'];
    $num = $image->kustutaPiltSaal($pildi_id, $ese->id);
    if($num != 0){
        echo "<div class=\"alert alert-success alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Pilt kustutati andmebaasist.";
        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 "Palun vali enne kustutamist uus kaanefoto.";
        echo "</div>";
    }
}

// Kui lisati saalioksjoni esemele uusi pilte
if(isset($_POST['submitPildid'])){

    include_once ROOT . 'scripts/thumbnail_resizer.php';
    
    foreach($_FILES['uploaded']['tmp_name'] as $key => $tmp_name){

        $failinimi = $key.$_FILES['uploaded']['name'][$key];

        $target = ADMIN_ROOT . "img/saal/{$ese->id}/";
        $target_thumb = $target."thumbnail_".$failinimi;
        $target = $target . $failinimi;

        $img = new Pilt($db);
        $img->nimi=$failinimi;
        
        $img->oksjoni_id=$ese->id;

        $img->lisamineSaal() && move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $target);

        resize_crop_image(200, 200, $target, $target_thumb);

    }
    echo "<div class=\"alert alert-success alert-dismissable\">";
    echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
    echo "Piltide üleslaadimine õnnestus.";
    echo "</div>";

}

?>

<h2 class='decorated'><span>Eseme muutmine</span></h2>
<br/><br/>
<form action='update_ese.php?id=<?php echo $id; ?>' method='post'>
   
    <table class='table table-hover table-responsive table-bordered'>
       
        <tr>
            <td width="200px">Nimi</td>
            <td><input type='text' name='nimi' value='<?php echo $ese->nimi; ?>' class='form-control' /></td>
        </tr>
        
        <tr>
            <td>Kirjeldus</td>
            <td><textarea name='kirjeldus' class='form-control'><?php echo $ese->kirjeldus; ?></textarea></td>
        </tr>
        
        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary" name="submitText">Salvesta muudatused</button>
            </td>
        </tr>
        
    </table>
</form>
<br/>

<?php
echo "<form action='update_ese.php?id={$ese->id}' method='post' enctype='multipart/form-data'>";
?>

<table class='table table-hover table-responsive table-bordered'>
    <td width='200px'>Pildid</td>
    <td>
        <label class='btn btn-info' for='my-file-selector'>
            <input id='my-file-selector' type='file' name='uploaded[]' multiple="" style='display:none;'>
            Vali pildid...
        </label>

        <button name='submitPildid' type='submit' class='btn btn-primary'>Lisa pildid</button>
        <?php
        echo"
        <a href='".ADMIN_HTTP."saalioksjon/kuva_esemed.php?id={$ese->kataloogi_id}' class='btn btn-default'>Tagasi nimekirja</a>
        ";
        ?>
    </td>


</table>
<center></center></form><br/>

<?php
$stmt = $image->readAllSaal($ese->id);

$num = $stmt->rowCount();

echo'<div class="col-md-9">';


if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        echo "<div class='col-sm-3'><div class='thumbnail'>";
        echo "<img src='".ADMIN_HTTP."img/saal/{$ese->id}/thumbnail_{$nimi}' alt=''><div class='caption'><form action='update_ese.php?id={$ese->id}' method='post'>
        <input type='hidden' name='pildi_id' value='{$id}'>
        <center><button name='submitKaanefoto' type='submit' class='btn btn-info'>Vali kaanefotoks</button></center>
    </form><br/>
    <form action='update_ese.php?id={$ese->id}' method='post'>
        <input type='hidden' name='pildi_id' value='{$id}'>
        <center><button name='submitKustuta' type='submit' class='btn btn-danger'>Kustuta</button></center>
    </form></div></div></div>";
}

}
else{
    echo "<center>Ilmnes viga. Palun proovi uuesti.</center>";
}

include ADMIN_ROOT . "footer.php";
?>