<?php 
ob_start();
session_start();


include('header.php');
?> 

<?php

$userid = $_SESSION['id'];
$webid = $json['frontend']['id'];

$offerprice =  $json['prices']['price'];
$originalprice =  $json['prices']['org_price'];


if(!isset($_SESSION['id']))
{
    header("Location:login.php");exit;
}


// $userid = '';
// if($_GET['userid']) {
// $userid = $_GET['userid'];
// }


// $webid = $json['frontend']['id'];

$handle = curl_init();
$url = "https://app.softsites.org/site/fetchcart.php";
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($handle, CURLOPT_POSTFIELDS, 'webid='.$webid.'&userid='.$userid.'');
$output = curl_exec($handle);
curl_close($handle);

$jsonpost = json_decode($output, true);

// echo "<pre>";
// print_r($json['couponcode']);
 
 for($j=0;$j<count($json['couponcode']);$j++)
 {
     if($json['couponcode'][$j]['couponname'] == $_GET['ccode'])
     {
         $coupontype = $json['couponcode'][$j]['coupontype'];
         $ratevalue = $json['couponcode'][$j]['ratevalue'];
         $couponname = $json['couponcode'][$j]['couponname'];
     }
 }
 
// echo count($jsonpost['ebookdata']);
?>		
        
        
        <!-- ======================== Breadcrumb Four Section Start ===================== -->
        <section class="breadcrumb breadcrumb-four padding-static-y-60 section-bg position-relative z-index-1 overflow-hidden">
        
            <img src="assets/images/gradients/breadcrumb-gradient-bg.png" alt="" class="bg--gradient">
        
            <img src="assets/images/shapes/element-moon3.png" alt="" class="element one">
            <img src="assets/images/shapes/element-moon1.png" alt="" class="element three">
            
            <div class="container container-two">
                <div class="row justify-content-center">
                    <div class="col-lg-12">
                        <div class="breadcrumb-four-content">
                            <h3 class="breadcrumb-four-content__title text-center mb-3 text-capitalize">Shopping Cart</h3>
                            <ul class="breadcrumb-list flx-align justify-content-center gap-2 mb-2">
                                <li class="breadcrumb-list__item font-14 text-body">
                                    <a href="index.php" class="breadcrumb-list__link text-body hover-text-main">Home</a>
                                </li>
                                <li class="breadcrumb-list__item font-14 text-body">
                                    <span class="breadcrumb-list__icon font-10"><i class="fas fa-chevron-right"></i></span>
                                </li>
                                <li class="breadcrumb-list__item font-14 text-body">
                                    <span class="breadcrumb-list__text">Cart</span>
                                </li>
                            </ul>
        
                            
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <!-- ======================== Breadcrumb Four Section End ===================== -->
        <!-- ======================= Cart Section Start ======================== -->
        <div class="cart padding-y-120">
            <div class="container">
                <div class="cart-content">
                    <?php
        
				if(count($jsonpost['ebookdata'])>=1){
				?>
				<div class="row">
					<div class="col-lg-8">
                        <div class="table-responsive">
                        <table class="table style-two">
                            <thead>
                              <tr>
                                <th>Product Details</th>
                                <th>Quantity</th>
                                <th>Price</th>
                                <th>Total</th>
                              </tr>
                            </thead>
                            <tbody>
                                <?php 
										$a = array();
									for ($i=0; $i < count($jsonpost['ebookdata']); $i++) { 
										
										if($offerprice != ''){  $a[$i] = $offerprice * $jsonpost['ebookdata'][$i]['qty']; }else{  $a[$i] = $jsonpost['ebookdata'][$i]['pageprice'] * $jsonpost['ebookdata'][$i]['qty'];}
											
									?>
									<form action="removecart.php" id="removecart<?php echo $i;?>" method="post">
                                  <tr>
                                    <td>
                                        <div class="cart-item">
                                            <div class="d-flex align-items-center gap-3">
                                                <div class="cart-item__thumb">
                                                    <a href="pdetail.php?id=<?php echo $jsonpost['ebookdata'][$i]['id'];?>" class="link">
                                                        <img src="<?php echo $jsonpost['ebookdata'][$i]['pageimg'];?>" alt="" class="cover-img">
                                                    </a>
                                                </div>
                                                <div class="cart-item__content">
                                                    <h6 class="cart-item__title font-heading fw-700 text-capitalize font-18 mb-4"> <a href="product-details.html" class="link"><?php echo $jsonpost['ebookdata'][$i]['pagename'];?></a></h6>
                                                    <span class="cart-item__price font-18 text-heading fw-500">Category: <span class="text-body font-14"><?php echo $jsonpost['ebookdata'][$i]['pagecatname'];?></span></span>
                                                </div>
                                            </div>
                                            <div class="flx-align gap-4 mt-3 mt-lg-4">
                                                <input class="qty" type="hidden" name="cartid" value="<?php echo $jsonpost['post'][$i]['id'];?>">
										        <input class="qty" type="hidden" name="userid" value="<?php echo $userid;?>">
												<input class="qty" type="hidden" name="domainid" value="<?php echo $json['frontend']['id'];?>">
                                                <a href="#" onclick="document.getElementById('removecart<?php echo $i;?>').submit();">
                                                    <button type="button" class="rounded-btn delete-btn text-danger hover-text-decoration-underline">Remove</button>
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="cart-item__count">
                                            <!--<button data-decrease="data-decrease"> <i class="fas fa-minus"></i></button>-->
                                            <input data-value="data-value" type="number" value="<?php echo $jsonpost['ebookdata'][$i]['qty'];?>" disabled>
                                            <!--<button data-increase="data-increase"><i class="fas fa-plus"></i></button>-->
                                        </div>
                                    </td>
                                    <td>
                                        <span class="cart-item__totalPrice text-body font-18 fw-400 mb-0">$<?php if($offerprice != ''){ echo $offerprice;}else{ echo $jsonpost['ebookdata'][$i]['pageprice'];}?></span>
                                    </td>
                                    <td>
                                        <span class="cart-item__totalPrice text-body font-18 fw-400 mb-0">$<?php if($offerprice != ''){ echo $offerprice * $jsonpost['ebookdata'][$i]['qty']; }else{ echo $jsonpost['ebookdata'][$i]['pageprice'] * $jsonpost['ebookdata'][$i]['qty'];}?></span>
                                    </td>
                                  </tr>
                                </form>
									<?php
									}?>
                            </tbody>
                            <tfoot>
								<tr>
									<td colspan="4" class="clearfix">
										<div class="float-left">
											<div class="cart-discount">
												<form action="cart.php" id="ccode" method="get">
													<div class="input-group">
														<input type="text" class="form-control form-control-sm"
															placeholder="Coupon Code" name="ccode" value="<?php if($_GET['ccode']){ echo $_GET['ccode'];}?>">
														<div class="input-group-append" style="margin-left:40px;">
															<button class="btn btn-outline-light flx-align gap-2 pill btn-lg" type="button" onclick="document.getElementById('ccode').submit();">Apply
																Coupon</button>
														</div>
													</div><!-- End .input-group -->
												</form>
											</div>
										</div><!-- End .float-left -->
										
									</td>
								</tr>
							</tfoot>
                        </table>
                    </div>
                    </div>
                    <div class="col-lg-4">
						<div class="cart-summary">
							<h5>Cart Totals</h5>
                            <div class="table-responsive">
							<table class="table style-two">
								<tbody>
									<tr>
										<td>Subtotal</td>
										<td>$<?php echo array_sum($a);?></td>
									</tr>
									<?php
                                    if($_GET['ccode'] == $couponname && isset($_GET['ccode'])){
                                    ?>
                                    <tr>
                                        <th>Discount Total</th>
                                        <td>
                                            <strong>
                                                <span class="amount">$<?php 
                                                
                                              $pricevalue = array_sum($a);
                                                if($coupontype == 'flatrate')
                                                {
                                                    $pricevalue =$ratevalue;
                                                }
                                                
                                                if($coupontype == 'percentrate')
                                                {
                                                    $d = $pricevalue*($ratevalue/100);
                                                    $pricevalue = $d;
                                                }
                                                
                                                echo $pricevalue;?></span>
                                            </strong>
                                        </td>
                                    </tr>
                                    <?php
                                    }
                                    ?>
									
								

							
									<tr>
										<td><b>Total</b></td>
										<td>$<?php 
                                    $pricevalue = array_sum($a);
                                    if($coupontype == 'flatrate')
                                    {
                                        $pricevalue = $pricevalue-$ratevalue;
                                    }
                                    
                                    if($coupontype == 'percentrate')
                                    {
                                        $d = $pricevalue*($ratevalue/100);
                                        $pricevalue = $pricevalue-$d;
                                    }
                                    
                                    echo $pricevalue;?></td>
									</tr>
								</tbody>
							</table>
                            </div>
                            <br>
							<div class="checkout-methods">
								<a href="checkout.php?ccode=<?php if($_GET['ccode']){ echo $_GET['ccode'];}?>" class="btn btn-main flx-align gap-2 pill btn-lg"> Proceed to Checkout
									<i class="fa fa-arrow-right"></i>
								</a>
							</div>
						</div><!-- End .cart-summary -->
					</div>
                </div>
                <?php }else{?>	
                
                <?php } ?>
                    <div class="cart-content__bottom flx-between gap-2">
                        <a href="index.php" class="btn btn-outline-light flx-align gap-2 pill btn-lg"> 
                            <span class="icon line-height-1 font-20"><i class="las la-arrow-left"></i></span> 
                            Continue Shopping
                        </a>
                        <!--<a href="cart-personal.html" class="btn btn-main flx-align gap-2 pill btn-lg"> -->
                        <!--    Next-->
                        <!--    <span class="icon line-height-1 font-20"><i class="las la-arrow-right"></i></span> -->
                        <!--</a>-->
                    </div>
                </div>
            </div>
        </div>
        <!-- ======================= Cart Section End ======================== -->



		

<?php include('footer.php');?>   