<?php echo $this->extend('template/admin/main') ?>

<?php echo $this->section('content') ?>

    <?php echo $this->include('common/message') ?>

    <div class="card mb-4">
        <div class="card-header">
            <h4><?php echo lang('Localize.online_cancellation_list') ?></h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table display table-bordered table-striped table-hover basic" id="onlinecancellist">
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col"><?php echo lang('Localize.booking') ?> <?php echo lang('Localize.id') ?></th>
                            <th scope="col"><?php echo lang('Localize.customer_mobile') ?></th>
                            <th scope="col"><?php echo lang('Localize.geidea_order_id') ?></th>
                            <th scope="col"><?php echo lang('Localize.ticket_amount') ?></th>
                            <th scope="col"><?php echo lang('Localize.cancellation_fee') ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_amount') ?></th>
                            <th scope="col"><?php echo lang('Localize.payment') ?> <?php echo lang('Localize.details') ?></th>
                            <th scope="col"><?php echo lang('Localize.journey_date') ?></th>
                            <th scope="col"><?php echo lang('Localize.cancel_date_time') ?></th>
                            <th scope="col"><?php echo lang('Localize.cancelled_by') ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_sent_by_user') ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_sent_datetime') ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_status') ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_note_label') ?></th>
                            <th scope="col"><?php echo lang('Localize.action') ?></th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="modal fade" id="moneySentModal" tabindex="-1" aria-labelledby="moneySentModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="post" action="<?= esc(base_url(route_to('mark-online-refund-done'))) ?>" id="moneySentForm">
                    <?= csrf_field() ?>
                    <input type="hidden" name="id" id="money_sent_cancel_id" value="">
                    <div class="modal-header">
                        <h5 class="modal-title" id="moneySentModalLabel"><?php echo lang('Localize.money_sent_modal_title') ?></h5>
                        <button type="button" class="close" data-dismiss="modal" data-bs-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="refund_note"><?php echo lang('Localize.refund_note_label') ?></label>
                            <textarea class="form-control" name="refund_note" id="refund_note" rows="3" placeholder="<?php echo esc(lang('Localize.refund_note_optional')) ?>"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal" data-bs-dismiss="modal"><?php echo lang('Localize.cancel') ?></button>
                        <button type="submit" class="btn btn-success"><?php echo lang('Localize.money_sent') ?></button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php echo $this->include('common/datatable_default_lang_change') ?>
    <input type="hidden" name="<?php echo csrf_token(); ?>" id="csrf_token" value="<?php echo csrf_hash(); ?>">

<?php echo $this->endSection() ?>

<?php echo $this->section('js') ?>
<script>
    $(document).ready(function() {
        if (typeof $.fn.DataTable === 'undefined') {
            console.error('DataTables is not loaded.');
            return;
        }
        if ($('#onlinecancellist').length === 0) {
            return;
        }
        if ($.fn.DataTable.isDataTable('#onlinecancellist')) {
            $('#onlinecancellist').DataTable().destroy();
        }
        var companyname = $("#logotext").val() || "<?php echo esc(lang('Localize.online_cancellation_list')); ?>";
        var exportCols = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
        var dt = $('#onlinecancellist').DataTable({
            processing: true,
            serverSide: true,
            order: [[9, 'desc']],
            lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
            pageLength: 10,
            dom: 'lBfrtip',
            language: {
                search: search,
                lengthMenu: lengthMenu,
                zeroRecords: zeroRecords,
                info: info,
                infoEmpty: infoEmpty,
                infoFiltered: infoFiltered,
                paginate: { first: first, previous: previous, next: next, last: last }
            },
            ajax: {
                url: "<?= base_url(route_to('server-list-online-cancel')); ?>",
                type: "POST",
                data: function(d) {
                    var csrfToken = $('#csrf_token').val();
                    if (csrfToken) {
                        d.<?php echo csrf_token(); ?> = csrfToken;
                    }
                    return d;
                },
                beforeSend: function(xhr) {
                    var csrfToken = $('#csrf_token').val();
                    if (csrfToken) {
                        xhr.setRequestHeader('X-CSRF-TOKEN', csrfToken);
                        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                    }
                },
                dataSrc: 'data'
            },
            buttons: [
                { extend: 'copy', title: companyname, exportOptions: { columns: exportCols } },
                { extend: 'csv', title: companyname, exportOptions: { columns: exportCols } },
                { extend: 'excel', title: companyname, exportOptions: { columns: exportCols } },
                { extend: 'pdf', title: companyname, exportOptions: { columns: exportCols } },
                { extend: 'print', title: companyname, exportOptions: { columns: exportCols } }
            ],
            columns: [
                { data: "index", orderable: false },
                { data: "booking_id" },
                { data: "customer_mobile" },
                { data: "geidea_order_id" },
                { data: "ticket_amount" },
                { data: "cancel_fee" },
                { data: "refund_amount" },
                { data: "detail" },
                { data: "journey_date" },
                { data: "cancelled_at" },
                { data: "cancelled_by" },
                { data: "refund_sent_by" },
                { data: "refund_sent_at" },
                { data: "status" },
                { data: "refund_note" },
                { data: "action", orderable: false, searchable: false }
            ],
            responsive: true
        });

        $(document).on('click', '.js-money-sent', function() {
            var cid = $(this).data('id');
            $('#money_sent_cancel_id').val(cid);
            $('#refund_note').val('');
            var modal = $('#moneySentModal');
            if (modal.modal) {
                modal.modal('show');
            } else if (typeof bootstrap !== 'undefined' && bootstrap.Modal) {
                new bootstrap.Modal(modal[0]).show();
            }
        });
    });
</script>
<?php echo $this->endSection() ?>
