<?php
use Core\App\Models\CategoryModel;
$categoriesModel = new CategoryModel();
$categories = $categoriesModel->getAll();
include __DIR__ . '/../header.php';
?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-tags me-2 text-primary"></i><?= __('categories.title','Categories') ?></h3>
        <p class="text-muted mb-0 small"><?= __('categories.subtitle','Organize your products into categories') ?></p>
    </div>
    <a href="/admin/categories/create" class="btn btn-primary">
        <i class="bi bi-plus-lg me-1"></i><?= __('categories.add','Add Category') ?>
    </a>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom d-flex justify-content-between align-items-center py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-list-ul me-2 text-muted"></i><?= __('categories.list','Categories List') ?></h6>
        <span class="badge bg-secondary rounded-pill"><?= count($categories) ?></span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($categories)): ?>
        <div class="text-center py-5 text-muted">
            <i class="bi bi-tags display-4 d-block mb-2 opacity-50"></i>
            <p class="mb-3"><?= __('categories.empty','No categories added yet.') ?></p>
            <a href="/admin/categories/create" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i><?= __('categories.add','Add Category') ?></a>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3" style="width:72px"><?= __('categories.table.image','Image') ?></th>
                        <th><?= __('categories.table.name','Name') ?></th>
                        <th><?= __('categories.table.status','Status') ?></th>
                        <th><?= __('categories.table.created','Created') ?></th>
                        <th class="text-end pe-3"><?= __('categories.table.actions','Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($categories as $cat): ?>
                <tr>
                    <td class="ps-3">
                        <?php if (!empty($cat['image'])): ?>
                            <img src="<?= htmlspecialchars($cat['image']) ?>" class="rounded-2" style="width:52px;height:52px;object-fit:cover">
                        <?php else: ?>
                            <div class="bg-light rounded-2 d-flex align-items-center justify-content-center" style="width:52px;height:52px">
                                <i class="bi bi-tag text-muted"></i>
                            </div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div class="fw-semibold"><?= htmlspecialchars($cat['name']) ?></div>
                        <div class="text-muted" style="font-size:.73rem">/<?= htmlspecialchars($cat['slug']) ?></div>
                    </td>
                    <td>
                        <?php if ($cat['status'] === 'active'): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle">
                                <i class="bi bi-check-circle me-1"></i><?= __('actions.active','Active') ?>
                            </span>
                        <?php else: ?>
                            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">
                                <i class="bi bi-pause-circle me-1"></i><?= __('actions.inactive','Inactive') ?>
                            </span>
                        <?php endif; ?>
                    </td>
                    <td class="text-muted small"><?= date('d/m/Y', strtotime($cat['created_at'])) ?></td>
                    <td class="text-end pe-3">
                        <a href="/admin/categories/edit/<?= (int)$cat['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                        <form method="post" action="/admin/categories/delete/<?= (int)$cat['id'] ?>" class="d-inline"
                              onsubmit="return confirm('<?= __('categories.confirm_delete','Delete this category?') ?>')">
                            <?= \Core\System\Csrf::field() ?>
                            <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

</div></section></div>
<?php include __DIR__ . '/../footer.php'; ?>