<?php
// admin/create_admin.php

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

// Cek apakah admin sudah ada
$stmt = $pdo->query("SELECT * FROM users WHERE role = 'admin'");
$admin = $stmt->fetch();

if ($admin) {
    echo "<h2>Admin sudah ada:</h2>";
    echo "Username: " . $admin['username'] . "<br>";
    echo "Role: " . $admin['role'] . "<br>";
    echo "ID: " . $admin['id'] . "<br>";
} else {
    // Buat admin baru
    $hashed_password = password_hash('admin123', PASSWORD_DEFAULT);
    $stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'admin')");
    $stmt->execute(['admin', $hashed_password]);
    
    echo "<h2>Admin berhasil dibuat:</h2>";
    echo "Username: admin<br>";
    echo "Password: admin123<br>";
    echo "Role: admin<br>";
}

echo "<br><a href='../login.php'>Login Sekarang</a>";
?>