<?php
/**
 * Login Page
 * Dozar & Tripper Management System
 */

require_once __DIR__ . '/config/config.php';

$error = '';

// If already logged in, redirect to dashboard
if (isLoggedIn()) {
    redirect(SITE_URL . '/dashboard.php');
}

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    
    if ($username && $password) {
        $pdo = getPDO();
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND is_active = 1");
        $stmt->execute([$username]);
        $user = $stmt->fetch();
        
        if ($user && password_verify($password, $user['password'])) {
            // Set session variables
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['full_name'] = $user['full_name'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['collector_access'] = $user['collector_access'];
            $_SESSION['assigned_projects'] = $user['assigned_projects'];
            $_SESSION['can_delete'] = $user['can_delete'];

            // Log login activity
            if (file_exists(__DIR__ . '/includes/activity_log.php')) {
                require_once __DIR__ . '/includes/activity_log.php';
                logActivity($pdo, 'login', 'auth', $user['id'], 'User logged in: ' . $user['full_name']);
            }

            redirect(SITE_URL . '/dashboard.php');
        } else {
            $error = 'Invalid username or password';
        }
    } else {
        $error = 'Please enter username and password';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - <?php echo APP_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: #2E7D32;
        }
        body {
            background: linear-gradient(135deg, #1B5E20 0%, #2E7D32 50%, #4CAF50 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .login-header {
            background: linear-gradient(135deg, #1B5E20 0%, #2E7D32 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .login-body {
            padding: 40px;
        }
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(46, 125, 50, 0.25);
        }
        .btn-login {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            padding: 12px;
            font-weight: 600;
        }
        .btn-login:hover {
            background-color: #1B5E20;
            border-color: #1B5E20;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="login-card">
                    <div class="login-header">
                        <h3><i class="fas fa-truck-monster"></i> <?php echo t('app_name', APP_NAME); ?></h3>
                        <p class="mb-0"><?php echo t('app_name_nepali', APP_NAME_NEPALI); ?></p>
                        <small><?php echo t('kathmandu_municipality', 'Kathmandu Municipality'); ?></small>
                    </div>
                    <div class="login-body">
                        <?php if ($error): ?>
                        <div class="alert alert-danger">
                            <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                        </div>
                        <?php endif; ?>
                        
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label for="username" class="form-label"><?php echo t('login_username', 'Username'); ?></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-user"></i></span>
                                    <input type="text" class="form-control" id="username" name="username" required autofocus>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label for="password" class="form-label"><?php echo t('login_password', 'Password'); ?></label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                    <input type="password" class="form-control" id="password" name="password" required>
                                </div>
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-primary btn-login">
                                    <i class="fas fa-sign-in-alt"></i> <?php echo t('login_btn', 'Login'); ?>
                                </button>
                            </div>
                        </form>
                        
                        <!-- Language Switcher -->
                        <div class="text-center mt-3">
                            <div class="btn-group" role="group" aria-label="Language switcher">
                                <a href="?lang=ne" class="btn btn-sm btn-outline-secondary <?php echo (isset($_SESSION['language']) && $_SESSION['language'] === 'ne') ? 'active' : ''; ?>">नेपाली</a>
                                <a href="?lang=en" class="btn btn-sm btn-outline-secondary <?php echo (isset($_SESSION['language']) && $_SESSION['language'] === 'en') ? 'active' : ''; ?>">English</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
