<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
include "../config/db.php";

if (isset($_GET['error'])) {
    $error = $_GET['error'];
}

if (isset($_POST['login'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];

    $query = "SELECT * FROM users WHERE email='$email'";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {
        $user = mysqli_fetch_assoc($result);

        // Verify password first
        if (password_verify($password, $user['password'])) {
            // Include and call rental status update to catch overdue users
            include_once "../includes/rental-functions.php";
            if (isset($conn)) {
                updateRentalStatuses($conn);
            }
            
            // Re-fetch user status to see if they were just blocked by the update
            $status_check = mysqli_query($conn, "SELECT status FROM users WHERE id = '{$user['id']}'");
            $updated_user = mysqli_fetch_assoc($status_check);
            
            if (isset($updated_user['status']) && $updated_user['status'] == 'blocked') {
                $error = "Your account has been blocked due to outstanding penalties or damages. Please contact support.";
            } else {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['name'];
                $_SESSION['user_email'] = $user['email'];

                header("Location: ../index.php");
                exit();
            }
        } else {
            $error = "Wrong password!";
        }
    } else {
        $error = "Email not registered!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - ShopNest Rentals</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(rgba(255,255,255,0.9), rgba(255,255,255,0.9)), 
                        url('../assets/images/fabric-bg.jpg');
            background-size: cover;
            background-attachment: fixed;
        }
        .login-container {
            min-height: 80vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px 0;
        }
        .login-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
            border-top: 5px solid #ff6b35;
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-header i {
            font-size: 3rem;
            color: #ff6b35;
            margin-bottom: 15px;
        }
        .form-control {
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            padding: 12px 15px;
        }
        .form-control:focus {
            border-color: #ff6b35;
            box-shadow: 0 0 0 0.2rem rgba(255, 107, 53, 0.25);
        }
    </style>
</head>
<body>

    <div class="container login-container">
        <div class="login-card fade-in">
            <div class="login-header">
                <i class="fas fa-sign-in-alt"></i>
                <h2 style="color: #2c3e50;">Welcome Back</h2>
                <p class="text-muted">Login to your ShopNest account</p>
            </div>

            <?php if(isset($error)): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i>
                    <?php echo $error; ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">
                        <i class="fas fa-envelope me-2"></i>Email Address
                    </label>
                    <input type="email" 
                           name="email" 
                           class="form-control"
                           placeholder="Enter your email"
                           required
                           value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>">
                </div>

                <div class="mb-3">
                    <label class="form-label">
                        <i class="fas fa-lock me-2"></i>Password
                    </label>
                    <input type="password" 
                           name="password" 
                           class="form-control"
                           placeholder="Enter your password"
                           required>
                </div>

                <div class="mb-3 form-check d-flex justify-content-between">
                    <div>
                        <input type="checkbox" class="form-check-input" id="remember">
                        <label class="form-check-label" for="remember">Remember me</label>
                    </div>
                    <a href="forgot-password.php" style="color: #ff6b35;">
                        Forgot Password?
                    </a>
                </div>

                <button type="submit" name="login" class="btn btn-orange w-100 py-2">
                    <i class="fas fa-sign-in-alt me-2"></i>Login
                </button>

                <div class="text-center mt-4">
                    <p class="text-muted">
                        Don't have an account? 
                        <a href="register.php" style="color: #ff6b35; font-weight: 600;">
                            Register here
                        </a>
                    </p>
                </div>
            </form>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>