<?php
/**
 * Enterprise HRM ERP System - Expense Heads Management
 */

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

$pageTitle = 'Expense Heads';
$currentModule = 'settings';

$siteId = getCurrentSiteId();

// Get expense heads
$expenseHeads = [];
try {
    $db = getDB();
    $stmt = $db->prepare("
        SELECT * FROM expense_heads
        WHERE site_id = ?
        ORDER BY expense_head_name
    ");
    $stmt->execute([$siteId]);
    $expenseHeads = $stmt->fetchAll();
} catch (Exception $e) {
    error_log("Expense heads error: " . $e->getMessage());
}

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

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Expense Heads Management</h2>
        <a href="<?php echo BASE_URL; ?>/admin/expense_heads_create.php" class="btn btn-primary">Add Expense Head</a>
    </div>
    
    <?php if (count($expenseHeads) > 0): ?>
    <div style="margin-top: 20px; overflow-x: auto;">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Expense Code</th>
                    <th>Expense Name</th>
                    <th>Category</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($expenseHeads as $eh): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($eh['expense_head_code']); ?></strong></td>
                    <td><?php echo htmlspecialchars($eh['expense_head_name']); ?></td>
                    <td><?php echo htmlspecialchars($eh['expense_category'] ?? 'N/A'); ?></td>
                    <td>
                        <span class="status-badge status-<?php echo $eh['is_active'] ? 'active' : 'inactive'; ?>">
                            <?php echo $eh['is_active'] ? 'Active' : 'Inactive'; ?>
                        </span>
                    </td>
                    <td class="table-actions-cell">
                        <a href="<?php echo BASE_URL; ?>/admin/expense_heads_edit.php?id=<?php echo $eh['expense_head_id']; ?>" 
                           class="btn btn-sm btn-primary">Edit</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div class="table-empty" style="margin-top: 30px;">
        <div class="table-empty-icon">💵</div>
        <div class="table-empty-message">No expense heads found</div>
        <div class="table-empty-submessage">
            <a href="<?php echo BASE_URL; ?>/admin/expense_heads_create.php" class="btn btn-primary">Add First Expense Head</a>
        </div>
    </div>
    <?php endif; ?>
</div>

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

