<?php
/**
 * @var object $services
 * @var array $categories
 * @var string $categoryId
 * @var string $search
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-concierge-bell me-2 text-gold"></i>Services</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Services</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('packages') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-box me-1"></i>Packages
            </a>
            <a href="<?= url('services/categories') ?>" class="btn btn-sm btn-outline-gold me-2">
                <i class="fas fa-tags me-1"></i>Categories
            </a>
            <?php if (can('services_create')): ?>
                <a href="<?= url('services/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Service
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-8">
                <form method="GET" action="<?= url('services') ?>" class="d-flex gap-2">
                    <div class="input-group">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                               placeholder="Search services..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search) || !empty($categoryId)): ?>
                            <a href="<?= url('services') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
            <div class="col-md-4">
                <select class="form-select form-select-sm bg-transparent" id="categoryFilter"
                        onchange="window.location.href=this.value">
                    <option value="<?= url('services') ?>">All Categories</option>
                    <?php foreach ($categories as $cat): ?>
                        <option value="<?= url('services?category_id=' . $cat->id) ?>"
                            <?= $categoryId == $cat->id ? 'selected' : '' ?>>
                            <?= e($cat->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th class="text-center">Duration</th>
                        <th class="text-end">Price</th>
                        <th class="text-center">Commission</th>
                        <th class="text-center">Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($services->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-cut fa-2x mb-2"></i>
                                <p class="mb-0">No services found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($services->currentPage - 1) * $services->perPage + 1; ?>
                        <?php foreach ($services->data as $service): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <span class="rounded-circle d-inline-block"
                                              style="width:12px;height:12px;background:<?= e($service->color ?? '#C8A44E') ?>;"></span>
                                        <strong><?= e($service->name) ?></strong>
                                    </div>
                                </td>
                                <td><?= e($service->category_name ?? '-') ?></td>
                                <td class="text-center"><?= (int)$service->duration_minutes ?> min</td>
                                <td class="text-end"><?= currency($service->price) ?></td>
                                <td class="text-center">
                                    <?php if ($service->commission_type !== 'none' && $service->commission_type): ?>
                                        <span class="badge bg-info">
                                            <?= $service->commission_type === 'percentage' ? $service->commission_value . '%' : currency($service->commission_value) ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-<?= statusBadge($service->is_active ? 'active' : 'inactive') ?>">
                                        <?= $service->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('services/edit/' . $service->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="<?= $service->id ?>"
                                                    data-name="<?= e($service->name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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


