<?php
require_once __DIR__ . '/_admin.php';
$pdo = pdoOrFail();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['delete'])) {
        $stmt = $pdo->prepare('DELETE FROM blog_posts WHERE id=:id');
        $stmt->execute([':id' => (int)$_POST['id']]);
    } elseif (!empty($_POST['id'])) {
        $stmt = $pdo->prepare('UPDATE blog_posts SET title=:title, slug=:slug, excerpt=:excerpt, content=:content, image=:image, seo_title=:seo_title, seo_description=:seo_description, seo_keywords=:seo_keywords WHERE id=:id');
        $stmt->execute([':id'=>(int)$_POST['id'], ':title'=>trim($_POST['title']), ':slug'=>slugify($_POST['title']), ':excerpt'=>trim($_POST['excerpt']), ':content'=>trim($_POST['content']), ':image'=>trim($_POST['image']), ':seo_title'=>trim($_POST['seo_title']), ':seo_description'=>trim($_POST['seo_description']), ':seo_keywords'=>trim($_POST['seo_keywords'])]);
    } else {
        $stmt = $pdo->prepare('INSERT INTO blog_posts (title, slug, excerpt, content, image, seo_title, seo_description, seo_keywords, created_at) VALUES (:title, :slug, :excerpt, :content, :image, :seo_title, :seo_description, :seo_keywords, NOW())');
        $stmt->execute([':title'=>trim($_POST['title']), ':slug'=>slugify($_POST['title']), ':excerpt'=>trim($_POST['excerpt']), ':content'=>trim($_POST['content']), ':image'=>trim($_POST['image']), ':seo_title'=>trim($_POST['seo_title']), ':seo_description'=>trim($_POST['seo_description']), ':seo_keywords'=>trim($_POST['seo_keywords'])]);
    }
    $_SESSION['flash'] = 'Blog kaydedildi.';
    header('Location: blog.php');
    exit;
}
$posts = $pdo->query('SELECT * FROM blog_posts ORDER BY created_at DESC, id DESC')->fetchAll();
adminHeader('Blog Yonetimi');
?>
<form class="admin-card admin-form" method="post"><h2>Yeni Blog</h2><div class="form-grid"><label>Baslik<input name="title" required></label><label>Gorsel<input name="image" value="assets/img/blog-placeholder.jpg"></label></div><label>Ozet<textarea name="excerpt"></textarea></label><label>Icerik HTML<textarea name="content" rows="6"></textarea></label><div class="form-grid"><label>SEO title<input name="seo_title"></label><label>SEO keywords<input name="seo_keywords"></label></div><label>SEO description<textarea name="seo_description"></textarea></label><button class="btn btn-primary">Ekle</button></form>
<?php foreach ($posts as $post): ?><form class="admin-card admin-form" method="post"><input type="hidden" name="id" value="<?= e($post['id']) ?>"><div class="form-grid"><label>Baslik<input name="title" value="<?= e($post['title']) ?>" required></label><label>Gorsel<input name="image" value="<?= e($post['image']) ?>"></label></div><label>Ozet<textarea name="excerpt"><?= e($post['excerpt']) ?></textarea></label><label>Icerik HTML<textarea name="content" rows="6"><?= e($post['content']) ?></textarea></label><div class="form-grid"><label>SEO title<input name="seo_title" value="<?= e($post['seo_title']) ?>"></label><label>SEO keywords<input name="seo_keywords" value="<?= e($post['seo_keywords']) ?>"></label></div><label>SEO description<textarea name="seo_description"><?= e($post['seo_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(); ?>
