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

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

// Koneksi database
try {
    $pdo = new PDO("mysql:host=localhost;dbname=ujian_system;charset=utf8", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Koneksi database gagal: " . $e->getMessage());
}

// Cari penguji_id
$penguji_id = null;

// Method 1: Dari session penguji_id
if (!empty($_SESSION['penguji_id'])) {
    $penguji_id = $_SESSION['penguji_id'];
}

// Method 2: Cari berdasarkan username (NIP) dari session
if (empty($penguji_id) && !empty($_SESSION['user_name'])) {
    $stmt = $pdo->prepare("SELECT id, nama FROM penguji WHERE nip = ?");
    $stmt->execute([$_SESSION['user_name']]);
    $penguji_data = $stmt->fetch();
    if ($penguji_data) {
        $penguji_id = $penguji_data['id'];
        $_SESSION['penguji_id'] = $penguji_id;
        $_SESSION['penguji_nama'] = $penguji_data['nama'];
    }
}

// Method 3: Cari berdasarkan user_id
if (empty($penguji_id) && !empty($_SESSION['user_id'])) {
    $stmt = $pdo->prepare("
        SELECT p.id, p.nama 
        FROM penguji p 
        WHERE p.nip IN (SELECT username FROM users WHERE id = ?)
           OR p.id = ?
    ");
    $stmt->execute([$_SESSION['user_id'], $_SESSION['user_id']]);
    $penguji_data = $stmt->fetch();
    if ($penguji_data) {
        $penguji_id = $penguji_data['id'];
        $_SESSION['penguji_id'] = $penguji_id;
        $_SESSION['penguji_nama'] = $penguji_data['nama'];
    }
}

if (empty($penguji_id)) {
    die("Error: Data Penguji tidak ditemukan");
}

// Ambil data penguji saat ini
$stmt = $pdo->prepare("SELECT * FROM penguji WHERE id = ?");
$stmt->execute([$penguji_id]);
$penguji = $stmt->fetch();

if (!$penguji) {
    die("Error: Penguji tidak ditemukan");
}

$error_message = "";

// Proses update profil
if (isset($_POST['update_profile'])) {
    $nama_baru = trim($_POST['nama']);
    
    if (empty($nama_baru)) {
        $error_message = "Nama tidak boleh kosong";
    } else {
        try {
            // Update data penguji di tabel penguji (hanya nama, NIP tidak diupdate)
            $stmt = $pdo->prepare("UPDATE penguji SET nama = ? WHERE id = ?");
            $stmt->execute([$nama_baru, $penguji_id]);
            
            // Update session
            $_SESSION['penguji_nama'] = $nama_baru;
            
            // Redirect ke dashboard setelah berhasil
            header("Location: dashboard_penguji.php?success=1");
            exit();
            
        } catch (PDOException $e) {
            $error_message = "Error: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Profil Penguji - SOCA OSCE</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 40px 20px;
        }
        
        .container {
            max-width: 600px;
            margin: 0 auto;
        }
        
        .card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .card-header {
            text-align: center;
            margin-bottom: 2rem;
        }
        
        .card-header h1 {
            color: #6a11cb;
            font-size: 1.8rem;
            margin-bottom: 0.5rem;
        }
        
        .card-header p {
            color: #7f8c8d;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: #2c3e50;
        }
        
        input {
            width: 100%;
            padding: 0.8rem 1rem;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s;
        }
        
        input:focus {
            outline: none;
            border-color: #6a11cb;
            box-shadow: 0 0 0 3px rgba(106, 17, 203, 0.1);
        }
        
        input:disabled {
            background: #e9ecef;
            color: #6c757d;
            cursor: not-allowed;
        }
        
        .btn {
            width: 100%;
            padding: 1rem;
            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(106, 17, 203, 0.3);
        }
        
        .btn-secondary {
            background: #95a5a6;
            margin-top: 1rem;
        }
        
        .btn-secondary:hover {
            background: #7f8c8d;
        }
        
        .alert {
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .info-text {
            background: #e8f4fd;
            padding: 1rem;
            border-radius: 8px;
            margin-top: 1.5rem;
            font-size: 0.9rem;
            color: #0c5460;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .info-text i {
            font-size: 1.2rem;
        }
        
        @media (max-width: 768px) {
            .card {
                padding: 1.5rem;
            }
            
            .card-header h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <div class="card-header">
                <h1><i class="fas fa-user-edit"></i> Edit Profil</h1>
                <p>Ubah nama yang ditampilkan di dashboard</p>
            </div>
            
            <?php if ($error_message): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle"></i>
                    <?php echo $error_message; ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label><i class="fas fa-user"></i> Nama Lengkap</label>
                    <input type="text" name="nama" value="<?php echo htmlspecialchars($penguji['nama']); ?>" required autofocus>
                </div>
                
                <div class="form-group">
                    <label><i class="fas fa-id-card"></i> NIP / Username</label>
                    <input type="text" name="nip" value="<?php echo htmlspecialchars($penguji['nip']); ?>" disabled>
                    <small style="color: #7f8c8d; display: block; margin-top: 0.25rem;">
                        <i class="fas fa-info-circle"></i> NIP tidak dapat diubah. Hubungi administrator jika perlu perubahan.
                    </small>
                </div>
                
                <button type="submit" name="update_profile" class="btn">
                    <i class="fas fa-save"></i> Simpan Perubahan
                </button>
                
                <a href="dashboard_penguji.php" class="btn btn-secondary" style="display: block; text-align: center; text-decoration: none;">
                    <i class="fas fa-arrow-left"></i> Kembali ke Dashboard
                </a>
            </form>
            
            <div class="info-text">
                <i class="fas fa-lightbulb"></i>
                <div>
                    <strong>Catatan:</strong> Perubahan nama akan langsung terlihat di dashboard setelah menyimpan.
                </div>
            </div>
        </div>
    </div>
</body>
</html>