<?php
/**
 * Enterprise HRM ERP System - Salary Components Management
 */

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

$pageTitle = 'Salary Components';
$currentModule = 'settings';

$siteId = getCurrentSiteId();
$type = $_GET['type'] ?? ''; // earning or deduction

try {
    $db = getDB();
    
    $where = "sc.site_id = ?";
    $params = [$siteId];
    
    if (!empty($type)) {
        $where .= " AND sc.component_type = ?";
        $params[] = $type;
    }
    
    $stmt = $db->prepare("
        SELECT sc.*,
               COUNT(DISTINCT ess.employee_id) as employee_count
        FROM salary_components sc
        LEFT JOIN employee_salary_structure ess ON sc.component_id = ess.component_id AND ess.structure_status = 'active'
        WHERE {$where}
        GROUP BY sc.component_id
        ORDER BY sc.component_type DESC, sc.component_name
    ");
    $stmt->execute($params);
    $components = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Salary components error: " . $e->getMessage());
    $components = [];
}

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

<div class="table-container">
    <div class="table-header">
        <h2 class="table-title">Salary Components Management</h2>
        <div class="table-actions">
            <a href="<?php echo BASE_URL; ?>/admin/salary_components_create.php" class="btn btn-primary">Add Component</a>
        </div>
    </div>
    
    <!-- Filter Tabs -->
    <div style="display: flex; gap: 10px; margin-bottom: 20px; border-bottom: 2px solid var(--border-color);">
        <button onclick="filterByType('')" 
                style="padding: 10px 20px; background: none; border: none; border-bottom: 2px solid <?php echo empty($type) ? 'var(--primary-color)' : 'transparent'; ?>; 
                       color: <?php echo empty($type) ? 'var(--primary-color)' : 'var(--text-secondary)'; ?>; 
                       font-weight: <?php echo empty($type) ? '600' : '400'; ?>; cursor: pointer;">
            All Components
        </button>
        <button onclick="filterByType('earning')" 
                style="padding: 10px 20px; background: none; border: none; border-bottom: 2px solid <?php echo $type === 'earning' ? 'var(--primary-color)' : 'transparent'; ?>; 
                       color: <?php echo $type === 'earning' ? 'var(--primary-color)' : 'var(--text-secondary)'; ?>; 
                       font-weight: <?php echo $type === 'earning' ? '600' : '400'; ?>; cursor: pointer;">
            💰 Earnings
        </button>
        <button onclick="filterByType('deduction')" 
                style="padding: 10px 20px; background: none; border: none; border-bottom: 2px solid <?php echo $type === 'deduction' ? 'var(--primary-color)' : 'transparent'; ?>; 
                       color: <?php echo $type === 'deduction' ? 'var(--primary-color)' : 'var(--text-secondary)'; ?>; 
                       font-weight: <?php echo $type === 'deduction' ? '600' : '400'; ?>; cursor: pointer;">
            ➖ Deductions
        </button>
    </div>
    
    <?php if (count($components) > 0): ?>
    <table class="data-table">
        <thead>
            <tr>
                <th class="sortable">Component Code</th>
                <th class="sortable">Component Name</th>
                <th>Type</th>
                <th>Calculation</th>
                <th>Value</th>
                <th>Taxable</th>
                <th>Employees</th>
                <th class="sortable">Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($components as $comp): ?>
            <tr>
                <td><strong><?php echo htmlspecialchars($comp['component_code']); ?></strong></td>
                <td><?php echo htmlspecialchars($comp['component_name']); ?></td>
                <td>
                    <span class="status-badge" style="background: <?php echo $comp['component_type'] === 'earning' ? '#d1fae5' : '#fee2e2'; ?>; 
                                                          color: <?php echo $comp['component_type'] === 'earning' ? '#065f46' : '#991b1b'; ?>;">
                        <?php echo ucfirst($comp['component_type']); ?>
                    </span>
                </td>
                <td><?php echo ucfirst($comp['calculation_type']); ?></td>
                <td>
                    <?php if ($comp['calculation_type'] === 'fixed'): ?>
                        <?php echo formatCurrency($comp['calculation_value']); ?>
                    <?php elseif ($comp['calculation_type'] === 'percentage'): ?>
                        <?php echo number_format($comp['calculation_value'], 2); ?>%
                    <?php else: ?>
                        Formula
                    <?php endif; ?>
                </td>
                <td><?php echo $comp['is_taxable'] ? 'Yes' : 'No'; ?></td>
                <td><?php echo number_format($comp['employee_count']); ?></td>
                <td>
                    <span class="status-badge status-<?php echo htmlspecialchars($comp['component_status']); ?>">
                        <?php echo ucfirst($comp['component_status']); ?>
                    </span>
                </td>
                <td class="table-actions-cell">
                    <a href="<?php echo BASE_URL; ?>/admin/salary_components_edit.php?id=<?php echo $comp['component_id']; ?>" 
                       class="btn btn-sm btn-primary">Edit</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php else: ?>
    <div class="table-empty">
        <div class="table-empty-icon">💵</div>
        <div class="table-empty-message">No salary components found</div>
        <div class="table-empty-submessage">
            <a href="<?php echo BASE_URL; ?>/admin/salary_components_create.php" class="btn btn-primary">Add First Component</a>
        </div>
    </div>
    <?php endif; ?>
</div>

<style>
.status-badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    text-transform: uppercase;
}

.status-active {
    background-color: #d1fae5;
    color: #065f46;
}

.status-inactive {
    background-color: #e5e7eb;
    color: #374151;
}
</style>

<script>
function filterByType(type) {
    if (type) {
        window.location.href = '?type=' + type;
    } else {
        window.location.href = '<?php echo BASE_URL; ?>/admin/salary_components.php';
    }
}
</script>

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

