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

// If already logged in, redirect to dashboard
if (isLoggedIn()) {
    header('Location: ' . APP_URL . '/admin/dashboard.php');
    exit;
}

$error = '';
$success = '';

// Handle registration
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $password_confirm = $_POST['password_confirm'] ?? '';
    $name = trim($_POST['name'] ?? '');
    
    // Validation
    if (empty($email) || empty($password) || empty($password_confirm) || empty($name)) {
        $error = 'Toate câmpurile sunt obligatorii';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Email invalid';
    } elseif (strlen($password) < 6) {
        $error = 'Parola trebuie să aibă cel puțin 6 caractere';
    } elseif ($password !== $password_confirm) {
        $error = 'Parolele nu se potrivesc';
    } else {
        // Check if email already exists
        $escaped_email = escape($email);
        $result = $db->query("SELECT id FROM users WHERE email = '$escaped_email' LIMIT 1");
        
        if ($result && $result->num_rows > 0) {
            $error = 'Acest email este deja înregistrat';
        } else {
            // Create user
            $password_hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
            $escaped_name = escape($name);
            $escaped_hash = escape($password_hash);
            
            $insert_query = "INSERT INTO users (email, password, name, role, active) VALUES ('$escaped_email', '$escaped_hash', '$escaped_name', 'user', 1)";
            
            if ($db->query($insert_query)) {
                $success = 'Cont creat cu succes! Acum te poți loga.';
                // Clear form
                $email = '';
                $name = '';
            } else {
                $error = 'Eroare la creare cont. Încearcă din nou.';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo APP_NAME; ?> — Înregistrare</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Syne:wght@400;500;600;700&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
    <style>
        *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
        :root{
          --bg:#0E0F11;--bg2:#141517;--bg3:#1A1C1F;--bg4:#222428;
          --border:#2A2D32;--border2:#333740;--text:#F0ECE3;--text2:#9A9590;
          --accent:#D4694A;--accent2:#E8856A;--green:#3DB87A;--red:#E05252;
        }
        html,body{height:100%;background:var(--bg);color:var(--text);font-family:'DM Sans',sans-serif;margin:0;padding:0}
        body{display:flex;align-items:center;justify-content:center}
        
        .login-container{width:100%;max-width:420px;padding:20px}
        .login-box{background:var(--bg2);border:1px solid var(--border);border-radius:14px;padding:40px;box-shadow:0 20px 60px rgba(0,0,0,0.3)}
        
        .logo-section{text-align:center;margin-bottom:30px}
        .logo-text{font-family:'Syne',sans-serif;font-size:28px;font-weight:700;margin-bottom:6px}
        .logo-text span{color:var(--accent)}
        .logo-sub{font-size:13px;color:var(--text2);letter-spacing:0.3px}
        
        .form-group{margin-bottom:14px}
        .form-label{display:block;font-size:12px;font-weight:600;color:var(--text2);margin-bottom:6px;text-transform:uppercase;letter-spacing:0.4px}
        .form-input{width:100%;padding:11px 14px;background:var(--bg3);border:1px solid var(--border);color:var(--text);border-radius:8px;font-family:'DM Sans',sans-serif;font-size:14px;outline:none;transition:border-color 0.15s}
        .form-input:focus{border-color:var(--accent)}
        .form-input::placeholder{color:var(--text2)}
        
        .btn-login{width:100%;padding:11px 16px;background:var(--accent);color:white;border:none;border-radius:8px;font-family:'DM Sans',sans-serif;font-size:14px;font-weight:600;cursor:pointer;transition:background 0.15s;margin-top:20px}
        .btn-login:hover{background:var(--accent2)}
        
        .error-msg{background:rgba(224,82,82,0.1);border:1px solid rgba(224,82,82,0.2);color:#E05252;padding:10px 14px;border-radius:8px;font-size:13px;margin-bottom:16px}
        
        .success-msg{background:rgba(61,184,122,0.1);border:1px solid rgba(61,184,122,0.2);color:#3DB87A;padding:10px 14px;border-radius:8px;font-size:13px;margin-bottom:16px}
        
        .form-footer{text-align:center;margin-top:20px;font-size:13px}
        .form-footer a{color:var(--accent);text-decoration:none;transition:color 0.15s}
        .form-footer a:hover{color:var(--accent2)}
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-box">
            <div class="logo-section">
                <div class="logo-text">Blid<span>Store</span></div>
                <div class="logo-sub">Magazin online</div>
            </div>
            
            <?php if ($error): ?>
                <div class="error-msg"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="success-msg"><?php echo htmlspecialchars($success); ?></div>
                <p style="text-align:center;margin-top:20px;">
                    <a href="login.php" style="color:var(--accent);text-decoration:none;">← Înapoi la conectare</a>
                </p>
            <?php else: ?>
                <form method="POST">
                    <div class="form-group">
                        <label class="form-label">Nume Complet</label>
                        <input type="text" name="name" class="form-input" placeholder="Ion Popescu" value="<?php echo htmlspecialchars($name); ?>" required autofocus>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-input" placeholder="email@example.com" value="<?php echo htmlspecialchars($email); ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Parolă</label>
                        <input type="password" name="password" class="form-input" placeholder="Minimum 6 caractere" required>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Confirmă Parolă</label>
                        <input type="password" name="password_confirm" class="form-input" placeholder="Repetă parola" required>
                    </div>
                    
                    <button type="submit" class="btn-login">Înregistrare</button>
                    
                    <div class="form-footer">
                        Ai deja cont? <a href="login.php">Conectează-te →</a>
                    </div>
                </form>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
