<link rel="stylesheet" href="<?php echo base_url(); ?>assets/ecommerce/scss/style.css" />
<!-- <script src="<?php //echo base_url(); ?>assets/ecommerce/js/custom.js"></script> -->
<!-- shopping-cart start -->
<div class="user-dashboard mt-4 mb-4">
    <div class="container">
        <div class="row justify-content-center">
          <div class="col-lg-12">
            <div class="alert alert-success alert-dismissible" role="alert" <?php if($this->session->flashdata('message')==''){ ?> style="display:none" <?php } ?> >
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                <?php echo $this->session->flashdata('message'); ?>
            </div>
            <div class="alert alert-danger alert-dismissible" role="alert" <?php if($this->session->flashdata('emessage')==''){ ?> style="display:none" <?php } ?> >
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                <?php echo $this->session->flashdata('emessage'); ?>
            </div>
          </div>
        </div>
        <div class="row justify-content-between mb-4">
            <div class="col-12">
                <div class="breadcrumb">
                    <ul class="d-flex align-items-center list-unstyled mb-4">
                        <li><i class="fa fa-chevron-left"></i></li>
                        <li><a href="<?php echo base_url('products'); ?>">SHOP</a></li>
                        <li>-</li>
                        <li>CART</li>
                    </ul>
                </div>
            </div>
            <div class="col-lg-8 col-md-7 col-12">
                <div class="shopping-cart-box mb-3">
                    <div class="table-responsive">
                        <form id="cartForm" method="post" action="<?php echo base_url("cart/updateCart"); ?>">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>ITEM</th>
                                        <th class="text-center quantity" width="35%">
                                            QUANTITY
                                        </th>
                                        <th class="text-center quantity">TOTAL</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $amount=array();
                                      if($this->cart->contents()){ 
                                        foreach ($this->cart->contents() as $key => $value) : 
                                          $image = $value["options"]["product_photos"];
                                          $price = 0;
                                          // $pcoupon = $this->common->getData('coupon',array('productid'=>$value['id'],'coupon_quantity>='=>1,'coupon_expiry_date>='=>date('Y-m-d')));
                                          $query = 'SELECT coupon_code,productid,coupon_amount,cart_amount,coupon_type FROM coupon WHERE productid regexp "[[:<:]]'.$value['id'].'[[:>:]]" AND coupon_quantity > 0 AND UNIX_TIMESTAMP() < UNIX_TIMESTAMP(coupon_expiry_date)';
                                          $pcoupon = $this->db->query($query)->result_array();
                                          if($this->session->userData('product_coupon')){
                                            $coupon_apply = $this->session->userData('product_coupon');
                                            if(isset($coupon_apply[$value['id']]) && $coupon_apply[$value['id']]){
                                              if($coupon_apply[$value['id']]['coupon_type']==0){
                                                $coupon_amount = $coupon_apply[$value['id']]['coupon_amount'];
                                              }else{
                                                $coupon_amount = ($value['price']*$coupon_apply[$value['id']]['coupon_amount'])/100;
                                              }
                                              $price = (($value['price']*$value["qty"])-$coupon_amount);
                                              $apply = 1;
                                            }else{
                                              $apply = 0;
                                              $price = (($value['price']*$value["qty"]));
                                            }
                                          }else{
                                            $price = (($value['price']*$value["qty"]));
                                            $apply = 0;
                                          }
                                          $amount[] = $price;
                                    ?>
                                    <tr>
                                        <td>
                                            <div class="status d-flex align-items-center">
                                                <div class="btn-image me-3">
                                                    <a data-id="<?php echo $value["rowid"]; ?>" title="Remove item" href="javascript:void(0)" class="deleteproduct">
                                                        <img src="<?php echo base_url(); ?>assets/ecommerce/img/bin-icon.svg" alt="bin icon">
                                                    </a>
                                                </div>
                                                <div class="status-img me-4 py-4">
                                                    <input type="hidden" name="rowid[]" value="<?php echo $value['rowid']; ?>">
                                                    <?php if($image!=''){ ?>
                                                      <img src="<?php echo base_url($image); ?>" alt="img">
                                                    <?php }else{ ?>
                                                      <img src="<?php echo base_url(); ?>assets/ecommerce/img/esg/p1.webp" alt="img">
                                                    <?php } ?>
                                                </div>
                                                <div class="product-name">
                                                    <p class="proname mb-2"><?php echo $value['options']['product_name']; ?></p>
                                                    <!-- <p class="price">SKU: 104238</p> -->
                                                    <?php /*if($apply==1){ ?>
                                                      <!-- <p>Coupon Applied : <?php //echo $coupon_apply[$value['id']]['coupon_code']; ?> <a class="text-danger" href="<?php //echo base_url('front/removeProductCoupon?productid='.$value['id']); ?>" style="opacity:1;">Remove</a></p> -->
                                                    <?php //}else if(isset($pcoupon) && $pcoupon){ ?>
                                                      <!-- <a href="javascript:void(0);" class="addProductCoupon" data-pid="<?php //echo $value['id']; ?>">Check For Available Coupon Codes</a> -->
                                                    <?php //}*/ ?>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="count-list">
                                                <ul class="d-flex justify-content-center align-items-center list-unstyled mb-0 mx-auto">
                                                    <!-- <li><a href="#">-</a></li>
                                                    <li class="count-number">0</li>
                                                    <li><a href="#">+</a></li> -->
                                                    <li style="display: inherit;width:auto;">
                                                        <a class="down me-2" onClick='decreaseCount(event, this)'>-</a>
                                                        <input class="count-number submitQty" type="text" name="qty[]" value="<?php echo $value['qty']; ?>" style="max-width: 50px;width: auto;text-align: center;">
                                                        <a class="up ms-2"  onClick='increaseCount(event, this)'>+</a>
                                                    </li>
                                                </ul>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="d-md-block">
                                                <p class="price text-center m-0">
                                                    <?php if($apply==1){ ?>
                                                        <del class="delprice"> $<?php echo number_format(($value['price']*$value['qty']),2); ?></del><br>
                                                        $<?php echo number_format(($price),2); ?>
                                                    <?php }else{ ?>
                                                        <?php if($value['options']['product_sale_price']>0){ ?>
                                                          <!-- <del class="delprice"> $<?php //echo number_format(($value['options']['product_price']*$value['qty']),2); ?></del><br> -->
                                                          $<?php echo number_format(($price),2); ?>
                                                        <?php }else{ ?>
                                                          $<?php echo number_format(($price),2); ?>
                                                        <?php } ?>
                                                    <?php } ?>
                                                </p>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; }else{ ?>
                                        <tr><td colspan="3"><h3 class="text-center">No Product in Cart!</h3></td></tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </form>
                    </div>
                </div>
                <div class="row justify-content-between align-items-center">
                    <div class="col-lg-12 p-sm-0">
                        <p class="cart-title headingh1 mb-3" style="color: #000 !important">DELIVERY METHOD</p>
                    </div>
                    <div class="col-lg-6 col-md-12 p-sm-0">
                        <ul class="delivery-method list-unstyled m-lg-0">
                            <li class="border-bottom-0 d-flex justify-content-between align-items-center">
                                <div class="form-check">
                                    <input class="form-check-input dtype" type="radio" name="delivery" id="d2" value="1" />
                                    <label class="form-check-label" for="d2">
											Standard <br />
											<span>Standard</span>
										</label>
                                </div>
                                <span class="price">$10.00</span>
                            </li>
                            <li class="d-flex justify-content-between align-items-cente">
                                <div class="form-check">
                                    <input class="form-check-input dtype" type="radio" name="delivery" id="d1" value="0" checked />
                                    <label class="form-check-label" for="d1">
											Self-Pickup <br />
											<span>Free</span>
										</label>
                                </div>
                                <span class="price">$0.00</span>
                            </li>
                        </ul>
                    </div>
                    <div class="col-lg-5 col-md-12 col-12">
                        <div class="discount-coupon mb-3 mb-md-0">
                            <p class="mb-2">Redeem Coupon From Points?</p>
                            <form action="<?php echo base_url('front/applyCoupon'); ?>" method="post">
                                <input type="text" name="coupon_code" placeholder="Enter If Applicable" />
                                <button type="submit">APPLY</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <?php
                $cart_amount = array_sum($amount);
                $delivery_charge=0;
            ?>
            <div class="col-lg-3 col-md-5 col-12">
                <div class="payment-section mt-5">
                    <div class="cart-total mt-2 mb-5">
                        <div class="row m-0">
                            <div class="col-8 p-0">
                                <label>SUBTOTAL</label>
                            </div>
                            <div class="col-4 p-0">
                                <p>$<?php echo number_format($cart_amount,2); ?></p>
                            </div>
                            <div class="col-8 p-0">
                                <label>DELIVERY</label>
                            </div>
                            <div class="col-4 p-0">
                                <p>$<span id="dcharge"><?php echo number_format($delivery_charge,2); ?></span></p>
                            </div>
                            <?php if($this->session->userdata('user_coupon')){ 
                                if($this->session->userdata('user_coupon_type')==0){
                                    $coupon_amount = $this->session->userdata('user_coupon');
                                }else{
                                    $coupon_amount = (($cart_amount)*$this->session->userdata('user_coupon'))/100;
                                    //echo $coupon_amount;
                                }
                            ?>
                             <div class=" col-8 p-0">
                                <label>COUPON DISCOUNT <br>(<?php echo $this->session->userdata('user_coupon_code'); ?> <a href="<?php echo base_url('cart/removeCoupon'); ?>" class="text-danger ms-2"><i class="fa fa-trash"></i></a>)</label>
                             </div>
                             <div class="col-4 p-0">
                                <p> - $<?php echo $coupon_amount; ?></p>
                             </div>
                            <?php }else{ $coupon_amount =0;} ?>
                            <div class="col-8 has-border mt-4 p-0">
                                <label><strong>TOTAL</strong></label>
                            </div>
                            <div class="col-4 has-border mt-4 p-0">
                                <p>$<span id="gt" data-total="<?php echo ($cart_amount+$delivery_charge-$coupon_amount); ?>"><?php echo number_format(($cart_amount+$delivery_charge-$coupon_amount),2);?></span></p>
                            </div>
                        </div>
                    </div>
                    <div class="payment-btn">
                        <div class="row">
                            <div class="col-12 text-center">
                                <a id="checkoutbtn" href="<?php echo base_url("checkout?dt=0"); ?>" class="btn btn-primary">CHECKOUT</a
									>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- shopping-cart end -->
<script>
    $('.submitQty').on('change', function () {
        $('#cartForm').submit();
    });
    $('body').on('click','.qty-count', function () {
        $('#cartForm').submit();
    });
    $('.dtype').on('click', function () {
        var val = $(this).val();
        var gt = parseFloat($('#gt').attr('data-total'));
        if(val==0){
            $('#dcharge').text('0.00');
            $('#gt').text(gt.toFixed(2));
            $('#checkoutbtn').attr('href','<?php echo base_url("checkout?dt=0"); ?>');
        }else{
            var amt = (gt+10);
            $('#dcharge').text('10.00');
            $('#gt').text(amt.toFixed(2));
            $('#checkoutbtn').attr('href','<?php echo base_url("checkout?dt=1"); ?>');
        }
    });
</script>
<script type="text/javascript">
      function increaseCount(a, b) {
        var input = b.previousElementSibling;
        var value = parseInt(input.value, 10); 
        value = isNaN(value)? 0 : value;
        value ++;
        input.value = value;
        $('#cartForm').submit();
      }
      function decreaseCount(a, b) {
        var input = b.nextElementSibling;
        var value = parseInt(input.value, 10); 
        if (value > 1) {
          value = isNaN(value)? 0 : value;
          value --;
          input.value = value;
          $('#cartForm').submit();
        }
      }
</script>