<?php 
/*
============================
Administraatori paneel
Saalioksjoni eseme lisamine
============================
*/
include "config.php";
include ADMIN_ROOT . "header.php";
?>

<!-- iCheck -->
<link rel="stylesheet" href="<?php echo HTTP ?>scripts/icheck/skins/square/red.css" />
<link href="<?php echo HTTP ?>scripts/icheck/skins/all.css" rel="stylesheet">
<script src="<?php echo HTTP ?>scripts/icheck/icheck.min.js"></script>

<!-- DATEPICKER -->
<script type="text/javascript" src="<?php echo HTTP ?>scripts/moment/min/moment.min.js"></script>
<script type="text/javascript" src="<?php echo HTTP ?>js/bootstrap.min.js"></script>
<script type="text/javascript" src="<?php echo HTTP ?>scripts/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<link rel="stylesheet" href="<?php echo HTTP ?>scripts/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" />

<?php

include_once ROOT . 'db/dbconfig.php';
include_once ADMIN_ROOT . 'objects/kataloog.class.php';

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

// Kui eseme lisamise vorm on esitatud. Käsitleme POST päringut.
if($_POST){
 include_once ADMIN_ROOT . 'objects/ese.class.php';
 include_once ADMIN_ROOT . 'objects/pilt.class.php';

 $ese = new Ese($db);

 $ese->nimi=$_POST['nimi'];
 $ese->kirjeldus=$_POST['kirjeldus'];
 $ese->kataloogi_id=$_POST['kataloogi_id'];
 $ese->alghind=$_POST['alghind'];

 // Lisame eseme andmebaasi
 $ese->lisamine();

 // Pildi üleslaadimine
 $viimane_id=$db->lastInsertId();
 mkdir(ADMIN_ROOT . "img/saal/{$viimane_id}");
 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/{$viimane_id}/";
    $target_thumb = $target."thumbnail_".$failinimi;
    $target = $target . $failinimi;

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

    $img->oksjoni_id=$viimane_id;

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

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

}

$URL=ADMIN_HTTP . "internetioksjon/oksjonite_lisamine_thumbnail.php?id={$viimane_id}&page=saal";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
}

?>
<h2 class='decorated'><span>Eseme lisamine</span></h2><br /><br />

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

</script>

<form action='esemete_lisamine.php' method='post' border='0' enctype="multipart/form-data">
   <table class='table table-hover table-responsive table-bordered'>
      <col width="20%">
      <tr>
         <td>Kataloog</td>
         <td>

            <?php
            $stmt = $kataloog->read();
            $num = $stmt->rowCount();

            if($num>0){
               echo "<select class ='form-control' name='kataloogi_id'>";
               while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                  extract($row);
                  $nimi = $nimi;
                  $id = $id;
                  echo "<option value='{$id}'>{$nimi}</option>";

              }
              echo "</input>";

          }
          else{
           echo "Kataloogid puuduvad";
       }

       ?>

   </td>
   <tr>
    <td>Nimi</td>
    <td><input type='text' name='nimi' class='form-control' required /></td>
</tr>
<tr>
    <td>Kirjeldus</td>
    <td><textarea name='kirjeldus' class='form-control' required></textarea></td>
</tr>

<tr>
    <td>Alghind</td>
    <td><input type='number' min='1' name='alghind' class='form-control' enctype="multipart/form-data" required /></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'>
          <span class='glyphicon glyphicon-plus'></span> Lisa ese
      </button>
  </td>
</tr>
</table>
</form>

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