<?php
session_name('PAKCLUB_ADMIN_SESSION');
session_start();
require_once __DIR__ . '/../config.php';

// Handle login
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
    if ($_POST['username'] === ADMIN_USERNAME && $_POST['password'] === ADMIN_PASSWORD) {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_login_time'] = time();
        try {
            $db = getDB();
            $db->prepare("INSERT INTO admin_logs (action, details, ip_address, user_agent) VALUES ('login', 'Admin login successful', ?, ?)")
               ->execute([$_SERVER['REMOTE_ADDR'] ?? '', $_SERVER['HTTP_USER_AGENT'] ?? '']);
        } catch (Exception $e) {}
        header('Location: ' . $_SERVER['PHP_SELF']);
        exit;
    } else {
        $loginError = true;
    }
}

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

$isLoggedIn = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PAKClub Admin Panel</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
</head>
<body>

<?php if (!$isLoggedIn): ?>
<!-- ═══════════ LOGIN PAGE ═══════════ -->
<div class="login-page">
    <div class="login-card">
        <div class="logo">
            <h1>⚡ PAKClub</h1>
            <p>Admin Control Panel</p>
        </div>
        <?php if (isset($loginError)): ?>
            <div style="background:rgba(255,23,68,0.1);border:1px solid rgba(255,23,68,0.2);border-radius:8px;padding:12px;margin-bottom:20px;color:#ff1744;font-size:13px;text-align:center;">
                ❌ Invalid username or password
            </div>
        <?php endif; ?>
        <form method="POST">
            <input type="hidden" name="login" value="1">
            <div class="form-group">
                <label>Username</label>
                <input type="text" name="username" placeholder="Enter username" required autofocus>
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" placeholder="Enter password" required>
            </div>
            <button type="submit" class="btn btn-primary btn-block" style="margin-top:10px;">
                🔐 Sign In
            </button>
        </form>
        <p style="text-align:center;margin-top:20px;font-size:12px;color:#555;">v<?= APP_VERSION ?></p>
    </div>
</div>

<?php else: ?>
<!-- ═══════════ ADMIN DASHBOARD ═══════════ -->
<button class="mobile-toggle" onclick="document.querySelector('.sidebar').classList.toggle('open')">☰</button>

<div class="admin-layout">
    <!-- Sidebar -->
    <aside class="sidebar">
        <div class="sidebar-header">
            <h1>⚡ PAKClub</h1>
            <span>Admin Panel v<?= APP_VERSION ?></span>
        </div>
        <nav class="sidebar-nav">
            <div class="nav-section">Main</div>
            <div class="nav-item active" data-page="dashboard"><span class="icon">📊</span> Dashboard</div>
            <div class="nav-item" data-page="apps"><span class="icon">📱</span> Apps</div>
            <div class="nav-item" data-page="audios"><span class="icon">🔊</span> Audio Manager</div>
            <div class="nav-section">System</div>
            <div class="nav-item" data-page="settings"><span class="icon">⚙️</span> Settings</div>
            <div class="nav-item" data-page="api-keys"><span class="icon">🔑</span> API Keys</div>
            <div class="nav-item" data-page="blocked"><span class="icon">🚫</span> Blocked Devices</div>
            <div class="nav-item" data-page="logs"><span class="icon">📋</span> Activity Logs</div>
        </nav>
        <div class="sidebar-footer">
            <a href="?logout=1" class="btn btn-danger btn-sm btn-block" style="text-decoration:none;">🚪 Logout</a>
        </div>
    </aside>

    <!-- Main Content -->
    <main class="main-content">
        <!-- DASHBOARD PAGE -->
        <div id="page-dashboard" class="page active"></div>
        <!-- APPS PAGE -->
        <div id="page-apps" class="page" style="display:none;"></div>
        <!-- AUDIOS PAGE -->
        <div id="page-audios" class="page" style="display:none;"></div>
        <!-- SETTINGS PAGE -->
        <div id="page-settings" class="page" style="display:none;"></div>
        <!-- API KEYS PAGE -->
        <div id="page-api-keys" class="page" style="display:none;"></div>
        <!-- BLOCKED DEVICES PAGE -->
        <div id="page-blocked" class="page" style="display:none;"></div>
        <!-- LOGS PAGE -->
        <div id="page-logs" class="page" style="display:none;"></div>
    </main>
</div>

<!-- Modal Overlay -->
<div class="modal-overlay" id="modal-overlay">
    <div class="modal" id="modal-container">
        <div class="modal-header">
            <h3 id="modal-title">Modal</h3>
            <button class="modal-close" onclick="closeModal()">✕</button>
        </div>
        <div class="modal-body" id="modal-body"></div>
        <div class="modal-footer" id="modal-footer"></div>
    </div>
</div>

<!-- Toast Container -->
<div class="toast-container" id="toast-container"></div>

<script src="pages_dashboard.js?v=<?= time() ?>"></script>
<script src="pages_apps.js?v=<?= time() ?>"></script>
<script src="pages_audios.js?v=<?= time() ?>"></script>
<script src="pages_system.js?v=<?= time() ?>"></script>
<script src="core.js?v=<?= time() ?>"></script>
<?php endif; ?>

</body>
</html>
