<?php
session_start();
include 'koneksi.php';

// Jika sudah login arahkan ke dashboard
if(isset($_SESSION['user_id'])){
    header("Location:index.php");
    exit;
}

$error="";

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

$name = trim($_POST['name']);
$hp = trim($_POST['hp']);
$email = strtolower(trim($_POST['email']));
$password = $_POST['password'];

if(empty($name) || empty($email) || empty($password)){
$error="Semua kolom harus diisi!";
}

elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$error="Format email tidak valid!";
}

else{

// cek email sudah ada
$stmt=$conn->prepare("SELECT id FROM users WHERE email=?");
$stmt->bind_param("s",$email);
$stmt->execute();
$result=$stmt->get_result();


if($result->num_rows>0){

$error="Email sudah terdaftar!";

}else{

// hash password
$hash=password_hash($password,PASSWORD_DEFAULT);

// insert user
$stmt2=$conn->prepare("INSERT INTO users(name,no_hp,email,password) VALUES(?,?,?,?)");
$stmt2->bind_param("ssss",$name,$hp,$email,$hash);

if($stmt2->execute()){

header("Location:login.php?success=1");
exit;

}else{

$error="Gagal menyimpan data!";
}

}

}

}

?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Daftar Akun - SILAKU</title>
    
    <!-- Font & Icons -->
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/@phosphor-icons/web"></script>

    <style>
        :root {
            --primary: #ef4444;
            --primary-dark: #b91c1c;
            --glass-bg: rgba(255, 255, 255, 0.05);
            --glass-border: rgba(255, 255, 255, 0.1);
            --text-color: #000000;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Plus Jakarta Sans', sans-serif;
        }

        body {
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            position: relative;
        }

        /* BACKGROUND FOTO ANDA */
        .background-image {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: url('') center/cover no-repeat;
            z-index: 1;
        }

   
        /* Animated Background Blobs */
        .blob {
            position: absolute;
            filter: blur(80px);
            z-index: 3;
            opacity: 0.4;
            animation: floatBlob 10s infinite alternate;
        }

        .blob-1 {
            top: 10%;
            left: 20%;
            width: 300px;
            height: 300px;
            background: #000000;
            border-radius: 50%;
        }

        .blob-2 {
            bottom: 10%;
            right: 20%;
            width: 350px;
            height: 350px;
            background: #7f1d1d;
            border-radius: 50%;
            animation-delay: -5s;
        }

        @keyframes floatBlob {
            0% { transform: translate(0, 0) scale(1); }
            100% { transform: translate(30px, 50px) scale(1.1); }
        }

        /* REGISTER CARD */
        .register-card {
            width: 100%;
            max-width: 420px;
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 24px;
            padding: 40px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
            z-index: 10;
            position: relative;
        }

        @keyframes slideUp {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* HEADER */
        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .logo-container {
            width: 70px;
            height: 70px;
            margin: 0 auto 15px;
            box-shadow: 0 10px 15px -3px rgba(239, 68, 68, 0.4);
            border: 3px solid rgba(0, 0, 0, 0.2);
            border-radius: 50%;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            animation: floatLogo 3s ease-in-out infinite;
        }

        @keyframes floatLogo {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-8px); }
        }

        .logo-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 50%;
        }

        .header h1 {
            color: black;
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 5px;
        }

        .header p {
            color: rgba(0, 0, 0, 0.8);
            font-size: 14px;
        }

        /* FORM ELEMENTS */
        .input-group {
            position: relative;
            margin-bottom: 20px;
        }

        .input-field {
            width: 100%;
            padding: 16px 16px 16px 45px;
            background: rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            color: white;
            font-size: 15px;
            outline: none;
            transition: all 0.3s ease;
        }

        .input-field::placeholder {
            color: rgba(0, 0, 0, 0.5);
        }

        .input-field:focus {
            background: rgba(0, 0, 0, 0.15);
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.2);
        }

        .input-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: rgb(0, 0, 0);
            font-size: 20px;
            transition: color 0.3s;
        }

        .input-field:focus + .input-icon {
            color: var(--primary);
        }

        /* BUTTON */
        .btn-register {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-register:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px -5px rgba(239, 68, 68, 0.4);
        }

        /* ERROR MESSAGE */
        .error-msg {
            background: rgba(239, 68, 68, 0.15);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fca5a5;
            padding: 12px;
            border-radius: 10px;
            font-size: 13px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 8px;
            animation: shake 0.5s ease-in-out;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-5px); }
            75% { transform: translateX(5px); }
        }

        /* FOOTER */
        .footer {
            margin-top: 25px;
            text-align: center;
            font-size: 14px;
            color: rgb(0, 0, 0);
        }

        .footer a {
            color: white;
            text-decoration: none;
            font-weight: 600;
            transition: color 0.2s;
        }

        .footer a:hover {
            color: var(--primary);
        }

        /* SUCCESS MESSAGE */
        .success-msg {
            background: rgba(34, 197, 94, 0.15);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #000000;
            padding: 12px;
            border-radius: 10px;
            font-size: 13px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        @media (max-width: 480px) {
            .register-card {
                padding: 30px 20px;
                margin: 20px;
            }
        }
    </style>
</head>

<body>

    <!-- BACKGROUND FOTO ANDA -->
    <div class="background-image"></div>
    
    <!-- OVERLAY GELAP -->
    <div class="overlay"></div>

    <!-- Background Elements -->
    <div class="blob blob-1"></div>
    <div class="blob blob-2"></div>

    <div class="register-card">
        <div class="header">
            <!-- FOTO ANDA SEBAGAI LOGO -->
            <div class="logo-container">
                <img src="silakumanagefinance .png" alt="Logo Silaku">
            </div>
            <h1>Buat Akun</h1>
            <p>Mulai kelola keuangan Anda sekarang</p>
        </div>

        <div class="content">
            <?php if(isset($error)): ?>
            <div class="error-msg">
                <i class="ph ph-warning-circle"></i>
                <?php echo $error; ?>
            </div>
            <?php endif; ?>

            <?php if(isset($_GET['success'])): ?>
            <div class="success-msg">
                <i class="ph ph-check-circle"></i>
                Pendaftaran berhasil! Silakan login.
            </div>
            <?php endif; ?>

            <form method="POST">
                <div class="input-group">
                    <input type="text" name="name" class="input-field" placeholder="Nama Lengkap" required>
                    <i class="ph ph-user input-icon"></i>
                </div>
          <div class="input-group">
    <input type="text" 
           name="hp" 
           class="input-field" 
           placeholder="Nomor HP (contoh: 08123456789)" 
           required>

    <i class="ph ph-phone input-icon"></i>
</div>
               <div class="input-group">
    <input type="email" name="email" class="input-field" placeholder="Alamat Email" required>
    <i class="ph ph-envelope input-icon"></i>
</div>

                <div class="input-group">
                    <input type="password" name="password" class="input-field" placeholder="Password" required>
                    <i class="ph ph-lock-key input-icon"></i>
                </div>

                <button type="submit" class="btn-register" name="daftar">
                    <span>Buat Akun</span>
                    <i class="ph ph-arrow-right"></i>
                </button>
            </form>

            <div class="footer">
                Sudah punya akun? <a href="login.php">Login disini</a>
            </div>
        </div>
    </div>

</body>
</html>