<?php
require_once 'config/db.php';
require_once 'includes/header.php';

// Handle soft delete or toggle status
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $pdo->prepare("DELETE FROM lead_services WHERE id = ?")->execute([$id]);
    header("Location: lead_services.php?msg=deleted");
    exit;
}

if (isset($_GET['toggle'])) {
    $id = (int)$_GET['toggle'];
    $pdo->prepare("UPDATE lead_services SET status = IF(status='active', 'inactive', 'active') WHERE id = ?")->execute([$id]);
    header("Location: lead_services.php?msg=updated");
    exit;
}

$services = $pdo->query("SELECT * FROM lead_services ORDER BY name ASC")->fetchAll();
?>

<div class="dashboard-content">
    <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 25px;">
        <h2><i class="fa-solid fa-briefcase"></i> Manage Services</h2>
        <a href="lead_service_add_edit.php" class="btn-primary"><i class="fa-solid fa-plus"></i> Add New Service</a>
    </div>

    <?php if(isset($_GET['msg'])): ?>
        <div class="badge badge-completed" style="display:block; padding:15px; margin-bottom: 20px; font-size:1rem;">Service <?= htmlspecialchars($_GET['msg']) ?> successfully.</div>
    <?php endif; ?>

    <div class="recent-activity" style="margin:0;">
        <div class="table-responsive">
            <table class="admin-table">
            <thead>
                <tr>
                    <th style="width: 80px;">ID</th>
                    <th>Service Name</th>
                    <th>Description</th>
                    <th>Status</th>
                    <th style="text-align: right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($services)): ?>
                    <tr><td colspan="5" style="text-align: center; padding: 30px; color: #94a3b8;">No services found.</td></tr>
                <?php else: foreach ($services as $srv): ?>
                    <tr>
                        <td>#<?= $srv['id'] ?></td>
                        <td style="font-weight: 600;"><?= htmlspecialchars($srv['name']) ?></td>
                        <td style="color: #64748b; font-size: 0.85rem; max-width: 300px;"><?= htmlspecialchars($srv['description'] ?: '—') ?></td>
                        <td>
                            <a href="?toggle=<?= $srv['id'] ?>" style="text-decoration: none;">
                                <span class="badge" style="background: <?= $srv['status'] == 'active' ? '#dcfce7' : '#fee2e2' ?>; color: <?= $srv['status'] == 'active' ? '#16a34a' : '#ef4444' ?>;">
                                    <?= ucfirst($srv['status']) ?>
                                </span>
                            </a>
                        </td>
                        <td style="text-align: right;">
                            <a href="lead_service_add_edit.php?edit=<?= $srv['id'] ?>" style="color:var(--primary); display:inline-flex; align-items:center; justify-content:center; width:32px; height:32px; background:#e0f2fe; border-radius:6px; margin-right:5px;" title="Edit"><i class="fa-solid fa-pen"></i></a>
                            <a href="?delete=<?= $srv['id'] ?>" style="color:var(--danger); display:inline-flex; align-items:center; justify-content:center; width:32px; height:32px; background:#fee2e2; border-radius:6px;" title="Delete" onclick="return confirm('Are you sure you want to delete this service?')"><i class="fa-solid fa-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
// Auto-hide success messages after 3 seconds
setTimeout(function() {
    const successMsg = document.querySelector('.alert-success');
    if (successMsg) {
        successMsg.style.transition = 'opacity 0.5s ease';
        successMsg.style.opacity = '0';
        setTimeout(() => successMsg.style.display = 'none', 500);
    }
}, 3000);
</script>

<?php require_once 'includes/footer.php'; ?>
