<?php
/**
 * PERBAIKAN UTAMA: 
 * Gunakan '../' untuk keluar dari folder 'pelapor' 
 * lalu masuk ke folder 'includes' untuk memanggil config.php
 */
include '../includes/config.php';

$status = ""; // Variabel untuk menampung status

if (isset($_POST['register'])) {
    $nama     = $_POST['nama_lengkap'];
    $email    = $_POST['email'];
    $password = $_POST['password'];
    $confirm  = $_POST['confirm_password'];

    if ($password !== $confirm) {
        $status = "password_salah";
    } else {
        try {
            // Mengecek apakah email sudah ada di database
            $stmt = $pdo->prepare("SELECT id FROM pelapor WHERE email = ?");
            $stmt->execute([$email]);
            
            if ($stmt->fetch()) {
                $status = "email_ada";
            } else {
                // Enkripsi password sebelum disimpan ke database
                $hashed = password_hash($password, PASSWORD_DEFAULT);
                $ins = $pdo->prepare("INSERT INTO pelapor (nama_lengkap, email, password) VALUES (?, ?, ?)");
                
                if ($ins->execute([$nama, $email, $hashed])) {
                    $status = "sukses";
                } else {
                    $status = "error";
                }
            }
        } catch (PDOException $e) {
            // Jika ada error database, status menjadi error
            $status = "error";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Daftar Pelapor - Tangkis BPS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <style>
        body { background: linear-gradient(135deg, #0d6efd 0%, #6610f2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; font-family: 'Segoe UI', sans-serif; }
        .card-regis { background: white; border-radius: 25px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); width: 100%; max-width: 450px; padding: 40px; border: none; }
        .btn-primary { border-radius: 12px; padding: 12px; font-weight: bold; background: #0d6efd; border: none; transition: 0.3s; }
        .btn-primary:hover { background: #0b5ed7; transform: translateY(-2px); }
        .form-control { border-radius: 10px; padding: 12px; background-color: #f8f9fa; }
    </style>
</head>
<body>
    <div class="card-regis">
        <div class="text-center mb-4">
            <i class="fas fa-user-plus fa-3x text-primary mb-3"></i>
            <h3 class="fw-bold">Buat Akun Pelapor</h3>
            <p class="text-muted small">Silakan daftar untuk akses pengaduan aman</p>
        </div>

        <form method="POST">
    <div class="mb-3">
        <label class="small fw-bold">Nama Lengkap</label>
        <input type="text" name="nama_lengkap" class="form-control" placeholder="Nama Anda" required 
               oninvalid="this.setCustomValidity('Mohon isi nama lengkap Anda')" 
               oninput="this.setCustomValidity('')">
    </div>
    <div class="mb-3">
        <label class="small fw-bold">Email</label>
        <input type="email" name="email" class="form-control" placeholder="email@contoh.com" required 
               oninvalid="this.setCustomValidity('Mohon isi alamat email yang valid')" 
               oninput="this.setCustomValidity('')">
    </div>
    <div class="mb-3">
        <label class="small fw-bold">Password</label>
        <input type="password" name="password" class="form-control" placeholder="******" required 
               oninvalid="this.setCustomValidity('Mohon isi kata sandi Anda')" 
               oninput="this.setCustomValidity('')">
    </div>
    <div class="mb-4">
        <label class="small fw-bold">Ulangi Password</label>
        <input type="password" name="confirm_password" class="form-control" placeholder="******" required 
               oninvalid="this.setCustomValidity('Mohon ulangi kata sandi Anda')" 
               oninput="this.setCustomValidity('')">
    </div>
    <button type="submit" name="register" class="btn btn-primary w-100 shadow">Daftar Sekarang <i class="fas fa-arrow-right ms-2"></i></button>
</form>
    <div class="text-center mt-4 small">
            Sudah punya akun? <a href="login_pelapor.php" class="text-decoration-none fw-bold">Login Masuk</a>
    </div>
    </div>

    <script>
        // Notifikasi SweetAlert berdasarkan variabel $status dari PHP
        <?php if($status == "sukses"): ?>
            Swal.fire({
                icon: 'success',
                title: 'Berhasil!',
                text: 'Registrasi Berhasil! Silakan Login.',
                confirmButtonColor: '#0d6efd'
            }).then(() => {
                window.location.href = 'login_pelapor.php';
            });
        <?php elseif($status == "password_salah"): ?>
            Swal.fire({
                icon: 'error',
                title: 'Gagal',
                text: 'Konfirmasi password tidak cocok!',
                confirmButtonColor: '#d33'
            });
        <?php elseif($status == "email_ada"): ?>
            Swal.fire({
                icon: 'warning',
                title: 'Email Terdaftar',
                text: 'Email ini sudah digunakan, silakan gunakan email lain.',
                confirmButtonColor: '#f8bb86'
            });
        <?php elseif($status == "error"): ?>
            Swal.fire({
                icon: 'error',
                title: 'Terjadi Kesalahan',
                text: 'Gagal menyimpan data ke database.',
                confirmButtonColor: '#d33'
            });
        <?php endif; ?>
    </script>
</body>
</html>