<?php
// Mulai session untuk pesan sukses atau error
session_start();
include 'connection.php'; // Memasukkan file connection.php untuk koneksi ke database

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nik = $_POST['nik'];
    $nama = $_POST['nama'];
    $kodedealer = $_POST['kodedealer'];
    $password = $_POST['password']; // Password yang diinput user

    // Cek apakah NIK sudah ada di database
    $stmt = $conn->prepare("SELECT nik FROM account WHERE nik = ?");
    $stmt->bind_param("s", $nik);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $_SESSION['error'] = "NIK sudah terdaftar!";
    } else {
        // Hashing password sebelum disimpan ke database
        $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

        // Menyimpan NIK dan password yang sudah di-hash ke database
        $stmt = $conn->prepare("INSERT INTO account (nik, nama, kodedealer, pass) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $nik, $nama, $kodedealer, $hashedPassword);

        if ($stmt->execute()) {
            $_SESSION['success'] = "Akun berhasil dibuat!";
        } else {
            $_SESSION['error'] = "Terjadi kesalahan saat menyimpan data.";
        }
    }
    header("Location: register.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pendaftaran Akun</title>
    <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-6 col-md-8 mt-5">
                <div class="card">
                    <div class="card-body">
                        <h3 class="text-center">Pendaftaran Akun</h3>

                        <!-- Pesan sukses atau error -->
                        <?php if (isset($_SESSION['success'])): ?>
                            <div class="alert alert-success">
                                <?= $_SESSION['success']; unset($_SESSION['success']); ?>
                            </div>
                        <?php elseif (isset($_SESSION['error'])): ?>
                            <div class="alert alert-danger">
                                <?= $_SESSION['error']; unset($_SESSION['error']); ?>
                            </div>
                        <?php endif; ?>

                        <form action="register.php" method="POST">
                            <div class="mb-3">
                                <label for="nik" class="form-label">NIK</label>
                                <input type="text" class="form-control" id="nik" name="nik" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="nama" class="form-label">Nama</label>
                                <input type="text" class="form-control" id="nama" name="nama" required>
                            </div>

                            <div class="mb-3">
                                <label for="kodedealer" class="form-label">Kode Dealer</label>
                                <input type="text" class="form-control" id="kodedealer" name="kodedealer" required>
                            </div>

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

                            <button type="submit" class="btn btn-primary w-100">Buat Akun</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>

</html>
