<?php
$admin_title = "Kelola Artikel & Blog";
require_once 'includes/header.php';

$error = '';
$success = '';

// Handle Delete Article
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    try {
        // Fetch article details
        $stmt = $pdo->prepare("SELECT image FROM articles WHERE id = ?");
        $stmt->execute([$id]);
        $art = $stmt->fetch();
        
        if ($art) {
            // Delete image file if it exists in uploads and isn't standard seeded image
            $img = $art['image'];
            if (!empty($img) && file_exists('../uploads/' . $img) && !in_array($img, ['blog_split.jpg', 'blog_terrazzo.jpg', 'blog_abubatu.jpg'])) {
                @unlink('../uploads/' . $img);
            }
            
            $stmt = $pdo->prepare("DELETE FROM articles WHERE id = ?");
            $stmt->execute([$id]);
            $success = "Artikel berhasil dihapus!";
        }
    } catch (Exception $e) {
        $error = "Gagal menghapus artikel: " . $e->getMessage();
    }
}

// Fetch all articles
try {
    $stmt = $pdo->query("SELECT * FROM articles ORDER BY id DESC");
    $articles = $stmt->fetchAll();
} catch (Exception $e) {
    $error = "Gagal memuat artikel: " . $e->getMessage();
    $articles = [];
}
?>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;">
    <h2 style="font-size: 20px; font-weight: 700; color: var(--primary);">Daftar Posting Artikel</h2>
    <a href="articles_add.php" class="btn-submit" style="text-decoration:none; display: inline-flex; align-items:center; gap: 8px; margin: 0; padding: 10px 20px;">
        <i class="fas fa-plus"></i> Tambah Artikel Baru
    </a>
</div>

<?php if (!empty($error)): ?>
    <div style="background-color: rgba(239, 68, 68, 0.15); border: 1px solid var(--error); color: #f87171; padding: 12px 15px; border-radius: 6px; font-size: 13px; margin-bottom: 20px;">
        <i class="fas fa-exclamation-circle" style="margin-right: 5px;"></i> <?php echo $error; ?>
    </div>
<?php endif; ?>

<?php if (!empty($success)): ?>
    <div style="background-color: rgba(16, 185, 129, 0.15); border: 1px solid var(--success); color: #34d399; padding: 12px 15px; border-radius: 6px; font-size: 13px; margin-bottom: 20px;">
        <i class="fas fa-check-circle" style="margin-right: 5px;"></i> <?php echo $success; ?>
    </div>
<?php endif; ?>

<div class="table-card">
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th style="width: 80px;">Cover</th>
                    <th>Judul Artikel</th>
                    <th>Penulis</th>
                    <th>Tanggal Terbit</th>
                    <th style="text-align: center;">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($articles) > 0): ?>
                    <?php foreach ($articles as $art): ?>
                        <tr>
                            <td>
                                <?php 
                                $img_src = '../assets/images/' . $art['image'];
                                if (!empty($art['image']) && file_exists('../uploads/' . $art['image'])) {
                                    $img_src = '../uploads/' . $art['image'];
                                }
                                ?>
                                <img src="<?php echo $img_src; ?>" style="width: 70px; height: 45px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border-color);" onerror="this.src='../assets/images/about_img.jpg'">
                            </td>
                            <td>
                                <strong style="color: var(--text-dark); font-size:14px;"><?php echo htmlspecialchars($art['title']); ?></strong>
                                <div style="font-size: 11px; color: var(--text-muted); margin-top:3px;">
                                    /blog-detail.php?slug=<?php echo htmlspecialchars($art['slug']); ?>
                                </div>
                            </td>
                            <td><?php echo htmlspecialchars($art['author']); ?></td>
                            <td><?php echo date('d M Y', strtotime($art['created_at'])); ?></td>
                            <td style="text-align: center; white-space: nowrap;">
                                <a href="articles_edit.php?id=<?php echo $art['id']; ?>" class="action-btn btn-edit" title="Edit Artikel" style="margin-right: 5px;">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="?delete=<?php echo $art['id']; ?>" class="action-btn btn-delete" onclick="return confirm('Apakah Anda yakin ingin menghapus artikel &quot;<?php echo htmlspecialchars($art['title']); ?>&quot;?');" title="Hapus Artikel">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" style="text-align: center; color: var(--text-muted);">Belum ada artikel terbit. Silakan tambahkan artikel baru.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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