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

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

    // Hapus dari tabel channel
    $queryHapusChannel = "DELETE FROM channel WHERE id = '$id'";
    if (mysqli_query($conn, $queryHapusChannel)) {
        echo "<script>
            alert('Data channel berhasil dihapus!');
            window.location = 'channel.php';
        </script>";
    } else {
        echo "<script>
            alert('Gagal menghapus data!');
        </script>";
    }
}

// Ambil data channel dari tabel channel
$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>DATA ALTERNATIF</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> Daftar Channel YouTube</span>
                    <a href="tambah_channel.php" class="btn btn-light btn-lg">
                        <i class="fas fa-plus"></i> Tambah Data Channel
                    </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%;">Nama Channel</th>
                                    <th style="width: 13%;">Jumlah Views</th>
                                    <th style="width: 13%;">Jumlah Subscriber</th>
                                    <th style="width: 15%;">Frekuensi Unggah Video</th>
                                    <th style="width: 13%;">Jumlah Video</th>
                                    <th style="width: 30%;">Latar Belakang Konten Kreator</th>
                                    <th style="width: 10%;">Aksi</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> Ubah
                                                </a>
                                                <a href="channel.php?id=<?= $row["id"]; ?>" class="btn btn-danger" onclick="return confirm('Yakin ingin menghapus?');">
                                                    <i class="fas fa-trash-alt"></i> Hapus
                                                </a>

                                            </div>
                                        </td>
                                    </tr>
                                    <?php $i++ ?>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div> <!-- Akhir dari card -->
        </div>
    </main>
</div>

<script>
    // Memastikan navigasi bisa dibuka dan tidak macet
    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>
