<?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="<?php echo site_url(); ?>">Home</a></li>
      <li>Shopping Cart</li>
    </ul>
  </div>
</div>
<section class="cart_page desktop_view">
  <div class="container">
    <?php
    if (is_array($cart) && !empty($cart)) {
      ?>
      <div class="row">
        <div class="col-xxl-9 col-xl-9 col-lg-9 col-md-8 col-sm-8 col-12">
          <div class="cart_left">
            <h1>Shopping Cart</h1>
            <div class="alert product_added">
              Your shopping cart contains: <?= count($this->cart->contents()); ?> Product(s)
              <button type="button" class="btn-close pull-right" data-bs-dismiss="alert" aria-hidden="true"></button>
            </div>
            <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) {
                  $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="<?= site_url() . $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="<?= site_url() . $url['friendly_url']; ?>"><?= $items['origname']; ?></a>
                        <!--<p>This Yellow long sleeved colourblocked T-shirt from Dillinger is a great way to look stylish as ever.</p>-->
                        <p>
                          <?php
                          if ($items['options']['Color'] != '' && $items['options']['Size'] != '') {
                            ?>
                            <strong>Color</strong>: <?php echo $color_name; ?>, <strong>Size:</strong> <?php echo $size_name; ?>
                            <?php
                          } elseif ($items['options']['Color'] == '' && $items['options']['Size'] != '') {
                            ?>
                            <strong>Size:</strong> <?php echo $size_name; ?>
                            <?php
                          } elseif ($items['options']['Color'] != '' && $items['options']['Size'] == '') {
                            ?>
                            <strong>Color</strong>: <?php echo $color_name; ?>
                            <?php
                          }
                          ?>
                        </p>
                      </div>
                    </td>
                    <td class="text-center" data-label="UNIT PRICE">
                      <div class="pro-price-cart details_price">
                        <div class="pro-home-price">
                          <?php
                          if ($items['discount_price'] > 0) {
                            $percent = (($items['product_price'] - $items['discount_price']) * 100) / $items['product_price'];
                            ?>
                            <span class="product_price">₹ <?php echo number_format($items['discount_price']); ?></span>
                            <span class="product_del">₹ <?php echo number_format($items['product_price']); ?> </span>
                            <span class="discount_value"><?= number_format($percent); ?>% Off</span>
                            <?php
                          } else {
                            ?>
                            <span class="product_price">₹ <?php echo number_format($items['product_price']); ?></span>
                            <?php
                          }
                          ?>
                        </div>
                      </div>
                    </td>
                    <td class="text-center" data-label="QTY">
                      <div class="details_qty">
                        <button type="button" class="minus" onclick="return incDnc(2, <?php echo $i; ?>, <?php echo $items['availableqty']; ?>);"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8z"/></svg></button>
                        <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">

                        <button type="button" class="plus" onclick="return incDnc(1, <?php echo $i; ?>, <?php echo $items['availableqty']; ?>);"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/></svg></button>
                      </div>
                    </td>
                    <td class="text-center" data-label="TOTAL">
                      <div class="pro-price-cart details_price">
                        <div class="pro-home-price">
                          <span class="product_price">₹ <?php echo number_format($pprice * $items['qty']); ?></span>
                        </div>
                      </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" 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>
            <a class="shopping_btn hover_btn" href="listing.php" title="Continue Shopping">Continue Shopping</a>
          </div>
        </div>
        <div class="col-xxl-3 col-xl-3 col-lg-3 col-md-4 col-sm-4 col-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) && !empty($couponss)) {
                ?>
                <span class="coupons_text">Use code <strong><?php echo $couponss['cpn_code']; ?></strong></span>
                <?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><?= number_format($totalAmount); ?></td>
                </tr>
                <?php
                if ($discount_amount > 0) {
                  ?>
                  <tr>
                    <td>Discount Price</td>
                    <td>- <?= number_format($discount_amount); ?></td>
                  </tr>
                  <?php
                }
                ?>
              </tbody>
              <tfoot>
                <tr>
                  <td>Amount Payable</td>
                  <td>
                    <div class="payable"><?= display_price(($totalAmount - $discount_amount)); ?></div>
                  </td>
                </tr>
              </tfoot>
            </table>

            <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>

      </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>    
</section>

<section class="cart-section mobile_view">
  <div class="cart_title">Item(s) <?= count($this->cart->contents()); ?> </div>
  <div class="clearfix"></div>
  <?php
  if (is_array($cart) && !empty($cart)) {
    ?>

    <div class="cart-box">
      <?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'];
        //$percent = $items['options']['GST'];
        //$gst_amount = $amount-($amount*(100/(100+$percent)));
        //$withoutgst = number_format(($amount - $gst_amount),2);
        ?>
        <div class="cart_item">
          <a class="cart-img" href="<?= site_url() . $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 class="cart-content">
            <h4><a href="<?= site_url() . $url['friendly_url']; ?>"><?= $items['origname']; ?></a></h4>
            <ul class="option_2">
              <?php
              if ($items['options']['Color'] != '' && $items['options']['Size'] != '') {
                ?>
                <li>Color: <?php echo $color_name; ?></li>, <li>Size: <?php echo $size_name; ?></li>
                <?php
              } elseif ($items['options']['Color'] == '' && $items['options']['Size'] != '') {
                ?>
                <li>Size: <?php echo $size_name; ?></li>
                <?php
              } elseif ($items['options']['Color'] != '' && $items['options']['Size'] == '') {
                ?>
                <li>Color: <?php echo $color_name; ?></li>
                <?php
              }
              ?>

              <li>Qty: <?php echo $items['qty']; ?></li>
            </ul>
            <div class="price">

              <?php
              if ($items['discount_price'] > 0) {
                $percent = (($items['product_price'] - $items['discount_price']) * 100) / $items['product_price'];
                ?>
                ₹ <?php echo number_format($items['discount_price']); ?>
                <del>₹ <?php echo number_format($items['product_price']); ?></del>
                <span class="discount_value"><?= number_format($percent); ?>% Off</span>
                <?php
              } else {
                ?>
                <span>₹ <?php echo number_format($items['product_price']); ?></span>
                <?php
              }
              ?>
            <!--<p>You Saved ₹ 99!</p>-->
            </div>                
          </div>
        </div>


        <div class="cart-option">
          <h5><a href="<?php echo site_url(); ?>cart/remove_item/<?php echo $items['rowid']; ?>" title="remove">Remove</a></h5>


          <span class="divider-cls">|</span>
          <h5><a href="javascript:void(0);" title="Move to wishlist">Move to wishlist</a></h5>
        </div>
        <div class="divider"></div>

        <?php
        $i++;
      }
      ?>
    </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 class="clearfix"></div>
  <p class="coupon_code_1">Make an online payment & get flat 10% off on your entire purchase. COUPON CODE - PREP10</p>

  <div class="coupon_section">
    <?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">
        <svg class="icon-discount" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M3 2v4.586l7 7L14.586 9l-7-7H3zM2 2a1 1 0 0 1 1-1h4.586a1 1 0 0 1 .707.293l7 7a1 1 0 0 1 0 1.414l-4.586 4.586a1 1 0 0 1-1.414 0l-7-7A1 1 0 0 1 2 6.586V2z"/><path d="M5.5 5a.5.5 0 1 1 0-1 .5.5 0 0 1 0 1zm0 1a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3zM1 7.086a1 1 0 0 0 .293.707L8.75 15.25l-.043.043a1 1 0 0 1-1.414 0l-7-7A1 1 0 0 1 0 7.586V3a1 1 0 0 1 1-1v5.086z"/></svg>
        <input class="form-control form-theme" placeholder="Apply Coupon" name="couponCode" id="couponCode" type="text" required="">
        <button type="button" name="applyCoupon" id="applyCoupon"><svg class="icon-right" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/></svg></button>
      </div>
      <?php
      $couponss = $this->db->query("SELECT * FROM wps_discount_coupans WHERE cpn_id='1'")->row_array();
      if (is_array($couponss) && !empty($couponss)) {
        ?>
        <span class="coupons_text">Use code <strong><?php echo $couponss['cpn_code']; ?></strong></span>
        <?php
      }
    }
    ?>
  </div>

</section>



<section class="price_details">
  <h2 class="title">Price Details(<?= count($this->cart->contents()); ?> Items)</h2>
  <div class="price_detail_1">
    <ul>
      <li><h4>Total MRP(Incl. of taxes) <span><?= number_format($totalAmount); ?></span></h4></li>
      <?php
      if ($discount_amount > 0) {
        ?>
        <li>
          <h4>Discount Price <span><?= number_format($discount_amount); ?></span></h4>
        </li>
        <?php
      }
      ?>

      <!-- <li><h4>Coupon Discount(SAVE100) <a href="javascript:void(0);" class="theme-color">-₹ 100</a></h4></li>
      <li><h4>BZ Discount <span>- ₹ 800</span></h4></li> -->
      <li><h4>Delivery Fee <span>FREE</span></h4></li>
    </ul>
    <div class="total_amount">
      <h4>Total Amount <span> <?= display_price(($totalAmount - $discount_amount)); ?></span></h4>
    </div>

    <div class="saving">Hay! You are saving ₹99 on this order!</div>

  </div>
</section>





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

<div class="cart-bottom">
  <div>
    <div class="left-content">
      <h4><?= display_price(($totalAmount - $discount_amount)); ?></h4>
      <a class="theme-color">View details</a>
    </div>
    <a href="<?php echo base_url('cart/checkout'); ?>" class="morder_btn_m">Place Order</a>
  </div>
</div>


<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>
