<?php include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

function limit_words($text, $limit) {
    $words = explode(' ', $text);
    if (count($words) > $limit) {
        return implode(' ', array_slice($words, 0, $limit)) . '...';
    }
    return $text;
}
?>
<!DOCTYPE html>
<html lang="en">
<head><link rel="icon" href="upload/bn2.png" type="image/png">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Tenaga Pendidikan | SMK Budiniah 2</title>
    <link rel="stylesheet" href="style_admin.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
        }
        .container {
            max-width: 1000px;
            margin: 50px  auto;
            padding: 30px;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
        }
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #0037fd, #5b8cffff);
            border-radius: 15px 15px 0 0;
        }
        h1 {
            text-align: center;
            color: #0037fd;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: bold;
        }
        .top-buttons {
            position: absolute;
            top: 20px;
            left: 30px;
            display: flex;
            gap: 10px;
        }
        .btn {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .guru-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 60px;
        }
        .top-buttons{
            position: static;
            justify-content: center;
            margin-bottom: 20px;
        }
        .guru-card {
            background: #fff;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
        }
        .guru-card:hover {
            transform: translateY(-5px);
        }
        .guru-card img {
            width: 100%;
            height: 300px;
            object-fit: cover;
        }
        .guru-content {
            padding: 15px;
        }
        .guru-content h3 {
            font-size: 18px;
            margin: 0 0 10px 0;
            text-transform: capitalize;
            color: #333;
        }
        .guru-content p {
            font-size: 14px;
            color: #666;
            text-transform: capitalize;
            margin: 0 0 10px 0;
            line-height: 1.4;
        }
        .jabatan {
            font-size: 12px;
            color: #999999ff;
            margin-bottom: 10px;
        }
        .guru-action {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .read-more {
            color: #0037fd;
            text-decoration: none;
            font-size: 14px;
            font-weight: bold;
        }
        .read-more:hover {
            color: #5b8cffff;
        }
        .edit-btn, .delete-btn {
            padding: 5px 10px;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 12px;
            text-decoration: none;
            margin-left: 5px;
        }
        .edit-btn {
            background: #28a745;
            color: #fff;
        }
        .edit-btn:hover {
            background: #218838;
        }
        .delete-btn {
            background: #dc3545;
            color: #fff;
        }
        .delete-btn:hover {
            background: #c82333;
        }
        .no-guru {
            text-align: center;
            color: #666;
            font-size: 18px;
            margin-top: 50px;
        }
        .teks-kecil {
            font-size: 12px;
            color: #b8860b;
            margin-bottom: 10px;
        }
        @media (max-width: 768px){
            .container{
                max-width: 500px;
                margin: 20px 20px 40px 80px;
                padding: 20px 5%;
            }
            .guru-grid{
                grid-template-columns: 1fr;
            }
            .guru-card{
                width: 100%;
                margin-bottom: 20px;
            }
            .guru-card img{
                width: 100%;
                height: 200px;
            }
            .guru-content{
                padding: 10px;
            }
            .guru-content h3{
                font-size: 16px;
            }
            .guru-content p{
                font-size: 14px;
            }
            .jabatan{
                font-size: 12px;
            }
            .guru-action{
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            .read-more{
                font-size: 12px;
            }
            .edit-btn, .delete-btn{
                padding: 5px 10px;
                font-size: 10px;
            }
            .no-guru{
                font-size: 14px;
                margin-top: 30px;
            }
            .teks-kecil{
                font-size: 10px;
            }
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Kelola Tenaga Pendidikan</h1>
        <div class="top-buttons">
            <a href="tambah_tenaga.php" class="btn">Tambah Data</a>
            <button class="btn" onclick="toggleEditMode()">Edit Mode</button>
        </div>
        <div class="guru-grid">
            <?php
            $query = "SELECT * FROM tb_guru WHERE jabatan='tenaga_pendidikan'";
            $result = mysqli_query($host, $query);
            if ($result && mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $imgSrc = !empty($row['gambar_guru']) ? "upload/" . htmlspecialchars($row['gambar_guru']) : "upload/default.jpg";
                    $gambar = !empty($row['gambar_guru']) ? "upload/" . htmlspecialchars($row['gambar_guru']) : "";
                    $judul = htmlspecialchars($row['nama_guru']);
                    $matapelajaran = htmlspecialchars($row['matapelajaran']);
                    $jabatan = htmlspecialchars($row['jabatan']);
                    echo '<div class="guru-card">';
                    echo "<img src='" . $imgSrc . "' alt='" . $judul ."'>";
                    echo '<div class="guru-content">';
                    echo '<h3>' . $judul . '</h3>';
                    echo '<p>' . limit_words($matapelajaran, 15) . '</p>';
                    echo '<div class="jabatan">' . ' - ' . $jabatan . '</div>';
                    echo '<div class="guru-action">';
                    echo '<a href="detail_guru_admin.php?id=' . $row['id_guru'] . '" class="read-more">Baca Selengkapnya</a>';
                    echo '<div class="action-buttons" style="display: none;">';
                    echo '<a href="edit_tenaga.php?id_guru=' . $row['id_guru'] . '" class="edit-btn">Edit</a>';
                    echo '<a href="hapus_tenaga.php?id_guru=' . $row['id_guru'] . '" class="delete-btn" onclick="return confirm(\'Apakah Anda yakin ingin menghapus tenaga pendidikan ini?\')">Hapus</a>';
                    echo '</div>';
                    echo '</div>';
                    echo '</div>';
                    echo '</div>';
                }
            } else {
                echo '<div class="no-guru">Belum ada tenaga pendidikan tersedia.</div>';
            }
            ?>
        </div>
    </div>
    <script>
        function toggleEditMode() {
            const actionButtons = document.querySelectorAll('.action-buttons');
            actionButtons.forEach(btn => {
                btn.style.display = btn.style.display === 'none' ? 'block' : 'none';
            });
        }
    </script>
</body>
</html>

