<?php
require_once __DIR__ . '/../includes/functions.php';
verify_csrf();

if (current_admin()) { redirect('dashboard.php'); }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $login = trim((string)($_POST['email'] ?? ''));
    $password = (string)($_POST['password'] ?? '');

    // Allows login by admin email OR admin name, helpful for localhost accounts like "root".
    $admin = one('SELECT * FROM mj_admin_users WHERE (email=? OR name=?) AND is_active=1 LIMIT 1', [$login, $login]);
    if ($admin && password_verify($password, (string)$admin['password_hash'])) {
        $_SESSION['admin_id'] = (int)$admin['id'];
        admin_log('admin_login','mj_admin_users',(int)$admin['id'],['login'=>$login]);
        redirect('dashboard.php');
    }
    flash('error', 'Invalid admin login. Please check your email/username and password.');
}

$adminTitle = 'Admin Login';
include __DIR__ . '/_header.php';
?>
<div class="mj-login-split">
    <div class="mj-login-hero d-none d-lg-flex">
        <div class="mj-login-brand">
            <div class="mj-login-logo"><i class="fas fa-store"></i></div>
            <div><h2>MJ Marketplace Admin</h2><p>Global marketplace control center</p></div>
        </div>
        <div class="mj-login-hero-copy">
            <span class="badge badge-info mb-3">Global Pro</span>
            <h1>Welcome back, Admin</h1>
            <p>Manage countries, listings, companies, imports, claims, reports and marketplace growth from one clean MJ Stable Admin dashboard.</p>
            <div class="mj-hero-points">
                <div><i class="fas fa-globe"></i><b>Country Marketplaces</b><span>Standalone country URLs with local time.</span></div>
                <div><i class="fas fa-database"></i><b>Import Center</b><span>Listings, companies, business seed and sample data.</span></div>
                <div><i class="fas fa-shield-alt"></i><b>Safety Tools</b><span>Reports, scam keywords and admin audit logs.</span></div>
            </div>
        </div>
        <img class="mj-login-illustration" src="<?= e(asset('images/admin-hero.svg')) ?>" alt="Marketplace illustration">
    </div>
    <div class="mj-login-form-wrap">
        <div class="login-box mj-login-box">
            <div class="card card-outline card-primary mj-login-card">
                <div class="card-header text-center">
                    <a href="index.php" class="h1"><b>MJ</b> Marketplace</a>
                    <p class="mb-0 text-muted">MJ Stable Admin Control Panel</p>
                </div>
                <div class="card-body">
                    <p class="login-box-msg">Sign in to start your admin session</p>
                    <form method="post" autocomplete="on">
                        <?= csrf_input() ?>
                        <div class="input-group mb-3">
                            <input type="text" name="email" class="form-control" placeholder="Email or username" required autofocus>
                            <div class="input-group-append"><div class="input-group-text"><span class="fas fa-envelope"></span></div></div>
                        </div>
                        <div class="input-group mb-3">
                            <input type="password" name="password" class="form-control" placeholder="Password" required>
                            <div class="input-group-append"><div class="input-group-text"><span class="fas fa-lock"></span></div></div>
                        </div>
                        <div class="row align-items-center">
                            <div class="col-7">
                                <div class="icheck-primary"><input type="checkbox" id="remember"><label for="remember" class="font-weight-normal"> Remember me</label></div>
                            </div>
                            <div class="col-5"><button type="submit" class="btn btn-primary btn-block">Sign In</button></div>
                        </div>
                    </form>
                    <div class="mj-login-note mt-4"><i class="fas fa-info-circle"></i> Localhost ready. You may sign in using admin email or admin name.</div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/_footer.php'; ?>
