<?php
/*
======================
"Hindamine" vaade
======================
*/
include "config.php";
session_start();
include_once ROOT . 'db/dbconfig.php';
require_once ROOT . 'admin/objects/kasutaja.class.php';
$user_home = new Kasutaja();
include(ROOT . "header.php");
?>

<div class="container">
  <div class="col-md-10 col-md-offset-1">     
    <?php

    $database = new Database();
    $db = $database->dbConnection();
    echo"<h2 class='decorated'><span>Esemete hindamine</span></h2><br/>";


    if($_POST){
        include_once ADMIN_ROOT . 'objects/hinnatav.class.php';
        include_once ADMIN_ROOT . 'objects/pilt.class.php';

    /*
    Pildifailide formaadikontrolli realisatsiooni alus:
    https://www.sitepoint.com/community/t/php-upload-limit-image-type-to-jpg-and-gif/3838 (10.05.2016)
    */

    $types = array('image/jpeg', 'image/gif', 'image/png');
    $staatus = 0;

    // Kontrollime kõikide failide vastavust lubatud formaatidele
    foreach($_FILES['uploaded']['tmp_name'] as $key => $tmp_name){
        if (in_array($_FILES['uploaded']['type'][$key], $types) == false) {
            $staatus += 1;
        }
    }

    // Kui kõik pildifailid vastasid nõuetele
    if ($staatus == 0){

        $hinnatav = new Hinnatav($db);

        $hinnatav->nimi=$_POST['nimi'];
        $hinnatav->email=$_POST['email'];
        $hinnatav->kirjeldus=$_POST['kirjeldus'];

        $hinnatav->lisamine();

        $viimane_id=$db->lastInsertId();
        mkdir(ADMIN_ROOT . "img/hindamine/{$viimane_id}");
        include_once ROOT . 'scripts/thumbnail_resizer.php';

        // Laeme ükshaaval pildid üles ja lisame oksjoni kausta
        foreach($_FILES['uploaded']['tmp_name'] as $key => $tmp_name){


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

            $target = ADMIN_ROOT . "img/hindamine/{$viimane_id}/";
            $target_thumb = $target."thumbnail_".$failinimi;
            $target = $target . $failinimi;

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

            $img->oksjoni_id=$viimane_id;

            $img->lisamineHinnatav() && 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 "Teie esemed edastati hindamisele. Vastus saadetakse 3 tööpäeva jooksul Teie e-posti aadressile.";
        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 "Teie pildid ei ole sobivas formaadis. Lubatud formaadid: JPG, JPEG, PNG ja GIF.";
        echo "</div>";
    }
}
?>

<script>

    $(document).ready(function () {
        $('input').iCheck({
            radioClass: 'iradio_square-blue'
        });
    });

</script>

<form action='index.php' method='post' border='0' enctype="multipart/form-data">
    <table class='table table-hover table-responsive table-bordered'>
        <col width="20%">
        <tr>
            <td>Ees- ja perekonnanimi</td>
            <td><input type='text' name='nimi' class='form-control' required /></td>
        </tr>
        <tr>
            <td>E-mail</td>
            <td><input type='email' name='email' class='form-control' required /></td>
        </tr>
        <tr>
            <td>Eseme(te) kirjeldus</td>
            <td><textarea name='kirjeldus' class='form-control' required></textarea></td>
        </tr>
        <tr>
            <td>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>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>                
            <button type='submit' class='btn btn-primary'>Saada hindamisele
            </button>
        </td>
    </tr>
</table>
</form>
</div>
</div>
</div>
</div>

<?php
include(ROOT . "footer.php");
?>