<?php
/**
 * Enterprise HRM ERP System - Super Admin Dashboard
 * Can see all HR Admins, create new HR Admins, and view all data
 */

require_once __DIR__ . '/../includes/init.php';
requireLogin();

// Only Super Admin can access
if (!isSuperAdmin()) {
    $_SESSION['error'] = 'Access denied. Super Admin only.';
    header('Location: ' . BASE_URL . '/dashboard.php');
    exit;
}

$pageTitle = 'Super Admin Dashboard';
$currentModule = 'dashboard';

// Get statistics
try {
    $db = getDB();
    
    // Total HR Admins
    $stmt = $db->prepare("
        SELECT COUNT(*) FROM users u
        INNER JOIN roles r ON u.role_id = r.role_id
        WHERE r.role_code = 'hr_admin' AND u.user_status = 'active'
    ");
    $stmt->execute();
    $totalHRAdmins = $stmt->fetchColumn();
    
    // Total Sites
    $stmt = $db->prepare("SELECT COUNT(*) FROM sites WHERE site_status = 'active'");
    $stmt->execute();
    $totalSites = $stmt->fetchColumn();
    
    // Total Employees (all sites)
    $stmt = $db->prepare("SELECT COUNT(*) FROM employees WHERE employment_status = 'active'");
    $stmt->execute();
    $totalEmployees = $stmt->fetchColumn();
    
    // Total Active Users
    $stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE user_status = 'active'");
    $stmt->execute();
    $totalUsers = $stmt->fetchColumn();
    
    // Recent HR Admins
    $stmt = $db->prepare("
        SELECT u.*, r.role_name, s.site_name, s.site_code
        FROM users u
        INNER JOIN roles r ON u.role_id = r.role_id
        LEFT JOIN sites s ON u.site_id = s.site_id
        WHERE r.role_code = 'hr_admin'
        ORDER BY u.created_at DESC
        LIMIT 10
    ");
    $stmt->execute();
    $recentHRAdmins = $stmt->fetchAll();
    
    // Sites with employee count
    $stmt = $db->prepare("
        SELECT s.*, COUNT(e.employee_id) as employee_count
        FROM sites s
        LEFT JOIN employees e ON s.site_id = e.site_id AND e.employment_status = 'active'
        WHERE s.site_status = 'active'
        GROUP BY s.site_id
        ORDER BY s.site_name
    ");
    $stmt->execute();
    $sites = $stmt->fetchAll();
    
} catch (Exception $e) {
    error_log("Super Admin Dashboard error: " . $e->getMessage());
    $totalHRAdmins = 0;
    $totalSites = 0;
    $totalEmployees = 0;
    $totalUsers = 0;
    $recentHRAdmins = [];
    $sites = [];
}

include __DIR__ . '/../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Super Admin Dashboard</h2>
        <a href="<?php echo BASE_URL; ?>/admin/hr_admin_create.php" class="btn btn-primary">
            ➕ Create HR Admin
        </a>
    </div>
    
    <!-- Key Metrics -->
    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-top: 20px;">
        <div class="stat-card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
            <div class="stat-icon">👥</div>
            <div class="stat-content">
                <div class="stat-value"><?php echo number_format($totalHRAdmins); ?></div>
                <div class="stat-label">HR Admins</div>
            </div>
        </div>
        
        <div class="stat-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white;">
            <div class="stat-icon">🏢</div>
            <div class="stat-content">
                <div class="stat-value"><?php echo number_format($totalSites); ?></div>
                <div class="stat-label">Sites</div>
            </div>
        </div>
        
        <div class="stat-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); color: white;">
            <div class="stat-icon">👤</div>
            <div class="stat-content">
                <div class="stat-value"><?php echo number_format($totalEmployees); ?></div>
                <div class="stat-label">Total Employees</div>
            </div>
        </div>
        
        <div class="stat-card" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%); color: white;">
            <div class="stat-icon">🔐</div>
            <div class="stat-content">
                <div class="stat-value"><?php echo number_format($totalUsers); ?></div>
                <div class="stat-label">Active Users</div>
            </div>
        </div>
    </div>
    
    <!-- Quick Actions -->
    <div style="margin-top: 30px;">
        <h3 style="margin-bottom: 20px;">Quick Actions</h3>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
            <a href="<?php echo BASE_URL; ?>/admin/hr_admin_create.php" class="quick-action-btn">
                <div class="quick-action-icon">➕</div>
                <div class="quick-action-label">Create HR Admin</div>
            </a>
            
            <a href="<?php echo BASE_URL; ?>/admin/users.php" class="quick-action-btn">
                <div class="quick-action-icon">👥</div>
                <div class="quick-action-label">Manage Users</div>
            </a>
            
            <a href="<?php echo BASE_URL; ?>/admin/sites.php" class="quick-action-btn">
                <div class="quick-action-icon">🏢</div>
                <div class="quick-action-label">Manage Sites</div>
            </a>
            
            <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="quick-action-btn">
                <div class="quick-action-icon">👤</div>
                <div class="quick-action-label">All Employees</div>
            </a>
        </div>
    </div>
    
    <!-- Recent HR Admins -->
    <div style="margin-top: 30px;">
        <h3 style="margin-bottom: 15px;">Recent HR Admins</h3>
        <table class="data-table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Site</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($recentHRAdmins)): ?>
                <tr>
                    <td colspan="7" style="text-align: center; padding: 20px;">No HR Admins found. <a href="<?php echo BASE_URL; ?>/admin/hr_admin_create.php">Create one</a></td>
                </tr>
                <?php else: ?>
                <?php foreach ($recentHRAdmins as $hr): ?>
                <tr>
                    <td><?php echo htmlspecialchars($hr['full_name']); ?></td>
                    <td><?php echo htmlspecialchars($hr['username']); ?></td>
                    <td><?php echo htmlspecialchars($hr['email']); ?></td>
                    <td><?php echo htmlspecialchars($hr['site_name'] ?? 'N/A'); ?></td>
                    <td>
                        <span class="badge badge-<?php echo $hr['user_status'] === 'active' ? 'success' : 'danger'; ?>">
                            <?php echo ucfirst($hr['user_status']); ?>
                        </span>
                    </td>
                    <td><?php echo formatDateTime($hr['created_at']); ?></td>
                    <td>
                        <a href="<?php echo BASE_URL; ?>/admin/users_view.php?id=<?php echo $hr['user_id']; ?>" class="btn btn-sm">View</a>
                        <a href="<?php echo BASE_URL; ?>/admin/users_edit.php?id=<?php echo $hr['user_id']; ?>" class="btn btn-sm">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- Sites Overview -->
    <div style="margin-top: 30px;">
        <h3 style="margin-bottom: 15px;">Sites Overview</h3>
        <table class="data-table">
            <thead>
                <tr>
                    <th>Site Code</th>
                    <th>Site Name</th>
                    <th>Employees</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($sites)): ?>
                <tr>
                    <td colspan="5" style="text-align: center; padding: 20px;">No sites found.</td>
                </tr>
                <?php else: ?>
                <?php foreach ($sites as $site): ?>
                <tr>
                    <td><?php echo htmlspecialchars($site['site_code']); ?></td>
                    <td><?php echo htmlspecialchars($site['site_name']); ?></td>
                    <td><?php echo number_format($site['employee_count']); ?></td>
                    <td>
                        <span class="badge badge-<?php echo $site['site_status'] === 'active' ? 'success' : 'danger'; ?>">
                            <?php echo ucfirst($site['site_status']); ?>
                        </span>
                    </td>
                    <td>
                        <a href="<?php echo BASE_URL; ?>/dashboard.php?site_id=<?php echo $site['site_id']; ?>" class="btn btn-sm">View</a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<style>
.stat-card {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 15px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.stat-icon {
    font-size: 40px;
    opacity: 0.9;
}

.stat-content {
    flex: 1;
}

.stat-value {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 5px;
}

.stat-label {
    font-size: 12px;
    opacity: 0.9;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.quick-action-btn {
    background: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    text-align: center;
    text-decoration: none;
    color: var(--text-primary);
    transition: all 0.2s;
    border: 2px solid transparent;
}

.quick-action-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.quick-action-icon {
    font-size: 36px;
    margin-bottom: 10px;
}

.quick-action-label {
    font-size: 14px;
    font-weight: 500;
}
</style>

<?php include __DIR__ . '/../includes/footer.php'; ?>

