<?php
session_start();

// Database connection
$host = "localhost";
$username = "root";
$password = "";
$database = "myfintech";
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
}

// Handle Registration
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = trim($_POST['username']);
    $phone = trim($_POST['phone']);
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);
    $confirm_password = trim($_POST['confirm_password']);
    $role = 'user'; // Default role for new registrations
    $balance = 0.00; // Initial balance

    // Validation
    if (empty($username) || empty($phone) || empty($email) || empty($password) || empty($confirm_password)) {
        $error = "All fields are required";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Invalid email format";
    } elseif ($password !== $confirm_password) {
        $error = "Passwords do not match";
    } else {
        // Check if phone or email already exists
        $stmt = $conn->prepare("SELECT id FROM users WHERE phone = ? OR email = ?");
        $stmt->bind_param("ss", $phone, $email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            $error = "Phone number or email already registered";
        } else {
            // Insert new user
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $conn->prepare("INSERT INTO users (username, phone, email, password, balance, role, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())");
            $stmt->bind_param("ssssds", $username, $phone, $email, $hashed_password, $balance, $role);

            if ($stmt->execute()) {
                $_SESSION['user_id'] = $stmt->insert_id;
                $_SESSION['username'] = $username;
                header("Location: my-dashboard.php");
                exit;
            } else {
                $error = "Registration failed, please try again.";
            }
        }
        $stmt->close();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            display: flex;
            height: 100vh;
            justify-content: center;
            align-items: center;
            background: #f8f9fa;
        }
        .register-container {
            display: flex;
            width: 800px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }
        .left-section {
            width: 40%;
            background: #2d60ff;
            color: white;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 40px;
            text-align: center;
        }
        .right-section {
            width: 60%;
            padding: 40px;
        }
        .form-control {
            margin-bottom: 15px;
        }
        .btn-primary {
            background: #2d60ff;
            border: none;
        }
    </style>
</head>
<body>

<div class="register-container">
    <div class="left-section">
        <h2>👋 Join Us!</h2>
        <p>Create your account to get started.</p>
    </div>
    
    <div class="right-section">
        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="post">
            <label>Username</label>
            <input type="text" name="username" class="form-control" required>

            <label>Phone Number</label>
            <input type="text" name="phone" class="form-control" required>

            <label>Email</label>
            <input type="email" name="email" class="form-control" required>

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

            <label>Confirm Password</label>
            <input type="password" name="confirm_password" class="form-control" required>

            <button type="submit" class="btn btn-primary w-100">Register</button>
        </form>
        
        <p class="mt-3 text-center">Already have an account? <a href="login.php">Login here</a></p>
    </div>
</div>

</body>
</html>
