<?php
$admin_title = "Kelola Produk";
require_once 'includes/header.php';

$message = '';
$status = '';

// Handle Delete Request
if (isset($_GET['delete'])) {
    $del_id = intval($_GET['delete']);
    
    try {
        // Fetch image first
        $stmt = $pdo->prepare("SELECT image FROM products WHERE id = ?");
        $stmt->execute([$del_id]);
        $prod = $stmt->fetch();
        
        if ($prod) {
            // Delete image file if it exists in uploads folder
            $image_path = __DIR__ . '/../uploads/' . $prod['image'];
            if (!empty($prod['image']) && file_exists($image_path)) {
                @unlink($image_path);
            }
            
            // Delete record
            $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
            $stmt->execute([$del_id]);
            
            $message = "Produk berhasil dihapus.";
            $status = "success";
        }
    } catch (Exception $e) {
        $message = "Gagal menghapus produk: " . $e->getMessage();
        $status = "error";
    }
}

// Fetch all products
try {
    $stmt = $pdo->prepare("SELECT * FROM products ORDER BY id DESC");
    $stmt->execute();
    $products = $stmt->fetchAll();
} catch (Exception $e) {
    $products = [];
}
?>

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

<div class="data-card">
    <div class="data-card-header">
        <h2>Daftar Produk Batu Alam</h2>
        <a href="products_add.php" class="btn-add">
            <i class="fas fa-plus"></i> Tambah Produk Baru
        </a>
    </div>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Gambar</th>
                    <th>Nama Produk</th>
                    <th>Kategori</th>
                    <th>Finishing</th>
                    <th>Ukuran</th>
                    <th>Unggulan</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($products) > 0): ?>
                    <?php foreach ($products as $p): ?>
                        <tr>
                            <td>
                                <?php 
                                $img_src = '../assets/images/' . $p['image'];
                                if (!empty($p['image']) && file_exists('../uploads/' . $p['image'])) {
                                    $img_src = '../uploads/' . $p['image'];
                                }
                                ?>
                                <img src="<?php echo $img_src; ?>" alt="Thumb" class="thumbnail" onerror="this.src='https://images.unsplash.com/photo-1590381105924-c72589b9ef3f?auto=format&fit=crop&w=100&q=80'">
                            </td>
                            <td><strong><?php echo htmlspecialchars($p['name']); ?></strong></td>
                            <td><span class="badge badge-info"><?php echo htmlspecialchars($p['category']); ?></span></td>
                            <td><?php echo htmlspecialchars($p['finishing']); ?></td>
                            <td><?php echo htmlspecialchars($p['size_available']); ?></td>
                            <td>
                                <?php if ($p['is_featured'] == 1): ?>
                                    <span class="badge badge-success">Ya</span>
                                <?php else: ?>
                                    <span class="badge badge-warning" style="background: rgba(100,116,139,0.1); color: var(--text-muted);">Tidak</span>
                                <?php endif; ?>
                            </td>
                            <td class="action-btns">
                                <a href="products_edit.php?id=<?php echo $p['id']; ?>" class="btn-action edit" title="Edit"><i class="fas fa-edit"></i></a>
                                <a href="products.php?delete=<?php echo $p['id']; ?>" class="btn-action delete" title="Hapus" onclick="return confirm('Apakah Anda yakin ingin menghapus produk ini? Semua data dan file gambar terkait akan terhapus.');"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7" style="text-align: center; color: var(--text-muted); padding: 30px;">Belum ada produk.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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