<?php
$allmikrotik = $obj->getAllData('mikrotik_user');
$middd = !empty($_GET['mikrotik_id']) ? $_GET['mikrotik_id'] : '';
?>

<div class="col-xl-12">
    <div class="card h-100 p-0">
        <div class="card-header border-bottom bg-base py-16 px-24">
            <h6 class="text-lg fw-semibold mb-0">Mikrotik Connection</h6>
        </div>
        <div class="card-body p-24">
            <div class="d-flex flex-wrap align-items-center gap-3">
                <?php foreach ($allmikrotik as $mikrotiks): ?>
                    <div class="col-md-2 mb-2">
                        <span id="m<?php echo $mikrotiks['id']; ?>">
                            <button class="btn btn-primary" type="button" disabled>
                                <span class="spinner-border spinner-border-sm me-1"></span>
                                Loading...
                            </button>
                        </span>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>

<div class="card basic-data-table mt-3">
    <div class="card-header d-flex flex-wrap align-items-center justify-content-between">
        <h5 class="card-title mb-0">Offline Secret List</h5>
    </div>
    <div class="card-body">
        <table class="table bordered-table mb-0" id="secrettable">
            <thead>
                <tr>
                    <th>SL</th>
                    <th>Name</th>
                    <th>Password</th>
                    <th>Profile</th>
                    <th>Service</th>
                    <th>Disable Status</th>
                    <th>Last Logout</th>
                </tr>
            </thead>
            <tbody id="secret_body"></tbody>
        </table>
    </div>
</div>

<?php $obj->start_script(); ?>

<script>
    let sectetShow = true;
    let defaultMikrotikId = "<?php echo $middd; ?>";

    function mkConnectCheck(id, mkip) {
        $.ajax({
            type: "GET",
            url: "./pages/mikrotik/connect_ajax.php",
            data: { mkid: id },
            dataType: "JSON",
            success: function (response) {

                if (response.connection) {

                    let btnClass = (defaultMikrotikId == id) ? 'btn-warning' : 'btn-success';

                    $('#m' + id).html(`
                        <button class="btn ${btnClass}" onclick="mkAllSecret(${id})" type="button">
                            ${mkip} ${response.status}
                        </button>
                    `);

        
                    if (defaultMikrotikId && defaultMikrotikId == id) {
                        mkAllSecret(id);
                    }

                    
                    if (!defaultMikrotikId && sectetShow) {
                        mkAllSecret(id);
                        sectetShow = false;
                    }

                } else {
                    $('#m' + id).html(`
                        <button class="btn btn-danger" disabled type="button">
                            ${mkip} ${response.status}
                        </button>
                    `);
                }
            }
        });
    }

    function mkAllSecret(id) {
        $.ajax({
            type: "GET",
            url: "./pages/mikrotik/connect_ajax.php",
            data: { mkidsecretofline: id },
            dataType: "JSON",
            success: function (response) {

                if ($.fn.DataTable.isDataTable('#secrettable')) {
                    $('#secrettable').DataTable().destroy();
                }

                $('#secret_body').html('');

                if (response.connection) {
                    $('#secret_body').html(response.status);
                }

                $('#secrettable').DataTable({
                    pageLength: 100,
                    lengthMenu: [[10, 25, 100, 500], [10, 25, 100, 500]],
                    responsive: true
                });
            }
        });
    }
</script>

<?php foreach ($allmikrotik as $mikrotiks): ?>
    <script>
        mkConnectCheck(
            "<?php echo $mikrotiks['id']; ?>",
            "<?php echo $mikrotiks['mik_ip']; ?>"
        );
    </script>
<?php endforeach; ?>

<?php $obj->end_script(); ?>
