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

// Muudetava oksjoni ID pärimine.
$id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: Selline ID puudub.');
 
include_once ROOT . 'db/dbconfig.php';
include_once ADMIN_ROOT . 'objects/oksjon.class.php';
 
$database = new Database();
$db = $database->dbConnection();
include_once ADMIN_ROOT . 'objects/pilt.class.php';
$image = new Pilt($db);
 
$oksjon = new Oksjon($db);
 
$oksjon->id = $id;
$oksjon->readOne();

// Kui muudeti oksjoni kirjeldust (tekstiväljad)
if(isset($_POST['submitText'])){
 
$oksjon->nimi=$_POST['nimi'];
$oksjon->kategooria_id = $_POST['kategooria_id'];
$oksjon->kirjeldus=$_POST['kirjeldus'];
 
    if($oksjon->update()){
        echo "<div class=\"alert alert-success alert-dismissable\">";
            echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
            echo "Oksjoni 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 "Oksjoni uuendamine ebaõnnestus.";
        echo "</div>";
    }
}

// Kui muudeti kaanefotot
if(isset($_POST['submitKaanefoto'])){
 
$pildi_id = $_POST['pildi_id'];
// Kustutame vana kaanefoto märke
$image->tyhjendaKaanefoto($oksjon->id);
    // Sisestame uue kaanefoto märke
    if($image->valiKaanefoto($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 kustutada pilti
if(isset($_POST['submitKustuta'])){
 
$pildi_id = $_POST['pildi_id'];
// Kustutame pildi
    $num = $image->kustutaPilt($pildi_id, $oksjon->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 internetioksjonile lisati pilte juurde
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/{$oksjon->id}/";
        $target_thumb = $target."thumbnail_".$failinimi;
        $target = $target . $failinimi;

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

        $img->create() && 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>Oksjoni muutmine</span></h2>
<br/><br/>
<form action='update_oksjon.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 $oksjon->nimi; ?>' class='form-control' /></td>
        </tr>
 
        <tr>
            <td>Kirjeldus</td>
            <td><textarea name='kirjeldus' class='form-control'><?php echo $oksjon->kirjeldus; ?></textarea></td>
        </tr>
 
        <tr>
            <td>Kategooria</td>
    <td>
        <?php
        include_once ADMIN_ROOT . 'objects/kategooria.class.php';
 
        $category = new Kategooria($db);
        $stmt = $category->read();
 
        echo "<select class='form-control' name='kategooria_id'>";
 
            echo "<option>Vali kategooria...</option>";
            while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row_category);
 
                if($oksjon->kategooria_id==$id){
                    echo "<option value='$id' selected>";
                }else{
                    echo "<option value='$id'>";
                }
 
                echo "$nimi</option>";
            }
        echo "</select>";
        ?>
    </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_oksjon.php?id={$oksjon->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>
<a href='<?php echo ADMIN_HTTP; ?>index.php' class='btn btn-default'>Tagasi nimekirja</a></td>

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

<?php
$stmt = $image->readAll($oksjon->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/{$oksjon->id}/thumbnail_{$nimi}' alt=''><div class='caption'><form action='update_oksjon.php?id={$oksjon->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_oksjon.php?id={$oksjon->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");
?>