<?php
/**
 * Data table component — with row animations and action buttons.
 *
 * @var array $columns [['key'=>..., 'label_ar'=>..., 'sortable'=>bool, 'class'=>'']]
 * @var array $rows
 * @var array $pagination (optional)
 * @var array $actions (optional) per-row actions
 */
$columns = $columns ?? [];
$rows = $rows ?? [];
$pagination = $pagination ?? null;
$actions = $actions ?? [];
?>
<?php if (empty($rows)): ?>
    <?php $__template->include('Shared.Components.empty-state', ['message' => $emptyMessage ?? 'لا توجد بيانات', 'icon' => $emptyIcon ?? 'inbox']); ?>
<?php else: ?>
<div class="table-responsive">
    <table class="data-table">
        <thead>
            <tr>
                <?php foreach ($columns as $col): ?>
                    <th class="<?= e($col['class'] ?? '') ?>">
                        <?= e($col['label_ar'] ?? $col['key']) ?>
                    </th>
                <?php endforeach; ?>
                <?php if (!empty($actions)): ?>
                    <th class="actions-col">الإجراءات</th>
                <?php endif; ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
                <tr>
                    <?php foreach ($columns as $col): ?>
                        <td class="<?= e($col['class'] ?? '') ?>">
                            <?php
                            $key = $col['key'];
                            $val = $row[$key] ?? '';
                            if (isset($col['render']) && is_callable($col['render'])) {
                                echo ($col['render'])($val, $row);
                            } else {
                                echo e((string) $val);
                            }
                            ?>
                        </td>
                    <?php endforeach; ?>
                    <?php if (!empty($actions)): ?>
                        <td class="actions-col">
                            <div class="action-buttons">
                                <?php foreach ($actions as $action): ?>
                                    <?php
                                    $href = $action['href'] ?? '#';
                                    if (is_callable($href)) $href = $href($row);
                                    $label = $action['label'] ?? '';
                                    $class = $action['class'] ?? 'btn btn-sm btn-outline';
                                    $iconName = $action['icon'] ?? null;
                                    ?>
                                    <a href="<?= e($href) ?>" class="<?= e($class) ?>">
                                        <?php if ($iconName): ?>
                                            <i data-lucide="<?= e($iconName) ?>" style="width:14px;height:14px;"></i>
                                        <?php endif; ?>
                                        <?= e($label) ?>
                                    </a>
                                <?php endforeach; ?>
                            </div>
                        </td>
                    <?php endif; ?>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php if ($pagination): ?>
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
<?php endif; ?>
<?php endif; ?>
