<?php include "header.php"; 


if(isset($_POST['checkouttocart'])){
	
	$scls = $_POST['crtchk'];
	$sc = count($scls);
	$c = 0;
	do{
		$clsnm = test_input($scls[$c]);
		$clsinf = test_input($_POST['mbcrtqty'][$c]);
		if(!empty($clsnm)){
			
			$chkct = getOne("my_cart"," pqrcode = '$clsnm' AND session_rec = '$set_sess' ","id");
			if(!empty($chkct)){
			$ins_subcls = $db_con->prepare("UPDATE my_cart SET qty =:qtyinf WHERE id =:cqid "); 
			$ins_subcls->bindParam(':qtyinf',$clsinf);
			$ins_subcls->bindParam(':cqid',$chkct);
			$ins_subcls->execute();
				
			}
		} $c++;
	}
	while($c < $sc);
	
	
	echo "<script>window.location.href ='checkout'</script>";
}

?>

	<div class="hero-section hero-background" style="background-image:url(img/bn3.jpg); height:230px;">
        <div class="container"><h1 class="pg-title text-left">CART</h1></div>
    </div>

	<div class="bcrumb">
    <div class="container">
        <nav class="biolife-nav nav-86px">
            <ul>
                <li class="nav-item"><a href="index" class="permal-link">Home</a></li>
                <li class="nav-item"><span class="current-page">Cart</span></li>
            </ul>
        </nav>
    </div>
	</div>

    <div class="page-contain shopping-cart">

        <!-- Main content -->
        <div id="main-content" class="main-content">
            <div class="container">

                <div class="shopping-cart-container">
				<?php
				if((!empty($chk_crt)) && (cart_stk("tot_crt") > 0)){
				?>
                    <div class="row text-center xs-margin-bottom-80px" id="incrtshw">
                        <div class="col-lg-9 col-md-12 col-sm-12 col-xs-12 float-none mx-auto">
                            <h3 class="box-title">Your cart items</h3>
                            <form class="shopping-cart-form" action="" autocomplete="" enctype="multipart/form-data" method="post">
                                <table class="shop_table cart-form">
                                    <thead>
                                    <tr>
                                        <th class="product-name">Product Name</th>
                                        <th class="product-price">Price</th>
                                        <th class="product-quantity">Quantity</th>
                                        <th class="product-subtotal">Total</th>
                                    </tr>
                                    </thead>
                                    <tbody>
									<?php
									$mainsubt = 0; $cv = 0;
									foreach($chk_crt as $dcv){
										$fdprp = $dcv['pqrcode'];
										
										$gt_prds = gtOne("kmartstockprds","  pqrcode = '$fdprp' ",'ksp_id');
										if(!empty($gt_prds)){
											$cv ++;
										$chk_prdz = $db_con->prepare("SELECT * FROM kmartstockprds WHERE ksp_id = '$gt_prds' ");
										$chk_prdz->execute(); $pdz = $chk_prdz->fetch();	
										
										$chkct = getOne("my_cart"," pqrcode = '$pdz[pqrcode]' AND session_rec = '$set_sess' ","qty");
										
										$gtctnm = gtOne("ktmmenu_name"," mn_id = $pdz[mn_id] ","mname");
										
										$gt_imgs = gtOne("kprdw_imgs"," prd_rec = '$pdz[pqrcode]' ORDER BY kmg_id ASC","pjm_pic");
										
										$chk_mgi = $gt_imgs;     $chk_sl = $pdz['psell'];    $chk_ds = $pdz['pdiscount'];
										if($dcv['prd_attr'] == 'promo'){
										//FROM GENERAL FEATURED SPECIAL PROMO
										$chk_ftp = $db_con->prepare("SELECT * FROM kmt_ft_prds WHERE ksp_id = '$pdz[ksp_id]'  AND delete_status = 0");
										$chk_ftp->execute(); $fpd = $chk_ftp->fetch();
										
										$chk_mgi = (!empty($fpd) && !empty($fpd['prd_img'])) ?  $fpd['prd_img'] : $gt_imgs;
										$chk_sl = (!empty($fpd) && !empty($fpd['km_sell'])) ? $fpd['km_sell'] : $pdz['psell'];
										$chk_ds = (!empty($fpd) && !empty($fpd['km_discount'])) ? $fpd['km_discount'] : $pdz['pdiscount'];
										}
										
										$prd_img = (!empty($chk_mgi) && file_exists('prdimgs/'.$chk_mgi)) ? "prdimgs/".$chk_mgi : 'img/kmart-icon.png';
										
										$shw_pec = calcprcs($chk_sl,$chk_ds,'spc');
										$shw_ds = calcprcs($chk_sl,$chk_ds,'ssl');
										$shw_dsc = calcprcs($chk_sl,$chk_ds,'sds');
										
										$imdsubt = ($shw_ds*$chkct);
										$mainsubt += $imdsubt;
										
										$ltrand = rand();
									?>
                                    <tr class="cart_item pdzkpm" data-cart="<?= $pdz['pqrcode']; ?>">
                                        <td class="product-thumbnail" data-title="Product Name">
                                            <a class="prd-thumb" href="product?kmt=<?= $pdz['purl']; ?>" target="_blank">
                                                <figure><img width="113" height="113" src="<?= $prd_img; ?>" alt="cart product photo"></figure>
                                            </a>
                                            <a class="prd-name" href="product?kmt=<?= $pdz['purl']; ?>" target="_blank"><?= $pdz['pname'] ?></a>
                                            <div class="action">
                                                <a href="javascript:void(0);" class="remove"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
                                            </div>
                                        </td>
                                        <td class="product-price" data-title="Price">
                                            <div class="price price-contain">
                                                <ins><span class="price-amount"><span class="currencySymbol">GH₵</span class="subttprc"><?= $shw_ds; ?></span></ins>
                                                <?= $shw_dsc; ?>
                                            </div>
                                        </td>
										<input type="hidden" name="crtchk[]" value="<?= $pdz['pqrcode']; ?>" />
                                        <td class="product-quantity" data-title="Quantity">
                                            <div class="quantity-box type1">
                                                <div class="qty-input">
												
													<input type="text" name="mbcrtqty[]" id="mbcrtqty" value="<?= (!empty($chkct)) ? $chkct : 1; ?>" data-maxvalue="20" data-subcost="<?= $shw_ds; ?>" data-min_value="1" data-step="1"  data-crtque=<?= $ltrand; ?>>
													
													<a href="javascript:void(0);" class="qty-btn btn-up"><i class="fa fa-caret-up" aria-hidden="true"></i></a>
													<a href="javascript:void(0);" class="qty-btn btn-down"><i class="fa fa-caret-down" aria-hidden="true"></i></a>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="product-subtotal" data-title="Total" id="lpc_<?= $ltrand; ?>">
                                            <div class="price price-contain">
                                                <ins><span class="price-amount subttprc"><span class="currencySymbol">GH₵</span><b class="mprc"><?= $imdsubt; ?></b></span></ins>
                                            </div>
                                        </td>
                                    </tr>
									<?php
										}
									}
									?>
                                    </tbody>
                                </table>
								
								<div class="width-100percent">
									<div class="shpcart-subtotal-block">
										<div class="subtotal-line">
											<b class="stt-name">Subtotal <span class="sub">(<b class="ctitm"><?= $cv ; ?></b> Item<?= ($cv > 1) ? 's' : ''; ?>)</span></b>
											<span class="stt-price">GH₵<b id="ttprcd"><?= $mainsubt ?></b></span>
										</div>
										<div class="btn-checkout">
											<a class="btn back-to-shop yw_bg">Back to Shop</a>
											
											<button type="submit" name="checkouttocart" class="btn checkout">Check out</button>
										</div>
										<?php /*<div class="biolife-progress-bar">
											<table>
												<tr>
													<td class="first-position">
														<span class="index">$0</span>
													</td>
													<td class="mid-position">
														<div class="progress">
															<div class="progress-bar" role="progressbar" style="width: 25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
														</div>
													</td>
													<td class="last-position">
														<span class="index">$99</span>
													</td>
												</tr>
											</table>
										</div>
										<p class="pickup-info"><b>Free Pickup</b> is available as soon as today More about shipping and pickup</p>
										*/ ?>
									</div>
								</div>
						
                            </form>
                        </div>
                        
                    </div>
				<?php
				}else{
					?>
				<div class="row">
					<div class="col-md-8 float-none mx-auto">
					<div class="content-404 w-100" style="padding-top: 0;margin-top: -25px;">
						<h1 class="heading"><i class="fa fa-shopping-cart"></i></h1>
						<h2 class="title">Oops! Empty Cart</h2>
						<a class="button" href="shop">Shop</a>
					</div>
					</div>
				</div>
					<?php
				}
				?>
                </div>

            </div>
        </div>
    </div>
	
<?php include "footer.php"; ?>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
	$("#minicrt").find(".cart-content").hide();
	
	$(document).on('click','.pdzkpm .action .remove',function (e){
        e.preventDefault();
        let _this = $(this), trcart_item = _this.closest('tr'); var _thiskp = _this.parents('.pdzkpm'); var _thiscart = _thiskp.attr('data-cart'); 
		$.ajax({ url: 'actions/carts.php', type: 'POST', data: {'outDelCart':1, 'shiftcart':_thiscart}, beforeSend: function() { _this.attr('disabled', 'disabled'); _this.html('<b class="fa fa-spin fa-spinner"></b>') }, 
		dataType:"json",success: function(response){ if(response.cartrec == 'cart'){ trcart_item.remove(); 
        if(response.itemscount <= 0){ $("#minicrtdrp").html('<p class="minicart-empty">No product in your cart</p>'); 
		$("#incrtshw").html('<div class="col-md-8 float-none mx-auto"> <div class="content-404 w-100" style="padding-top: 0;margin-top: -25px;">'+
		'<h1 class="heading"><i class="fa fa-shopping-cart"></i></h1> <h2 class="title">Oops! Empty Cart</h2> <a class="button" href="shop">Shop</a></div></div>'); 
		 } $("#cart_count").val(response.itemscount); $('#shcrcnt, .ctitm').html(response.itemscount); $('#shcrtmt, #ttprcd').html(response.amt_count);
        }} 
        });
    });
	
});
</script>