<?php 
require_once 'includes/db.php';

$error = '';

if(isset($_POST['login'])) {
    $email = escape($_POST['email']);
    $password = $_POST['password'];

    $check = $conn->query("SELECT * FROM users WHERE email='$email'");
    if($check->num_rows > 0) {
        $user = $check->fetch_assoc();
        if(password_verify($password, $user['password'])) {
            if($user['status'] == 'pending') {
                $error = "Your account is currently pending admin approval. Please wait for an administrator to activate your account.";
            } else {
                // Active account, proceed to login
                $_SESSION['member_id'] = $user['id'];
                $_SESSION['member_name'] = $user['name'];
                $_SESSION['member_type'] = $user['type'];
                header("Location: member/index.php");
                exit;
            }
        } else {
            $error = "Invalid password.";
        }
    } else {
        $error = "Email not registered.";
    }
}

require_once 'includes/header.php'; 
?>

<div class="container-fluid min-vh-100 d-flex align-items-center justify-content-center py-5" style="background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);">
    <div class="container">
        <div class="row justify-content-center">
            
            <div class="col-lg-5 col-md-8">
                <div class="card border-0 shadow-lg" style="border-radius: 20px;">
                    <div class="card-body p-4 p-md-5">
                        
                        <div class="text-center mb-4">
                            <h2 class="fw-bold color-primary">Member Login</h2>
                            <p class="text-muted">Don't have an account? <a href="signup.php" class="text-primary fw-bold text-decoration-none">Sign up</a></p>
                        </div>

                        <?php if($error): ?>
                            <div class="alert alert-danger fw-bold"><i class="fas fa-exclamation-triangle me-2"></i> <?php echo $error ?></div>
                        <?php endif; ?>

                        <form action="login.php" method="POST">
                            <div class="mb-3">
                                <label class="form-label fw-bold">Email ID <span class="text-danger">*</span></label>
                                <input type="email" name="email" class="form-control bg-light border-0 py-2" required placeholder="Enter your registered email">
                            </div>
                            <div class="mb-4">
                                <label class="form-label fw-bold">Password <span class="text-danger">*</span></label>
                                <input type="password" name="password" class="form-control bg-light border-0 py-2" required placeholder="Enter password">
                            </div>

                            <button type="submit" name="login" class="btn btn-primary w-100 py-3 rounded-pill fw-bold shadow">Login to Dashboard</button>
                        </form>

                        <div class="mt-4 text-center">
                            <a href="index.php" class="btn btn-warning btn-sm rounded-pill px-4 fw-bold shadow-sm">Go Back to Home</a>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .color-primary { color: #A20E37; }
    .form-control:focus { background-color: #fff !important; box-shadow: 0 0 0 3px rgba(162, 14, 55, 0.2) !important; border: 1px solid #A20E37 !important; }
</style>

<?php require_once 'includes/footer.php'; ?>
