<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="sepetim">Sepetim</a></li>
  				</ul>
  			</div>
  		</div>
  	</div>
  </div>
</div>

<section class="bg-dis">
	<div class="container">

      <?php
      if(@count($_SESSION['sepet']['key']) > 0){
      ?>
        <div class="row mt-15">
          <div class="col-md-6 sepet_dis">
            <div class="alan">
            <h3 style="margin-top: 0px">Sepetinizde ki Ürünler</h3>
            <div class="row sepet_baslik hidden-xs">
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-3">Ürün Fotoğrafı</div>
                  <div class="col-md-4">Ürün Bilgisi</div>
                  <div class="col-md-3">Adet</div>
                  <div class="col-md-2">İşlem</div>
                </div>
              </div>
            </div>
            <?php
              $toplam = 0;
              foreach ($_SESSION['sepet']['key'] as $key) {

                $urunquery = $db->prepare("SELECT * FROM urun where id=:id LIMIT 1");
                $urun = $urunquery->execute(array(":id"=>$_SESSION['sepet']['urun_id'][$key]));
                $urun = $urunquery->fetch(PDO::FETCH_ASSOC);

                $urunimg = $db->prepare("SELECT * FROM urun_img where urun_id=:urun_id LIMIT 1");
                $uimg = $urunimg->execute(array(":urun_id"=>$_SESSION['sepet']['urun_id'][$key]));
                $uimg = $urunimg->fetch(PDO::FETCH_ASSOC);

                $secenek = '';
                $secenek_fiyat = 0;
                if($_SESSION['sepet']['secenek_id'][$key] !=0){
                  $alt_secenek = $db->prepare("SELECT * FROM urun_secenek_alt where id=:id LIMIT 1");
                          $as = $alt_secenek->execute(array(":id"=>$_SESSION['sepet']['secenek_id'][$key]));
                          $as = $alt_secenek->fetch(PDO::FETCH_ASSOC);

                          $ust_secenek = $db->prepare("SELECT * FROM urun_secenek where id=:id LIMIT 1");
                          $us = $ust_secenek->execute(array(":id"=>$as['urun_secenek_id']));
                          $us = $ust_secenek->fetch(PDO::FETCH_ASSOC);
                          if($as['fiyat'] > 0){
                            $secenek_fiyat = $as['fiyat'];
                          }
                          $secenek = '<div><span>'.$us['baslik'].':</span> <span>'.$as['baslik'].'</span></div>';
                }

                $toplam += $_SESSION['sepet']['adet'][$key] * ($urun['fiyat'] + $secenek_fiyat);
            ?>
            <div class="row sepet_urun">
              <div class="col-md-3 col-xs-4">
                <a href="urun/<?php echo $urun['sef']; ?>">
                  <img src="upload/<?php echo $uimg['img']; ?>" alt="" class="img-responsive">
                </a>
              </div>
              <div class="col-md-4 col-xs-8">
                <a href="<?php echo 'urun/'.$urun['sef']; ?>"><?php echo $urun['baslik']; ?></a>
                <div class="fiyat">
                  <span><?php if(!empty($urun['eski_fiyat'])){ echo $urun['eski_fiyat'].' TL'; } ?></span>
                  <span><?php echo ($urun['fiyat'] + $secenek_fiyat); ?> TL</span>
                </div>
              </div>
              <div class="col-md-3"><span><?php echo $_SESSION['sepet']['adet'][$key]; ?> Adet</span></div>
              <div class="col-md-2"><button class="btn icon-btn btn-default" style="color: red;padding: 2px 7px" data-sepet-sayfada-sil="<?php echo $key; ?>"><i class="fa fa-trash"></i></button></div>
            </div>
            <?php } ?>

          </div>

            <div class="alan mb-15">
              <form action="" method="post">
                <fieldset class="form-group col-md-8" style="margin-bottom: 0px">
                  <input type="text" class="form-control" name="kod" required="" placeholder="Kupon Kodu Giriniz">
                </fieldset>
                <fieldset class="form-group col-md-4" style="margin-bottom: 0px">
                  <button type="submit" class="btn btn-success" style="height: 33px;width: 100%"><i class="fa fa-thumbs-up"></i> Kupon Kullan</button>
                </fieldset>
              </form>
            </div>

            <?php
              if($cek['kargo_bedava_limit'] < $toplam){
                $kargo_ucreti = $cek['kargo_ucreti'];
              }else{
                $kargo_ucreti = 0;
              }
            ?>
            <div class="alan mb-15">
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-12"><div class="sepet_tutar_bilgi"><span><i class="fa fa-truck"></i> Kargo Ücreti: </span><?php echo $kargo_ucreti == 0 ? 'Kargo Bedava' : $kargo_ucreti.' TL'; ?></div></div>

                  <?php

                    $kupon_text = 'Geçersiz Kupon';

                    if($_POST){
                      $_SESSION['kupon'] = $_POST['kod'];
                    }

                    if(isset($_SESSION['kupon'])){

                      $kupon = $db->prepare("SELECT * FROM kupon where kod=:kod LIMIT 1");
                      $k = $kupon->execute(array(":kod"=>$_SESSION['kupon']));
                      $k = $kupon->fetch(PDO::FETCH_ASSOC);

                      if($k){
                        if($k['indirim_turu'] == 1){
                          $toplam = ($toplam * $k['tutar']) / 100;
                          $kupon_text = '%'.$k['tutar'].' İndirim';
                        }else{
                          $toplam = $toplam - $k['tutar'];
                          $kupon_text = '-'.$k['tutar'].' İndirim';
                        }
                      }


                    }

                    if(isset($_SESSION['kupon'])){
                      echo '<div class="col-md-12"><div class="sepet_tutar_bilgi"><span><i class="fa fa-tag"></i> Kupon İndirimi: </span> '.$kupon_text.'</div></div>';
                    }
                  ?>

                  <div class="col-md-12"><div class="sepet_tutar_bilgi"><span><i class="fa fa-shopping-cart"></i> Sepet Tutarı: </span><?php echo $toplam - (($toplam * $cek['kdv']) / 100); ?> TL</div></div>
                  
                  <div class="col-md-12"><div class="sepet_tutar_bilgi"><span><i class="fa fa-shopping-cart"></i> Kdv (%<?php echo $cek['kdv']; ?>): </span><?php echo (($toplam * $cek['kdv']) / 100); ?> TL</div></div>
                  <div class="col-md-12"><div class="sepet_tutar_bilgi"><span><i class="fa fa-credit-card"></i> Toplam Tutar: </span><?php echo $toplam + $kargo_ucreti; ?> TL</div></div>
                </div>
              </div>
            </div>

          </div>
          <?php
            $query = $db->prepare("SELECT * FROM kullanici where id=:id LIMIT 1");
            $kullanici = $query->execute(array(":id"=>$_SESSION['kullanici']['id']));
            $kullanici = $query->fetch(PDO::FETCH_ASSOC);
          ?>
          <div class="col-md-6">
            <div class="alan" style="margin-bottom: 0px;border-bottom: 0px">
              <h3 style="margin-top: 0px;float: left;width: 100%;margin-bottom: 0px">Sipariş Bilgileriniz</h3>
            </div>
            <div class="alan">
              <form action="alisverisi-tamamla" method="post">
                  <fieldset class="form-group mt-4 col-md-6">
                     <label>Adınız</label>
                     <input type="text" class="form-control" name="ad" required="" placeholder="Adınız" value="<?php if(isset($_SESSION['kullanici']['login'])){ echo $kullanici['ad']; } ?>">
                  </fieldset>
                  <fieldset class="form-group mt-4 col-md-6">
                     <label>Soyadınız</label>
                     <input type="text" class="form-control" name="soyad" required="" placeholder="Soyadınız" value="<?php if(isset($_SESSION['kullanici']['login'])){ echo $kullanici['soyad']; } ?>">
                  </fieldset>
                  <fieldset class="form-group mt-4 col-md-6">
                     <label>Telefon</label>
                     <input type="text" class="form-control" name="telefon" required="" placeholder="Telefon" value="<?php if(isset($_SESSION['kullanici']['login'])){ echo $kullanici['telefon']; } ?>">
                  </fieldset>
                  <fieldset class="form-group mt-4 col-md-6">
                     <label>Email Adresiniz</label>
                     <input type="email" class="form-control" name="email" required="" placeholder="Email Adresiniz" value="<?php if(isset($_SESSION['kullanici']['login'])){ echo $kullanici['email']; } ?>">
                  </fieldset>
                  <fieldset class="form-group mt-4 col-md-12">
                     <label>Fatura İçin Tc Kimik No</label>
                     <input type="text" class="form-control" name="tc" placeholder="Tc Kimlik No" value="">
                  </fieldset>
                  <?php if(!isset($_SESSION['kullanici']['login'])){ ?>
                  <fieldset class="form-group col-md-12">
                     <label>Şifreniz</label>
                     <input type="password" class="form-control" name="sifre" required="" placeholder="********">
                  </fieldset>
                  <?php } ?>
                  <fieldset class="form-group col-md-12">
                     <label>Sipariş Adresiniz</label>
                     <textarea class="form-control" name="adres" required="" placeholder="Sipariş Adresiniz" rows="3"><?php if(isset($_SESSION['kullanici']['login'])){ echo $kullanici['adres']; } ?></textarea>
                  </fieldset>
                  <fieldset class="form-group col-md-12">
                    <select name="odeme_yontemi" required="" class="form-control">
                      <option value="">Ödeme Yöntemi Seçiniz</option>
                      <?php if($cek['online_odeme'] == 1){ echo '<option value="1">Online Kredi Kartı</option>'; } ?>
                      <?php if($cek['kapida_kredi_karti'] == 1){ echo '<option value="2">Kapıda Kredi Kartı</option>'; } ?>
                      <?php if($cek['kapida_nakit'] == 1){ echo ' <option value="3">Kapıda Nakit</option>'; } ?>
                      <?php if($cek['banka_havalesi'] == 1){ echo '<option value="4">Banka Havalesi</option>'; } ?>
                    </select>
                  </fieldset>
                  <fieldset class="form-group col-md-12">
                    <div class="form-check">
                      <input type="checkbox" class="form-check-input" id="exampleCheck1" required="">
                      <label class="form-check-label" for="exampleCheck1">Talebim doğrultusunda <a href="sayfa/satis-sozlesmesi" target="_blank">satış sözleşmesini</a> onaylıyorum.</label>
                    </div>
                  </fieldset>
                  <fieldset class="form-group col-md-12">
                     <button class="btn icon-btn btn-success" style="width: 100%">Siparişi Oluştur</button>
                  </fieldset>
                </form>
            </div>
          </div>
        </div>
      <?php 
      }else{
        echo '<div class="row mt-15"><div class="alan"><center><h3>Sepetinizde ürün bulunmuyor...</h3></center></div></div>';
      }
      ?>

  </div>
</section>
