<?php
/*
======================
Saalioksjoni eseme
detailne 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';
date_default_timezone_set('Europe/Tallinn');
?>
<link rel="stylesheet" href="<?php echo HTTP ?>css/stylesheet.css">
<link rel="stylesheet" href="<?php echo HTTP ?>scripts/fancybox/jquery.fancybox.css">
<div class="container">
  <div class="detailid">
   <center>
    <?php

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

    include_once ROOT . 'admin/objects/kataloog.class.php';
    $kataloog = new Kataloog($db);
    $kataloog->id = $_GET["kataloog"];

    $kataloog->readInfo();
    $aeg_ = $kataloog->aeg;

    // Eelpakkumise käsitlemine
    if($_POST){

      // Kontrollime, kas eelpakkumisi võetakse veel vastu
      // st kas oksjon on juba alanud
      if(date('Y-m-d H:i:s') < $aeg_){

        include_once ROOT . 'admin/objects/eelpakkumine.class.php';
        $eelpakkumine = new Eelpakkumine($db);

        $eseme_id = $_GET['id'];
        $kasutaja_id = $_SESSION['userSession'];
        $summa = $_POST['summa'];
        date_default_timezone_set('Europe/Tallinn');
        $t=time();
        $aeg = date('Y-m-d H:i:s', $t);
        $eelpakkumine->eseme_id=$eseme_id;
        $eelpakkumine->kasutaja_id=$kasutaja_id;
        $eelpakkumine->summa=$summa;
        $eelpakkumine->aeg=$aeg;

        // Loome eelpakkumise
        $eelpakkumine->looEelpakkumine();

      } else {
        $url = $_SERVER['REQUEST_URI'];
        echo"<script>window.location.replace('$url');</script>";
      }
    }

    // Kuvame eseme info ID järgi
    if(isset($_GET['id'])){

      include_once ROOT . 'admin/objects/ese.class.php';
      include_once ROOT . 'admin/objects/pilt.class.php';

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

      $ese = new Ese($db);
      $image = new Pilt($db);

      $id = $_GET['id'];
      $stmt = $ese->showOne($id);
      $num = $stmt->rowCount();

      if($num>0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          extract($row);
          $eseme_id = $id;

          $stmt_img = $image->readAllSaal($eseme_id);

          echo '<div class="previews">';

          while ($row_img = $stmt_img->fetch(PDO::FETCH_ASSOC)){
            extract($row_img);
            echo"<a href='#' data-full='".HTTP."admin/img/saal/{$eseme_id}/{$nimi}'><img src='".HTTP."admin/img/saal/{$eseme_id}/{$nimi}' height='50' width='50'/></a>";
          }

          $stmt_kaanefoto = $image->readOneSaal($eseme_id);
          $row_kaanefoto = $stmt_kaanefoto->fetch(PDO::FETCH_ASSOC);
          extract($row_kaanefoto);

          echo "</div>
          <div class='full'>
            <img src='".HTTP."admin/img/saal/{$eseme_id}/{$nimi}' />
          </div>
          <script src='".HTTP."scripts/fancybox/jquery.fancybox.js?v=2.1.4'></script> 
          <script src='".HTTP."js/pildid1.js'></script><script src='".HTTP."js/pildid2.js'></script>";

          extract($row);

          echo "<h2><center>{$nimi}</center></h2>";
          echo "
          <table class='table table-bordered'>
            <tr>
              <th style='width:40%' colspan='2'><center>Kirjeldus</center></th>
              <th colspan='2'><center>Informatsioon</center></th>
            </tr>
            <tr>
              <td colspan='2' rowspan='2'>{$kirjeldus}</td>
              <td>ID: {$eseme_id}<br/>Alghind: <strong>{$alghind}€</strong>
              <p><center><a href='".HTTP."internetioksjon/lisainfo.php?id={$eseme_id}'><kbd>Küsi lisainformatsiooni</kbd></a></center></p>
              </tr>
              <tr>
                <td align='center'>";
                  if(date('Y-m-d H:i:s') < $aeg_){

                    if(!$user_home->is_logged_in()){
                     echo 

                     "<p><center><div class='alert alert-danger'>Pakkumiseks logi sisse!</div></center></p></td>
                   </td>
                 </tr>
               </table>
               ";
             } else {
              echo "<form class='form-inline' action='ese.php?kataloog={$kataloog->id}&id={$eseme_id}' method='post' enctype='multipart/form-data'>
              <p>Sinu eelpakkumine:</p>
              <div class='form-group'>
                <div class='input-group'>
                  <input style='width:80px' min='1' type='number' class='form-control' id='summa' name='summa' required>
                  <div class='input-group-addon'>€</div>
                </div>
              </div>
              <button type='submit' class='btn btn-primary'>Tee pakkumine</button>
            </form>
          </td>
        </tr>
      </table>
      ";
    }} else {
     echo 
     "<p><center><div class='alert alert-danger'>Eelpakkumised on suletud!</div></center></p></td>
   </td>
</tr>
</table>
";
}
}

}
else{
  echo "<center><h3>Eset ei leitud</h3></center>";
}
} else {
  $url = $_SERVER['REQUEST_URI'];
  echo"<script>window.location.replace('$url');</script>";
}
?>
</center><div  class="col-md-12">
<ul style="margin-left: -15px" class="nav navbar-nav navbar-left">
  <li><button class="btn btn-large" onclick="goBack()"><span class="glyphicon glyphicon-chevron-left"></span>&nbsp;Tagasi</button>&nbsp;</li>
  <li>

  </li></ul><ul style="list-style-type: none;" class="navbar-right">
  <li><a  class="btn btn-info" data-toggle="modal" data-target="#myModal">Tutvu oksjoni sammudega</a></li>
</ul>

</div><br/>

<!-- Modal oksjoni sammude jaoks -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h3 class="modal-title" id="myModalLabel"><center>Oksjoni sammud</center></h3>
      </div>
      <div class="modal-body"><br/>
        <table class="table table-hover table-bordered">
          <tr>
            <th width="50%">Summa (EUR)</th>
            <th>Samm (EUR)</th>
          </tr>
          <tr>
            <td>1 - 10</td>
            <td>1</td>
          </tr>
          <tr>
            <td>11 - 50</td>
            <td>2</td>
          </tr>
          <tr>
            <td>51 - 100</td>
            <td>5</td>
          </tr>
          <tr>
            <td>101 - 500</td>
            <td>10</td>
          </tr>
          <tr>
            <td>501 - 5000</td>
            <td>50</td>
          </tr>
          <tr>
            <td>5001 - ...</td>
            <td>100</td>
          </tr>
        </table>
      </div>

    </div>
  </div>
</div>
<script>
  function goBack() {
    window.history.back();
  }
</script><br/><br/>

</div>
</div>
</div>

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