<?php
// Include session management and language
require_once 'includes/session.php';
require_once 'includes/language.php';

// Redirect if already logged in
redirectIfLoggedIn();

// Include database and user class
require_once 'config/database.php';
require_once 'classes/User.php';

// Initialize variables
$username = "";
$password = "";
$error_msg = "";

// Process login form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form data
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    
    // Validate input
    if (empty($username) || empty($password)) {
        $error_msg = __('login_error');
    } else {
        try {
            // Create database connection
            $database = new Database();
            $db = $database->getConnection();
            
            // Create user object
            $user = new User($db);
            $user->username = $username;
            $user->password = $password;
            
            // Attempt login
            try {
                if ($user->login()) {
                    // Store session data
                    $_SESSION['user_id'] = $user->id;
                    $_SESSION['username'] = $user->username;
                    $_SESSION['user_role'] = $user->role;
                    
                    // Redirect to home page
                    header("Location: home.php");
                    exit;
                } else {
                    $error_msg = __('login_error');
                }
            } catch (Exception $e) {
                $error_msg = $e->getMessage();
            }
        } catch (Exception $e) {
            $error_msg = "An error occurred: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="<?php echo $_SESSION['lang']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo __('login') . ' - ' . __('climbing_journal'); ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="container">
        <div class="login-container">
            <h1><?php echo __('climbing_journal'); ?></h1>
            
            <div class="language-switcher">
                <a href="?lang=en" class="lang-link <?php echo ($_SESSION['lang'] == 'en') ? 'active' : ''; ?>">ENG</a> / 
                <a href="?lang=sr" class="lang-link <?php echo ($_SESSION['lang'] == 'sr') ? 'active' : ''; ?>">SRB</a>
            </div>
            
            <div class="login-form">
                <h2><?php echo __('login'); ?></h2>
                
                <?php if (!empty($error_msg)): ?>
                <div class="alert alert-danger">
                    <?php echo $error_msg; ?>
                </div>
                <?php endif; ?>
                
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                    <div class="form-group">
                        <label for="username"><?php echo __('username'); ?></label>
                        <input type="text" name="username" id="username" value="<?php echo htmlspecialchars($username); ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="password"><?php echo __('password'); ?></label>
                        <input type="password" name="password" id="password" required>
                    </div>
                    
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary"><?php echo __('login_button'); ?></button>
                    </div>
                </form>
                
                <div class="register-link">
                    <?php echo __('register_link'); ?> <a href="register.php"><?php echo __('register'); ?></a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
