<?php $__env->startSection('content'); ?>

<div class="card">
    <div class="card-header"><?php echo e(trans('cruds.transaction.title')); ?></div>
    <div class="card-body">
        <?php if(session('success')): ?>
            <div class="alert alert-success">
                <?php echo e(session('success')); ?>

            </div>
        <?php endif; ?>

        <div class="alert alert-success d-none successMessage">
            <p><?php echo e(trans('global.payment_status_updated_message')); ?></p>
        </div>

        
        <ul class="nav nav-tabs">
            <li class="nav-item">
                <a class="nav-link <?php echo e(request('type') == 'online-payment' ? 'active' : ''); ?>" href="#online-payment">
                   <?php echo e(trans('global.tabbing.online_payments')); ?>

                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo e(request('type') == 'cash-payment' ? 'active' : ''); ?>" href="#cash-payment">
                   <?php echo e(trans('global.tabbing.cash_payments')); ?>

                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo e(request('type') == 'cash-payment-requested' ? 'active' : ''); ?>" href="#cash-payment-requested">
                   <?php echo e(trans('global.tabbing.cash_payment_requested')); ?> (<?php echo e(getCashPaymentPendingRequestCount()); ?>)
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?php echo e(request('type') == 'cash-payment-rejected' ? 'active' : ''); ?>" href="#cash-payment-rejected">
                   <?php echo e(trans('global.tabbing.cash_payment_rejected')); ?>

                </a>
            </li>
        </ul>

        <table class="table table-bordered table-hover ajaxTable datatable datatable-Make">
            <thead>
                <tr>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.id')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.buyer_name')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.mobile_number')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.order_id')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.amount')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.payment_status')); ?>

                    </th>
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.note')); ?>

                    </th>
                    
                    <th>
                        <?php echo e(trans('cruds.transaction.fields.created_at')); ?>

                    </th>
                    <th>
                        &nbsp;
                    </th>
                </tr>
            </thead>
        </table>
    </div>
</div>

<!-- Change Status Modal -->
<div class="modal fade" id="statusModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo e(trans('global.update_status')); ?></h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="payment_id">
                <div class="form-group">
                    <label><?php echo e(trans('cruds.transaction.fields.status_label')); ?></label>
                    <select class="form-control status" name="status">
                        <option value=""><?php echo e(trans('cruds.transaction.fields.status.select_status')); ?></option>
                        <option value="cash_payment"><?php echo e(trans('cruds.transaction.fields.status.cash_payment')); ?></option>
                        <option value="paid_on_delivery"><?php echo e(trans('cruds.transaction.fields.status.paid_on_delivery')); ?></option>
                        <option value="rejected"><?php echo e(trans('cruds.transaction.fields.status.rejected')); ?></option>
                    </select>
                </div>
                <div class="form-group">
                    <label><?php echo e(trans('cruds.transaction.fields.note')); ?></label>
                    <textarea class="form-control" id="note" rows="4" placeholder="<?php echo e(trans('cruds.transaction.fields.note_placeholder')); ?>"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo e(trans('global.cancel')); ?></button>
                <button type="button" class="btn btn-primary" id="saveNote"><?php echo e(trans('global.submit')); ?></button>
            </div>
        </div>
    </div>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>

<?php echo \Illuminate\View\Factory::parentPlaceholder('scripts'); ?>
<script>
    $(function () {
        let currentType = "<?php echo e($type); ?>";
        let dtButtons = $.extend(true, [], $.fn.dataTable.defaults.buttons)

        let dtOverrideGlobals = {
            buttons: dtButtons,
            processing: true,
            serverSide: true,
            retrieve: true,
            aaSorting: [],
            ajax: {
                url: "<?php echo e(route('admin.transaction.index')); ?>",
                data: function (d) {
                    d.type = currentType;
                }
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'buyer_name', name: 'buyer_name' },
                { data: 'mobile_number', name: 'mobile_number' },
                { data: 'order_id', name: 'order_id' },
                { data: 'amount', name: 'amount' },
                { data: 'payment_status', name: 'payment_status' },
                { data: 'note', name: 'note' },
                // { data: 'order_status', name: 'order_status' },
                { data: 'created_at', name: 'created_at' },
                { data: 'actions', name: "<?php echo e(trans('global.actions')); ?>" }
            ],
            orderCellsTop: true,
            order: [[ 1, 'desc' ]],
            pageLength: 10,
        };

        // let table = $('.datatable-Make').DataTable(dtOverrideGlobals);
        // $('a[data-toggle="tab"]').on('shown.bs.tab click', function(e){
        //     ($.fn.dataTable.tables(true)).DataTable().columns.adjust();
        // });

        let table = $('.datatable-Make').DataTable($.extend(true, dtOverrideGlobals, {
            initComplete: function () {
                toggleOrderIdColumn(); // SAFE now
            }
        }));

        // Reload table when tab clicked
        $('.nav-link').on('click', function (e) {
            e.preventDefault();

            // Remove active from all tabs
            $('.nav-link').removeClass('active');

            // Add active to clicked tab
            $(this).addClass('active');

            if($(this).attr('href') == '#online-payment'){
                currentType = 'online-payment';
            } else if($(this).attr('href') == '#cash-payment-requested'){
                currentType = 'cash-payment-requested';
            } else if($(this).attr('href') == '#cash-payment'){
                currentType = 'cash-payment';
            } else if($(this).attr('href') == '#cash-payment-rejected'){
                currentType = 'cash-payment-rejected';
            }

            toggleOrderIdColumn();

            table.ajax.reload();
        });

        /* ================================
            SHOW / HIDE ORDER ID COLUMN
        ================================ */
        function toggleOrderIdColumn() {
            if (!table) return;

            if (currentType === 'cash-payment' || currentType === 'cash-payment-rejected') {
                table.column(6).visible(true); // show Note
            } else {
                table.column(6).visible(false); // hide Note
            }
        }

        // Open modal on change status button click
        $(document).on('click', '.wait_for_cash_payment', function () {
            let id = $(this).data('id');
            $('#payment_id').val(id);
            $('#note').val('');

            $('#statusModal').modal('show');
        });

        $('#saveNote').click(function () {
            let payment_id = $('#payment_id').val();
            let note = $('#note').val();
            let status = $(".status").val();

            $.ajax({
                url: "<?php echo e(route('admin.transaction.update_paid_on_delivery')); ?>",
                type: "POST",
                data: {
                    _token: "<?php echo e(csrf_token()); ?>",
                    payment_id: payment_id,
                    status: status,
                    note: note
                },
                success: function (response) {
                    $('#statusModal').modal('hide');

                    if(response.status === 'success'){
                        $('.successMessage').removeClass('d-none');

                        setTimeout(function() {
                            $('.successMessage').addClass('d-none');
                        }, 3000);
                    }

                    table.ajax.reload();
                },
                error: function () {
                    alert('Something went wrong!');
                }
            });
        });
    });
</script>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /var/www/html/resources/views/admin/transaction/index.blade.php ENDPATH**/ ?>