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

// Hanya admin utama boleh daftar admin
if (!isset($_SESSION['id_admin']) || ($_SESSION['jenis_admin'] ?? '') != 'utama') {
    die("Akses ditolak!");
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = trim($_POST['nama']);
    $no_ic = trim($_POST['no_ic']);
    $jawatan = trim($_POST['jawatan']);
    $no_telefon = trim($_POST['no_telefon']);
    $unit_jabatan = trim($_POST['unit_jabatan']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $jenis_admin = 'admin'; // Semua yang didaftarkan sini adalah admin biasa

    $stmt = mysqli_prepare($conn, "INSERT INTO admin (nama, no_ic, jawatan, no_telefon, unit_jabatan, password, jenis_admin) VALUES (?, ?, ?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, 'sssssss', $nama, $no_ic, $jawatan, $no_telefon, $unit_jabatan, $password, $jenis_admin);

    if (mysqli_stmt_execute($stmt)) {
        echo "<script>alert('Admin berjaya didaftarkan.'); window.location='senarai_admin.php';</script>";
        exit;
    } else {
        $error = "Ralat: Tidak dapat daftar admin.";
    }
}
?>

<style>
.main-content {
    margin-left: 200px; /* Lebar sidebar */
    padding: 120px 40px 40px 40px; /* Ruang atas supaya tak tutup header */
    max-width: 900px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 3px 12px rgba(0,0,0,0.12);
    font-size: 16px;
}

.main-content h3 {
    font-size: 36px;
    font-weight: 700;
    color: #3b0a77;
    margin-bottom: 30px;
    text-align: center;
}

.form-label {
    font-weight: 600;
    font-size: 16px;
}

.btn-primary {
    background-color: #3b0a77;
    border-color: #3b0a77;
    font-size: 18px;
    padding: 12px;
    width: 100%;
    border-radius: 6px;
}

.btn-primary:hover {
    background-color: #ff6f00;
    border-color: #ff6f00;
}

.alert-error {
    background-color: #f8d7da;
    color: #842029;
    border: 1px solid #f5c2c7;
    padding: 12px;
    margin-bottom: 20px;
    border-radius: 6px;
    max-width: 900px;
    margin-left: 200px;
}
</style>

<div class="main-content">
    <h3>Daftar Admin Baru</h3>

    <?php if ($error): ?>
        <div class="alert-error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="post" action="">
        <div class="mb-4">
            <label class="form-label">Nama Penuh</label>
            <input type="text" name="nama" class="form-control" required />
        </div>
        <div class="mb-4">
            <label class="form-label">No. Kad Pengenalan</label>
            <input type="text" name="no_ic" class="form-control" required />
        </div>
        <div class="mb-4">
            <label class="form-label">Jawatan</label>
            <input type="text" name="jawatan" class="form-control" required />
        </div>
        <div class="mb-4">
            <label class="form-label">No. Telefon</label>
            <input type="text" name="no_telefon" class="form-control" required />
        </div>
        <div class="mb-4">
            <label class="form-label">Unit / Jabatan</label>
            <input type="text" name="unit_jabatan" class="form-control" required />
        </div>
        <div class="mb-4">
            <label class="form-label">Kata Laluan</label>
            <input type="password" name="password" class="form-control" required />
        </div>

        <button type="submit" class="btn btn-primary">Daftar Admin</button>
    </form>
</div>

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