<?php
/**
 * Login Page
 * Video Portal Management System
 */

require_once 'config/database.php';
require_once 'includes/functions.php';
require_once 'includes/auth.php';

// Redirect if already logged in
if (isLoggedIn() && isAdmin()) {
    redirect('admin/dashboard.php');
}

$error = '';
$settings = getAllSettings();

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitize($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = 'Please enter username and password';
    } else {
        $result = authenticateUser($username, $password);
        if ($result['success']) {
            if (isAdmin()) {
                redirect('admin/dashboard.php');
            } else {
                // Non-admin users don't need login
                logoutUser();
                session_start();
                $error = 'Admin access only';
            }
        } else {
            $error = $result['error'];
        }
    }
}

$siteName = $settings['site_name'] ?? 'Video Portal';
$loginLogo = $settings['login_logo'] ?? '';
$loginBg = $settings['login_background'] ?? '';
$rtl = $settings['rtl_direction'] ?? '0';
$allowRegistration = $settings['allow_registration'] ?? '1';
?>
<!DOCTYPE html>
<html lang="<?php echo $rtl == '1' ? 'he' : 'en'; ?>" dir="<?php echo $rtl == '1' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?php echo htmlspecialchars($siteName); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <?php if (!empty($settings['primary_color'])): ?>
    <style>
        :root {
            --primary-color: <?php echo $settings['primary_color']; ?>;
            --secondary-color: <?php echo $settings['secondary_color'] ?? '#2c3e50'; ?>;
        }
    </style>
    <?php endif; ?>
</head>
<body class="<?php echo $rtl == '1' ? 'rtl' : ''; ?>">
    <div class="login-page <?php echo !empty($loginBg) ? 'has-bg' : ''; ?>" 
         <?php if (!empty($loginBg)): ?>style="background-image: url('assets/uploads/logos/<?php echo $loginBg; ?>');"<?php endif; ?>>
        <div class="login-container">
            <div class="login-logo">
                <?php if (!empty($loginLogo)): ?>
                    <img src="assets/uploads/logos/<?php echo $loginLogo; ?>" alt="<?php echo htmlspecialchars($siteName); ?>">
                <?php else: ?>
                    <i class="fas fa-play-circle" style="font-size: 60px; color: var(--primary-color);"></i>
                <?php endif; ?>
                <h1><?php echo htmlspecialchars($siteName); ?></h1>
                <p style="color: #888; margin-top: 5px;">Admin Login</p>
            </div>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>
            
            <form class="login-form" method="POST" action="">
                <div class="form-group">
                    <label for="username">
                        <i class="fas fa-user"></i> Username or Email
                    </label>
                    <input type="text" id="username" name="username" required 
                           value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                           placeholder="Enter your username or email">
                </div>
                
                <div class="form-group">
                    <label for="password">
                        <i class="fas fa-lock"></i> Password
                    </label>
                    <input type="password" id="password" name="password" required
                           placeholder="Enter your password">
                </div>
                
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-sign-in-alt"></i> Login
                </button>
            </form>
            
            <div class="login-footer">
                <a href="index.php"><i class="fas fa-arrow-left"></i> Back to Portal</a>
            </div>
        </div>
    </div>
</body>
</html>
