<?php 
include 'header2.php'; 
include 'koneksi.php';

if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Delete from channel table
    $queryDeleteChannel = "DELETE FROM channel WHERE id = '$id'";
    if (mysqli_query($conn, $queryDeleteChannel)) {
        echo "<script>
            alert('Channel data has been successfully deleted!');
            window.location = 'channel.php';
        </script>";
    } else {
        echo "<script>
            alert('Failed to delete data!');
        </script>";
    }
}

// Retrieve channel data from the channel table
$channel = query("SELECT * FROM channel ORDER BY id");

?>

<div id="layoutSidenav_content">
    <main>
        <div class="container-fluid mt-4">
            <h2 class="text-center mb-4 py-4 bg-light rounded" style="font-size: 28px;">
                <strong>ALTERNATIVE DATA</strong>
            </h2>

            <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
                <span style="font-size: 20px;"><i class="fab fa-youtube"></i> YouTube Channel List</span>
                <a href="tambah_channel.php" class="btn btn-light btn-lg">
                    <i class="fas fa-plus"></i> Add Channel Data
                </a>
            </div>

            <div class="card-body">
                <div class="table-responsive">
                    <table id="datatablesSimple" class="table table-striped table-bordered table-hover w-100">
                        <thead class="table-success text-center" style="font-size: 20px;">
                            <tr>
                                <th style="width: 5%;">No</th>
                                <th style="width: 20%;">Channel Name</th>
                                <th style="width: 13%;">Total Views</th>
                                <th style="width: 13%;">Total Subscribers</th>
                                <th style="width: 15%;">Upload Frequency</th>
                                <th style="width: 13%;">Total Videos</th>
                                <th style="width: 30%;">Creator's Background</th>
                                <th style="width: 10%;">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="text-center" style="font-size: 17px;">
                            <?php $i = 1; ?>
                            <?php foreach ($channel as $row) : ?>    
                                <tr>
                                    <td class="align-middle"><?= $i ?></td>
                                    <td class="align-middle"><?= htmlspecialchars($row["nama"]); ?></td>
                                    <td class="align-middle"><?= number_format($row["views"]); ?></td>
                                    <td class="align-middle"><?= number_format($row["subscribers"]); ?></td>
                                    <td class="align-middle"><?= htmlspecialchars($row["frekuensi"]); ?></td>
                                    <td class="align-middle"><?= number_format($row["videos"]); ?></td>
                                    <td class="align-middle text-start"><?= htmlspecialchars($row["background"]); ?></td>
                                    <td class="align-middle">
                                        <div class="d-flex justify-content-center gap-2">
                                            <a href="ubah_channel.php?id=<?= $row["id"]; ?>" class="btn btn-warning">
                                                <i class="fas fa-edit"></i> Edit
                                            </a>
                                            <a href="channel.php?id=<?= $row["id"]; ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this?');">
                                                <i class="fas fa-trash-alt"></i> Delete
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                <?php $i++ ?>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div> <!-- End of card -->
    </main>
</div>

<script>
    // Ensure the sidebar navigation can toggle properly
    document.addEventListener("DOMContentLoaded", function () {
        let sidebarToggle = document.querySelector("#sidebarToggle");
        if (sidebarToggle) {
            sidebarToggle.addEventListener("click", function (e) {
                e.preventDefault();
                document.body.classList.toggle("sb-sidenav-toggled");
            });
        }
    });
</script>
