<?php
$pageTitle = 'Services';
require_once dirname(__DIR__) . '/includes/header.php';

if (isset($_GET['delete'])) {
    db()->query("DELETE FROM services WHERE id=".(int)$_GET['delete']);
    flash('Service deleted.','success');
    redirect(ADMIN_URL.'/pages/services.php');
}

$services = db()->query("SELECT * FROM services ORDER BY sort_order,id")->fetch_all(MYSQLI_ASSOC);
?>
<div class="page-header">
    <h1>Services</h1>
    <div class="page-header-actions">
        <a href="service-add.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Service</a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-concierge-bell" style="color:var(--brand)"></i>
        <h2>All Services</h2>
        <span style="color:var(--muted);font-size:13px;margin-left:auto"><?= count($services) ?> services</span>
    </div>
    <div class="table-wrap">
        <?php if (empty($services)): ?>
        <div style="padding:48px;text-align:center;color:var(--muted)">
            <i class="fas fa-concierge-bell" style="font-size:36px;opacity:.3;display:block;margin-bottom:12px"></i>
            No services yet. <a href="service-add.php" style="color:var(--brand)">Add first service</a>
        </div>
        <?php else: ?>
        <table>
            <thead>
                <tr><th>Icon</th><th>Title</th><th>Short Description</th><th>Status</th><th>Sort</th><th>Actions</th></tr>
            </thead>
            <tbody>
            <?php foreach ($services as $s): ?>
            <tr>
                <td><i class="<?= e($s['icon_class'] ?: 'fas fa-star') ?>" style="font-size:20px;color:var(--brand)"></i></td>
                <td style="font-weight:600"><?= e($s['title']) ?></td>
                <td style="color:var(--muted);font-size:13px;max-width:220px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= e($s['short_desc']) ?></td>
                <td><span class="badge <?= $s['status']==='active'?'badge-green':'badge-yellow' ?>"><?= ucfirst($s['status']) ?></span></td>
                <td style="color:var(--muted)"><?= (int)$s['sort_order'] ?></td>
                <td>
                    <div style="display:flex;gap:6px">
                        <a href="service-edit.php?id=<?= $s['id'] ?>" class="btn btn-secondary btn-sm btn-icon" title="Edit"><i class="fas fa-edit"></i></a>
                        <button onclick="confirmDelete('services.php?delete=<?= $s['id'] ?>','<?= e(addslashes($s['title'])) ?>')" class="btn btn-danger btn-sm btn-icon" title="Delete"><i class="fas fa-trash"></i></button>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
