<?php
/**
 * @var object $packages
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-box me-2 text-gold"></i>Packages</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-box card-icon"></i>
            <h6 class="card-title">All Packages</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('services') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-concierge-bell me-1"></i>Services
            </a>
            <?php if (can('services_create')): ?>
                <a href="<?= url('packages/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Package
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Description</th>
                        <th class="text-center">Services</th>
                        <th class="text-end">Package Price</th>
                        <th class="text-end">Total Value</th>
                        <th class="text-center">Validity</th>
                        <th class="text-center">Max Visits</th>
                        <th class="text-center">Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($packages->data)): ?>
                        <tr>
                            <td colspan="10" class="text-center text-muted py-4">
                                <i class="fas fa-box-open fa-2x mb-2"></i>
                                <p class="mb-0">No packages found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($packages->currentPage - 1) * $packages->perPage + 1; ?>
                        <?php foreach ($packages->data as $package): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><strong><?= e($package->name) ?></strong></td>
                                <td><?= e(truncate($package->description ?? '-', 60)) ?></td>
                                <td class="text-center"><?= (int)$package->service_count ?></td>
                                <td class="text-end"><strong class="text-gold"><?= currency($package->price) ?></strong></td>
                                <td class="text-end text-muted"><?= currency($package->total_value ?? 0) ?></td>
                                <td class="text-center"><?= (int)$package->validity_days ?> days</td>
                                <td class="text-center"><?= (int)$package->max_visits ?></td>
                                <td class="text-center">
                                    <span class="badge bg-<?= statusBadge($package->is_active ? 'active' : 'inactive') ?>">
                                        <?= $package->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <?php if (can('services_edit')): ?>
                                            <a href="<?= url('packages/edit/' . $package->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('services_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $package->id ?>"
                                                    data-name="<?= e($package->name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($packages->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($packages->currentPage - 1) * $packages->perPage) + 1 ?>
                    to <?= min($packages->currentPage * $packages->perPage, $packages->total) ?>
                    of <?= $packages->total ?> entries
                </div>
                <?= paginateLinks($packages, url('packages')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>


