<?php
require_once __DIR__ . '/../config/config.php';
requireAdmin();

$pdo = getDB();

// Get statistics
$totalDoctors = $pdo->query("SELECT COUNT(*) FROM users WHERE role = 'doctor'")->fetchColumn();
$totalCases = $pdo->query("SELECT COUNT(*) FROM cases")->fetchColumn();
$totalRubrics = $pdo->query("SELECT COUNT(*) FROM rubrics")->fetchColumn();
$totalRemedies = $pdo->query("SELECT COUNT(*) FROM remedies")->fetchColumn();
$activeSubscriptions = $pdo->query("SELECT COUNT(*) FROM subscriptions WHERE is_active = 1")->fetchColumn();

$user = getCurrentUser();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - Homoeopathy System</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f0f2f5; }
        .sidebar {
            width: 260px;
            background: linear-gradient(135deg, #1a472a 0%, #0d2818 100%);
            position: fixed;
            left: 0;
            top: 0;
            height: 100vh;
            color: white;
            overflow-y: auto;
        }
        .sidebar h3 {
            text-align: center;
            padding: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .sidebar a {
            display: block;
            padding: 12px 20px;
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            transition: all 0.3s;
        }
        .sidebar a:hover {
            background: rgba(255,255,255,0.1);
            padding-left: 25px;
        }
        .sidebar a i { margin-right: 10px; width: 20px; }
        .main-content {
            margin-left: 260px;
            padding: 20px;
        }
        .header {
            background: white;
            padding: 15px 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            transition: transform 0.2s;
        }
        .stat-card:hover { transform: translateY(-5px); }
        .stat-number { font-size: 32px; font-weight: bold; color: #2ecc71; }
        .stat-label { color: #666; margin-top: 5px; }
        .card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .card h3 { margin-bottom: 15px; color: #2c3e50; border-bottom: 1px solid #eee; padding-bottom: 10px; }
        .btn {
            display: inline-block;
            padding: 8px 15px;
            background: #2ecc71;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        .btn-blue { background: #3498db; }
        .btn-orange { background: #e67e22; }
        .btn-purple { background: #9b59b6; }
        .btn:hover { opacity: 0.9; transform: translateY(-2px); }
        .footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <!-- Sidebar -->
    <div class="sidebar">
        <h3>🏥 Homoeopathy Admin</h3>
        <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
        <a href="doctors/index.php"><i class="fas fa-user-md"></i> Doctors</a>
        <a href="subscriptions/index.php"><i class="fas fa-credit-card"></i> Subscriptions</a>
        <a href="rubrics/index.php"><i class="fas fa-tree"></i> Rubrics</a>
        <a href="remedies/index.php"><i class="fas fa-capsules"></i> Remedies</a>
        <a href="reports/index.php"><i class="fas fa-chart-line"></i> Reports</a>
        <a href="settings/index.php"><i class="fas fa-cog"></i> Settings</a>
        <a href="../logout.php" style="margin-top: 20px;"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <div class="header">
            <h2><i class="fas fa-tachometer-alt"></i> Admin Dashboard</h2>
            <div>
                <i class="fas fa-user-circle"></i> <?php echo escape($user['full_name'] ?? 'Admin'); ?>
            </div>
        </div>

        <!-- Statistics Cards -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-number"><?php echo $totalDoctors; ?></div>
                <div class="stat-label">Total Doctors</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><?php echo $totalCases; ?></div>
                <div class="stat-label">Total Cases</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><?php echo $totalRubrics; ?></div>
                <div class="stat-label">Rubrics</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><?php echo $totalRemedies; ?></div>
                <div class="stat-label">Remedies</div>
            </div>
            <div class="stat-card">
                <div class="stat-number"><?php echo $activeSubscriptions; ?></div>
                <div class="stat-label">Active Subscriptions</div>
            </div>
        </div>

        <!-- Management Cards -->
        <div class="card">
            <h3><i class="fas fa-users"></i> User Management</h3>
            <a href="doctors/index.php" class="btn"><i class="fas fa-user-md"></i> Manage Doctors</a>
            <a href="doctors/add.php" class="btn btn-blue"><i class="fas fa-user-plus"></i> Add Doctor</a>
        </div>

        <div class="card">
            <h3><i class="fas fa-credit-card"></i> Subscription Management</h3>
            <a href="subscriptions/index.php" class="btn"><i class="fas fa-list"></i> All Subscriptions</a>
            <a href="subscriptions/add.php" class="btn btn-blue"><i class="fas fa-plus"></i> Add Subscription</a>
            <a href="subscriptions/plans.php" class="btn btn-orange"><i class="fas fa-tags"></i> Manage Plans</a>
            <a href="subscriptions/reports.php" class="btn btn-purple"><i class="fas fa-chart-line"></i> Reports</a>
        </div>

        <div class="card">
            <h3><i class="fas fa-tree"></i> Repertory Management (Kent's Repertory)</h3>
            <a href="rubrics/index.php" class="btn"><i class="fas fa-sitemap"></i> View Rubrics</a>
            <a href="rubrics/add.php" class="btn btn-blue"><i class="fas fa-plus"></i> Add Rubric</a>
            <a href="rubrics/bulk-import.php" class="btn btn-orange"><i class="fas fa-upload"></i> Bulk Import</a>
            <a href="rubrics/export.php" class="btn btn-purple"><i class="fas fa-download"></i> Export</a>
            <a href="rubrics/reorder.php" class="btn"><i class="fas fa-arrows-alt"></i> Reorder Hierarchy</a>
        </div>

        <div class="card">
            <h3><i class="fas fa-capsules"></i> Remedy Management (Materia Medica)</h3>
            <a href="remedies/index.php" class="btn"><i class="fas fa-list"></i> View Remedies</a>
            <a href="remedies/add.php" class="btn btn-blue"><i class="fas fa-plus"></i> Add Remedy</a>
            <a href="remedies/bulk-import.php" class="btn btn-orange"><i class="fas fa-upload"></i> Bulk Import</a>
            <a href="remedies/export.php" class="btn btn-purple"><i class="fas fa-download"></i> Export</a>
            <a href="remedies/assign-grade.php" class="btn"><i class="fas fa-star"></i> Assign Grades</a>
        </div>

        <div class="card">
            <h3><i class="fas fa-chart-line"></i> Reports & Analytics</h3>
            <a href="reports/index.php" class="btn"><i class="fas fa-chart-bar"></i> Reports Dashboard</a>
            <a href="reports/doctors.php" class="btn btn-blue"><i class="fas fa-user-md"></i> Doctor Activity</a>
            <a href="reports/subscriptions.php" class="btn btn-orange"><i class="fas fa-credit-card"></i> Subscription Report</a>
            <a href="reports/revenue.php" class="btn btn-purple"><i class="fas fa-dollar-sign"></i> Revenue Report</a>
            <a href="reports/export.php" class="btn"><i class="fas fa-download"></i> Export Data</a>
        </div>

        <div class="card">
            <h3><i class="fas fa-cog"></i> System Settings</h3>
            <a href="settings/index.php" class="btn"><i class="fas fa-sliders-h"></i> General Settings</a>
            <a href="settings/ai-settings.php" class="btn btn-blue"><i class="fas fa-robot"></i> AI Configuration</a>
            <a href="settings/backup.php" class="btn btn-orange"><i class="fas fa-database"></i> Database Backup</a>
            <a href="settings/system-info.php" class="btn btn-purple"><i class="fas fa-info-circle"></i> System Info</a>
        </div>

        <div class="footer">
            Homoeopathy System v<?php echo APP_VERSION; ?> | Kent's Repertory | Classical Homeopathy
        </div>
    </div>
</body>
</html>