<?php
/**
 * SalonPro X - Data Table Component
 * Usage: <?php component('table', ['headers' => ['Name','Email'], 'rows' => $data, 'actions' => true]); ?>
 * @var array $headers Column headers
 * @var array $rows Array of row data (objects or arrays)
 * @var bool $actions Show actions column (default: true)
 * @var string $editUrl Edit URL pattern with {id} placeholder
 * @var string $deleteUrl Delete URL pattern with {id} placeholder
 * @var string $viewUrl View URL pattern with {id} placeholder
 * @var string $customActions Custom actions HTML per row (use {id} placeholder)
 * @var string $emptyMessage Message when no data
 * @var string $class Additional CSS classes
 * @var object $pagination Pagination object
 * @var array $rowCallbacks Array of callbacks for custom cell rendering [col_index => function($row) returns HTML]
 */
$headers = $headers ?? [];
$rows = $rows ?? [];
$actions = $actions ?? true;
$editUrl = $editUrl ?? '';
$deleteUrl = $deleteUrl ?? '';
$viewUrl = $viewUrl ?? '';
$customActions = $customActions ?? '';
$emptyMessage = $emptyMessage ?? 'No records found.';
$class = $class ?? '';
$pagination = $pagination ?? null;
$rowCallbacks = $rowCallbacks ?? [];
$idField = $idField ?? 'id';
?>
<div class="table-container glass-card <?= e($class) ?>">
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <?php if (!empty($headers)): ?>
            <thead>
                <tr>
                    <th class="text-muted" style="width: 60px;">#</th>
                    <?php foreach ($headers as $header): ?>
                        <th><?= e($header) ?></th>
                    <?php endforeach; ?>
                    <?php if ($actions): ?>
                        <th class="text-end" style="width: 120px;">Actions</th>
                    <?php endif; ?>
                </tr>
            </thead>
            <?php endif; ?>
            <tbody>
                <?php if (empty($rows)): ?>
                <tr>
                    <td colspan="<?= count($headers) + ($actions ? 2 : 1) ?>" class="text-center py-5">
                        <div class="empty-state">
                            <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                            <p class="text-muted"><?= e($emptyMessage) ?></p>
                        </div>
                    </td>
                </tr>
                <?php else: ?>
                    <?php $index = 1; ?>
                    <?php foreach ($rows as $row): $row = (object)$row; ?>
                    <tr>
                        <td class="text-muted"><?= $index++ ?></td>
                        <?php foreach ($headers as $colIndex => $header): $field = strtolower(str_replace(' ', '_', $header)); ?>
                            <td>
                                <?php if (isset($rowCallbacks[$colIndex])): ?>
                                    <?= $rowCallbacks[$colIndex]($row) ?>
                                <?php elseif (isset($row->$field)): ?>
                                    <?php if (str_contains($field, 'status')): ?>
                                        <span class="badge bg-<?= statusBadge($row->$field) ?>"><?= e(ucfirst($row->$field)) ?></span>
                                    <?php elseif (str_contains($field, 'amount') || str_contains($field, 'price') || str_contains($field, 'total') || str_contains($field, 'salary')): ?>
                                        <?= currency($row->$field) ?>
                                    <?php elseif (str_contains($field, 'date') || str_contains($field, 'at')): ?>
                                        <?= formatDateTime($row->$field) ?>
                                    <?php elseif (str_contains($field, 'email')): ?>
                                        <a href="mailto:<?= e($row->$field) ?>"><?= e($row->$field) ?></a>
                                    <?php else: ?>
                                        <?= e($row->$field) ?>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                        <?php endforeach; ?>
                        <?php if ($actions): ?>
                        <td class="text-end">
                            <div class="table-actions">
                                <?php if ($viewUrl): ?>
                                    <a href="<?= str_replace('{id}', $row->$idField, $viewUrl) ?>" class="btn btn-sm btn-ghost" title="View">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                <?php endif; ?>
                                <?php if ($editUrl): ?>
                                    <a href="<?= str_replace('{id}', $row->$idField, $editUrl) ?>" class="btn btn-sm btn-ghost" title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                <?php endif; ?>
                                <?php if ($deleteUrl): ?>
                                    <button type="button" class="btn btn-sm btn-ghost text-danger" title="Delete"
                                            onclick="confirmDelete('<?= str_replace('{id}', $row->$idField, $deleteUrl) ?>')">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                <?php endif; ?>
                                <?php if ($customActions): ?>
                                    <?= str_replace('{id}', $row->$idField, $customActions) ?>
                                <?php endif; ?>
                            </div>
                        </td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if ($pagination): ?>
        <div class="table-footer">
            <?= paginateLinks($pagination) ?>
        </div>
    <?php endif; ?>
</div>
