<?php
if ($c['level'] == "superadmin") {
    // Handle game maintenance update
    if (isset($_POST['provider_maintenance'])) {
        foreach ($_POST['provider_maintenance'] as $provider_name => $data) {
            $status = $data ? '1' : '0'; // Convert boolean to '1' or '0'
            $query = "UPDATE tb_provider SET provider_status = '$status' WHERE provider_name = '$provider_name'";
            mysqli_query($koneksi, $query);
        }
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
        echo "<script type='text/javascript'>
                window.addEventListener('load', function() {
                    Swal.fire({
                        icon: 'success',
                        title: 'Success!',
                        text: 'Game maintenance status updated successfully.',
                        showConfirmButton: true
                    });
                });
              </script>";
    }

    // Handle Config Data Update
    if (isset($_POST['agent_code']) && isset($_POST['signature']) && isset($_POST['url_request'])) {
        $agent_code = $_POST['agent_code'];
        $signature = $_POST['signature'];
        $url_request = $_POST['url_request'];
        mysqli_query($koneksi, "UPDATE config SET agent_code = '$agent_code', signature = '$signature', url_request = '$url_request' WHERE id = 1");
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
        echo "<script type='text/javascript'>
                window.addEventListener('load', function() {
                    Swal.fire({
                        icon: 'success',
                        title: 'Success!',
                        text: 'API status updated successfully.',
                        showConfirmButton: true
                    });
                });
              </script>";
    }
    $result = mysqli_query($koneksi, "SELECT * FROM tb_provider");
    $game_maintenance_status = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $game_maintenance_status[$row['provider_name']] = [
            'status' => $row['provider_status'] == '1' ? true : false, // Convert status to boolean for form
            'code' => $row['provider_code'] // Fetch provider code
        ];
    }
    ?>

    <div class="page-breadcrumb">
        <div class="row">
            <div class="col-5 align-self-center">
                <h4 class="page-title">Game Manage</h4>
            </div>
            <div class="col-7 align-self-center">
                <div class="d-flex align-items-center justify-content-end">
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item">
                                <a href="#">Maintenance</a>
                            </li>
                            <li class="breadcrumb-item active" aria-current="page">Manage Game Maintenance</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">Manage Game Maintenance</h4>
                        <input type="text" id="searchInput" class="form-control" placeholder="Search game...">
                        <br>
                        <form method="post" id="gameForm">
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th>Provider Name</th>
                                            <th>Provider Code</th>
                                            <th>Status</th>
                                            <th>Change Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody id="gameTableBody">
                                        <?php
                                        $number = 1;
                                        foreach ($game_maintenance_status as $provider_name => $provider_info) {
                                            $status = $provider_info['status'];
                                            $code = $provider_info['code'];
                                            $status_text = $status ? 'ACTIVE' : 'MAINTENANCE';
                                            $status_color = $status ? 'red' : 'green';
                                            ?>
                                            <tr class="game-item">
                                                <td class="number"><?php echo $number; ?></td>
                                                <td><?php echo $provider_name; ?></td>
                                                <td><?php echo $code; ?></td>
                                                <td>
                                                    <div class="status-indicator" style="background-color: <?php echo $status_color; ?>;"></div>
                                                    <?php echo $status_text; ?>
                                                </td>
                                                <td>
                                                    <select class="custom-select col-12" name="provider_maintenance[<?php echo $provider_name; ?>]">
                                                        <option value="1" <?php if ($status) echo 'selected'; ?>>ACTIVE</option>
                                                        <option value="0" <?php if (!$status) echo 'selected'; ?>>MAINTENANCE</option>
                                                    </select>
                                                </td>
                                                <td>
                                                    <button type="submit" class="btn btn-success m-r-10">Submit</button>
                                                </td>
                                            </tr>
                                            <?php
                                            $number++;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                        <br>
                        <h4 class="card-title">Update API</h4>
                        <form method="post">
                            <div class="form-group">
                                <label for="agent_code">Agent Code</label>
                                <input type="text" class="form-control" id="agent_code" name="agent_code" required>
                            </div>
                            <div class="form-group">
                                <label for="signature">Signature</label>
                                <input type="text" class="form-control" id="signature" name="signature" required>
                            </div>
                            <div class="form-group">
                                <label for="url_request">URL Request</label>
                                <input type="text" class="form-control" id="url_request" name="url_request" value="https://api.apimax.site/v2/" required>
                            </div>
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php
    
} else {
    ?>
    <script type="text/javascript">
        window.location = "?page=dashboard";
    </script>
    <?php
}
?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    function updateNumbering() {
        var gameItems = document.querySelectorAll('.game-item');
        var number = 1;
        gameItems.forEach(function(item) {
            if (item.style.display !== 'none') {
                item.querySelector('.number').textContent = number;
                number++;
            }
        });
    }

    document.getElementById('searchInput').addEventListener('input', function() {
        var filter = this.value.toUpperCase();
        var gameItems = document.querySelectorAll('.game-item');
        gameItems.forEach(function(item) {
            var label = item.querySelector('td:nth-child(2)').textContent.toUpperCase();
            if (label.indexOf(filter) > -1) {
                item.style.display = '';
            } else {
                item.style.display = 'none';
            }
        });
        updateNumbering();
    });

    document.getElementById('categoryFilter').addEventListener('change', function() {
        var category = this.value;
        var gameItems = document.querySelectorAll('.game-item');
        gameItems.forEach(function(item) {
            if (category === "" || item.getAttribute('data-category') === category) {
                item.style.display = '';
            } else {
                item.style.display = 'none';
            }
        });
        updateNumbering();
    });

    window.addEventListener('load', updateNumbering);
</script>

<style>
    @media (max-width: 768px) {
        .table-responsive {
            overflow-x: auto; /* Enable horizontal scrolling on tables */
        }
    }
</style>
