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

// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
    $role = strtolower($_SESSION['role']);
    if ($role === 'md') {
        header("Location: dashboard_md.php");
    } elseif ($role === 'superadmin' || $role === 'admin') {
        header("Location: dashboard_admin.php");
    } elseif ($role === 'employee') {
        header("Location: dashboard_employee.php");
    } else {
        session_unset();
        session_destroy();
    }
    exit();
}

$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = "Please enter both username and password.";
    } else {
        $sql = "SELECT id, username, role, password FROM users WHERE username = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();

            // HASHED PASSWORD CHECK
            $hashed_entered = hash('sha256', $password);
            if ($hashed_entered === $row['password']) {
                $_SESSION['user_id'] = $row['id'];
                $_SESSION['username'] = $row['username'];
                $_SESSION['role'] = strtolower($row['role']);

                if ($_SESSION['role'] === 'md') {
                    header("Location: dashboard_md.php");
                } elseif ($_SESSION['role'] === 'superadmin' || $_SESSION['role'] === 'admin') {
                    header("Location: dashboard_admin.php");
                } elseif ($_SESSION['role'] === 'employee') {
                    header("Location: dashboard_employee.php");
                } else {
                    $error = "Unknown role assigned.";
                }
                exit();
            } else {
                $error = "Invalid username or password.";
            }
        } else {
            $error = "Invalid username or password.";
        }

        $stmt->close();
    }

    $conn->close();
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MPC - Login</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        body {
            background: linear-gradient(135deg, #2c3e50, #1abc9c);
            height: 100vh;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: Arial, sans-serif;
        }

        .login-box {
            width: 100%;
            max-width: 420px;
            background: #fff;
            border-radius: 12px;
            padding: 2rem 2.5rem;
            text-align: center;
            box-shadow: 0 8px 25px rgba(0,0,0,0.2);
        }

        /* FULL IMAGE – No crop – No round icon */
        .logo-container img {
            width: 200px;
            height: auto;
            object-fit: contain;
            margin-bottom: 20px;
            border-radius: 0 !important; /* remove rounding */
        }

        h2 {
            margin-bottom: 1.3rem;
            color: #2c3e50;
            font-weight: 700;
        }

        .form-control {
            border-radius: 8px;
            padding: 12px;
        }

        .btn-login {
            width: 100%;
            background: #2c3e50;
            color: #fff;
            border-radius: 8px;
            padding: 12px;
            border: none;
            font-size: 1rem;
        }

        .btn-login:hover {
            background: #1abc9c;
        }

        .footer {
            margin-top: 1rem;
            font-size: 0.85rem;
            color: #666;
        }
    </style>
</head>

<body>

<div class="login-box">

    <div class="logo-container">
        <img src="assets/arostar-logo.jpg" alt="Company Logo">
    </div>

    <h2>My People Connect</h2>

    <?php if (!empty($error)): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="mb-3">
            <input type="text" name="username" class="form-control" placeholder="Username" required>
        </div>

        <div class="mb-3">
            <input type="password" name="password" class="form-control" placeholder="Password" required>
        </div>

        <button type="submit" class="btn btn-login">Sign In</button>
    </form>

    <div class="footer">© 2025 My People Connect</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
