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

$cart = $this->cart->contents();
$totalAmounts = 0;
foreach ($cart as $items) {
  $pprice = ($items['discount_price'] > 0) ? $items['discount_price'] : $items['product_price'];
  $totalAmounts += ($pprice * $items['qty']);
}
//echo "hhhhh<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">
        <div class="container">
            <div class="row">
              <?php if (is_array($cart) && !empty($cart)) {   ?>

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

                    <div class="cart_left">
                        <h1>Shopping Cart</h1>
                        <div class="alert product_added">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Your shopping cart contains: <?=count($this->cart->contents());?> Product</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" style="width:20%">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) {
                                $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['discount_price'] > 0) ? $items['discount_price'] : $items['product_price'];
                                $totalAmount += ($pprice * $items['qty']);
                                $discountAmt += $pprice* $items['qty'];
                                
                                $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'];
                                
                                ?>
                                <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" style="width: 15%;">
                                        <div class="price">
                                            <?php if($items['discount_price'] > 0){
                                              $percent = (($items['product_price'] - $items['discount_price'])*100)/$items['product_price'];  ?>
                                            <div class="cart_off_label"><?=round($percent);?> %off</div>
                                            <span class="old_price"><?=display_price($items['product_price']);?></span>
                                            <?=display_price($items['discount_price']);?>
                                            <?php }else{ ?>
                                            <?=display_price($items['product_price']);?>
                                            <?php } ?>
                                        </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"><i class="fa fa-remove"></i></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(count($couponss)>0){ ?>
                        <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 = delivery_charge(0,$totalAmount); 
                               echo ($deliveryCharge>0)?display_price($deliveryCharge):'Free'; ?></td>
                              </tr>
                              <tr>
                                <td>GST(18%)</td>
                                <td class="free"><?php $gst = gst($totalAmount); 
                               echo display_price($gst); ?></td>
                              </tr>
                               
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td>Amount Payable</td>
                                    <td>
                                        <div class="payable"><?=display_price(($totalAmount-$discount_amount)+$deliveryCharge+$gst);?></div>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                        <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"><i class="fa fa-shield"></i> 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>
