<?php include 'includes/header.php';?>
<title><?php echo $dil['078'];?></title>
<meta name="description" content="Sepetim">
<meta name="keywords" content="sepetim,">
<meta name="author" content="ParsTech">


<body class="mobile_panel black_theme">

    <!-- Sepet_Urunler_Wrapper -->
    <div class="cart_product_wrapper">
        <div class="container">
            <div class="cart_product_list flex justify-center items-center flex-col">
                <div class="product_detail_title "><?php echo $dil['078'];?></div>


                <?php 
                //print_r($_SESSION['sepet']);
                if(count($_SESSION['sepet']) > 0){
                  foreach ($_SESSION['sepet'] as $siparis => $siparisdetay) {
                    $array=$siparisdetay;
                    $urun_getir = $dbh -> prepare("SELECT * FROM product WHERE id = ?");
                    $urun_getir-> execute(array($siparisdetay["urunid"]));
                    $urun = $urun_getir->fetch(PDO::FETCH_ASSOC);

                    $urun = cevir("product",$urun,$_SESSION["dil"]);

                    $fiyat = $urun["product_price"];
                    
                    $ekstraistek=0;
                    $porsyon="";
                    $pfiyat=0;
                    if($siparisdetay["menu_porsiyon"]>0){
                        $porsyon = $dbh -> prepare("SELECT * FROM product_portion WHERE id = ?");
                        $porsyon-> execute(array($siparisdetay["menu_porsiyon"]));
                        $porsyon = $porsyon->fetch(PDO::FETCH_ASSOC);
                        $key= array_search($siparisdetay["menu_porsiyon"],json_decode($urun['product_portion']) );
                        $porsiyonfler=json_decode($urun['product_portion_price']) ;
                        $pfiyat= $porsiyonfler[$key];
                    }  
                    if($siparisdetay["ekstra"]){
                        $ekstralara=" ";
                        $eksftop=0;
                        foreach ($siparisdetay["ekstra"] as $eks) {
                            $ekstrad = $dbh -> prepare("SELECT * FROM selectable WHERE id = ?");
                            $ekstrad-> execute(array($eks));
                            $ekstrad = $ekstrad->fetch(PDO::FETCH_ASSOC);
                            $ekstrad = cevir("selectable",$ekstrad,$_SESSION["dil"]);
                            
                            $key= array_search($eks,json_decode($urun['product_optionek']) );
                            $ekstrafiyat=json_decode($urun['product_optionek_price']) ;
                            $ekfiyat= $ekstrafiyat[$key];
                            $eksftop+=$ekfiyat;

                            if($ekfiyat>0){
                                $ekstralara.= $ekstrad['option']." ".$ekfiyat." TL / ";
                            }else{
                                $ekstralara.= $ekstrad['option']." / ";
                            }
                        }
                        $ekstralara=rtrim($ekstralara," / " );
                    }

                    $toplam = ($fiyat + $pfiyat +  $eksftop)*$array['qty']; 
                    $toplamb = ($fiyat + $pfiyat +  $eksftop); 

                    $urun = cevir("product",$urun,$_SESSION["dil"]);
                    $toplam2 += $toplam;

                    $porsyon = cevir("product_portion",$porsyon,$_SESSION["dil"]);
                    ?>


                    <!-- Sepet Urunler_Start -->
                    <div class="cart_product card border-0 shadow-light mb-4 w-full">
                        <div class="card-body">
                            <div class="row">
                                <div class="cart_product_image col-md-12">
                                    <img src="img/<?=$urun['foto']?>" alt="">
                                </div>
                                <div class="col-md-12 text-center text-lg mb-4">
                                    <div class="flex flex-col">
                                        <p class="mb-1"><a href="#" class="text-default"><?=$urun['product_name']?> <font  style="font-size: 14px">+ <?= $fiyat ?> TL</font> <font style="font-size: 14px"><?php if($porsyon['portion']){
                                            echo "/ ".$porsyon['portion'] ." + " .$pfiyat . "TL";
                                        }?> </font></a></p>
                                        <p style="font-size: 13px">+<?=$ekstralara;?></p>
                                        <p class="text-success my-0"><?=$toplam?> TL</p>
                                    </div>

                                </div>
                                <div class="col-md-12 my-2 flex justify-between align-items-centers">
                                   <div class="input-group text-center increasenumber">
                                    <div class="input-group-prepend ">
                                        <button onclick="azalt(<?=$siparis?>,<?= $toplamb;?>)"  class="btn btn-sm btn-outline-success px-3 remove" type="button">-</button>
                                    </div>
                                    <input type="text" id="qty<?=$siparis?>" class="form-control form-control-sm text-center w-50px" placeholder="" value="<?=$siparisdetay['qty']?>">
                                    <div class="input-group-append">
                                        <button onclick="arttir(<?=$siparis?>,<?= $toplamb;?>)" class="btn btn-sm btn-outline-success px-3 add" type="button">+</button>
                                    </div>
                                </div>


                                <form action="sepet.php" method="post">

                                  <input type="hidden" name="tip" value="sil">
                                  <input type="hidden" value="<?=$siparis; ?>" name="id"> 


                                  <button type="submit" class="btn btn-sm btn-link text-danger p-0 flex justify-center items-center"><i class="material-icons">delete</i></button>
                              </form>

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


              <?php
          }

      }else{
        echo '
        <div class="alert alert-warning mb-12" role="alert">
        ' . $dil['152'] . '
        </div>
        ';
    }

    ?> 


    <!-- Sepet Urunler_End -->
</div>





<!-- Sepet Detaylar -->
<form action="" method="post">

    <div class="input-group text-center mb-4">
        <input type="text"  class="form-control" name="code" placeholder="<?php echo $dil['155'];?>">
        <div class="input-group-append">
            <button class="btn btn-success px-3" type="submit"><i class="material-icons h6 my-0">done</i> <?php echo $dil['079'];?></button>

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

<?php 
if($_POST){
    $code= $_POST['code'];

    $kodkontrol = $dbh -> prepare("SELECT * FROM coupon WHERE coupon_code = ?");
    $kodkontrol-> execute(array($code));
    $kodkontrol = $kodkontrol->fetch(PDO::FETCH_OBJ);
    if(!$kodkontrol){
        echo '
        <div class="alert alert-danger mb-4" role="alert">
        ' . $dil['153'] . ' 
        </div>';
    }elseif($kodkontrol->status==2){
        echo '  <div class="alert alert-warning mb-4" role="alert">
        ' . $dil['154'] . ' 
        </div>';
    }elseif($toplam2<$kodkontrol->minimum_amount){
      echo '  <div class="alert alert-warning mb-4" role="alert">
      ' . $dil['155'] . '  '.$kodkontrol->minimum_amount.' ' . $dil['156'] . ' 
      </div>';
  }else{
      $_SESSION['indirim']=$code;
      echo ' <div class="alert alert-success mb-4" role="alert">
      ' . $dil['157'] . ' 
      </div>';
  }

}

if($_SESSION['indirim']){
    $kodkontrol = $dbh -> prepare("SELECT * FROM coupon WHERE coupon_code = ?");
    $kodkontrol-> execute(array($_SESSION['indirim']));
    $kodkontrol = $kodkontrol->fetch(PDO::FETCH_OBJ);
    if($kodkontrol->status==2 || $toplam2<$kodkontrol->minimum_amount){
      $_SESSION['indirim']="";
  }
}
?>
<div class="card border-0 shadow-light mb-4">
    <div class="card-body">
        <ul class="list-group list-group-flush">
            <li class="list-group-item"><span><?php echo $dil['070'];?></span> <span class="float-right font-weight-bold"> <span id="toplamtutart"> <?php if($toplam2>0){echo $toplam2;}else{echo "0";} ?> </span>TL</span></li>
            <!--  <li class="list-group-item"><span>Vergi</span> <span class="float-right font-weight-bold">₺ 18</span></li> -->
            <?php 

            $indirim = $dbh -> prepare("SELECT * FROM coupon WHERE coupon_code = ?");
            $indirim-> execute(array($_SESSION['indirim']));
            $indirim = $indirim->fetch(PDO::FETCH_OBJ);

            ?>
            <li class="list-group-item"><span><?php echo $dil['095'];?></span> <span class="float-right font-weight-bold text-success"><?php if($indirim){echo ''.$indirim->discount_amount." TL";
            $indt= $indirim->discount_amount;
        }else{echo "0 TL";} ?>

    </span></li>

    <li class="list-group-item"><span class=" font-weight-bold"><?php echo $dil['070'];?></span> <span class="float-right font-weight-bold"><span  id="toplamtutar"><?php
    echo $toplam2- $indt; 
    ?> </span> TL</span></li>
</ul>
</div>
</div>
<a href="siparis-tamamla" class=" btn btn-lg btn-default default-shadow btn-block"><?php echo $dil['071'];?> <span class="ml-2 icon arrow_right"></span></a>
<!-- Sepet Detaylar End -->


</div>
</div>
<?php include 'includes/modal.php';?>
<?php include 'includes/footer.php';?>


<script type="text/javascript">
    function azalt(id,tutar){
        var deger= $('#qty'+id).val();
        var sepet= $('#market_count').text();

        if(deger>1){
            deger--;
            sepet--;

            $('#market_count').text(sepet);
            $("#qty"+id).val(deger);
            $('#amount'+id).text(deger*tutar);

            var tutart= Number($('#toplamtutar').text());
            var tutarts= Number($('#toplamtutart').text());
            var islem = tutart-tutar;
            var islems = tutarts-tutar;

            $("#toplamtutar").text(islem);
            $("#toplamtutart").text(islems);


            $.ajax({
                type: "POST",
                url: "adet.php",
                data:'id='+id+'&adet='+deger,
                success: function(data){
                }
            });

        }
    }
    function arttir(id,tutar){
        var deger= $('#qty'+id).val();
        var sepet= $('#market_count').text();
        if(deger<12){
            deger++;
            sepet++;

            $('#market_count').text(sepet);
            $("#qty"+id).val(deger);
            $('#amount'+id).text(deger*tutar);
            var tutart= Number($('#toplamtutar').text());
            var islem = tutart+tutar;
            var tutarts= Number($('#toplamtutart').text());
            var islems = tutarts+tutar;


            $("#toplamtutart").text(islems);
            $("#toplamtutar").text(islem);


            $.ajax({
                type: "POST",
                url: "adet.php",
                data:'id='+id+'&adet='+deger,
                success: function(data){
                }
            });

        }
    }
</script>
