<?php

$query = $db->prepare("SELECT * FROM kategori where sef=:sef LIMIT 1");
$kategori = $query->execute(array(":sef"=>$_GET['sef']));
$kategori = $query->fetch(PDO::FETCH_ASSOC);

if(!$kategori){
  echo '<meta http-equiv="refresh" content="0;URL=index.php">';
}


$_title         =  $kategori['baslik'];
$_description   =  $kategori['description'];

$endusuk_fiyat = $db->query("SELECT min(Convert(fiyat, SIGNED)) as fiyat FROM urun LIMIT 1")->fetch(PDO::FETCH_ASSOC);
$enyuksek_fiyat = $db->query("SELECT max(Convert(fiyat, SIGNED))  as fiyat FROM urun LIMIT 1")->fetch(PDO::FETCH_ASSOC);

if(!isset($_GET['fiyat1']) AND !isset($_GET['fiyat2'])){
  $dusuk_fiyat = $endusuk_fiyat['fiyat'];
  $yuksek_fiyat = $enyuksek_fiyat['fiyat'];
}else{
  $dusuk_fiyat = $_GET['fiyat1'];
  $yuksek_fiyat = $_GET['fiyat2'];
}

?>
<div class="container-fluid adres_cubugu">
  <div class="row">
  	<div class="container">
  		<div class="row">
  			<div class="col-md-12">
  				<ul>
  					<li><a href="index.php"><i class="fa fa-home"></i> Anasayfa</a> <span><i class="fa fa-arrow-right"></i></span></li>
  					<li><a href="kategori/<?php echo $kategori['sef']; ?>"><?php echo $kategori['baslik']; ?></a></li>
  				</ul>
  			</div>
  		</div>
  	</div>
  </div>
</div>

<section class="bg-dis">
	<div class="container">
		<div class="row mt-15 mb-15">
			<div class="col-md-3">

        <div class="price-range-slider">
          <p class="range-value"><input type="text" id="amount" readonly></p>
          <div id="slider-range" class="range-bar"></div>
        </div>
        <button class="btn icon-btn btn-default mt-10" id="filtrele" style="width: 100%"><i class="fa fa-tags"></i> Fiyat Aralığına Göre Filtrele</button>

        <script type="text/javascript">
          $(function(){
            $('#filtrele').click(function(){
                var temizle = $('#amount').val().replaceAll(' ', '');
                var temizle1 = temizle.replaceAll('TL', '');
                var bol = temizle1.split('-');
                $(location).attr('href', '<?php echo $site; ?>kategori/<?php echo $kategori['sef']; ?>?fiyat1='+bol[0]+'&fiyat2='+bol[1]);
            });
          });
        </script>

        <div class="alan mt-15">
          <div class="filtre_dis">
            <span class="filtre_baslik">Kategoriler</span>
            <ul class="sol_kategoriler">
                <?php
                  $bir_ust = $db->query("SELECT * FROM kategori WHERE id = '{$kategori['ust_kategori']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
                  if($bir_ust){
                    echo '<li><a href="kategori/'.$bir_ust['sef'].'" title="" style="font-weight: bold;color: green;"><i class="fa fa-arrow-left"></i> Üst Kategoriye Dön</a></li>';
                  }

                  $query = $db->query("SELECT * FROM kategori WHERE ust_kategori = '{$kategori['id']}' ORDER BY sira ASC", PDO::FETCH_ASSOC);
                  if($query->rowCount()){
                      foreach($query as $row){
                        echo '<li><a href="kategori/'.$row['sef'].'" title="'.$row['baslik'].'">'.$row['baslik'].'</a></li>';
                      }
                  }else{
                    $query = $db->query("SELECT * FROM kategori WHERE ust_kategori = '{$kategori['ust_kategori']}' ORDER BY sira ASC", PDO::FETCH_ASSOC);
                    if($query->rowCount()){
                        foreach($query as $row){
                          echo '<li><a href="kategori/'.$row['sef'].'" title="'.$row['baslik'].'">'.$row['baslik'].'</a></li>';
                        }
                    }
                  }
                ?>
            </ul>
          </div>
        </div>

      </div>

      <div class="col-md-9">
        <div class="row">

          <?php
          $baslangic = ($_GET['no'] * 12) - 12;

          $kataegori_idleri = alt_kategori_bul($kategori['id']).'0';
          $query = $db->query("SELECT * FROM urun_kategori INNER JOIN urun ON urun_kategori.urun_id = urun.id WHERE urun_kategori.kategori_id IN ({$kataegori_idleri}) AND Convert(urun.fiyat, SIGNED) >= {$dusuk_fiyat} AND Convert(urun.fiyat, SIGNED) <= {$yuksek_fiyat} GROUP BY urun.id DESC LIMIT {$baslangic},12", PDO::FETCH_ASSOC);
          if($query->rowCount()){
              foreach($query as $row){
                $img1 = $db->query("SELECT * FROM urun_img WHERE urun_id = '{$row['urun_id']}' LIMIT 0,1")->fetch(PDO::FETCH_ASSOC);
                $img2 = $db->query("SELECT * FROM urun_img WHERE urun_id = '{$row['urun_id']}' LIMIT 1,1")->fetch(PDO::FETCH_ASSOC);
                if(!$img2){
                  $img2['img'] = $img1['img'];
                }
          ?>
          <div class="col-md-3 mb-15 col-xs-6">
            <a href="urun/<?php echo $row['sef']; ?>" title="<?php echo $row['baslik']; ?>" class="urun">
              <img src="upload/<?php echo $img1['img']; ?>" alt="<?php echo $row['baslik']; ?>" class="img-responsive img1">
              <img src="upload/<?php echo $img2['img']; ?>" alt="<?php echo $row['baslik']; ?>" class="img-responsive img2">
              <b><?php echo $row['baslik']; ?></b>
              <div>
                  <span><?php echo $row['fiyat']; ?> TL</span>
                  <span><?php echo !empty($row['eski_fiyat']) ? $row['eski_fiyat'].' TL' : ''; ?></span>
              </div>
            </a>
          </div>
          <?php 
              } 
          }else{
            echo '<div class="col-md-12"><h3>Ürün Bulunamadı...</h3></div>';
          }
          ?>
        </div>

        <?php
        $say = $db->query("SELECT * FROM urun_kategori INNER JOIN urun ON urun_kategori.urun_id = urun.id WHERE urun_kategori.kategori_id IN ({$kataegori_idleri}) AND Convert(urun.fiyat, SIGNED) >= {$dusuk_fiyat} AND Convert(urun.fiyat, SIGNED) <= {$yuksek_fiyat} GROUP BY urun.id DESC")->fetchAll();

        $top_sayfa = count($say);
        $page      = $_GET['no'];
        $limit     = 12;
        $page_url  = 'kategori/'.$kategori['sef'].'/';
        $baslangic = ($page * $limit) - $limit;
        ?>
        <div class="row">
          <div class="col-md-12">
              <div class="pagination-wrapper" style="margin-bottom: 10px;float: right;">
                <nav aria-label="Page navigation">
                  <ul class="pagination mb-0">
                    <?php Sayfala($top_sayfa,$page,$limit,$page_url); ?>
                  </ul>
                </nav>
              </div>
          </div>
        </div>

        <?php if(!empty($kategori['aciklama'])){ ?>
        <div class="row">
          <div class="col-md-12">
            <div class="alan">
              <?php echo $kategori['aciklama']; ?>
            </div>
          </div>
        </div>
        <?php } ?>

      </div>

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


<script type="text/javascript" src="js/fiyat-araligi.js"></script>
<script type="text/javascript">
$(function(){
  $('#slider-range').slider({
    range: true,
    min: <?php echo $endusuk_fiyat['fiyat']; ?>,
    max: <?php echo $enyuksek_fiyat['fiyat']; ?>,
    values: [ <?php echo $dusuk_fiyat; ?>, <?php echo $yuksek_fiyat; ?> ],
    slide: function( event, ui ) {
      $('#amount').val(ui.values[ 0 ] + ' TL - ' + ui.values[ 1 ] +' TL' );
    }
  });
  $('#amount').val( $('#slider-range').slider('values', 0 ) + ' TL - ' + $('#slider-range').slider('values', 1 ) + ' TL');
});
</script>