<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> -->
<?php  
   if($this->session->userdata('profileid')){
       $userdata = $this->common->getsData('user',array('profileid'=>$this->session->userdata('profileid')));
   }else{
       $userdata=array();
   }
   $bname = (isset($userdata['name']))?$userdata['name']:'';
   $blname = (isset($userdata['last_name']))?$userdata['last_name']:'';
   $bemail = (isset($userdata['email']))?$userdata['email']:'';
   $bmobile_no = (isset($userdata['mobile_no']))?$userdata['mobile_no']:'';
   $baddress1 = (isset($userdata['address']))?$userdata['address']:'';
   $baddress2 = (isset($userdata['address2']))?$userdata['address2']:'';
   $bcity = (isset($userdata['bcity']))?$userdata['bcity']:'0';
   $bcountry = (isset($userdata['country']))?$userdata['country']:'';
   $bstate = (isset($userdata['bstate']))?$userdata['bstate']:'0';
   $bpincode = (isset($userdata['pincode']))?$userdata['pincode']:'';
   $utype = (isset($userdata['user_type']))?$userdata['user_type']:'';
   $dob = (isset($userdata['dob']) && $userdata['dob']!='0000-00-00')?$userdata['dob']:'';
   $points = (isset($userdata['remain_point']))?$userdata['remain_point']:0;
   ?>
<?php $countrylist = $this->config->item('country_list'); ?>
<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 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>CHECKOUT</li>
                    </ul>
                </div>
            </div>
            <div class="col-lg-8 col-md-12 col-12">
                <div class="user-dashboard-title">
                    <h1 class="headingh1 pb-2" style="color: #000 !important">DELIVERY DETAILS</h1>
                </div>
            </div>
        </div>
        <form id="checkout-form" class="user-dashboard-form ps-lg-5 ps-0" action="<?php echo base_url('front/placeOrder'); ?>" method="post">
            <input type="hidden" name="isWallet" id="isWallet" value="0">
            <div class="form-group">
                <input type="hidden" name="bemail" value="<?php echo $bemail; ?>" placeholder="*Email Address" class="form-control">
                <input type="hidden" name="semail" value="<?php echo $bemail; ?>" placeholder="*Email Address" class="form-control">
                <input type="hidden" name="bstate" class="form-control" value="<?php echo $bstate; ?>">
                <input type="hidden" name="bcity" class="form-control" value="<?php echo $bcity; ?>">
                <input type="hidden" name="sstate" class="form-control" value="<?php echo $bstate; ?>">
                <input type="hidden" name="scity" class="form-control" value="<?php echo $bcity; ?>">
            </div>
            <div class="row justify-content-center ">
                <div class="col-lg-8 col-md-7 col-12">                           
                    <div class="row">
                        <div class="col-12">
                            <div class="form-group mb-3 select">
                                <label>COUNTRY/ REGION</label>
                                <select class="form-control" name="bcountry">
                                    <option value="">Select</option>
                                    <?php if(isset($countrylist) && !empty($countrylist)){ foreach($countrylist as $key=>$value): ?>
                                      <option selected <?php echo($bcountry==$value)?'selected':''; ?> value="<?php echo $value; ?>"><?php echo $value; ?></option>
                                    <?php endforeach; } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-6">
                            <div class="form-group mb-3">
                                <label>*NAME</label>
                                <input type="text" name="bname" value="<?php echo $bname; ?>" class="form-control" placeholder="*Name">    
                            </div>
                        </div>
                        <!-- <div class="col-lg-6 col-md-6">
                            <div class="form-group mb-3">
                                <label>*LAST NAME</label> -->
                                <input type="hidden" name="blname" value="<?php echo $blname; ?>" class="form-control" placeholder="*Last Name">      
                            <!-- </div>
                        </div> -->
                        <div class="col-lg-6 col-md-6">
                            <div class="form-group mb-3">
                                <label>*CONTACT NUMBER</label>
                                <input type="text" name="bmobile_no" value="<?php echo $bmobile_no; ?>" class="form-control" placeholder="*Contact Number">
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-6">
                            <div class="form-group mb-3">
                                <label>*ADDRESS</label>
                                <input type="text" name="baddress1" value="<?php echo $baddress1; ?>" class="form-control" placeholder="*Address">    
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-6">
                            <div class="form-group mb-3">
                                <label>*UNIT NUMBER</label>
                                <input type="text" name="baddress2" value="<?php echo $baddress2; ?>" class="form-control" placeholder="*Unit Number">  
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group mb-3">
                                <label>*POSTAL CODE</label>
                                <input type="text" name="bpincode" value="<?php echo($bpincode!='' && $bpincode!=0)?$bpincode:''; ?>" class="form-control" placeholder="*Postal Code">
                            </div>
                        </div>
                        <div class="col-lg-12">
                            <div class="form-group mb-3">         
                                <div class="form_group me-3">
                                  <input type="checkbox" id="ch" class="addressType" checked>
                                  <label for="ch">Same as billing address</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12 mt-4" id="addressType" style="display:none;">
                            <div class="row">
                                <div class="col-lg-12 select-option">
                                    <div class="form-group mb-3">
                                        <label>COUNTRY/ REGION</label>
                                        <select class="form-control" name="scountry">
                                            <option value="">Select</option>
                                            <?php if(isset($countrylist) && !empty($countrylist)){ foreach($countrylist as $key=>$value): ?>
                                              <option <?php echo($bcountry==$value)?'selected':''; ?> value="<?php echo $value; ?>"><?php echo $value; ?></option>
                                            <?php endforeach; } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-lg-6 col-md-6">
                                    <div class="form-group mb-3">
                                        <label>*NAME</label>
                                        <input type="text" name="sname" value="<?php echo $bname; ?>" class="form-control" placeholder="*Name">    
                                    </div>
                                </div>
                                <!-- <div class="col-lg-6 col-md-6">
                                    <div class="form-group mb-3">
                                        <label>*LAST NAME</label> -->
                                        <input type="hidden" name="slname" value="<?php echo $blname; ?>" class="form-control" placeholder="*Last Name">      
                                    <!-- </div>
                                </div> -->
                                <div class="col-lg-6 col-md-6">
                                    <div class="form-group mb-3">
                                        <label>*CONTACT NUMBER</label>
                                        <input type="text" name="smobile_no" value="<?php echo $bmobile_no; ?>" class="form-control" placeholder="*Contact Number">   
                                    </div>
                                </div>
                                <div class="col-lg-6 col-md-6">
                                    <div class="form-group mb-3">
                                        <label>*ADDRESS</label>
                                        <input type="text" name="saddress1" value="<?php echo $baddress1; ?>" class="form-control" placeholder="*Address">    
                                    </div>
                                </div>
                                <div class="col-lg-6 col-md-6">
                                    <div class="form-group mb-3">
                                        <label>*UNIT NUMBER</label>
                                        <input type="text" name="saddress2" value="<?php echo $baddress2; ?>" class="form-control" placeholder="*Unit Number">  
                                    </div>
                                </div>
                                <div class="col-lg-12">
                                    <div class="form-group mb-3">
                                        <label>*POSTAL CODE</label>
                                        <input type="text" name="spincode" value="<?php echo($bpincode!='' && $bpincode!=0)?$bpincode:''; ?>" class="form-control" placeholder="*Postal Code"> 
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4 col-md-5 col-12">
                    <div class="payment-section">
                        <div class="cart-summary">
                            <div>
                                <p class="headingh1 text-left">SUMMARY</p>
                            </div>
                            <div class="table-responsive">
                                <table class="table">
                                    <tbody>
                                        <?php $amount=$pweight=$points=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')));
                                            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;
                                              }
                                            }else{
                                              $price = (($value['price']*$value["qty"]));
                                              $apply = 0;
                                            }
                                            $amount[] = $price;
                                        ?>
                                        <tr>
                                            <td>
                                                <div class="status mb-3 d-flex align-items-center">
                                                   
                                                    <div class="status-img me-4 py-4">
                                                        <img src="<?php echo base_url($image); ?>">
                                                        <!-- <small class="pro-count"><?php //echo $value['qty']; ?></small> -->
                                                    </div>
                                                    <div class="product-name">
                                                        <p class="proname mb-2"><?php echo $value["options"]["product_name"];  ?></p>
                                                        <p class="price">
                                                            <?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>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php endforeach; } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <?php
                            $cart_amount = array_sum($amount);
                            $delivery_charge = $this->session->userdata('delivery_charge');;
                        ?>
                        <div class="cart-total my-4">
                            <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>
                                <?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</label>
                                    </div>
                                    <div class="col-4 p-0">
                                        <p>- $<?php echo number_format($coupon_amount,2); ?></p>
                                    </div>
                                <?php }else{ $coupon_amount =0;} ?>
                                <div class="col-8 p-0">
                                    <label>DELIVERY</label>
                                </div>
                                <div class="col-4 p-0">
                                    <p>$<?php echo number_format($delivery_charge,2); ?></p>
                                </div>
                                <div class="col-8 has-border p-0">
                                    <h6>TOTAL</h6>
                                </div>
                                <div class="col-4 has-border p-0">
                                    <h6 class="text-center">$<?php echo number_format(($cart_amount+$delivery_charge-$coupon_amount),2);?></h6>
                                </div>
                            </div>
                        </div>
                        <div class="discount-coupon mt-4">
                            <p class="mb-2">Redeem Coupon From Points?</p>
                            <div class="form">
                                <input type="text" id="apply_coupon_code" name="coupon_code" placeholder="Enter If Applicable" />
                                <button type="button" id="applyCoupon">APPLY</button>
                            </div>
                        </div>
                        <div class="delivery-method mb-5 mt-4">
                            <p class="cart-title headingh1">PAYMENT TYPE</p>
                            <div class="form-check mt-3">
                                <input class="form-check-input" type="radio" name="payment_method" id="payment_method1" value="paynow" checked>
                                <label class="form-check-label" for="payment_method1">
                                    <img src="<?php echo base_url(); ?>assets/ecommerce/img/credit-card.png" class="me-3" alt="credit card icon"> CREDIT CARD
                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="payment_method" id="payment_method2" value="COD">
                                <label class="form-check-label" for="payment_method2">
                                    <img src="<?php echo base_url(); ?>assets/ecommerce/img/dollar.svg" class="me-3" alt="dollor icon"> CASH ON DELIVERY
                                </label>
                            </div>
                        </div>
                        <div class="payment-btn">
                            <div class="row">
                                <div class="col-12 text-center">
                                    <button type="submit" id="submitbtn" class="btn btn-primary">CHECKOUT</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <form id="couponForm" action="<?php echo base_url('front/applyCoupon'); ?>" method="post">
            <input type="hidden" id="couponCodeInput" name="coupon_code" placeholder="Enter If Applicable" />
        </form>
    </div>
</div>
<script type="text/javascript">
   $('.addressType').on('click',function(){
      var val = ($(this).is(':checked'))?1:0;
      if(val==0){
         $('#addressType').css('display','block');
      }else{
         $('#addressType').css('display','none');
      }
   });
   $('#applyCoupon').on('click', function () {
       var code = $('#apply_coupon_code').val();
       $('#couponCodeInput').val(code);
       $('#couponForm').submit();
   });
</script>
<!-- <style>
    .form-control{color:#000 !important;}
</style> -->