@extends('layout.app')

@section('content')

    <div class="card">
        <div class="card-body">
            <form action="" id="search" class="resellerRechargeLog">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Select Manager</label>
                            <select name="reseller" id="reseller" class="form-control form-control-sm select2">
                                <option value="">Select One</option>
                                @foreach ($reseller as $r)
                                    <option value="{{ $r->id }}">{{ $r->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                    <div class="col-md-4">
                        <label>Select POP</label>
                        <select name="pop" id="pop" class="form-control form-control-sm select2">
                            <option value="">Select One</option>
                        </select>
                    </div>

                    @if (checkAPI())
                        <div class="col-md-4">
                            <label>Status</label>
                            <select name="clients_status" id="clients_status" class="form-control form-control-sm select2">
                                <option value="">Select One</option>
                                <option value="active">Active</option>
                                <option value="expired">Expire</option>
                                <option value="deactive">Deactive</option>
                                <option value="disable">Disable</option>
                            </select>
                        </div>
                    @endif

                    <!-- Buttons aligned right -->
                    <div class="col-md-12 d-flex justify-content-end align-items-end mt-3">
                        <button class="btn btn-primary mr-2" type="submit" id="searchBtn">
                            <i class="fa fa-search"></i> Search
                        </button>
                        <button class="btn btn-secondary" type="reset" id="reset">
                            <i class="fa fa-undo"></i> Reset
                        </button>
                    </div>
                </div>
            </form>



        </div>
    </div>


    <div class="mt-2 card">
        <div class="card-body">
            <div id="customer_list_body">
                <div id="result" class="table-responsive" style="">
                    @if (true)
                    @else
                        <h4>Total Online: {{ $list->count() }}</h4>
                        <table id="dataTable" class="display table">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col"> User ID </th>
                                    <th scope="col"> Client IP </th>
                                    <th scope="col"> NAS IP </th>
                                    <th scope="col"> MAC Address </th>
                                    <th scope="col"> Reason </th>
                                    <th scope="col"> Last Update </th>
                                    <th scope="col"> Uptime </th>
                                    <th>Date</th>
                                    <th scope="col"></th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">

                                @foreach ($list as $r)
                                    {{-- {{dd($r)}} --}}
                                    <tr>
                                        <td> {{ $r->username }} </td>
                                        <td> {{ $r->framedipaddress }} </td>
                                        <td> {{ $r->nasipaddress }} </td>
                                        <td> {{ $r->callingstationid }} </td>
                                        <td>{{ $r->acctterminatecause }}</td>
                                        <td>{{ $r->acctupdatetime }}</td>
                                        <td> {{ \Carbon\Carbon::parse($r->acctstarttime)->diffForHumans() }} </td>
                                        <td> {{ $r->acctstarttime }} </td>

                                        <td class="px-6 text-center text-sm font-medium">
                                            <a href="{{ route('onlineUserDisconnect') }}?id={{ $r->radacctid }}"
                                                class="btn btn-danger">Disconnect</a>
                                        </td>
                                    </tr>
                                @endforeach

                                <!-- More items... -->
                            </tbody>
                        </table>
                    @endif

                </div>
            </div>
        </div>
        <div class="card-footer">
        </div>
    </div>

    <style>
        div#processing {
            display: none;
        }

        div#processing div {
            background: rgba(0, 0, 0, .5);
            position: fixed;
            width: 100vw;
            height: 100vh;
            color: #fff;
            text-align: center;
            vertical-align: middle;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        @media print {
            a {
                text-decoration: none !important;
                color: #222 !important;
            }

            th {
                font-size: 10px !important;
                padding: 2px !important;
                text-align: center;
            }

            tr {
                font-size: 10px !important;
                margin: 2px !important;
                padding: 2px !important;
            }

            td {
                font-size: 10px !important;
                margin: 2px !important;
                padding: 2px !important;
            }

            table {
                width: 100%;
            }

            .shadow {
                box-shadow: none;
            }

            tr th:last-child {
                display: none !important;
            }

            tr td:last-child {
                display: none !important;
            }

        }

        .dt-buttons {
            margin-bottom: 10px;
        }

        .dataTables_filter {
            padding-top: 10px;
            padding-right: 10px;
        }

        .dataTables_length select {
            width: 60px !important;
            /* Adjust this value as needed */
            min-width: auto;
            display: inline-block;
            margin: 0 5px;
            padding-top: 10px;
            padding-left: 10px;
        }

        .br-spacer {
            height: 10px;
        }
    </style>


@endsection

@section('ajax')
    <!-- DataTables CSS -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.min.css">

    <!-- DataTables JS -->
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $('#reset').click(function() {

                $("#loader").show();
                $('#result').html('');
                $('#pop').html('<option value="all">Select One</option>');
                $("#loader").hide();
            });

            $('#searchBtn').on('click', function(e) {
                $("#loader").show();
                e.preventDefault();
                // $('body').append('<div id="processing">processing.....</div>');

                $.ajax({
                    type: 'get',
                    url: '{{ route('onlineCustomerSearch') }}',
                    data: $('#search').serialize(),
                    success: function(result) {
                        // console.log(result);
                        $('#result').html(result);
                        initializeDataTable();
                        $('#processing').remove();
                        // $('#dataTable').DataTable({
                        //     'destroy': true,
                        // });
                        $("#loader").hide();

                    }
                });
            });




            $('#reseller').on('change', function(e) {
                e.preventDefault();
                $("#loader").show();

                $.ajax({
                    type: 'get',
                    url: '{{ route('getResellerPopList') }}',
                    data: {
                        id: $(this).val()
                    },
                    success: function(result) {
                        $('#pop').html(result);
                        $('#processing').remove();
                        $("#loader").hide();
                    }
                });
            });

            function initializeDataTable() {
                // Destroy any existing DataTable instance first
                if ($.fn.DataTable.isDataTable('#dataTable')) {
                    $('#dataTable').DataTable().destroy();
                }

                var table = $('#dataTable').DataTable({
                    dom: '<"top"lfB><"br-spacer">rt<"bottom"ip><"clear">',
                    buttons: [{
                        extend: 'csv',
                        text: '<i class="fa fa-file-csv"></i> CSV',
                        className: 'btn btn-success btn-sm',
                        title: 'Online_Customers_' + new Date().toISOString().slice(0, 10),
                        exportOptions: {
                            columns: ':visible',
                            modifier: {
                                page: 'all'
                            }
                        },
                        action: function(e, dt, button, config) {
                            // Get the CSV data
                            var csv = dt.buttons.exportData(config.exportOptions);
                            var csvContent = '';

                            // Add headers
                            if (csv.header.length > 0) {
                                csvContent += csv.header.join(',') + '\n';
                            }

                            // Add CSV data rows
                            csvContent += csv.body.join('\n');

                            // Add UTF-8 BOM for proper Bengali text display in Excel
                            var BOM = '\uFEFF';
                            var finalCsv = BOM + csvContent;

                            // Create and download the file
                            var blob = new Blob([finalCsv], { type: 'text/csv;charset=utf-8;' });
                            var link = document.createElement('a');
                            var url = URL.createObjectURL(blob);
                            link.setAttribute('href', url);
                            var filename = config.title ? config.title.replace(/[^a-z0-9]/gi, '_') : 'Online_Customers_' + new Date().toISOString().slice(0, 10);
                            link.setAttribute('download', filename + '.csv');
                            link.style.visibility = 'hidden';
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        }
                    }],
                    responsive: true,
                    lengthMenu: [
                        [10, 25, 50, 100, -1],
                        [10, 25, 50, 100, "All"]
                    ],
                    pageLength: 25,
                    initComplete: function() {
                        // Hide the default DataTables button and use our custom button
                        $('.dt-buttons').hide();
                    }
                });

                // Trigger CSV export when our custom button is clicked
                $(document).on('click', '#downloadCsv', function() {
                    table.button('.buttons-csv').trigger();
                });
            }

        });
    </script>
@endsection
