<?php session_start();
include("includes/db_config.php");
?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content=" ">
    <meta name="author" content="">
    <title>Orange Groceries</title>
    <?php include("includes/css.php");?>
</head>
<body>
    <?php include("includes/header.php");?>
    <section class="page-info section-padding border-bottom topthumb">
       <div class="container-fluid">
            <div class="row"> 
            <div class="col-md-12"> 
                    <a href="index.php"> <span class="mdi mdi-home"></span> &nbsp; Home</a> <span class="mdi mdi-chevron-right"></span> <a href="#">Cart</a>
                </div> 
                </div> 
        </div>
    </section>
 <?php  $user_id=$_SESSION['id'];
$session_id=$_SESSION['prd_session_id'] ?? 0;
$sql_qrys="select count(*) as catprd from addto_cart where (user_id='".$user_id."' OR session_id='".$session_id."') and invoice_id='1'";
$resuss = mysqli_query($conn, $sql_qrys);
$res_datass= mysqli_fetch_assoc($resuss);
if($res_datass['catprd'] == 0){
 ?> 
    <section class="shop-list section-padding">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                  <h4> Your Cart Is Empty Please </h4>
                </div>
                <div class="col-md-3">
              <a href="index.php" class="text-white"><button type="button" name="submit" class="btn btn-lg text-center" style="background:#ec1d27;color: white;margin-top:10px;font-size:18px; cursor: pointer;color:#fff">Continue Shopping</a>
            </div>
          </div>
              </div>
          </section>
       <?php }else{ ?>   
       <section class="shop-list section-padding">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-9">
        <h3>My Cart</h3>
                    <div class="card card-body cart-table anajcart">
                        <div class="table-responsive">
                            <table class="table cart_summary">
                                <thead>
                                    <tr>
                                        <th class="cart_product" colspan="2">Item Details</th>
                                        <th>Product Size</th>
                                        <th>Unit Price</th>
                                        <th class="text-center">Qty</th>
                                        <th>Sub Total</th>
                                        <th class="action"> </th>
                                    </tr>
                                </thead> 
                                <tbody>
                          <?php 
                              $user_id=$_SESSION['id'];
                              if(!empty($_SESSION['prd_session_id'])){
                              $session_id=$_SESSION['prd_session_id']; }else{
                              $session_id="";  
                              }
                            $sql_qry="select addto_cart.*,product.prod_img1 from addto_cart join product on addto_cart.prod_id=product.id where (user_id='".$user_id."' OR session_id='".$session_id."') and invoice_id='1'"; 
                            $resu = mysqli_query($conn, $sql_qry);
                            while($res_data= mysqli_fetch_assoc($resu)){
                            $prd_did=$res_data['prod_details_id'];
                            $prd_id=$res_data['prod_id'];
                            $sql_prdd="SELECT * from product_detail where id='$prd_did'";
                            $exe_prdd=mysqli_query($conn,$sql_prdd);
                            $res_prdd=mysqli_fetch_array($exe_prdd);
                             $sql_prd="select sub_category.sub_category_name,category.cat_name,sub_sub_category.sub_sub_cat_name,product.* from product join category on product.category_id=category.id join sub_category on product.sub_category_id=sub_category.id join sub_sub_category on product.sub_sub_cat_id=sub_sub_category.id where product.id='$prd_id'";
                            $result_prd = mysqli_query($conn, $sql_prd);
                             $res_prd= mysqli_fetch_array($result_prd);
                             $cat_name=$res_prd['cat_name']; 
                             $scat_name=$res_prd['sub_category_name']; 
                             $sub_sub_cat_name=$res_prd['sub_sub_cat_name']; 
                            $prod_name=$res_prd['prod_name']; 
                            $category = strtolower(str_replace(" ", "-", $cat_name));
                            $sub_category = strtolower(str_replace(" ", "-", $scat_name));
                            $ssub_category = strtolower(str_replace(" ", "-", $sub_sub_cat_name));
                            $product_name = strtolower(str_replace(" ", "-", $prod_name));
                                $ttl=(int)$res_data['price'] * (int)$res_data['quantity']; ?>  <tr class="container">
                                        <td class="cart_product" width="10%">
                                            <a class="category-item" href=""><img class="img-fluid" style="border:1px solid #919904;" src="admin/product/<?php echo $res_data['prod_img1'];?>" alt="<?php echo $res_data['prod_name'];?>"></a>
                                        </td>
                                        <td class="cart_description" width="35%">
                                            <h4 class="product-name"><a href="https://orangegroceries.com/<?php echo "$category/$sub_category/$ssub_category/$product_name.php"; ?>"><?php echo $res_data['prod_name'];?></a></h4>
                                        </td>
                                        <td class="availability in-stock text-black f-16" width="15%"><?php echo $res_prdd['prod_quantity']." ".$res_prdd['prod_quantity_type'];?></td>
                                        <td class="availability in-stock text-black f-16" width="15%">₹ <?php echo $res_data['price'];?></td>
                                        <td class="qty" width="10%">
                                             <input type="hidden" name="price" id="price" value="<?php echo $res_data['price']; ?>" >
                                             <!-- <input type="text"  name="cart_quantity" id="qty" value="<?php echo $res_data['quantity']; ?>" class="form-control" style="color:#fff; border:1px solid #919904; font-size:20px;background-color:#bfca02; width:100px;"> -->
                                            <input type="hidden" name="id" id="id" value="<?php echo $res_data['id']; ?>" >
                                            <div class="quantity" style="padding-top: 1px; width:150px; height:40px;">
                        <input type="button" class="dec button" onclick="decrementValue()" value="-" style="border:none;padding:5px;width: 40px;font-size: 40px;background-color:#f6b208;color:#000;float: left;height: 55px; text-align: center" />
                        <input type="text" name="quantity" value="<?php echo $res_data['quantity']; ?>" maxlength="2" max="10" size="1" id="number" style="border:1px solid #ccc;font-size: 15px;width:60px;padding:13px 13px 14px 13px; float: left" />
                        <input type="button" class="inc button" style="border:none;padding:5px;width: 40px;font-size: 35px;background-color:#f6b208;color:#000;float: left;height: 55px; text-align: center" value="+" />
                    </div>
                                         </td>
                                        <td class="price text-black f-16" width="20%"><span class="currency-sign" id="price_576"><input type="text" id="total" value="<?php echo $ttl; ?>" name="total"  class="form-control " readonly style="background-color:transparent; color:#0091ae;text-align:center; border:0px solid #ccc; box-shadow: inset 0 0px 0px rgba(0,0,0,.075);"></span></td>
                                        <td class="action" width="10%">
                                            <a class="btn btn-sm" data-original-title="Remove" href="javascript:delete_cart_by_ID('<?php echo $res_data['id'] ?>');" title="" data-placement="top"><img src="img/delete-icons.png" style="width:30px; height:37px"></a>
                                        </td>
                                    </tr>
                                    <?php } ?>

                                </tbody>
                            </table>
                        </div>
                    </div>
          <div class="col-md-3">
              <a href="index.php" class="text-white"><button type="button" name="submit" class="btn btn-lg text-center" style="background:#ec1d27;color: white;margin-top:10px;font-size:18px; cursor: pointer;color:#fff">Continue Shopping</a>
            </div>
                </div>
                <div class="col-md-3">
        
          <div class="row" style="margin-top:20px;">
            <div class="carts"> 
                <div class="summary">
                  <h5 class="summary-total-items text-white text-center">Payment Details</h5>
                  <hr />
                  <div class="summary-promo">
                <?php $user_id=$_SESSION['id'];
                  if(!empty($_SESSION['prd_session_id'])){
                  $session_id=$_SESSION['prd_session_id']; }else{
                  $session_id=""; }
                  $sql_qryss="select Sum(product_detail.prod_cross_price * addto_cart.quantity) as prcs from addto_cart join product_detail on addto_cart.prod_details_id=product_detail.id where (addto_cart.user_id='".$user_id."' OR addto_cart.session_id='".$session_id."') and addto_cart.invoice_id='1'"; 
                  $resuss = mysqli_query($conn, $sql_qryss);
                  $res_prdss= mysqli_fetch_assoc($resuss);
                   $mpr= $res_prdss['prcs'];
                   $price_cross= $mpr;
                  $user_id=$_SESSION['id'];
                 if(!empty($_SESSION['prd_session_id'])){
                              $session_id=$_SESSION['prd_session_id']; }else{
                              $session_id="";  
                              }
                 $sql_ttl="select SUM(quantity * price) as ttl from addto_cart  where (addto_cart.user_id='".$user_id."' OR addto_cart.session_id='".$session_id."') and addto_cart.invoice_id='1'"; 
                    $resu_ttl = mysqli_query($conn, $sql_ttl);
                    $res_ttl= mysqli_fetch_assoc($resu_ttl); 
                    $discount=$price_cross - $res_ttl['ttl']; ?>
                    <?php  $dis_price=$res_ttl['ttl'];  ?>
                    <div class="subtotal-title">Price:</div>
                    <div class="promo-value">Rs. <?php echo $price_cross; ?></div>
                  </div> 
                  <div class="summary-promo">
                    <div class="subtotal-title">Product Discount</div>
                    <div class="promo-value"> <?php echo $discount; ?></div>
                  </div>
                  <?php if($dis_price <= 1800){
                    $dis_price=$dis_price+100;  ?>
                  <div class="summary-promo">
                    <div class="subtotal-title">Delivery Charge</div>
                    <div class="promo-value">₹<?php echo "100"; ?></div>
                  </div><?php } ?>
                 </div>
                                
                <div class="summary-total mt-4">
                  <div class="subtotal-title text-yellow"><strong>Total</strong></div>
                  <div class="promo-value text-yellow"><strong>Rs.<?php echo $dis_price; ?></strong></div>
                </div>
                <div class="summary-total mt-4"><hr/></div>
                 
            </div>
            <div class="col-md-12"><h5 class="text-reds" style="text-align:right;padding-top:10px;"> &nbsp; You Save:Rs.<?php echo $discount; ?></h5></div>
            <?php $result="";?>
            <div class="col-md-12">
            <div class="row">
            <?php if (isset($_POST['showbtn'])) {
                echo $_POST['message']; }?>    
             <?php 
             if($dis_price >= 800){
             if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){ ?> 
              <a href="checkout.php" style="width:100%;"><button type="button" name="submit" value="" class="list-group-item list-group-item-action text-center btn-lg" style="background:#ec1d27;color: white;margin-top:20px;font-size:18px; cursor: pointer;">Place Order</button></a><?php }else{ ?>
             <a href="check-login.php" style="width:100%;"><button type="button" class="list-group-item list-group-item-action text-center btn-lg" style="background:#ec1d27;color: white;margin-top:20px;font-size:18px; cursor: pointer;">Place Order</button></a><?php } }else{ $rpr=1000-$dis_price; ?>
              <form method="post" action="" style="width:100%;">
                <input type="hidden" name="message" value="<?= "Add Items Worth ₹$rpr More To Place Order" ?>">
             <button type="submit" name="showbtn" class="list-group-item list-group-item-action text-center btn-lg" style="width:100%;background:#ec1d27;color: white;margin-top:20px;font-size:18px; cursor: pointer;">Place Order</button> </form>
             <?php  
             } ?>
            </div>
            </div>
            </div>
        </div>
            </div>
        </div>
        </div>
    </section>
<?php } ?>
    <section class="">
  <div class="container-fluid"> 
  <?php  $sqlg = "SELECT * from cart_ads";
  $resultg = mysqli_query($conn,$sqlg) or die(mysqli_error());
  $resg= mysqli_fetch_array($resultg); ?>
  <div class="row" style="margin-bottom:20px; margin:auto:"> 
<center><a href="<?php echo $resg['url']; ?>"><img src="admin/img/ads/<?php echo $resg['image']; ?>" class="img-responsive"></a></center>
</div>
  </div>
</section>
    <?php include("includes/footer.php");?>
    <?php include("includes/js.php");?>
 <script type="text/javascript">

function delete_cart_by_ID(id)
{
    if (confirm('Do You Want to Deleting This \nContinue anyway?')) {
        window.location.href = 'delete_cart.php?id=' + id;
    }
}
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#showbtn").click(function(){

            $.ajax({
                type: 'POST',
                url: 'script.php',
                success: function(data) {
                    alert(data);
                    $("p").text(data);

                }
            });
   });
});
</script>
 <script>
       (function() {
    "use strict";

    $("table").on("keyup", "input", function() {
      var row = $(this).closest("tr");
      var qty = parseFloat(row.find("#qty").val());
      var price = parseFloat(row.find("#price").val());
      var total = qty * price;
      row.find("#total").val(isNaN(total) ? "" : total);
    });
  })();
$(function(){
$('body').on('click', '.button', function(){
  var $container = $(this).closest(".container");
 // var id = $container.find("#id").val();
  //var qty = $container.find("#number").val();
  //alert(id);
  //alert(qty);
   $.ajax({
      type : 'POST',
      url : 'update_qty.php', // --> server side code to insert data into db
      data : {
        id: $container.find("#id").val(),
        val : $container.find("#number").val()
      },
      success : function(msg){
        console.log(msg);
        window.location.href=window.location.href;
      }
  });
});
});
    </script>
    
    <script type="text/javascript">
      incrementVar = 0;
$('.inc.button').click(function(){
    var $this = $(this),
        $input = $this.prev('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val())+1;
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});
$('.dec.button').click(function(){
    var $this = $(this),
        $input = $this.next('input'),
        $parent = $input.closest('div'),
        newValue = parseInt($input.val())-1;
    console.log($parent);
    $parent.find('.inc').addClass('a'+newValue);
    $input.val(newValue);
    incrementVar += newValue;
});

</script>
</body>

</html>
