<?php
session_start();

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// ** NOTE ** Why are you repeating your database connections in several files.

// 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 Login
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $phone = trim($_POST['phone']);
    $password = trim($_POST['password']);
    $role = $_POST['role']; // Capture user role selection

    if (empty($phone) || empty($password) || empty($role)) {
        $error = "All fields are required";
    } else {
        // Corrected query: Fetch user based on phone and role
        $stmt = $conn->prepare("SELECT id, username, password, role FROM users WHERE phone = ? AND role = ?");
        $stmt->bind_param("ss", $phone, $role);
        $stmt->execute();
        $result = $stmt->get_result();
        $user = $result->fetch_assoc();
        $stmt->close();

        if ($user && password_verify($password, $user['password'])) {
            // Store session data
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role']; // Store actual role from DB
            $_SESSION['phone'] = $user['phone'];

            // Redirect based on role
            if ($user['role'] == "user") {
                header("Location: my-dashboard.php");
            } else if ($user['role'] == "super-admin") {
                header("Location: super-admin.php");
            }
            exit;
        } else {
            $error = "Invalid phone number or password";
        }
    }
}

// Logout Logic
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: login.php");
    exit;
}

// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
    if ($_SESSION['role'] == "user") {
        header("Location: my-dashboard.php");
    } else if ($_SESSION['role'] == "super-admin") {
        header("Location: super-admin.php");
    }
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</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;
        }
        .login-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;
        }
        body {
            background: url('images/log.jpg') no-repeat center center/cover;
            height: 100vh;
        }
    </style>
</head>
<body>

<div class="login-container">
    <div class="left-section">
        <h2>👋 Welcome Back!</h2>
        <p>Please enter your login details.</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>Phone Number</label>
            <input type="text" name="phone" class="form-control" required>

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

            <label>Login as</label>
            <select name="role" class="form-control" required>
                <option value="user">User</option>
                <option value="super-admin">Super Admin</option>
            </select>

            <button type="submit" class="btn btn-primary w-100">Sign In</button>
        </form>
        
        <p class="mt-3 text-center">
            <a href="register.php">Register Now</a> (For Users Only)
        </p>
    </div>
</div>

</body>
</html>
