<?php
require_once '../config/database.php';
require_once '../includes/lang.php';

if (isset($_SESSION['user'])) {
    $roleId = (int)($_SESSION['user']['role_id'] ?? 0);
    if ($roleId === 1) {
        header("Location: admin.php");
    } else {
        header("Location: dashboard.php");
    }
    exit;
}
$csrf = function_exists('csrfToken') ? csrfToken() : '';
?>
<!DOCTYPE HTML>
<html lang="<?php echo htmlspecialchars(currentLang(), ENT_QUOTES, 'UTF-8'); ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="keywords" content="">
<meta name="description" content="">
<title><?php echo htmlspecialchars(t('app_name'), ENT_QUOTES, 'UTF-8'); ?></title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/style.css" type="text/css">
<link rel="stylesheet" href="assets/css/owl.carousel.min.css" type="text/css">
<link href="assets/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/colors/purple.css">
<link rel="shortcut icon" href="assets/images/favicon-icon/favicon.png">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
</head>
<body>
<section class="primary-bg">
    <div class="container">
        <div id="login_signup">
            <div class="form_wrap_m">
                <div class="white_box">

                    <div style="margin-bottom:12px;">
                        <img src="assets/images/gthlogo.png" alt="Logo">
                    </div>

                    <div style="margin-bottom:12px;">
                        <a class="btn btn-sm btn-default" href="?lang=en">EN</a>
                        <a class="btn btn-sm btn-default" href="?lang=sr">SR</a>
                    </div>

                    <form id="loginForm" method="post" action="#">
                        <input type="hidden" id="csrf" value="<?php echo htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8'); ?>">
                        <div class="form-group">
                            <input type="email" id="email" class="form-control" placeholder="<?php echo htmlspecialchars(t('email'), ENT_QUOTES, 'UTF-8'); ?>" required>
                        </div>
                        <div class="form-group">
                            <input type="password" id="password" class="form-control" placeholder="<?php echo htmlspecialchars(t('password'), ENT_QUOTES, 'UTF-8'); ?>" required>
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-block" value="<?php echo htmlspecialchars(t('login'), ENT_QUOTES, 'UTF-8'); ?>">
                        </div>
                    </form>

                    <div id="loginError" style="color:#c0392b;display:none;"></div>
                    <div style="margin-top:12px;text-align:center;display:flex;gap:8px;justify-content:center;flex-wrap:wrap;">
                        <a class="btn btn-sm btn-default" href="lost-password.php"><?php echo htmlspecialchars(t('lost_password'), ENT_QUOTES, 'UTF-8'); ?></a>
                        <a class="btn btn-sm btn-default" href="pricing.php"><?php echo htmlspecialchars(t('pricing'), ENT_QUOTES, 'UTF-8'); ?></a>
                        <a class="btn btn-sm btn-default" href="terms.php"><?php echo htmlspecialchars(t('terms_of_use'), ENT_QUOTES, 'UTF-8'); ?></a>
                        <a class="btn btn-sm btn-default" href="cookies.php"><?php echo htmlspecialchars(t('cookies'), ENT_QUOTES, 'UTF-8'); ?></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/owl.carousel.min.js"></script>
<script src="assets/js/interface.js"></script>
<script>
document.getElementById('loginForm').addEventListener('submit', async function(e){
    e.preventDefault();
    const payload = {
        email: document.getElementById('email').value.trim(),
        password: document.getElementById('password').value
    };
    const res = await fetch('../api/login.php', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify(payload)
    });
    const json = await res.json();
    if (json.success) {
        if (json.redirect) {
            window.location.href = json.redirect;
        } else {
            window.location.href = 'dashboard.php';
        }
    } else {
        const el = document.getElementById('loginError');
        el.style.display = 'block';
        el.textContent = json.error || 'Login failed';
    }
});
</script>
</body>
</html>
