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

// Semak jika admin utama (contohnya id_admin = 1)
if ($_SESSION['id_admin'] != 1) {
    die("Akses ditolak!");
}

$success = $error = '';

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'];
    $kata_laluan = password_hash($_POST['kata_laluan'], PASSWORD_DEFAULT);

    $stmt = $conn->prepare("INSERT INTO admin (nama, no_ic, jawatan, no_telefon, unit_jabatan, kata_laluan) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $nama, $no_ic, $jawatan, $no_telefon, $unit_jabatan, $kata_laluan);

    if ($stmt->execute()) {
        $success = "Admin berjaya didaftarkan!";
    } else {
        $error = "Ralat: " . $stmt->error;
    }
}
?>
<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<title>Daftar Admin - e-SPEKTRA</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-4">
    <h3>Daftar Admin Baru</h3>
    <?php if ($success): ?><div class="alert alert-success"><?= $success ?></div><?php endif; ?>
    <?php if ($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
    <form method="POST">
        <div class="mb-3">
            <label>Nama Penuh</label>
            <input type="text" name="nama" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>No. Kad Pengenalan</label>
            <input type="text" name="no_ic" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>Jawatan</label>
            <input type="text" name="jawatan" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>No. Telefon</label>
            <input type="text" name="no_telefon" class="form-control">
        </div>
        <div class="mb-3">
            <label>Unit / Jabatan</label>
            <input type="text" name="unit_jabatan" class="form-control">
        </div>
        <div class="mb-3">
            <label>Kata Laluan</label>
            <input type="password" name="kata_laluan" class="form-control" required>
        </div>
        <button type="submit" class="btn btn-success">Daftar Admin</button>
    </form>
</div>
</body>
</html>
