<?php
/**
 * IBVTE Management System - Main Login Page
 */

require_once 'config.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'middleware.php';

// Redirect if already logged in
if (Auth()->isLoggedIn()) {
    header('Location: ' . Auth()->getRoleDashboard());
    exit;
}

$error = '';
$otpRequired = false;
$contactMask = '';
$username = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = cleanInput($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $otp = $_POST['otp'] ?? null;
    $captcha = $_POST['captcha'] ?? null;
    
    $result = Auth()->login($username, $password, $otp, $captcha);
    
    if ($result['success']) {
        header('Location: ' . Auth()->getRoleDashboard());
        exit;
    } elseif (isset($result['otp_required']) && $result['otp_required']) {
        $otpRequired = true;
        $contactMask = $result['contact_mask'] ?? '';
    } else {
        $error = $result['message'];
    }
}

// Get flash message
$flash = getFlashMessage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #3b0b7c;
            --secondary-color: #6c4ab6;
            --accent-color: #ffd700;
        }
        body {
            background: linear-gradient(135deg, #3b0b7c 0%, #6c4ab6 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', sans-serif;
        }
        .login-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
            max-width: 900px;
            width: 100%;
            margin: 20px;
        }
        .login-left {
            background: linear-gradient(135deg, #3b0b7c 0%, #6c4ab6 100%);
            color: white;
            padding: 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
        }
        .login-left img {
            max-width: 120px;
            margin-bottom: 20px;
            border-radius: 50%;
            border: 4px solid rgba(255,255,255,0.3);
        }
        .login-left h2 {
            font-size: 1.5rem;
            margin-bottom: 10px;
        }
        .login-left p {
            opacity: 0.9;
            font-size: 0.9rem;
        }
        .login-right {
            padding: 40px;
        }
        .login-right h3 {
            color: var(--primary-color);
            margin-bottom: 25px;
            font-weight: 600;
        }
        .form-floating {
            margin-bottom: 15px;
        }
        .form-floating input {
            border: 2px solid #e0e0e0;
            border-radius: 10px;
        }
        .form-floating input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(59,11,124,0.1);
        }
        .btn-login {
            background: linear-gradient(135deg, #3b0b7c 0%, #6c4ab6 100%);
            border: none;
            border-radius: 10px;
            padding: 12px 30px;
            font-weight: 600;
            width: 100%;
            color: white;
            transition: all 0.3s;
        }
        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(59,11,124,0.3);
            color: white;
        }
        .otp-section {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
        }
        .otp-input {
            letter-spacing: 10px;
            font-size: 1.5rem;
            text-align: center;
            font-weight: bold;
        }
        .captcha-box {
            background: #f0f0f0;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
            margin-bottom: 10px;
            font-family: 'Courier New', monospace;
            font-size: 1.5rem;
            letter-spacing: 5px;
            color: #3b0b7c;
            font-weight: bold;
        }
        .alert {
            border-radius: 10px;
        }
        .login-links {
            text-align: center;
            margin-top: 20px;
        }
        .login-links a {
            color: var(--primary-color);
            text-decoration: none;
        }
        .login-links a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="row g-0">
            <div class="col-md-5">
                <div class="login-left h-100">
                    <img src="assets/images/logo.jpeg" alt="IBVTE Logo">
                    <h2><?= APP_NAME ?></h2>
                    <p>An Autonomous Body Recognized by Government of India</p>
                    <hr class="w-50 my-4" style="border-color: rgba(255,255,255,0.3);">
                    <p class="small">
                        <i class="fas fa-map-marker-alt me-2"></i>
                        <?= INSTITUTION_ADDRESS ?>
                    </p>
                    <p class="small">
                        <i class="fas fa-phone me-2"></i>
                        <?= INSTITUTION_PHONE ?>
                    </p>
                </div>
            </div>
            <div class="col-md-7">
                <div class="login-right">
                    <h3><i class="fas fa-user-circle me-2"></i>Welcome Back</h3>
                    
                    <?php if ($flash): ?>
                        <div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show">
                            <?= $flash['message'] ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if ($error): ?>
                        <div class="alert alert-danger alert-dismissible fade show">
                            <i class="fas fa-exclamation-circle me-2"></i><?= $error ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <form method="POST" action="" id="loginForm">
                        <div class="form-floating">
                            <input type="text" class="form-control" id="username" name="username" 
                                   placeholder="Username/Email" required value="<?= htmlspecialchars($username) ?>">
                            <label for="username"><i class="fas fa-user me-2"></i>Username or Email</label>
                        </div>
                        
                        <div class="form-floating">
                            <input type="password" class="form-control" id="password" name="password" 
                                   placeholder="Password" required>
                            <label for="password"><i class="fas fa-lock me-2"></i>Password</label>
                        </div>
                        
                        <?php if ($otpRequired): ?>
                            <div class="otp-section">
                                <h5 class="text-center mb-3">
                                    <i class="fas fa-shield-alt me-2 text-primary"></i>
                                    Two-Factor Authentication
                                </h5>
                                <p class="text-muted text-center small">
                                    Enter the OTP sent to <?= $contactMask ?>
                                </p>
                                <div class="form-floating">
                                    <input type="text" class="form-control otp-input" id="otp" name="otp" 
                                           placeholder="000000" maxlength="6" pattern="\d{6}" required>
                                    <label for="otp">Enter 6-digit OTP</label>
                                </div>
                                <button type="button" class="btn btn-link btn-sm w-100" onclick="resendOTP()">
                                    <i class="fas fa-redo me-1"></i>Resend OTP
                                </button>
                            </div>
                        <?php endif; ?>
                        
                        <button type="submit" class="btn btn-login" id="submitBtn">
                            <i class="fas fa-sign-in-alt me-2"></i>
                            <?= $otpRequired ? 'Verify & Login' : 'Login' ?>
                        </button>
                    </form>
                    
                    <div class="login-links">
                        <a href="forgot-password.php" class="small">
                            <i class="fas fa-key me-1"></i>Forgot Password?
                        </a>
                        <span class="mx-2 text-muted">|</span>
                        <a href="student/login.php" class="small">
                            <i class="fas fa-graduation-cap me-1"></i>Student Login
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('loginForm').addEventListener('submit', function() {
            document.getElementById('submitBtn').disabled = true;
            document.getElementById('submitBtn').innerHTML = 
                '<i class="fas fa-spinner fa-spin me-2"></i>Processing...';
        });
        
        function resendOTP() {
            // Implement AJAX call to resend OTP
            alert('OTP resend functionality will be implemented');
        }
    </script>
</body>
</html>
