<?php include_once "template/common.php" ?>

<style type="text/css">
    .hide {
        display: none;
    }
    .alert .alert-success {
        margin-bottom: 20px;
    }
    .rounded-fill {
        border-radius: 10px !important;
    }
    @media (min-width: 500px) {
        .alert {
            margin-top: 10px;
        }
    }
</style>

<div class="body-content">
    <div class="top-area">
        <div class="search-box d-flex flex-wrap align-items-center d-none d-lg-block">
            <h3 class="title bg-white d-inline-block profile-title rounded">Cart</h3>
        </div>
    </div>

     <?php $this->load->view('template/message.php');?>
   <?php 
            if(count($cart_list) > 0)
            {
                ?>
    <div class="mt-4">
        <div class="mobile-table-wrapper">
         
                <form method="post" action="" id="FormSubmit">
                 
                   <input type="hidden" name="edit_id" id="edit_id">
                   <input type="hidden" name="del_btn" value="ok">
                   
                <?php
                $total_val = 0;
                $pro_id    = [];
                foreach($cart_list as $key => $val)
                {
                    $pro_id[] = $val['id'];
                ?>
                    <div class="mobile-item d-flex flex-wrap justify-content-between" style="background: white;">
                        <div class="content-wrapper d-flex flex-wrap align-items-center">
                            <div class="thumb">
                                <img style="width: 80px;" src="http://d38x82mkkml5o1.cloudfront.net/<?php echo $val['comp_photo_url'];?>" alt="thumb">
                            </div>
                            <div class="content">
                                <h6 class="title fs--16px text--accent"><?=$val['merchandise_title']?></h6>
                                <p  id="row_total_price<?php echo $val['id']; ?>"  class="fs--14px fw-bold d-block mb-0 text--base item_price"> 
                                        <?php  echo $val['symbol'];?>

                                       <?php
                                        if($val['discount'] != 0)
                                        {
                                            $discount_price = $val['price'] * $val['discount'] / 100;
                                            $price          = ($val['price'] - $discount_price) * $val['amount'];

                                            $total_val = $total_val + $price;
                                        }
                                        else
                                        {
                                            $price = $val['price'] * $val['amount'];
                                            $total_val =  $total_val + $price;
                                        }
                                        echo $price . '<span iten-price="' . number_format($price, 2) . '"></span>';
                                        ?>

                                   
                                   
                                </p>
                            </div>
                        </div>
                        <div class="content-right">
                            <div class="cart-right cart-control-bg">
                                <div class="d-flex align-items-center justify-content-center" >
                                    <!-- <span class="minus-plus cart-minus"  onclick="get_total_price('<?php if($val['discount'] != 0){ $discount_price = $val['price'] * $val['discount'] / 100; echo ($val['price'] - $discount_price); }else{ echo $val['price']; }?>', '<?php echo $val['symbol'];?>', '<?php echo $val['id'];?>');" ><i class="fas fa-minus"></i></span> -->

                                    <input type="text" value="<?php echo $val['amount'];?>" style="background-color: transparent;" name="demo_vertical" id="merchandise_count<?php echo $val['id'];?>" class="product-number text-center mx-2">
                                    
                                    <!-- <span class="minus-plus cart-plus" onclick="get_total_price('<?php if($val['discount'] != 0){ $discount_price = $val['price'] * $val['discount'] / 100; echo ($val['price'] - $discount_price); }else{ echo $val['price']; }?>', '<?php echo $val['symbol'];?>', '<?php echo $val['id'];?>');" ><i class="fas fa-plus"></i></span> -->
                                </div>
                            </div>
                        </div>
                        <div class="delete-thumb">
                            <img onclick="del_func('<?php echo $val['id'];?>');" src="<?php echo base_url();?>files/assets/images/icon/delete.svg" alt="icon">
                        </div>
                    </div>
            <?php }
            ?>
        </form>
            

    
        </div>

        <input type="hidden" id="hdndiscount" name="hdndiscount" value="0.00"/>
        <input type="hidden" id="hdndiscountType" name="hdndiscountType" value=""/>
        <input type="hidden" id="hdntotalDiscount" name="hdntotalDiscount" value="0.00"/>
        <input type="hidden" id="hdntotal_val" name="hdntotal_val" value="0.00"/>

        <div class="cart-bottom">
            <div class="row justify-content-between gy-5">
                <div class="col-md-6 col-xl-4">
                    <div class="voucher-form">
                        <div class="from-group">
                              <input type="text" class="form-control form--control" name="discountcodes_txt" id="discountcodes_txt" placeholder="Enter Coupon Code">
                        </div>
                        <a class="btn cmn--btn" style="color: white !important;" onclick="applyDiscountCodes('<?php echo $cart_list[0]['symbol'] ?>')">Apply</a> 
                    </div>
                </div>
                <div class="col-md-6 col-xl-4" style="background: white; padding: 20px; border-radius: 10px;">
                    <div class="checkout-box">
                        <ul class="checkout-list">
                            <li class="d-flex flex-wrap justify-content-between">
                                <span style="color: #9098B1;">Discount</span>
                                <span id="tdTotalDiscount">
                                     <?php echo $cart_list[0]['symbol']; ?>
                                    <span id="spanTotalDiscount">0.00</span>
                                </span>
                            </li>
                            <li class="d-flex flex-wrap justify-content-between py-3">
                                <span>Total Price </span>
                                <span class="right-text" id="total_val"><?php if(count($cart_list) > 0){echo $cart_list[0]['symbol']." ".number_format($total_val, 2);}else{echo 0;}?></span>

                                <input type="hidden" name="main_total_val" id="main_total_val" value="<?=number_format($total_val, 2)?>">

                            </li>
                        </ul>
                        <a href="javascript:;" class="cmn--btn d-block text-center theme-button" <?php if(count($cart_list) == 0){?>disabled<?php }?> style="border-radius: 7px; color: white !important;" onclick="checkout_func();">Pay</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php
            }
            else
            {
                ?>
                    <div style="margin-top: 50px; color: #444;">Please proceed to the <a href="<?=base_url()?>merchandise" style="color: #BEDD5C;">Shop</a> to add items to your cart.</div>
                <?php
            }
            ?>
</div>




<!-- <div class="modal fade qrviewModal qrviewModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
 <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
       <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Details</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
       </div>
       <div class="modal-body">
          <img src="" id="qrImg" style="width: 100%;">
       </div>
       <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       </div>
    </div>
 </div>
</div>
         -->





<div class="modal fade checkOutModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content bg-transparent border-0">
            <div class="modal-body p-0">
                <div class="popup-wrapper share-modal detail-padding-wrapper" style="overflow-y: auto; height: 100%;padding-top: 25px;padding-bottom: 25px;">

                        <div class="card detail-wrapper" style="border: none;">

                            <div class="card-body">
                                <h5 class="checkout-detail-title pt-1" style="margin-bottom: 8px;" >Checkout Detail</h5>
                                <div>
                                    <div class="custom-control custom-radio custom-control-inline mr-4">
                                        <input type="radio" id="customRadioInline1" name="isDelivery" value="0" class="custom-control-input" checked onchange="sel_delivery_func(0)">
                                        <label class="custom-control-label" for="customRadioInline1" style="margin-left: 5px;">
                                            <i style="margin-top: 6px;" class="fas fa-lock font-size-20 align-top"></i> Pick Up
                                        </label>
                                    </div>
                                    <?php
                                    
                                    if($site_data['shipping'] == 'Yes')
                                    {
                                    ?>
                                    <div class="custom-control custom-radio custom-control-inline mr-4">
                                        <input type="radio" id="customRadioInline3" name="isDelivery" value="1" class="custom-control-input" onchange="sel_delivery_func(1)">
                                        <label class="custom-control-label" for="customRadioInline3" style="margin-left: 5px;">
                                            <i style="margin-top: 6px;"  class="fas fa-shipping-fast font-size-20 align-top"></i> Delivery
                                        </label>
                                    </div>
                                    <?php
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>



                    <div class="card detail-wrapper" id="shipping_div" style="margin-top: 25px; display: none;">
                                    <div class="card-body">
                                        <div class="tab-content" id="v-pills-tabContent">
                                            <div class="tab-pane fade show active" id="v-pills-shipping" role="tabpanel" aria-labelledby="v-pills-shipping-tab">
                                                <div>
                                                    <h5 style="margin-bottom: 20px;" class="checkout-detail-title pt-1">Shipping information</h5>
                                                    <div class="form-group row mb-4">
                                                        <label for="billing-name" class="col-md-4 col-form-label">Name</label>
                                                        <div class="col-md-8">
                                                            <input type="text" class="form-control" id="name" placeholder="Enter your name">
                                                        </div>
                                                    </div>
                                                    <div class="form-group row mb-4">
                                                        <label for="billing-phone" class="col-md-4 col-form-label">Phone</label>
                                                        <div class="col-md-8">
                                                            <input type="text" class="form-control" id="phone" placeholder="Enter your Phone no.">
                                                        </div>
                                                    </div>
                                                    <div class="form-group row mb-4">
                                                        <label for="billing-address" class="col-md-4 col-form-label">Address</label>
                                                        <div class="col-md-8">
                                                            <textarea class="form-control" id="address" rows="3" placeholder="Enter full address"></textarea>
                                                        </div>
                                                    </div>
                                                    <div class="form-group row mb-4">
                                                        <label class="col-md-4 col-form-label">Country</label>
                                                        <div class="col-md-8" style="padding-top: 8px;">
                                                            <span class="contry-name"><?php echo $country_data['title'];?></span>
                                                        </div>
                                                        <input type="hidden" name="country" id="country" value="<?php echo $country_data['id'];?>">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                    <div class="detail-wrapper container mt-4">
                        <h5 class="checkout-detail-title pt-1">Summary</h5>

                        <ul class="checkout-list">

                            <li id="checkout_subtotal_tr" class="d-flex flex-wrap justify-content-between" style="display: none !important; ">
                                <span class="subtotal">Sub Total</span>
                                <span id="checkout_subtotal_val">0.00</span>
                            </li>
                            <li id="shipping_fee_tr" class="d-flex flex-wrap justify-content-between" style="display: none !important;">
                                <span class="subtotal">Shipping Fee</span>
                                <span id="shipping_fee_val">0.00</span>
                            </li>
                            <li class="d-flex flex-wrap justify-content-between">
                                <span class="subtotal">Discount</span>
                                <span id="spanTotalDiscountSum">0.00</span>
                            </li>
                            <hr class="hr-m0">
                            <li class="d-flex flex-wrap justify-content-between">
                                <span class="coupon">Total</span>
                                <span id="checkout_total_val">0.00</span>
                            </li>
                        </ul>

                    </div>

                    <input type="hidden" name="site_id" id="site_id" value="<?php if(count($cart_list) > 0)
                    {
                        echo $cart_list[0]['site_id'];
                    }
                    else
                    {
                        echo '0';
                    }
                    $pro_ids = implode(',', $pro_id);
                    ?>">


                    <input type="hidden" name="total_price" id="total_price" value="">
                    <input type="hidden" name="pro_id" id="pro_id" value="<?php echo $pro_ids ?>">



                    <a href="javascript:void(0)" style="margin-top: 25px !important; color: white !important;" onclick="checkoutfunc(<?php echo $cart_list[0]['currency_rate'];?>,<?php echo $cart_list[0]['id'];?>)"class="cmn--btn mt-3 d-block text-center padding-y-sm theme-button" data-bs-target="#popup-address" data-bs-toggle="modal">Checkout</a>

                    <!-- <a href="javascript:void(0)"  onclick="payfunc(<?php echo $cart_list[0]['currency_rate'];?>)" class="cmn--btn mb-2 mt-2 d-block text-center btn--outline-light padding-y-sm" >Pay at Retail</a> -->
                </div>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="popup-wrapper-checkout" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content bg-transparent border-0">
            <div class="modal-body p-0">
                <div class="popup-wrapper share-modal payment-padding-wrapper" style="overflow-y: auto; height: 100%;">
                    <div class="payment-wrapper">


                    <form role="form" action="<?php echo base_url('CartController/purchase'); ?>" method="post" class="form-validation" data-cc-on-file="false" data-stripe-publishable-key="<?php echo $this->config->item('stripe_key') ?>" id="payment-form">


                                <div class='form-row row'>
                                    <div class='col-md-12 error form-group hide' style="margin-top: 10px;">
                                        <div class='alert-danger alert' style="color: #fff;background: #B22126;border: #B22126;text-align: center;">Error occured.</div>
                                    </div>
                                </div>

                        <div class="row gx-3 gy-2 gy-sm-3">

                            <div class="col-12">
                                <div class="form-group position-relative">
                                    <label for="" class="form-label">Name on Card</label>
                                    <input type="text" class="form-control form--control rounded-fill" size='4' placeholder="Name on Card">
                                </div>
                            </div>


                            <div class="col-12">
                                <div class="form-group position-relative">
                                    <label for="" class="form-label">Card Number</label>
                                    <input type="text" class="form-control form--control card-number rounded-fill" placeholder="1234 1234 1234 1234">
                                    <img src="<?php echo base_url();?>files/assets/images/icon/payment-list.png" alt="icon" class="payment-list">
                                </div>
                            </div>



                            <div class="col-12">
                                <div class="form-group">
                                    <label for="" class="form-label">CVC</label>
                                    <input type="text" class="form-control form--control card-cvc rounded-fill" placeholder="ex. 311" size="4">
                                </div>
                            </div>

                            <div class="col-6">
                                <div class="form-group">
                                    <label for="" class="form-label">Expiration Month </label>
                                    <input type="text" class="form-control form--control card-expiry-month rounded-fill" placeholder="MM">
                                </div>
                            </div>

                            <div class="col-6">
                                <div class="form-group">
                                    <label for="" class="form-label">Expiration Year </label>
                                    <input type="text" class="form-control form--control card-expiry-year rounded-fill" placeholder="YYYY">
                                </div>
                            </div>




                           

                            <input class='getid' name="id" value="<?php echo $_GET['id'] ?>" type='hidden'>
                            <input class='getaddress' name="address" value="<?php echo $_GET['address'] ?>" type='hidden'>
                            <input class='getisDelivery' name="isDelivery" value="<?php echo $_GET['isDelivery'] ?>" type='hidden'>
                            <input class='getname' name="name" value="<?php echo $_GET['name'] ?>" type='hidden'>
                            <input class='getphone' name="phone" value="<?php echo $_GET['phone'] ?>" type='hidden'>
                            <input class='getcountry' name="country" value="<?php echo $_GET['country'] ?>" type='hidden'>
                            <input class='gettotal_price' name="total_price" value="<?php echo $_GET['total_price'] ?>" type='hidden'>
                            <input class='getsite_id' name="site_id" value="<?php echo $_GET['site_id'] ?>" type='hidden'>
                            <input class='getprice' name="price" value="<?php echo $_GET['price'] ?>" type='hidden'>

                            <input name="getpackage_id" value="<?php echo $_GET['package_id'] ?>" type='hidden'>
                            <input name="getguest_id" value="<?php echo $_GET['guest_id'] ?>" type='hidden'>
                            
                            <input type="hidden" name="discount_code_qr_id" id="discount_code_qr_id" value="">
                            <input type="hidden" name="discount_code_qr" id="discount_code_qr" value="">

                            <div class="col-sm-6 order-last">
                                <button class="cmn--btn active px-2 d-block w-100" data-bs-dismiss="modal">Cancel</button>
                            </div>
                            <div class="col-sm-6 ">
                                <button class="cmn--btn px-2 d-block w-100 btn-effect theme-button" type="submit" style="color: white !important; border: none;">Pay</button>
                            </div>
                        </div>
                     </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>





<script src="<?php echo base_url();?>files/assets/js/lib/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>    

    <script type="text/javascript">
            var total_val = '<?php echo $total_val ;?>';
            var currency = '<?php echo $cart_list[0]["symbol"];?>';
            var shipping_fee = '<?php echo number_format($country_data['tax'], 2);?>';
            
            document.getElementById("hdntotal_val").value = total_val;
               
            $(document).ready(function () {
                $("#spanDiscountMessage").hide();
            });
        
            function get_total_price(price, symbol, id)
            {
                 setTimeout(function(){

                    var merchandise_count = $("#merchandise_count"+id).val();
                    console.log(merchandise_count);
                    if(merchandise_count == 0)
                    {
                        $("#merchandise_count"+id).val('1');
                        alert('Not Valid Quantity');
                        return false
                    }
                    
                    $.ajax({
                        method: "POST",
                        url: "<?php echo base_url();?>CartController/update_amount_func",
                        data: {
                            id: id, 
                            amount: merchandise_count
                        }
                    })
                    .done(function(msg) {
                        var json_obj = JSON.parse(msg);
                        var merchandise_count = $("#merchandise_count" + id).val();
                        
                        var row_total_price = parseFloat(merchandise_count * price).toFixed(2);
                        // $("#row_total_price" + id).html(symbol + row_total_price);
                        
                        
                        var add_span = '<span iten-price="'+row_total_price+'"></span>';
                        $("#row_total_price"+id).html(symbol + row_total_price + add_span);

                        total_val = parseFloat(parseFloat(total_val) + parseFloat(price)).toFixed(2);
                        currency = symbol;
                        
                        
                        var total_price = 0;
                       
                        $('.item_price span').each(function (index) {
                            var price = Number($(this).attr('iten-price'));
                            total_price = total_price + parseFloat(price);
                            //total_price = parseFloat(parseFloat(total_price) + parseFloat(price)).toFixed(2);
                        });
                        
                        var hdndiscountType = $("#hdndiscountType").val();
                        var totalDiscount = 0;
                        if (hdndiscountType == "C") 
                        {
                            var totalDiscount = parseFloat(document.getElementById("hdntotalDiscount").value);
                            document.getElementById("hdntotalDiscount").value = totalDiscount;
                        }
                        else
                        {
                            var disPercentage = parseFloat(document.getElementById("hdndiscount").value).toFixed(2);
                            // totalDiscount = disPercentage;

                            totalDiscount = parseFloat((total_price * disPercentage) / 100).toFixed(2);
                            $("#spanTotalDiscount").html(totalDiscount);
                            document.getElementById("hdntotalDiscount").value = totalDiscount;
                            
                        }
                        
                        var final_price = total_price - totalDiscount;
                        console.log('totalDiscount = ' +totalDiscount);
                        console.log('final_price ='+ final_price);
                        
                        $("#total_val").html(currency +' '+ final_price.toFixed(2));
                        $("#total_price").val(final_price.toFixed(2));

                        $("#hdntotal_val").val(final_price.toFixed(2));


                        $("#checkout_total_val").html(currency+' '+final_price.toFixed(2));
                        
                    });


                },500);

            }
            
            function get_album_func(album_id){
                $.ajax({
                    method: "POST",
                    url: "<?php echo base_url();?>GalleryController/cart_album_func",
                    data: {
                        album_id: album_id
                    }
                })
                .done(function(msg) {
                    var json_obj = JSON.parse(msg);

                    var shtml = '';
                    
                    if(json_obj.length > 0){
                        for (let i = 0; i < json_obj.length; i++) {
                            shtml += ' <div class="col-md-6 mt-3 mb-3">';
                            shtml += '     <img src="http://d38x82mkkml5o1.cloudfront.net/'+json_obj[i].process_comp_file_url+'">';
                            shtml += ' </div> ';
                        }                     
                    }
            
                    $("#album").html(shtml);
                });
                
                $(".bs-example-modal-lg").modal('show');
            }
            
            function del_func(id){
                if(confirm("Are you sure?")){
                    $("#edit_id").val(id);
                    $('#FormSubmit').submit();
                }
            }
            
            function checkout_func(){
                var total_val = $("#total_val").html();
                var myString = total_val.replace(currency,'');
                $("#total_price").val(myString);
                $("#checkout_total_val").html(total_val);
                $(".checkOutModal").modal('show');

                SiteDataStore('Pay', 'Checkout');
            }
            
            function sel_delivery_func(id){
                if(id == 0){
                    $("#total_price").val(total_val);
                    $("#checkout_total_val").html(currency+' '+(parseFloat(total_val)).toFixed(2));
                    $("#checkout_subtotal_tr").hide();
                    $("#shipping_fee_tr").hide();
                    $("#shipping_div").hide();
                }else if(id == 1){
                    $("#total_price").val((parseFloat(total_val) + parseFloat(shipping_fee)).toFixed(2));
                    $("#checkout_subtotal_val").html(currency+' '+total_val);
                    $("#checkout_total_val").html(currency+' '+(parseFloat(total_val) + parseFloat(shipping_fee)).toFixed(2));
                    $("#shipping_fee_val").html(currency+' '+parseFloat(shipping_fee).toFixed(2));
                    $("#checkout_subtotal_tr").show();
                    $("#shipping_fee_tr").show();
                    $("#shipping_div").show();
                }
            }
            
            // function payfunc(currency_rate){
            //     $.ajax({
            //         method: "POST",
            //         url: "<?php echo base_url();?>CartController/payfunc",
            //         data: {
            //             isDelivery: $("input[name='isDelivery']:checked").val(),
            //             total_price: parseFloat($("#total_price").val()).toFixed(2),
            //             total_price_usd: parseFloat($("#total_price").val() * currency_rate).toFixed(2),
            //             site_id: $("#site_id").val(),
            //             name: $("#name").val(),
            //             phone: $("#phone").val(),
            //             address: $("#address").val(),
            //             country: $("#country").val()
            //         }
                    
            //     })
            //     .done(function(res) {
            //         var json_obj = JSON.parse(res);
            //         if(json_obj.status == 200){
            //             alert('Your order has been successfully!');
            //         }
                    
            //         $(".checkOutModal").modal('hide');
                    
            //         setTimeout(function(){
            //             window.location.reload();
            //         }, 1500);
            //     });
            // }
            
            function checkoutfunc(currency_rate,id){

                SiteDataStore('Checkout', 'Checkout');


                var isDelivery = $("input[name='isDelivery']:checked").val();
                var total_price = parseFloat($("#total_price").val()).toFixed(2);
                var pro_id = $("#pro_id").val();
                var price = parseFloat($("#total_price").val() * currency_rate).toFixed(2);
                var site_id = $("#site_id").val();
                var name = $("#name").val();
                var phone = $("#phone").val();
                var address = $("#address").val();
                var country = $("#country").val();

                $('.getid').val(pro_id);
                $('.getaddress').val(address);
                $('.getisDelivery').val(isDelivery);
                $('.getname').val(name);
                $('.getphone').val(phone);
                $('.getcountry').val(country);
                $('.gettotal_price').val(total_price);
                $('.getsite_id').val(site_id);
                $('.getprice').val(price);
                $('.getpackage_id').val('');
                $('.getguest_id').val('');

                // $('#getPriceHTML').html(total_price);
                
                $('#popup-wrapper-checkout').modal('show');

                // document.location.href = "<?php echo base_url();?>stripe-payment?id=" + pro_id + "&isDelivery=" + isDelivery + "&price=" + price + "&total_price=" + total_price + "&site_id=" + site_id + "&name=" + name + "&phone=" + phone + "&address=" + address + "&country=" + country;
            }
            
            function applyDiscountCodes(symbol) {
            if (document.getElementById("discountcodes_txt") != null) {
                var aa = $('#total_val').html();
                text = aa.replace(currency, ' ');
                ab = text.trim();
             
                var discountcode = document.getElementById("discountcodes_txt").value;
                if (discountcode != "") {
                    $.ajax({
                        method: "POST",
                        url: "<?php echo base_url();?>CartController/get_discount",
                        data: {
                            discountcode: discountcode
                        }
                    })
                        .done(function (msg) {
                            var json_obj = JSON.parse(msg);
                            if (json_obj != null) {
                                $("#spanDiscountMessage").hide();
                                $("#hdndiscount").val(json_obj.discount);
                                $("#hdndiscountType").val(json_obj.discountType);
                                
                                var totalDiscount;
                                var hdntotal_val = document.getElementById("hdntotal_val").value;

                                if(json_obj.discount_code_qr_id)
                                {
                                     $("#discount_code_qr_id").val(json_obj.discount_code_qr_id);
                                     $("#discount_code_qr").val(discountcode);
                                     
                                }
                                else
                                {
                                     $("#discount_code_qr_id").val('');
                                     $("#discount_code_qr").val('');
                                }

                                if (json_obj.discountType == "C") {
                                    totalDiscount = parseFloat(json_obj.discount).toFixed(2);
                                    document.getElementById("spanTotalDiscount").textContent = totalDiscount;
                                    document.getElementById("spanTotalDiscountSum").textContent = totalDiscount;
                                    document.getElementById("hdntotalDiscount").value = totalDiscount;
                                } else {
                                    var disPercentage = parseFloat(document.getElementById("hdndiscount").value).toFixed(2);
                                    totalDiscount = disPercentage;

                                    totalDiscount = parseFloat((hdntotal_val * disPercentage) / 100).toFixed(2);

                                    document.getElementById("spanTotalDiscount").textContent = totalDiscount;
                                    document.getElementById("spanTotalDiscountSum").textContent = totalDiscount;
                                    document.getElementById("hdntotalDiscount").value = totalDiscount;
                                }
                                hdntotal_val = parseFloat(hdntotal_val - totalDiscount).toFixed(2);
                                
                                if(parseFloat(ab) < parseFloat(totalDiscount))
                                {
                                    $("#discount_code_qr_id").val('');
                                    $("#discount_code_qr").val('');
                                    // $("#spanDiscountMessage").show();
                                    alert('Cart value is not sufficient.');
                                    return false
                                }
                                
                                $("#total_val").html(symbol + hdntotal_val);
                            } else {
                                alert('Discount Code is not valid');
                                var main_total_val = $('#main_total_val').val();

                                $("#total_val").html(symbol +' '+ main_total_val);
                                $("#discount_code_qr_id").val('');
                                $("#discount_code_qr").val('');
                                

                                document.getElementById("spanTotalDiscount").textContent = '0.00';
                                document.getElementById("spanTotalDiscountSum").textContent = '0.00';
                                document.getElementById("hdntotalDiscount").value = '0.00';

                                // $("#spanDiscountMessage").show();
                            }
                        });
                } else {
                    // var hdntotal_val = document.getElementById("hdntotal_val").value;
                    // $("#total_val").html(symbol + hdntotal_val);
                    // $("#tdTotalDiscount").html(symbol + " 0.00");
                    // document.getElementById("hdntotalDiscount").value = "0.00";
                    alert("Please Enter Discount Code !");
                }
            }
        }


        function SiteDataStore(type, desc) 
        {
            $.ajax({
                    url : '<?=base_url()?>SiteDataStoreController/SiteDataStore',
                    type : 'POST',
                    data : {
                        id: '',
                        type : type,
                        desc : desc,
                    },
                    success : function(msg){
                      
                    }
                });

        }

        </script>



<script type="text/javascript">
    $(function () {
        var $stripePg = $(".form-validation");
        $('form.form-validation').bind('submit', function (e) {
            var $stripePg = $(".form-validation"),
                inputSelector = ['input[type=email]', 'input[type=password]',
                    'input[type=text]', 'input[type=file]',
                    'textarea'
                ].join(', '),
                $inputs = $stripePg.find('.required').find(inputSelector),
                $errorMessage = $stripePg.find('div.error'),
                valid = true;
            $errorMessage.addClass('hide');

            $('.has-error').removeClass('has-error');
            $inputs.each(function (i, el) {
                var $input = $(el);
                if ($input.val() === '') {
                    $input.parent().addClass('has-error');
                    $errorMessage.removeClass('hide');
                    e.preventDefault();
                }
            });

            if (!$stripePg.data('cc-on-file')) {
                e.preventDefault();
                Stripe.setPublishableKey($stripePg.data('stripe-publishable-key'));
                Stripe.createToken({
                    number: $('.card-number').val(),
                    cvc: $('.card-cvc').val(),
                    exp_month: $('.card-expiry-month').val(),
                    exp_year: $('.card-expiry-year').val()
                }, handleStripePayment);
            }

        });

        function handleStripePayment(status, res) {
            if (res.error) {
                $('.error')
                    .removeClass('hide')
                    .find('.alert')
                    .text(res.error.message);
            } else {
                var token = res['id'];
                $stripePg.find('input[type=text]').empty();
                $stripePg.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
                $stripePg.get(0).submit();
            }
        }



 
    });
</script>