<?php
session_start();
include 'includes/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = $_POST['nama'] ?? '';
    $no_ic = $_POST['no_ic'] ?? '';
    $jawatan = $_POST['jawatan'] ?? '';
    $no_telefon = $_POST['no_telefon'] ?? '';
    $unit_jabatan = $_POST['unit_jabatan'] ?? '';
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $jenis_admin = 'utama';

    $stmt = $conn->prepare("INSERT INTO admin (nama, no_ic, jawatan, no_telefon, unit_jabatan, password, jenis_admin) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssss", $nama, $no_ic, $jawatan, $no_telefon, $unit_jabatan, $password, $jenis_admin);
    
    if ($stmt->execute()) {
        echo "<script>alert('Admin Utama berjaya didaftarkan!'); window.location='login_admin.php';</script>";
    } else {
        echo "Ralat: " . $stmt->error;
    }
}
?>

<form method="post">
    <h2>Daftar Admin Utama</h2>
    <label>Nama Penuh:</label><br>
    <input type="text" name="nama" required><br><br>

    <label>No IC:</label><br>
    <input type="text" name="no_ic" required><br><br>

    <label>Jawatan:</label><br>
    <input type="text" name="jawatan" required><br><br>

    <label>No Telefon:</label><br>
    <input type="text" name="no_telefon" required><br><br>

    <label>Unit/Jabatan:</label><br>
    <input type="text" name="unit_jabatan" required><br><br>

    <label>Kata Laluan:</label><br>
    <input type="password" name="password" required><br><br>

    <button type="submit">Daftar</button>
</form>
