<?php
session_start();
define('BASE_URL', '/eleganceofindia/admin-panal/');
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/db.php';
require_auth();
$pageTitle = 'Blogs';
$currentPage = 'blogs';
$flash = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);
$editId = (int)($_GET['edit'] ?? 0);
$editRow = null;
if ($editId) {
$stmt = $pdo->prepare('SELECT * FROM blogs WHERE id = ?');
$stmt->execute([$editId]);
$editRow = $stmt->fetch();
}
$blogs = $pdo->query('SELECT * FROM blogs ORDER BY created_at DESC')->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Blogs — Elegance of India Admin</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" />
<link rel="stylesheet" href="assets/admin.css" />
<!-- Optional: Include a rich text editor if needed, but standard textarea for now -->
</head>
<body>
<?php include __DIR__ . '/includes/sidebar.php'; ?>
<div class="main-content">
<?php include __DIR__ . '/includes/topbar.php'; ?>
<div class="page-body">
<?php if ($flash): ?>
<div class="alert alert-<?= $flash['type'] ?>"><i class="fas fa-<?= $flash['type'] === 'success' ? 'check-circle' : 'exclamation-circle' ?>"></i> <?= htmlspecialchars($flash['msg']) ?></div>
<?php endif; ?>
<!-- Add / Edit Form -->
<div class="acard">
<div class="acard-header">
<span class="acard-title"><?= $editRow ? 'Edit Blog' : 'Add New Blog' ?></span>
<?php if ($editRow): ?>
<a href="blogs.php" class="btn btn-outline btn-sm"><i class="fas fa-times"></i> Cancel Edit</a>
<?php endif; ?>
</div>
<div class="acard-body">
<form method="POST" action="blogs_action.php" enctype="multipart/form-data">
<input type="hidden" name="action" value="<?= $editRow ? 'edit' : 'add' ?>" />
<?php if ($editRow): ?>
<input type="hidden" name="id" value="<?= $editRow['id'] ?>" />
<?php endif; ?>
<div class="form-row" style="display:flex; flex-wrap:wrap; gap:15px;">
<div class="form-group" style="flex:1; min-width:300px;">
<label>Title *</label>
<input type="text" name="title" class="form-control" placeholder="e.g. Top 10 Sarees for Wedding"
value="<?= htmlspecialchars($editRow['title'] ?? '') ?>" required />
</div>
<div class="form-group" style="flex:1; min-width:300px;">
<label>Slug (Leave blank to auto-generate)</label>
<input type="text" name="slug" class="form-control" placeholder="top-10-sarees"
value="<?= htmlspecialchars($editRow['slug'] ?? '') ?>" />
</div>
</div>
<div class="form-group">
<label>Short Description</label>
<textarea name="short_description" class="form-control" rows="2" placeholder="Brief summary of the blog..."><?= htmlspecialchars($editRow['short_description'] ?? '') ?></textarea>
</div>
<div class="form-group">
<label>Content</label>
<textarea name="content" class="form-control" rows="8" placeholder="Full blog content (HTML allowed)..."><?= htmlspecialchars($editRow['content'] ?? '') ?></textarea>
</div>
<div class="form-row" style="display:flex; flex-wrap:wrap; gap:15px;">
<div class="form-group" style="flex:1;">
<label>Featured Image</label>
<input type="hidden" name="existing_image" value="<?= htmlspecialchars($editRow['image_url'] ?? '') ?>" />
<input type="file" name="image" class="form-control" accept="image/*" />
<?php if (!empty($editRow['image_url'])): ?>
<div style="margin-top:10px;">
<img src="../<?= htmlspecialchars($editRow['image_url']) ?>"
style="height:60px; border-radius:4px; border:1px solid #ddd;" alt="Current" />
<p style="font-size:10px; color:#888; margin-top:2px;">Current Image</p>
</div>
<?php endif; ?>
</div>
<div class="form-group" style="flex:1; display:flex; align-items:center; gap:10px; padding-top:25px;">
<input type="checkbox" name="is_published" id="is_published" <?= ($editRow['is_published'] ?? 1) ? 'checked' : '' ?> />
<label for="is_published" style="margin:0; cursor:pointer;">Publish Now</label>
</div>
</div>
<div class="form-group" style="margin-top:15px;">
<button type="submit" class="btn btn-gold">
<i class="fas fa-<?= $editRow ? 'save' : 'plus' ?>"></i> <?= $editRow ? 'Update Blog' : 'Add Blog' ?>
</button>
</div>
</form>
</div>
</div>
<!-- Blogs List -->
<div class="acard">
<div class="acard-header">
<span class="acard-title">All Blogs (<?= count($blogs) ?>)</span>
</div>
<div class="atable-wrap">
<?php if (empty($blogs)): ?>
<div class="empty-state"><i class="fas fa-blog"></i><p>No blogs yet.</p></div>
<?php else: ?>
<table class="atable">
<thead>
<tr>
<th>#</th>
<th>Image</th>
<th>Title</th>
<th>Status</th>
<th>Created On</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($blogs as $i => $blog): ?>
<tr>
<td><?= $i + 1 ?></td>
<td>
<?php if (!empty($blog['image_url'])): ?>
<img src="../<?= htmlspecialchars($blog['image_url']) ?>"
style="width:50px; height:40px; object-fit:cover; border-radius:4px; border:1px solid rgba(255,255,255,0.1);" />
<?php else: ?>
<div style="width:50px; height:40px; background:rgba(255,255,255,0.05); border-radius:4px; display:flex; align-items:center; justify-content:center; color:#555;">
<i class="fas fa-image" style="font-size:12px;"></i>
</div>
<?php endif; ?>
</td>
<td class="name-cell">
<div style="font-weight:600;"><?= htmlspecialchars($blog['title']) ?></div>
<div style="font-size:11px; color:#888;">/blog-details.php?slug=<?= htmlspecialchars($blog['slug']) ?></div>
</td>
<td>
<?php if ($blog['is_published']): ?>
<span style="background:#2ecc71; color:#fff; padding:2px 8px; border-radius:12px; font-size:11px;">Published</span>
<?php else: ?>
<span style="background:#e74c3c; color:#fff; padding:2px 8px; border-radius:12px; font-size:11px;">Draft</span>
<?php endif; ?>
</td>
<td><?= date('d M Y, H:i', strtotime($blog['created_at'])) ?></td>
<td>
<div class="actions">
<a href="blogs.php?edit=<?= $blog['id'] ?>" class="btn btn-outline btn-sm"><i class="fas fa-pen"></i> Edit</a>
<form method="POST" action="blogs_action.php" style="display:inline;"
onsubmit="return confirm('Delete blog "<?= htmlspecialchars(addslashes($blog['title'])) ?>"?')">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= $blog['id'] ?>" />
<button type="submit" class="btn btn-danger btn-sm"><i class="fas fa-trash"></i></button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.getElementById('sidebarToggle')?.addEventListener('click', () => {
document.querySelector('.sidebar').classList.toggle('open');
});
</script>
</body>
</html>