<?= $this->extend('admin/layout') ?>

<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="h3 text-gray-800 mb-0">Manage Products</h2>
    <a href="<?= base_url('admin/products/create') ?>" class="btn btn-primary" style="background-color: var(--primary-green); border-color: var(--primary-green);">
        <i class="bi bi-plus-circle"></i> Add New Product
    </a>
</div>

<div class="card shadow mb-4">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Status</th>
                        <th>Featured</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($products)): ?>
                        <?php foreach ($products as $prod) : ?>
                        <tr>
                            <td>
                                <?php if($prod['featured_image']): ?>
                                    <img src="<?= base_url('uploads/products/' . $prod['featured_image']) ?>" alt="Img" width="50" class="img-thumbnail">
                                <?php else: ?>
                                    <span class="text-muted">No Image</span>
                                <?php endif; ?>
                            </td>
                            <td><?= esc($prod['name']) ?></td>
                            <td><?= esc($prod['category_name'] ?? 'N/A') ?></td>
                            <td>
                                <?php if($prod['status'] == 1): ?>
                                    <span class="badge bg-success">Active</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if($prod['is_featured'] == 1): ?>
                                    <i class="bi bi-star-fill text-warning"></i>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="<?= base_url('admin/products/edit/' . $prod['id']) ?>" class="btn btn-sm btn-primary">
                                    <i class="bi bi-pencil"></i> Edit
                                </a>
                                <a href="<?= base_url('admin/products/delete/' . $prod['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this product?');">
                                    <i class="bi bi-trash"></i> Delete
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center">No products found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
