<?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 class="mb-0"><?php echo lang('Localize.online_refund_list'); ?></h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table display table-bordered table-striped table-hover basic" id="onlinerefundlist">
                    <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.geidea_order_id'); ?></th>
                            <th scope="col"><?php echo lang('Localize.customer_mobile'); ?></th>
                            <th scope="col"><?php echo lang('Localize.ticket_amount'); ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_fee_amount'); ?></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.refund_date_time'); ?></th>
                            <th scope="col"><?php echo lang('Localize.refunded_by'); ?></th>
                            <th scope="col"><?php echo lang('Localize.refund_status'); ?></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_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="onlineRefundMoneyModal" tabindex="-1" aria-labelledby="onlineRefundMoneyModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="post" action="<?= esc(base_url(route_to('mark-online-manual-refund-done'))); ?>" id="onlineRefundMoneyForm">
                    <?= csrf_field(); ?>
                    <input type="hidden" name="id" id="online_refund_row_id" value="">
                    <div class="modal-header">
                        <h5 class="modal-title" id="onlineRefundMoneyModalLabel"><?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">
                        <p class="text-muted small mb-3"><?php echo lang('Localize.money_sent_modal_intro'); ?></p>
                        <div class="form-group">
                            <label for="online_refund_note"><?php echo lang('Localize.refund_note_label'); ?></label>
                            <textarea class="form-control" name="refund_note" id="online_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 ($('#onlinerefundlist').length === 0) {
            return;
        }
        if ($.fn.DataTable.isDataTable('#onlinerefundlist')) {
            $('#onlinerefundlist').DataTable().destroy();
        }
        var companyname = $('#logotext').val() || "<?php echo esc(lang('Localize.online_refund_list')); ?>";
        var exportCols = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
        $('#onlinerefundlist').DataTable({
            processing: true,
            serverSide: true,
            order: [[9, 'desc']],
            lengthMenu: [[10, 25, 50, -1], [10, 25, 50, 'All']],
            pageLength: 25,
            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-manual-refund')); ?>",
                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: 'geidea_order_id' },
                { data: 'customer_mobile' },
                { data: 'ticket_amount' },
                { data: 'refund_fee' },
                { data: 'refund_amount' },
                { data: 'payment_detail' },
                { data: 'journey_date' },
                { data: 'refund_at' },
                { data: 'refunded_by' },
                { data: 'status' },
                { data: 'sent_by' },
                { data: 'sent_at' },
                { data: 'refund_note' },
                { data: 'action', orderable: false, searchable: false }
            ],
            responsive: true
        });

        $(document).on('click', '.js-online-refund-money-sent', function(e) {
            e.preventDefault();
            var rid = $(this).data('id');
            if (rid === undefined || rid === '') {
                rid = $(this).attr('data-id');
            }
            $('#online_refund_row_id').val(rid);
            $('#online_refund_note').val('');
            var el = document.getElementById('onlineRefundMoneyModal');
            if (!el) {
                return;
            }
            // Bootstrap 5.0 beta (this project) has no getOrCreateInstance — use getInstance + constructor.
            if (typeof bootstrap !== 'undefined' && bootstrap.Modal) {
                try {
                    var modal = typeof bootstrap.Modal.getInstance === 'function'
                        ? bootstrap.Modal.getInstance(el)
                        : null;
                    if (!modal) {
                        modal = new bootstrap.Modal(el);
                    }
                    modal.show();
                } catch (err) {
                    console.error('Online refund modal:', err);
                    if (typeof $ !== 'undefined' && $.fn.modal) {
                        $('#onlineRefundMoneyModal').modal('show');
                    }
                }
            } else if (typeof $ !== 'undefined' && $.fn.modal) {
                $('#onlineRefundMoneyModal').modal('show');
            }
        });
    });
</script>
<?php echo $this->endSection(); ?>
