<?php 
/*
============================================
Administraatori paneel
Internetioksjonite ja saalioksjonite esemete
kaanefoto valimine
============================================
*/
include "config.php";
include ADMIN_ROOT . "header.php";
?>

<!-- iCheck skin -->
<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">

<!-- iCheck plugin -->
<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" />

<h2 class='decorated'><span>Kaanefoto valimine</span></h2><br /><br />
<?php

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

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

$image = new Pilt($db);

$oksjoni_id = $_GET['id'];
if(isset($_GET['page'])){
  $image->tabel = "img_saal";
}

// Kui kaanefoto on valitud
if($_POST){
  $pildi_id = $_POST['pildi_id'];

  // Kui tegu on saalioksjoni esemega
  if(isset($_GET["page"])){
    if($image->valiKaanefoto($pildi_id)){

      $URL=ADMIN_HTTP . "saalioksjon/esemete_lisamine.php";
      echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
    } else {
      echo "Kaanefoto seadmisel ilmnes viga. Palun proovi uuesti.";
    }
  }else {
    // Internetioksjon
    if($image->valiKaanefoto($pildi_id)){
      $URL=ADMIN_HTTP . "internetioksjon/oksjonite_lisamine.php";
      echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
    } else {
      echo "Kaanefoto seadmisel ilmnes viga. Palun proovi uuesti.";
    }
  }
} 

if(isset($_GET['page'])){
  // Loeme saalioksjoni pildid andmebaasist
  $stmt = $image->readAllSaal($oksjoni_id);

  $num = $stmt->rowCount();

  // Kuvame pildid valimiseks
  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/saal/{$oksjoni_id}/thumbnail_{$nimi}' alt=''><div class='caption'><form action='oksjonite_lisamine_thumbnail.php?id={$oksjoni_id}&page=saal' method='post'>
      <input type='hidden' name='pildi_id' value='{$id}'>
      <center><button type='submit' class='btn btn-primary'>Vali kaanefotoks</button></center>
    </form>";
    echo "</div></div></div>";
  }

}else{
  echo "<center>Ilmnes viga. Palun proovi uuesti.</center>";
}
} else {
  // Internetioksjon
  $stmt = $image->readAll($oksjoni_id);
  $num = $stmt->rowCount();

  echo'<div class="col-md-9">';

  // Kuvame pildid valimiseks
  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/{$oksjoni_id}/thumbnail_{$nimi}' alt=''><div class='caption'><form action='oksjonite_lisamine_thumbnail.php?id={$oksjoni_id}' method='post'>
      <input type='hidden' name='pildi_id' value='{$id}'>
      <center><button type='submit' class='btn btn-primary'>Vali kaanefotoks</button></center>
    </form>";
    echo "</div></div></div>";
  }

}else{
  echo "<center>Ilmnes viga. Palun proovi uuesti.</center>";
}
}

echo '</div>';

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

include ROOT . "footer.php";
?>