<?php $cart_items = $this->session->userdata($session_data); 

$currencySymbol = @getCurrencySymbol();
?>
<?php if(empty($cart_items)){redirect(base_url('membership-cart')); } ?>

<?php $this->load->view('includes/head') ?>
<?php $this->load->view('includes/topbar') ?>
<?php $this->load->view('includes/header') ?>

<main id="main">
    <section  style="background-image: url(<?=base_url('assets/img/elegant-background-with-mandala.png');?>);background-size: auto;">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">

        <span id="cart-amount"  class="allPrice d-none"><?= sprintf("%.2f", $cart_items['service_price']); ?></span>

        <div class="container table-responsive">
            <table id="cart" class="table table-responsive shadow" style="width:100%">
                <thead style="background-color: #70011D; color: #fff;">
                    <tr>
                        <th style="width:10%">Event&nbsp;Name</th>
                        <th style="width:55%"></th>
                        <th style="width:30%" class="text-end">Amount</th>
                        <th style="width:5%" class="text-end"></th>
                    </tr>
                </thead>
                <tbody>
                    <?php 

                    if(@$cart_items['service_id'])
                    {
                        foreach (@$cart_items['service_id'] as $key => $value) { ?>


                            <tr style=" background: white;vertical-align: middle;">

                                <td style="width:10%">
                                    <img src="<?=ApiBaseUrl()['url'].@$cart_items['image']; ?>" alt="<?=$cart_items['service_name']; ?>" height="60px" width="60px"/>
                                </td>
                                <td style="width:55%">
                                    <h4><?=$cart_items['service_name']; ?></h4>

                                </td>

                                <td style="width:15%" class="text-end" data-th="Price"><?= @$currencySymbol['currencySymbol'].' '.price_format($cart_items['service_price'], 2); ?></td>
                                
                                <td style="width:5%" class="actions" data-th="">
                                    <a href="javascript:void(0)" class="text-danger btn-sm delete-row" data-row-id="<?= $cart_items['service_id']; ?>" style="font-size:17px"><i class="fa fa-trash-o"></i></a>
                                </td>
                            </tr>

                        <?php } } ?>
                    </tbody>
                    
                </table>
                <div class="row">
                    <div class="col-md-6">
                        <a href="<?=base_url('all-upcoming-events')?>" class="btn btn-warning"><i class="fa fa-angle-left"></i> Go Back to Events</a>
                    </div>
                    <div class="col-md-6 text-end">
                        <span  id="cart-amount" class="hidden-xs text-center"><strong>Total <?=    @$currencySymbol['currencySymbol'].' '.price_format($cart_items['service_price'], 2 );?></strong>
                        </span>
                        <span><a href="javascript:void(0)" onclick="CartCheckLogin('membership-form')" class="btn btn-success btn-block">Checkout <i class="fa fa-angle-right"></i></a></span>
                    </tr>
                </div>
            </div>
        </div>
    </section>
</main>

<!-- ==========Language Translator Code============= -->
<div class="ldld full" style="    z-index: 999;"></div>
<?php $this->load->view('includes/footer') ?>
<?php $this->load->view('includes/script') ?>
<script type="text/javascript">

    function CartCheckLogin(page){
        if ( $(".allPrice").text() != 0.00) {
            var page_name = page;
            event.preventDefault();
            $.ajax({
                url: "<?=base_url('check-login-status')?>",
                type: "POST",
                dataType: "json",
                success: function(data) 
                {
                    if (data == 0) {
                        openLoginModal(page_name);
                    }else{
                        window.location.href = "<?=base_url('events/checkout')?>";

                    }
                }
            });
        }else{
            swal({
                title: "Your cart is empty!",
                text: "Please add some items to your cart.",
                icon: "warning",
                confirmButtonText: 'OK',
                confirmButtonColor: '#005D4B',
            });
        }
    }
    $(document).on('click', '.delete-row', function(e){
        e.preventDefault();
        var row_id = $(this).data('row-id');
        var selected_date = $(this).data('selected-date');
        var current_row = $(this).closest('tr'); 
        $.ajax({
            url: '<?= base_url('Events/delete_cart_row')?>',
            method: 'POST',
            data: {row_id: row_id, selected_date:selected_date},
            dataType: 'json',
            success: function(response){
                if(response.status == 'success'){
                    current_row.remove();
                    var updated_amount = response.updated_amount;
                    $('#cart-amount').text(updated_amount);
                    location.reload();
                }
            },
            error: function(){
                alert('Error deleting cart row. Please try again.');
            }
        });
    });
</script>