<?php
$this->load->view('top');
$discount_amount = $this->session->userdata('discount_amount');

$cart = $this->cart->contents();
$totalAmounts = 0;
foreach ($cart as $items) {
  $pprice = $items['product_price'];
  $totalAmounts += ($pprice * $items['qty']);
}
//echo "<pre>";print_r($cart);
?>
 <div class="page_breadcrumbs">
        <div class="container">
            <ul>
               <li><a href="<?=site_url();?>" title="Home">Home</a>
                </li>
               <li>Shopping Cart</li>
            </ul>
        </div>
    </div>
    <section class="cart_page bg-grey">
        <div class="inner">
            <div class="row">
              <?php if (is_array($cart) && !empty($cart)) {   ?>

                <div class="col-xxl-9 col-xl-9 col-lg-9 col-md-8 col-sm-8 col-12">
                  <?php echo error_message(); echo validation_message(); ?>

                    <div class="cart_left">
                        <h1 class="bg-title">Shopping Cart</h1>
                        <div class="alert product_added">                        
                        Your shopping cart contains: <?=count($this->cart->contents());?> Product
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                        
                            <?php 

                  echo form_open('cart/', 'name="cart_frm" id="cart_frm" '); ?>
                        <table>
                            <thead>
                                <tr>
                                <th>Product</th>
                                <th class="text-left">Unit Price </th>
                                <th class="text-center">QTY</th>
                                <th class="text-center">Total</th>
                                <th class="text-center">Remove</th>
                                </tr>
                            </thead>
                            <tbody>
                              <?php
                                  $totalAmount = $discountAmt = 0;
                                  $i = 1;
                                  $cart = $this->cart->contents();
                              foreach ($cart as $items) {
                               // trace($items);
                                $link = ($this->session->userdata('user_id') > 0) ? 'href="' . site_url() . 'cart/add_to_wishlist/' . $items['pid'] . '"' : 'href="#" data-toggle="modal" data-target="#log-modal"';
                                $pprice =  $items['product_price'];
                                $totalAmount += ($pprice * $items['qty']);
                                $discountAmt += $pprice* $items['qty'];
                                
                                $pcat_id = $this->db->query("SELECT category_id FROM wps_products WHERE products_id = '" . $items['pid'] . "'")->row_array();
                                $url = $this->db->query("SELECT friendly_url FROM wps_products WHERE products_id = '" . $items['pid'] . "'")->row_array();
                               if (!is_array($items['options']['Size'])) {
                                  $size_name = get_db_field_value("wps_sizes", "size_name", "WHERE size_id = '" . $items['options']['Size'] . "'");
                                }
                                if (!is_array($items['options']['Color'])) {
                                  $color_name = get_db_field_value("wps_colors", "color_name", "WHERE color_id = '" . $items['options']['Color'] . "'");
                                }
                                $amount = $pprice * $items['qty'];
                                $percent = $items['options']['GST'];
                                $shipping_charge = $items['options']['Shipping_charges'];
                                 //$gst_amount = $amount-($amount*(100/(100+$percent)));
                                $gst_amount = $amount*$percent / 100;
                                $withoutgst = number_format(($amount - $gst_amount),2);
                                ?>
                                <tr>
                                    <td data-label="PRODUCT">
                                        <div class="cart_image">
                                             <a href="<?=$url['friendly_url'];?>" title="<?=$items['origname'];?>">
                                              <img src="<?php echo get_image('products', $items['img'], '141', '141', 'R', $url['friendly_url']); ?>" alt="<?=$items['origname'];?>" title="<?=$items['origname'];?>">
                                            </a>
                                        </div>
                                        <div class="cart_info">
                                            <a href="<?=$url['friendly_url'];?>"><?=$items['origname'];?></a>
                                            <?php if(isset($items['options']['Size']) && $items['options']['Size']!=''){ ?>
                                                <strong>Size: </strong><?=$size_name;?>
                                                <?php if(isset($color_name) && $color_name!=''){ ?><br/><strong>Color: </strong><?=$color_name;?> <?php } ?>
                                            <?php } ?>
                                        </div>
                                    </td>
                                 
                                    
                                    <td class="text-center" data-label="UNIT PRICE">
                                     <div class="price"><?php if($pcat_id['category_id']==2) { echo display_price($items['product_price']);} else{ echo display_price_india($items['product_price']);}?></div>
                                     </td>
                                    <td class="text-center" data-label="QTY">
                                        <div class="details_qty">
                                            <input type="button" value="-" class="minus" onclick="return incDnc(2, <?php echo $i; ?>, <?php echo $items['availableqty']; ?>);" />
                                            <input type="text" readonly="readonly" name="<?php echo $i; ?>[qty]" id="qty_<?php echo $i; ?>" value="<?php echo $items['qty']; ?>" class="qty qty_value" maxlength="10">
                                            <input type="button" value="+" class="plus" onclick="return incDnc(1, <?php echo $i; ?>, <?php echo $items['availableqty']; ?>);" />
                                            <input type="hidden" name="<?php echo $i; ?>[rowid]" id='cart_rowid_<?php echo $i; ?>' value="<?php echo $items['rowid']; ?>" />
                                        </div>
                                    </td>
                                   
                                    
                                    <td class="text-center" data-label="TOTAL">
                                        <div class="price"><?php echo display_price($pprice * $items['qty']); ?></div>
                                    </td>
                                      <td class="text-center" data-label="REMOVE">
                                    <a class="cart_remove" href="<?php echo site_url(); ?>cart/remove_item/<?php echo $items['rowid']; ?>" title="remove"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x" viewBox="0 0 16 16">  <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/></svg></a>
                                </td>
                                    
                               
                                </tr>
                               <?php $i++; } ?>
                            </tbody>
                        </table>

                      <?php echo form_close(); ?>
                        <a class="shopping_btn" href="<?php echo site_url('products'); ?>" title="Continue Shopping"><i class="fa fa-angle-left"></i> Continue Shopping</a>
                    </div>
                    
                </div>
                <div class="col-lg-3 col-md-3 col-sm-4  col-xs-12">
                    <div class="cart_right_title">Coupons</div>
                    <div class="cart_right">
                      <?php if ($discount_amount > 0) { ?>
                       <div class="coupon_form">
                          <button class="apply_btn" id="removeCoupon" type="button" style="width:100%;">Remove Coupon</button>
                          <br/><br/>
                        </div>
                      <?php }else{ ?>
                        <div class="coupon_form">
                            <input class="form-control" placeholder="Enter coupon code" name="couponCode" id="couponCode" type="text" required="">
                            <button class="apply_btn" type="button" name="applyCoupon" id="applyCoupon">Apply</button>
                        </div>
                       
                       <?php  $couponss = $this->db->query("SELECT * FROM wps_discount_coupans WHERE cpn_id='1'")->row_array(); 
                       if(is_array($couponss)){ ?>
                        <span class="coupons_text">Use code <strong><?php echo $couponss['cpn_code']; ?></strong></span>
                       <?php } ?>
                     <?php } ?>
                    </div>
                    <div class="cart_right_title">Price Details</div>
                    <div class="cart_right">
                        <table class="table table-totals">
                            <tbody>
                              <tr>
                                <td>Sub Total</td>
                                <td><?=display_price($totalAmount);?></td>
                              </tr>
                               <?php if ($discount_amount > 0) { ?>
                              <tr>
                                <td>Discount Price</td>
                                <td>- <?=display_price($discount_amount); ?></td>
                              </tr>
                              <?php } ?>
                              
                              <tr>
                                <td>Shipping Charges</td>
                                
                                <td class="free">
                                    <?php $deliveryCharge = $shipping_charge;
                                    //delivery_charge(0,$totalAmount); 
                               echo ($deliveryCharge>0)?display_price($deliveryCharge):'Free'; ?>
                                   
                               </td>
                              </tr>
<!--
                              <tr>
                                <td>GST </td>

                                <td class="free"><?php $gst = number_format($gst_amount,2); 
                               echo display_price($gst); ?></td>

                                  
                           <td class="free">  <?php echo number_format($gst_amount,2); ?></td>
                              </tr>
-->
                               
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td>Amount Payable</td>
                                    <td>
                                        <div class="payable"><?=display_price(($totalAmount-$discount_amount)+$deliveryCharge);?></div>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                        <p>Includes GST Excludes Shipping & Delivery</p>
                        <input type="hidden" value="<?=$totalAmount;?>" id="amt" name="amt" />
                        <a class="order_btn" href="<?php echo base_url('cart/checkout'); ?>" title="Place Order">Place Order</a>
                        <p class="safe_info"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M5.338 1.59a61.44 61.44 0 0 0-2.837.856.481.481 0 0 0-.328.39c-.554 4.157.726 7.19 2.253 9.188a10.725 10.725 0 0 0 2.287 2.233c.346.244.652.42.893.533.12.057.218.095.293.118a.55.55 0 0 0 .101.025.615.615 0 0 0 .1-.025c.076-.023.174-.061.294-.118.24-.113.547-.29.893-.533a10.726 10.726 0 0 0 2.287-2.233c1.527-1.997 2.807-5.031 2.253-9.188a.48.48 0 0 0-.328-.39c-.651-.213-1.75-.56-2.837-.855C9.552 1.29 8.531 1.067 8 1.067c-.53 0-1.552.223-2.662.524zM5.072.56C6.157.265 7.31 0 8 0s1.843.265 2.928.56c1.11.3 2.229.655 2.887.87a1.54 1.54 0 0 1 1.044 1.262c.596 4.477-.787 7.795-2.465 9.99a11.775 11.775 0 0 1-2.517 2.453 7.159 7.159 0 0 1-1.048.625c-.28.132-.581.24-.829.24s-.548-.108-.829-.24a7.158 7.158 0 0 1-1.048-.625 11.777 11.777 0 0 1-2.517-2.453C1.928 10.487.545 7.169 1.141 2.692A1.54 1.54 0 0 1 2.185 1.43 62.456 62.456 0 0 1 5.072.56z"/><path d="M10.854 5.146a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7.5 7.793l2.646-2.647a.5.5 0 0 1 .708 0z"/></svg> Safe and Secure Payments. Easy returns. 100% Authentic products.</p>
                        </div>
                </div>

             <?php }else{ ?>
                <div class="cb"></div>
                <div class="cart_right">
                  <div class="d-flex justify-content-center">
                    <div class="text-center">
                      <img src="<?php echo theme_url(); ?>images/shopping_cart_empty.jpg" alt="Cart Empty" title="Cart Empty">
                    </div>
                  </div>
                </div>
            <?php } ?>
            </div>
        </div>
    </section>

<?php $this->load->view('bottom'); ?>

<script src="<?php echo site_url(); ?>assets/developer/js/common.js"></script> 
<script type="text/javascript">
    $('#applyCoupon').click(function () {
      if ($('#couponCode').val() == '') {
        alert('please enter valid coupon code');
      } else {
        $.post(site_url + 'cart/applycoupon', {couponcode: $('#couponCode').val(),amt: $('#amt').val()}, function (data) {
          alert(data);
          window.location.reload();
        });
      }
    });
    $('#removeCoupon').click(function () {
      $.post(site_url + 'cart/removecoupon', {}, function (data) {
        alert(data);
        window.location.reload();
      });
    });
</script>
