<?php
require_once __DIR__ . '/_admin.php';
$pdo = pdoOrFail();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['delete'])) {
        $stmt = $pdo->prepare('DELETE FROM services WHERE id=:id');
        $stmt->execute([':id' => (int)$_POST['id']]);
    } elseif (!empty($_POST['id'])) {
        $stmt = $pdo->prepare('UPDATE services SET title=:title, slug=:slug, description=:description, icon=:icon WHERE id=:id');
        $stmt->execute([':id'=>(int)$_POST['id'], ':title'=>trim($_POST['title']), ':slug'=>slugify($_POST['title']), ':description'=>trim($_POST['description']), ':icon'=>trim($_POST['icon'])]);
    } else {
        $stmt = $pdo->prepare('INSERT INTO services (title, slug, description, icon, created_at) VALUES (:title, :slug, :description, :icon, NOW())');
        $stmt->execute([':title'=>trim($_POST['title']), ':slug'=>slugify($_POST['title']), ':description'=>trim($_POST['description']), ':icon'=>trim($_POST['icon'])]);
    }
    $_SESSION['flash'] = 'Hizmet kaydedildi.';
    header('Location: services.php');
    exit;
}
$services = $pdo->query('SELECT * FROM services ORDER BY id ASC')->fetchAll();
adminHeader('Hizmet Yonetimi');
?>
<form class="admin-card admin-form" method="post"><h2>Yeni Hizmet</h2><div class="form-grid"><label>Baslik<input name="title" required></label><label>Ikon<input name="icon" value="waves"></label></div><label>Aciklama<textarea name="description" required></textarea></label><button class="btn btn-primary">Ekle</button></form>
<?php foreach ($services as $service): ?><form class="admin-card admin-form" method="post"><input type="hidden" name="id" value="<?= e($service['id']) ?>"><div class="form-grid"><label>Baslik<input name="title" value="<?= e($service['title']) ?>" required></label><label>Ikon<input name="icon" value="<?= e($service['icon']) ?>"></label></div><label>Aciklama<textarea name="description"><?= e($service['description']) ?></textarea></label><button class="btn btn-primary">Kaydet</button><button class="btn btn-ghost" name="delete" value="1" onclick="return confirm('Silinsin mi?')">Sil</button></form><?php endforeach; ?>
<?php adminFooter(); ?>
