<!-- 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
"Oksjonite lisamine" vaade
===========================
*/
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';

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

// Lisamisvormi esitamisel andmete töötlemine
if($_POST){

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

  // Salvestame POST väärtused
  $kestvus = $_POST['kestvus'];
  $algusaeg = $_POST['algusaeg'];
  $lopuaeg = date("Y-m-d H:i:s",strtotime("+$kestvus days",strtotime($algusaeg)));
  $oksjon->nimi=$_POST['nimi'];
  $oksjon->kirjeldus=$_POST['kirjeldus'];
  $oksjon->kategooria_id = $_POST['kategooria_id'];
  $oksjon->alghind=$_POST['alghind'];
  $oksjon->hetkehind=$_POST['alghind'];
  $oksjon->algusaeg=$algusaeg;
  $oksjon->lopuaeg=$lopuaeg;
  $oksjon->kestvus=$kestvus;

  // Loome oksjoni
  $oksjon->create();

  // Piltide lisamine kaustadesse ja andmebaasi kirjete salvestamine
  $viimane_id=$db->lastInsertId();
  mkdir(ADMIN_ROOT . "img/{$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/{$viimane_id}/";
    $target_thumb = $target."thumbnail_".$failinimi;
    $target = $target . $failinimi;

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

    $img->oksjoni_id=$viimane_id;
    $img->create() && move_uploaded_file($_FILES['uploaded']['tmp_name'][$key], $target);

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

  }

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

}

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

<script>

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

</script>

<form action='oksjonite_lisamine.php' method='post' border='0' enctype="multipart/form-data">
  <table class='table table-hover table-responsive table-bordered'>
    <col width="20%">
    <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>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);
          echo "<option value='{$id}'>{$nimi}</option>";
        }

        echo "</select>";
        ?>
      </td>
    </tr>
    <tr>
      <td>Alghind</td>
      <td><input type='number' min='1' name='alghind' class='form-control' required /></td>
    </tr>
    <tr>
      <td>Algusaeg</td>
      <td>
        <div class="row">
          <div class='col-sm-6'>

          <!--
          Bootstrap 3 Datepicker v4
          https://eonasdan.github.io/bootstrap-datetimepicker/ (11.05.2016)
          -->
            <script type="text/javascript">
              $(function () {
                $('#datetimepicker').datetimepicker({
                  format: 'YYYY-MM-DD HH:mm:SS'
                });
              });
              var today = new Date();
            </script>

            <div class='input-group date' id='datetimepicker'>
              <?php
              date_default_timezone_set('Europe/Tallinn');
              $t=time();
              $aeg = date('Y-m-d H:i:s', $t);
              echo "
              <input name='algusaeg' type='text' class='form-control' value='{$aeg}' required />
              ";
              ?>
              <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
              </span>
            </div></div>

          </div>

        </td>

      </tr>
      <tr>
        <td>Kestvus</td>
        <td>
          <div class="col-xs-6">
            <div class="radio">
              <label>
                <input type="radio" name="kestvus" value="7" /> 7 päeva
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="kestvus" value="14" /> 14 päeva
              </label>
            </div>
            <div class="radio">
              <label>
                <input type="radio" name="kestvus" value="30" /> 30 päeva
              </label>
            </div>

          </div>
        </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;' required>
            Vali pildid...
          </label>
        </td>
      </tr>
      <tr>
        <td></td>
        <td>                
          <button type='submit' class='btn btn-primary'>
            <span class='glyphicon glyphicon-plus'></span> Lisa oksjon
          </button>
        </td>
      </tr>
    </table>
  </form>

  <?php

  include(ADMIN_ROOT . "footer.php");
  ?>