<?php
/*
===============================
Internetioksjoni detailne vaade
Detailide kuvamine
===============================
*/
include_once ROOT.'db/dbconfig.php';

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

date_default_timezone_set('Europe/Tallinn');


// ID järgi andmete kuvamine
if(isset($_GET['id'])){

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

	$oksjon = new Oksjon($db);
	$image = new Pilt($db);

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

    // Pakkumise edastamine
    if($_POST){
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(date('Y-m-d H:i:s') < $row["lopuaeg"]){

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

            $oksjoni_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);

            $pakkumine->oksjoni_id=$oksjoni_id;
            $pakkumine->kasutaja_id=$kasutaja_id;
            $pakkumine->summa=$summa;
            $pakkumine->aeg=$aeg;
            $pakkumine->create();

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

    }

    // Oksjoni informatsiooni lugemine andmebaasist
    $stmt = $oksjon->showOne($id);
    $num = $stmt->rowCount();
    if($num>0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $oksjoni_id = $id;

            $stmt_img = $image->readAll($oksjoni_id);

            // jQuery Gallery moodul
            echo '<div class="previews">';

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

            $stmt_kaanefoto = $image->readOne($oksjoni_id);
            $row_kaanefoto = $stmt_kaanefoto->fetch(PDO::FETCH_ASSOC);
            extract($row_kaanefoto);

            echo "</div>
            <div class='full'>
                <img src='".HTTP."admin/img/{$oksjoni_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);
            $a = substr($lopuaeg, 0,4);
            $k = substr($lopuaeg, 5,2);
            $p = substr($lopuaeg, 8,2);
            $t = substr($lopuaeg, 11,2);
            $m = substr($lopuaeg, 14,2);
            $s = substr($lopuaeg, 17,2);

            // Järgmise pakkumise arvutamine
            include(ROOT."scripts/getPakkumine.php");

            echo "
            <script>
                $(function () {
                    var lopuaeg = new Date();
    //Thu Jan 26 2017 00:00:00 GMT+0200 (FLE Standard Time)
                    lopuaeg = new Date($a, $k-1, $p, $t, $m, $s);
    //document.write(lopuaeg);
                    $('#defaultCountdown').countdown({until: lopuaeg});
                });

            </script>";
            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='3'><center>Informatsioon</center></th>
                </tr>
                <tr>
                    <td colspan='2' rowspan='2'>{$kirjeldus}</td>
                    <td>ID: {$oksjoni_id}<br/>Hetkehind: <strong>{$hetkehind}€</strong><br/>Alghind: <strong>{$alghind}€</strong><br />Pakkumisi: <strong>{$pakkumised}</strong><br/>

                        <td align='center'>
                            <br/>
                            Algusaeg: <strong>{$algusaeg}</strong><br/>Lõpuaeg: <strong>{$lopuaeg}</strong>
                            <p><center><a href='lisainfo.php?id={$oksjon->id}'><kbd>Küsi lisainformatsiooni</kbd></a></center></p>
                        </td>
                    </tr>
                    <tr>
                        <td align='center'>";
                            if(date('Y-m-d H:i:s') < $lopuaeg){
                              if(!$user_home->is_logged_in()){
                               echo 

                               "<p><center><div class='alert alert-danger'>Pakkumiseks logi sisse!</div></center></p></td>
                           </td>

                           <td align='center'>
                            Aega jäänud:<div id='defaultCountdown'></div>
                        </td>
                    </tr>
                </table>
                ";

            } else {
              echo "<form class='form-inline' action='oksjon.php?id={$oksjoni_id}' method='post' enctype='multipart/form-data'>

              <p>Sinu pakkumine:</p>
              <div class='form-group'>

                <div class='input-group'>
                  <input style='width:80px' min='1' type='number' class='form-control' id='summa' name='summa' placeholder='{$jargminePakkumine}' required>
                  <div class='input-group-addon'>€</div>
              </div>
          </div>
          <button type='submit' class='btn btn-primary'>Tee pakkumine</button>
      </form>
  </td>

  <td align='center'>
    Aega jäänud:<div id='defaultCountdown'></div>
</td>
</tr>
</table>
";
}
} else {
   echo 

   "<p><center><div class='alert alert-danger'>Oksjon on lõppenud.</div></center></p></td>
</td>

<td align='center'>
    Aega jäänud:<div id='defaultCountdown'></div>
</td>
</tr>
</table>
";
}
?>

</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>
        <?php
        if(isset($_SESSION['userSession'])){
            include_once(ROOT."scripts/jalgitavCheck.php");
            if(jalgitavCheck($db, $oksjon->id, $_SESSION['userSession']) == $_SESSION['userSession']){
                    // On jälgitavas
                echo '<ul style="list-style-type: none;" class="navbar-left">
                <a href="oksjon.php?id='.$oksjon->id.'&jalgitav=false" class="btn"><span class="glyphicon glyphicon-star"></span>&nbsp;Eemalda jälgitavatest</a></ul>
                ';

            } else {
                echo '<ul style="list-style-type: none;" class="navbar-left">
                <a href="oksjon.php?id='.$oksjon->id.'&jalgitav=true" class="btn"><span class="glyphicon glyphicon-star-empty"></span>&nbsp;Lisa jälgitavatesse</a></ul>
                ';
            }
        }
        // Eseme lisamine ja eemaldamine jälgitavate nimekirjast
        if(isset($_GET['jalgitav'])){
            include_once ROOT."admin/objects/jalgitav.class.php";
            $jalgitav = new Jalgitav($db);
            $jalgitav->kasutaja_id = $_SESSION['userSession'];
            $jalgitav->oksjoni_id = $oksjon->id;

            if($_GET['jalgitav'] == 'true'){
                $jalgitav->lisamine();

            } else if($_GET['jalgitav'] == 'false'){
                $jalgitav->eemaldamine();
            }
            $url = HTTP."internetioksjon/oksjon.php?id=".$oksjon->id;
            echo"<script>window.location.replace('$url');</script>";
        }
        ?>
    </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>

<?php
}
}
else{
    echo "<center><h3>Eset ei leitud</h3></center>";
}
}
?>

