<?php
/**
 * Admin Sidebar
 * LandSales Pro - Admin panel navigation
 */

// Get current page for active state
$currentPage = basename($_SERVER['PHP_SELF']);
$currentDir = basename(dirname($_SERVER['PHP_SELF']));

function isAdminActive($page, $dir = null) {
    global $currentPage, $currentDir;
    if ($dir && $currentDir === $dir) return true;
    return $currentPage === $page;
}

// Get counts for badges
$pendingSubscriptions = db_fetch_one("SELECT COUNT(*) as count FROM subscriptions WHERE status = 'trial' AND trial_ends_at <= DATE_ADD(NOW(), INTERVAL 3 DAY)")['count'] ?? 0;
$expiredToday = db_fetch_one("SELECT COUNT(*) as count FROM subscriptions WHERE status IN ('active','trial') AND DATE(expires_at) = CURDATE()")['count'] ?? 0;
?>

<!-- Sidebar Overlay for Mobile -->
<div class="sidebar-overlay" onclick="toggleSidebar()"></div>

<!-- Sidebar -->
<aside class="sidebar" id="sidebar">
    <div class="sidebar-header">
        <a href="<?php echo SITE_URL; ?>/admin/" class="sidebar-logo">
            <i class="fas fa-map-marked-alt"></i>
            <span><?php echo APP_NAME; ?></span>
        </a>
        <button class="sidebar-close" onclick="toggleSidebar()">
            <i class="fas fa-times"></i>
        </button>
    </div>

    <div class="sidebar-user">
        <div class="user-avatar">
            <?php echo strtoupper(substr($_SESSION['user_name'] ?? 'A', 0, 1)); ?>
        </div>
        <div class="user-info">
            <div class="user-name"><?php echo htmlspecialchars($_SESSION['user_name'] ?? 'Admin'); ?></div>
            <div class="user-role">Administrator</div>
        </div>
    </div>

    <nav class="sidebar-nav">
        <div class="nav-section">
            <div class="nav-section-title">Main</div>
            <a href="<?php echo SITE_URL; ?>/admin/" class="nav-link <?php echo $currentPage === 'index.php' && $currentDir === 'admin' ? 'active' : ''; ?>">
                <i class="fas fa-tachometer-alt"></i>
                <span>Dashboard</span>
            </a>
        </div>

        <div class="nav-section">
            <div class="nav-section-title">Subscription Management</div>
            <a href="<?php echo SITE_URL; ?>/admin/subscribers/" class="nav-link <?php echo $currentDir === 'subscribers' ? 'active' : ''; ?>">
                <i class="fas fa-users"></i>
                <span>Subscribers</span>
            </a>
            <a href="<?php echo SITE_URL; ?>/admin/subscriptions/" class="nav-link <?php echo $currentDir === 'subscriptions' && $currentPage !== 'payments.php' ? 'active' : ''; ?>">
                <i class="fas fa-credit-card"></i>
                <span>Subscriptions</span>
                <?php if ($pendingSubscriptions > 0): ?>
                <span class="nav-badge"><?php echo $pendingSubscriptions; ?></span>
                <?php endif; ?>
            </a>
            <a href="<?php echo SITE_URL; ?>/admin/subscriptions/payments.php" class="nav-link <?php echo $currentPage === 'payments.php' ? 'active' : ''; ?>">
                <i class="fas fa-money-bill-wave"></i>
                <span>Payments</span>
            </a>
        </div>

        <div class="nav-section">
            <div class="nav-section-title">Platform</div>
            <a href="<?php echo SITE_URL; ?>/admin/plans/" class="nav-link <?php echo $currentDir === 'plans' ? 'active' : ''; ?>">
                <i class="fas fa-tags"></i>
                <span>Plans</span>
            </a>
            <a href="<?php echo SITE_URL; ?>/admin/settings.php" class="nav-link <?php echo $currentPage === 'settings.php' ? 'active' : ''; ?>">
                <i class="fas fa-cog"></i>
                <span>Settings</span>
            </a>
            <a href="<?php echo SITE_URL; ?>/dashboard/settings/integrations/landsecure.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'landsecure.php') !== false ? 'active' : ''; ?>">
                <i class="fas fa-plug"></i>
                <span>LandSecure Sync</span>
            </a>
        </div>

        <div class="nav-section">
            <div class="nav-section-title">Analytics</div>
            <a href="<?php echo SITE_URL; ?>/admin/visitors/" class="nav-link <?php echo $currentDir === 'visitors' ? 'active' : ''; ?>">
                <i class="fas fa-chart-bar"></i>
                <span>Visitor Analytics</span>
            </a>
        </div>

        <div class="nav-section">
            <div class="nav-section-title">Reports</div>
            <a href="<?php echo SITE_URL; ?>/admin/reports/revenue.php" class="nav-link <?php echo $currentPage === 'revenue.php' ? 'active' : ''; ?>">
                <i class="fas fa-chart-line"></i>
                <span>Revenue Report</span>
            </a>
            <a href="<?php echo SITE_URL; ?>/admin/reports/activity.php" class="nav-link <?php echo $currentPage === 'activity.php' ? 'active' : ''; ?>">
                <i class="fas fa-history"></i>
                <span>Activity Log</span>
            </a>
        </div>

        <div class="nav-section">
            <div class="nav-section-title">Account</div>
            <a href="<?php echo SITE_URL; ?>/home.php" class="nav-link" target="_blank">
                <i class="fas fa-external-link-alt"></i>
                <span>View Website</span>
            </a>
            <a href="<?php echo SITE_URL; ?>/logout.php" class="nav-link">
                <i class="fas fa-sign-out-alt"></i>
                <span>Logout</span>
            </a>
        </div>
    </nav>

    <div class="sidebar-footer">
        <small>&copy; <?php echo date('Y'); ?> <?php echo APP_NAME; ?></small>
    </div>
</aside>

<style>
    /* Sidebar Styles */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: 260px;
        height: 100vh;
        background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
        color: white;
        z-index: 1000;
        display: flex;
        flex-direction: column;
        transition: transform 0.3s ease;
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 999;
    }

    .sidebar-header {
        padding: 1.5rem;
        border-bottom: 1px solid rgba(255,255,255,0.1);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .sidebar-logo {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        color: white;
        text-decoration: none;
        font-size: 1.25rem;
        font-weight: 700;
    }

    .sidebar-logo i {
        color: #667eea;
    }

    .sidebar-close {
        display: none;
        background: none;
        border: none;
        color: #94a3b8;
        font-size: 1.25rem;
        cursor: pointer;
    }

    .sidebar-user {
        padding: 1.25rem 1.5rem;
        border-bottom: 1px solid rgba(255,255,255,0.1);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .user-avatar {
        width: 42px;
        height: 42px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: 1rem;
    }

    .user-info {
        flex: 1;
    }

    .user-name {
        font-weight: 600;
        font-size: 0.9rem;
    }

    .user-role {
        font-size: 0.75rem;
        color: #94a3b8;
    }

    .sidebar-nav {
        flex: 1;
        overflow-y: auto;
        padding: 1rem 0;
    }

    .nav-section {
        margin-bottom: 1rem;
    }

    .nav-section-title {
        padding: 0.5rem 1.5rem;
        font-size: 0.7rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #64748b;
    }

    .nav-link {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.75rem 1.5rem;
        color: #94a3b8;
        text-decoration: none;
        transition: all 0.2s;
        border-left: 3px solid transparent;
    }

    .nav-link:hover {
        background: rgba(255,255,255,0.05);
        color: white;
    }

    .nav-link.active {
        background: rgba(102, 126, 234, 0.15);
        color: #667eea;
        border-left-color: #667eea;
    }

    .nav-link i {
        width: 20px;
        text-align: center;
    }

    .nav-badge {
        margin-left: auto;
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        color: white;
        font-size: 0.7rem;
        font-weight: 600;
        padding: 0.2rem 0.5rem;
        border-radius: 10px;
        min-width: 20px;
        text-align: center;
    }

    .sidebar-footer {
        padding: 1rem 1.5rem;
        border-top: 1px solid rgba(255,255,255,0.1);
        color: #64748b;
        font-size: 0.75rem;
        text-align: center;
    }

    /* Main Content Area */
    .main-content {
        margin-left: 260px;
        min-height: 100vh;
        background: #f1f5f9;
    }

    .top-header {
        background: white;
        padding: 1rem 2rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .menu-toggle {
        display: none;
        background: none;
        border: none;
        font-size: 1.25rem;
        cursor: pointer;
        color: #64748b;
        padding: 0.5rem;
    }

    .page-title {
        font-size: 1.25rem;
        font-weight: 600;
        color: #1e293b;
    }

    .content-wrapper {
        padding: 2rem;
    }

    /* Mobile Responsive */
    @media (max-width: 992px) {
        .sidebar {
            transform: translateX(-100%);
        }

        .sidebar.active {
            transform: translateX(0);
        }

        .sidebar-overlay.active {
            display: block;
        }

        .sidebar-close {
            display: block;
        }

        .main-content {
            margin-left: 0;
        }

        .menu-toggle {
            display: block;
        }
    }
</style>

<script>
function toggleSidebar() {
    document.getElementById('sidebar').classList.toggle('active');
    document.querySelector('.sidebar-overlay').classList.toggle('active');
}
</script>
