<?php
/**
 * Enterprise HRM ERP System - HR Admin Management
 * Create unlimited HR Admins (Super Admin Only)
 */

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/check_admin_access.php';

if (!canManageHRAdmin()) {
    $_SESSION['error'] = 'Only Super Admin can manage HR Admins.';
    header('Location: ' . BASE_URL . '/dashboard.php');
    exit;
}

$pageTitle = 'HR Admin Management';
$currentModule = 'settings';

// Get HR Admin role ID
try {
    $db = getDB();
    $stmt = $db->prepare("SELECT role_id FROM roles WHERE role_code = 'hr_admin'");
    $stmt->execute();
    $hrAdminRole = $stmt->fetch();
    $hrAdminRoleId = $hrAdminRole ? $hrAdminRole['role_id'] : 0;
    
    // Get all HR Admins
    $stmt = $db->prepare("
        SELECT u.*, r.role_name,
               GROUP_CONCAT(s.site_name ORDER BY us.is_primary DESC SEPARATOR ', ') as assigned_sites,
               COUNT(DISTINCT us.site_id) as site_count
        FROM users u
        INNER JOIN roles r ON u.role_id = r.role_id
        LEFT JOIN user_sites us ON u.user_id = us.user_id
        LEFT JOIN sites s ON us.site_id = s.site_id
        WHERE u.role_id = ?
        GROUP BY u.user_id
        ORDER BY u.created_at DESC
    ");
    $stmt->execute([$hrAdminRoleId]);
    $hrAdmins = $stmt->fetchAll();
    
    // Get all sites
    $stmt = $db->prepare("SELECT site_id, site_name FROM sites WHERE site_status = 'active' ORDER BY site_name");
    $stmt->execute();
    $sites = $stmt->fetchAll();
    
} catch (Exception $e) {
    error_log("HR Admins error: " . $e->getMessage());
    $hrAdmins = [];
    $sites = [];
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">HR Admin Management</h2>
        <a href="<?php echo BASE_URL; ?>/admin/hr_admins_create.php" class="btn btn-primary">Create HR Admin</a>
    </div>
    
    <div style="margin-top: 20px;">
        <p style="color: var(--text-secondary); margin-bottom: 20px;">
            <strong>Note:</strong> Each HR Admin can be assigned to one or more sites. They will only see data for their assigned sites.
        </p>
        
        <?php if (count($hrAdmins) > 0): ?>
        <div style="overflow-x: auto;">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Full Name</th>
                        <th>Email</th>
                        <th>Assigned Sites</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($hrAdmins as $hr): ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($hr['username']); ?></strong></td>
                        <td><?php echo htmlspecialchars($hr['full_name']); ?></td>
                        <td><?php echo htmlspecialchars($hr['email']); ?></td>
                        <td>
                            <?php if ($hr['assigned_sites']): ?>
                            <span style="color: var(--primary-color);"><?php echo htmlspecialchars($hr['assigned_sites']); ?></span>
                            <br><small style="color: var(--text-secondary);"><?php echo $hr['site_count']; ?> site(s)</small>
                            <?php else: ?>
                            <span style="color: var(--text-secondary);">No sites assigned</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <span class="status-badge status-<?php echo $hr['user_status']; ?>">
                                <?php echo ucfirst($hr['user_status']); ?>
                            </span>
                        </td>
                        <td class="table-actions-cell">
                            <a href="<?php echo BASE_URL; ?>/admin/hr_admins_edit.php?id=<?php echo $hr['user_id']; ?>" 
                               class="btn btn-sm btn-primary">Edit/Assign Sites</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div class="table-empty">
            <div class="table-empty-icon">👥</div>
            <div class="table-empty-message">No HR Admins found</div>
            <div class="table-empty-submessage">
                <a href="<?php echo BASE_URL; ?>/admin/hr_admins_create.php" class="btn btn-primary">Create First HR Admin</a>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

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

