<?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);
?>
<section class="banner_category" style="background-image: url('<?php echo theme_url();?>images/listing-banner.jpg');">
    <div class="container">

        <h1 class="banner_title">Shopping Cart</h1>
    </div>
</section>
<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"><i class="fa fa-remove"></i></button>Your shopping cart contains: <?=count($this->cart->contents());?> Product(s)
                    </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) {
                                // 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['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'];
//                                $percent = $items['options']['GST'];
//                                 $gst_amount = $amount-($amount*(100/(100+$percent)));
//                                $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'];?>"><?=substr($items['origname'],0,65);?>..</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($items['discount_price'] > 0){
                                              $percent = (($items['product_price'] - $items['discount_price'])*100)/$items['product_price'];  ?>
                                        <div class="cart_off_label"><?=round($percent);?> %off</div>

                                        <?=display_price($items['discount_price']);?>
                                        <?php }else{ ?>
                                        <?=display_price($items['product_price']);?>
                                        <?php } ?>
                                        <span class="old_price"><?=display_price($items['product_price']);?></span>
                                    </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 } ?>
                </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):'Excluding'; ?></td>
                            </tr>


                            <tr>
                                <td>GST(18%)</td>
                                <!--
<td class="free"><?php $gst = gst($totalAmount,2); 
                               echo display_price($gst); ?></td>
-->
                                <td>Included</td>
                            </tr>



                        </tbody>
                        <tfoot>
                            <tr>
                                <td>Amount Payable</td>
                                <td>
                                    <div class="payable"><?=display_price(($totalAmount-$discount_amount)+$deliveryCharge);?></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>
                </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>
