<?php
require_once __DIR__ . '/config/config.php';

$error = '';

if (isset($_POST['simpan'])) {

    $nama_lengkap = $_POST['nama_lengkap'] ?? '';
    $username     = $_POST['username'] ?? '';
    $password     = $_POST['password'] ?? '';
    $role         = $_POST['role'] ?? '';
    $status       = $_POST['status'] ?? 'aktif';

    if ($nama_lengkap === '' || $username === '' || $password === '' || $role === '') {
        $error = "Semua field wajib diisi!";
    } else {

        // cek username sudah ada
        $cek = $conn->prepare("SELECT id_user FROM users WHERE username = ?");
        $cek->bind_param("s", $username);
        $cek->execute();
        $cek->store_result();

        if ($cek->num_rows > 0) {
            $error = "Username sudah digunakan!";
        } else {

            $password_hash = password_hash($password, PASSWORD_DEFAULT);

            $stmt = $conn->prepare(
                "INSERT INTO users (nama_lengkap, username, password, role, status)
                 VALUES (?, ?, ?, ?, ?)"
            );

            $stmt->bind_param(
                "sssss",
                $nama_lengkap,
                $username,
                $password_hash,
                $role,
                $status
            );

            if ($stmt->execute()) {
                header("Location: users.php?ok=1");
                exit;
            } else {
                $error = "Gagal menyimpan user!";
            }
        }
    }
}
?>


<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Tambah User</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{
    background:#f4f6f9;
    padding:25px;
    font-family:"Segoe UI",sans-serif;
}
.card-custom{
    max-width:550px;
    margin:auto;
    background:#fff;
    padding:35px;
    border-radius:14px;
    box-shadow:0 8px 20px rgba(0,0,0,.08);
}
</style>
</head>

<body>

<div class="card-custom">
    <h4 class="mb-4">➕ Tambah User</h4>

    <?php if($error){ ?>
        <div class="alert alert-danger"><?= $error ?></div>
    <?php } ?>

    <form method="post">
        <div class="mb-3">
            <label class="form-label">Nama Lengkap *</label>
            <input type="text" name="nama_lengkap" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label">Username *</label>
            <input type="text" name="username" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label">Password *</label>
            <input type="password" name="password" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label">Role *</label>
            <select name="role" class="form-select" required>
                <option value="">-- Pilih Role --</option>
                <option value="admin">Admin</option>
                <option value="sekolah">sekolah</option>
            </select>
        </div>

        <div class="mb-4">
            <label class="form-label">Status</label>
            <select name="status" class="form-select">
                <option value="aktif">Aktif</option>
                <option value="nonaktif">Nonaktif</option>
            </select>
        </div>

        <div class="d-flex justify-content-between">
            <a href="users.php" class="btn btn-outline-secondary">
                ⬅ Kembali
            </a>
            <button name="simpan" class="btn btn-primary">
                💾 Simpan User
            </button>
        </div>
    </form>
</div>

</body>
</html>
