/home/awneajlw/www/codestechvista.com/admin/dashboard.php
<?php
session_start();
require_once '../config/database.php';
require_once '../includes/auth.php';

// Check if user is admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

$database = new Database();
$db = $database->getConnection();

// Get Statistics
$stats = [];

// Total Users (Main users only, excluding sub users)
$query = "SELECT COUNT(*) as total FROM users WHERE role IN ('user', 'shop_owner') AND (user_type IS NULL OR user_type = 'main')";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['total_users'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Total Sub Users
$query = "SELECT COUNT(*) as total FROM users WHERE user_type = 'sub'";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['total_sub_users'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Total Orders
$query = "SELECT COUNT(*) as total FROM orders";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['total_orders'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Pending Orders
$query = "SELECT COUNT(*) as total FROM orders WHERE status = 'Pending'";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['pending_orders'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Completed Orders
$query = "SELECT COUNT(*) as total FROM orders WHERE status IN ('Done', 'Completed')";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['completed_orders'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Active Users (logged in last 24 hours - we'll simulate this)
$query = "SELECT COUNT(*) as total FROM users WHERE role IN ('user', 'shop_owner') AND (user_type IS NULL OR user_type = 'main')";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['active_users'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Total Shops
$query = "SELECT COUNT(*) as total FROM shops WHERE status = 'approved'";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['total_shops'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Recent Orders
$query = "SELECT o.*, u.name as user_name, u.email as user_email 
          FROM orders o 
          LEFT JOIN users u ON o.user_id = u.id 
          ORDER BY o.created_at DESC 
          LIMIT 10";
$stmt = $db->prepare($query);
$stmt->execute();
$recent_orders = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Recent Users
$query = "SELECT * FROM users WHERE role IN ('user', 'shop_owner') ORDER BY created_at DESC LIMIT 5";
$stmt = $db->prepare($query);
$stmt->execute();
$recent_users = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Dashboard";
?>
<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>

<div class="main-content">
    <!-- Page Header -->
    <div class="page-header">
        <h1 class="page-title">
            <i class="fas fa-tachometer-alt"></i> Dashboard Overview
        </h1>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item active">Dashboard</li>
            </ol>
        </nav>
    </div>
    
    <!-- Statistics Cards -->
    <div class="row g-4 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['total_users']); ?></div>
                <div class="stat-label">Total Users</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-user-friends"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['total_sub_users']); ?></div>
                <div class="stat-label">Sub Users</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-shopping-cart"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['total_orders']); ?></div>
                <div class="stat-label">Total Orders</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-clock"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['pending_orders']); ?></div>
                <div class="stat-label">Pending Orders</div>
            </div>
        </div>
    </div>
    
    <div class="row g-4 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['completed_orders']); ?></div>
                <div class="stat-label">Completed Orders</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-user-check"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['active_users']); ?></div>
                <div class="stat-label">Active Users</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-store"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['total_shops']); ?></div>
                <div class="stat-label">Active Shops</div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6">
            <div class="stat-card">
                <div class="stat-icon">
                    <i class="fas fa-chart-line"></i>
                </div>
                <div class="stat-value"><?php echo number_format($stats['completed_orders'] > 0 ? ($stats['completed_orders'] / $stats['total_orders']) * 100 : 0, 1); ?>%</div>
                <div class="stat-label">Completion Rate</div>
            </div>
        </div>
    </div>
    
    <!-- Recent Activity -->
    <div class="row g-4">
        <!-- Recent Orders -->
        <div class="col-lg-8">
            <div class="content-card">
                <div class="card-header-custom">
                    <h3 class="card-title-custom">
                        <i class="fas fa-shopping-bag"></i> Recent Orders
                    </h3>
                    <a href="orders.php" class="btn-primary-custom">
                        <i class="fas fa-eye"></i> View All
                    </a>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-custom">
                        <thead>
                            <tr>
                                <th>Tracking ID</th>
                                <th>Patient Name</th>
                                <th>Amount</th>
                                <th>Status</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($recent_orders)): ?>
                                <tr>
                                    <td colspan="5" class="text-center">No orders found</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($recent_orders as $order): ?>
                                    <tr>
                                        <td><strong><?php echo htmlspecialchars($order['tracking_id']); ?></strong></td>
                                        <td><?php echo htmlspecialchars($order['patient_name']); ?></td>
                                        <td>Rs. <?php echo number_format($order['total_amount'], 2); ?></td>
                                        <td>
                                            <?php
                                            $badge_class = 'badge-info';
                                            if ($order['status'] == 'Done' || $order['status'] == 'Completed') {
                                                $badge_class = 'badge-success';
                                            } elseif ($order['status'] == 'Pending') {
                                                $badge_class = 'badge-warning';
                                            }
                                            ?>
                                            <span class="badge-custom <?php echo $badge_class; ?>">
                                                <?php echo htmlspecialchars($order['status']); ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('M d, Y', strtotime($order['created_at'])); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <!-- Recent Users -->
        <div class="col-lg-4">
            <div class="content-card">
                <div class="card-header-custom">
                    <h3 class="card-title-custom">
                        <i class="fas fa-user-plus"></i> Recent Users
                    </h3>
                    <a href="users.php" class="btn-primary-custom">
                        <i class="fas fa-eye"></i> View All
                    </a>
                </div>
                
                <div class="list-group list-group-flush">
                    <?php if (empty($recent_users)): ?>
                        <div class="text-center text-muted py-3">No users found</div>
                    <?php else: ?>
                        <?php foreach ($recent_users as $user): ?>
                            <div class="list-group-item border-0 px-0">
                                <div class="d-flex align-items-center">
                                    <div class="flex-shrink-0">
                                        <div class="bg-light rounded-circle d-flex align-items-center justify-content-center" style="width: 40px; height: 40px;">
                                            <i class="fas fa-user text-muted"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <h6 class="mb-0"><?php echo htmlspecialchars($user['name']); ?></h6>
                                        <small class="text-muted"><?php echo htmlspecialchars($user['email']); ?></small>
                                    </div>
                                    <span class="badge-custom badge-<?php echo $user['role'] == 'shop_owner' ? 'info' : 'success'; ?>">
                                        <?php echo ucfirst($user['role']); ?>
                                    </span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

</body>
</html>