<?php
      session_start();
  if($_SESSION["user"]=="")
  {
    echo"<script>window.location.href='cikis.php'</script>";
  }
  else{
include("baglanti.php");

 if (isset($_GET['sil'])) {
    $id = intval($_GET['sil']);
    $stmt = $baglan->prepare("DELETE FROM egitimler WHERE id = ?");
    $stmt->bind_param("i", $id); // 'i' = integer
    $stmt->execute();
    $stmt->close();
    header("Location: Adminegitim.php");
    exit;
}
  }
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>DİJ-EĞİTİM</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="https://kit.fontawesome.com/52d8d9fe06.js" crossorigin="anonymous"></script>

</head>
<body>

<section id="menu">
    <div id="logo">Eğitim Düzenle</div>
    <nav>
        <a href="Admin.php"><i class="fa-solid fa-graduation-cap"></i> Eğitim Ekle</a>
        <a href="Adminiletisim.php"><i class="fa-solid fa-envelope ikon"></i> Gelen Mesajlar</a>
        <a href="Admingiris.php"><i class="fa-solid fa-arrow-right-from-bracket ikon"></i> Çıkış</a>
    </nav>
</section>

<input type="text" id="arama" placeholder="Başlık, açıklama veya tarih ara...">

<table id="customers">
<tr>
    <th>Başlık</th>
    <th>Açıklama</th>
    <th>Resim</th>
    <th>Video</th>
    <th>Tarih</th>
    <th>İzlenme</th>
    <th>Sil</th>
</tr>
<?php

if ($_SESSION["user"] == "") {
    echo "<script>window.location.href='cikis.php'</script>";
} 
else {
    $sorgu = $baglan->query("SELECT * FROM egitimler ORDER BY id DESC");

    foreach ($sorgu as $satir) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($satir['baslik']) . "</td>";
        echo "<td>" . htmlspecialchars($satir['aciklama']) . "</td>";
        echo "<td><img src='" . htmlspecialchars($satir['resim_url']) . "' width='100'></td>";
        echo "<td><a href='" . htmlspecialchars($satir['video_url']) . "' target='_blank'>Video</a></td>";
        echo "<td>" . htmlspecialchars($satir['tarih']) . "</td>";
        echo "<td>" . intval($satir['tiklanma']) . "</td>";
        echo "<td><a class='sil-buton' href='?sil=" . intval($satir['id']) . "' onclick=\"return confirm('Silmek istediğinize emin misiniz?')\">Sil</a></td>";
        echo "</tr>";
    }
}
?>

</table>

<script>
document.getElementById('arama').addEventListener('keyup', function () {
    var filtre = this.value.toLowerCase();
    var satirlar = document.querySelectorAll("#customers tr:not(:first-child)");

    satirlar.forEach(function (satir) {
        var metin = satir.textContent.toLowerCase();
        satir.style.display = metin.includes(filtre) ? "" : "none";
    });
});
</script>

</body>
</html>