<?php
require_once '../includes/auth.php';

if (!isAdmin()) {
    redirect('../login.php');
}

$success = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['tambah_penguji'])) {
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    $nama_lengkap = sanitize($_POST['nama_lengkap']);
    
    // Cek apakah username sudah ada
    $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
    $stmt->execute([$username]);
    
    if ($stmt->rowCount() > 0) {
        $error = "Username sudah digunakan!";
    } else {
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("INSERT INTO users (username, password, role, nama_lengkap) VALUES (?, ?, 'penguji', ?)");
        
        if ($stmt->execute([$username, $hashed_password, $nama_lengkap])) {
            $success = "Penguji berhasil ditambahkan!";
        } else {
            $error = "Gagal menambahkan penguji!";
        }
    }
}

// Hapus penguji
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    
    // Cek apakah penguji sudah melakukan penilaian
    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM penilaian WHERE penguji_id = ?");
    $stmt->execute([$id]);
    $total_penilaian = $stmt->fetch()['total'];
    
    if ($total_penilaian > 0) {
        $error = "Tidak dapat menghapus penguji yang sudah melakukan penilaian!";
    } else {
        $stmt = $pdo->prepare("DELETE FROM users WHERE id = ? AND role = 'penguji'");
        if ($stmt->execute([$id])) {
            $success = "Penguji berhasil dihapus!";
        } else {
            $error = "Gagal menghapus penguji!";
        }
    }
}

// Reset password penguji
if (isset($_GET['reset_password'])) {
    $id = $_GET['reset_password'];
    $default_password = password_hash('password', PASSWORD_DEFAULT);
    
    $stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ? AND role = 'penguji'");
    if ($stmt->execute([$default_password, $id])) {
        $success = "Password berhasil direset ke 'password'!";
    } else {
        $error = "Gagal mereset password!";
    }
}

// Ambil data penguji
$stmt = $pdo->query("SELECT * FROM users WHERE role = 'penguji' ORDER BY nama_lengkap");
$penguji = $stmt->fetchAll();
?>

<?php include 'header.php'; ?>

<div class="content">
    <h2>Kelola Penguji</h2>
    
    <?php if ($success): ?>
        <div class="alert alert-success"><?php echo $success; ?></div>
    <?php endif; ?>
    
    <?php if ($error): ?>
        <div class="alert alert-error"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <!-- Form Tambah Penguji -->
    <div class="card">
        <h3>Tambah Penguji Baru</h3>
        <form method="POST">
            <div class="form-row">
                <div class="form-group">
                    <label for="username">Username:</label>
                    <input type="text" id="username" name="username" required>
                </div>
                
                <div class="form-group">
                    <label for="password">Password:</label>
                    <input type="password" id="password" name="password" value="password" required>
                    <small class="form-hint">Default password: password</small>
                </div>
            </div>
            
            <div class="form-group">
                <label for="nama_lengkap">Nama Lengkap:</label>
                <input type="text" id="nama_lengkap" name="nama_lengkap" placeholder="Contoh: Dr. Ahmad Wijaya, M.Kes" required>
            </div>
            
            <button type="submit" name="tambah_penguji" class="btn btn-primary">Tambah Penguji</button>
        </form>
    </div>
    
    <!-- Daftar Penguji -->
    <div class="card">
        <h3>Daftar Penguji</h3>
        <div class="table-container">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Username</th>
                        <th>Nama Lengkap</th>
                        <th>Tanggal Dibuat</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($penguji)): ?>
                        <tr>
                            <td colspan="5" style="text-align: center;">Belum ada penguji</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($penguji as $index => $p): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td><?php echo $p['username']; ?></td>
                                <td><?php echo $p['nama_lengkap']; ?></td>
                                <td><?php echo formatTanggal($p['created_at']); ?></td>
                                <td>
                                    <div class="action-buttons">
                                        <a href="?reset_password=<?php echo $p['id']; ?>" class="btn btn-warning btn-sm" onclick="return confirm('Reset password ke default?')">
                                            <i class="fas fa-key"></i> Reset Password
                                        </a>
                                        <a href="?hapus=<?php echo $p['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus penguji ini?')">
                                            <i class="fas fa-trash"></i> Hapus
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Informasi Login -->
    <div class="card">
        <h3>Informasi Login Penguji</h3>
        <div class="info-box">
            <p><strong>Username:</strong> penguji1, penguji2, penguji3, dll.</p>
            <p><strong>Password:</strong> password (untuk semua akun)</p>
            <p><strong>Default Password:</strong> password</p>
        </div>
    </div>
</div>

<style>
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.action-buttons {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.info-box {
    background: #e8f4fd;
    border: 1px solid #b8daff;
    border-radius: 6px;
    padding: 1.5rem;
}

.info-box p {
    margin: 0.5rem 0;
    color: #004085;
}

.btn-warning {
    background: #ffc107;
    color: #212529;
    border: 1px solid #ffc107;
}

.btn-warning:hover {
    background: #e0a800;
    border-color: #e0a800;
}

@media (max-width: 768px) {
    .form-row {
        grid-template-columns: 1fr;
    }
    
    .action-buttons {
        flex-direction: column;
    }
}
</style>

<?php include 'footer.php'; ?>