<?php
/**
 * User Login Page
 * Location: public_html/login.php
 */

require_once __DIR__ . '/connections/connect.php';
require_once __DIR__ . '/includes/functions.php';

if (is_logged_in()) {
    header("Location: team/dashboard.php");
    exit;
}

$error_msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = filter_var(trim($_POST['email'] ?? ''), FILTER_VALIDATE_EMAIL);
    $password = $_POST['password'] ?? '';
    $remember_me = isset($_POST['remember']);

    if (!$email || empty($password)) {
        $error_msg = "Please enter both email address and password.";
    } else {
        $stmt = $pdo->prepare("SELECT u.*, a.company_name FROM users u JOIN accounts a ON u.account_id = a.id WHERE u.email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password_hash'])) {
            if (!$user['is_verified']) {
                $error_msg = "Your email address has not been verified yet. Please check your inbox.";
            } else {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['account_id'] = $user['account_id'];
                $_SESSION['first_name'] = $user['first_name'];
                $_SESSION['last_name'] = $user['last_name'];
                $_SESSION['email'] = $user['email'];
                $_SESSION['role'] = $user['role'];
                $_SESSION['company_name'] = $user['company_name'];

                if ($remember_me) {
                    setcookie('remember_user', $user['id'], time() + (86400 * 30), "/");
                }

                header("Location: team/dashboard.php");
                exit;
            }
        } else {
            $error_msg = "Invalid email address or password.";
        }
    }
}
?>


<!DOCTYPE html>
<html data-bs-theme="light" lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>mibuildqc</title>
    <link rel="apple-touch-icon" type="image/png" sizes="180x180" href="assets/img/ios-home-screen-180x180.png">
    <link rel="icon" type="image/png" sizes="16x16" href="assets/img/favicon-16x16.png">
    <link rel="icon" type="image/png" sizes="16x16" href="assets/img/favicon-16x16.png" media="(prefers-color-scheme: dark)">
    <link rel="icon" type="image/png" sizes="32x32" href="assets/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="32x32" href="assets/img/favicon-32x32.png" media="(prefers-color-scheme: dark)">
    <link rel="icon" type="image/png" sizes="180x180" href="assets/img/ios-home-screen-180x180.png">
    <link rel="icon" type="image/svg+xml" sizes="150x150" href="assets/img/application-icon-192x192.svg">
    <link rel="icon" type="image/svg+xml" sizes="150x150" href="assets/img/splash-screen-512x512.svg">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="manifest" href="manifest.json" crossorigin="use-credentials">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,400;0,700;1,400;1,700&amp;display=swap">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Actor&amp;display=swap">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Afacad&amp;display=swap">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Akshar&amp;display=swap">
    <link rel="stylesheet" href="assets/css/bss-overrides.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="assets/css/buttons.css">
    <link rel="stylesheet" href="assets/css/Footer-Dark-Multi-Column-icons.css">
    <link rel="stylesheet" href="assets/css/styles.css">
    <link rel="stylesheet" href="assets/css/tables.css">
</head>

<body>
    <main style="background-color:#f8f7f5;">
        <section class="py-5">
            <div class="container py-5">
                <div class="row justify-content-center">
                    <div class="col-md-8 col-lg-6 col-xl-5">
                        <div class="card shadow">
                            <div class="card-body p-4 p-md-5">
                                <div class="text-center border rounded-3 mb-3" style="background: #0f172a;"><span style="font-weight: bold;color: rgb(255, 255, 255);font-family: Actor, sans-serif;font-size: 40px;">MiBuild</span><span style="font-weight: bold;color: #c3b4ff;font-family: Actor, sans-serif;font-size: 40px;">qc</span></div>
                                <h2 class="fw-bold text-center mb-4" style="color:#6a5d55;">Login</h2>
                                        <?php if (!empty($error_msg)): ?>
                                            <div class="p-3.5 rounded-xl bg-rose-50 border border-rose-200 text-rose-800 text-xs text-center">
                                                <?php echo htmlspecialchars($error_msg); ?>
                                            </div>
                                        <?php endif; ?>

                                        <?php if (isset($_GET['error'])): ?>
                                            <div class="p-3.5 rounded-xl bg-amber-50 border border-amber-200 text-amber-900 text-xs text-center">
                                                <?php echo htmlspecialchars($_GET['error']); ?>
                                            </div>
                                        <?php endif; ?>

                                <form action="login.php" method="POST" class="space-y-4">
                                    
                                    <div class="mb-3">
                                        <label class="form-label">Email Address</label>
                                        <input class="form-control" type="email" name="email" required value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" placeholder="john@example.com" required="">
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Password</label>
                                        <input class="form-control" type="password" name="password" required="">
                                    </div>
                                    <div class="d-flex justify-content-between mb-4">
                                        <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="formCheck-1"><label class="form-check-label" for="formCheck-1">Remember me</label>
                                    </div>
                                    <a class="text-decoration-none" href="#" style="color:#6a5d55;">Forgot Password?</a>
                                    </div>
                                        <button class="btn w-100 py-2 btn-login" type="submit">LOGIN</button>
                                </form>
                                <div class="text-center mt-4">
                                    <p class="text-muted mb-0">Don't have an account? <a class="fw-bold text-decoration-none" href="register.php" style="color:#6a5d55;">Register</a></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
</body>

</html>