<?php

$labels = [
    'products' => 'Sản phẩm',
    'posts' => 'Bài viết',
    'gallery' => 'Gallery',
    'reviews' => 'Đánh giá'
];
$pageTitle = 'Kiểm duyệt ' . $labels[$type] . ' - PET SPA';
$activeAdminPage = 'moderation';
$successMessage = $_SESSION['flash_success'] ?? null;
$errorMessage = $_SESSION['flash_error'] ?? null;
unset($_SESSION['flash_success'], $_SESSION['flash_error']);

$route = 'admin/moderation/' . $type;
$statusOptions = match ($type) {
    'products' => ['active' => 'Đang bán', 'out_of_stock' => 'Hết hàng', 'hidden' => 'Đã ẩn'],
    'posts' => ['draft' => 'Nháp', 'published' => 'Đã xuất bản', 'hidden' => 'Đã ẩn'],
    default => ['visible' => 'Đang hiển thị', 'hidden' => 'Đã ẩn']
};

require BASE_PATH . '/app/views/layouts/header.php';
require BASE_PATH . '/app/views/layouts/navbar.php';
?>

<main class="admin-area"><div class="container-fluid"><div class="row g-4">
    <div class="col-lg-3 col-xl-2"><?php require BASE_PATH . '/app/views/layouts/admin-sidebar.php'; ?></div>
    <div class="col-lg-9 col-xl-10">
        <div class="d-flex justify-content-between align-items-center mb-3"><div><h1 class="h3 mb-1">Kiểm duyệt <?= htmlspecialchars($labels[$type]) ?></h1><p class="text-muted mb-0"><?= number_format($totalItems) ?> mục.</p></div><a href="<?= BASE_URL ?>/?url=admin/moderation" class="btn btn-outline-secondary">Danh mục kiểm duyệt</a></div>
        <?php if ($successMessage): ?><div class="alert alert-success"><?= htmlspecialchars($successMessage) ?></div><?php endif; ?>
        <?php if ($errorMessage): ?><div class="alert alert-danger"><?= htmlspecialchars($errorMessage) ?></div><?php endif; ?>

        <form method="GET" class="card card-body mb-3"><input type="hidden" name="url" value="<?= htmlspecialchars($route) ?>"><div class="row g-2"><div class="col-md-7"><input name="search" class="form-control" value="<?= htmlspecialchars($search) ?>" placeholder="Tìm kiếm nội dung"></div><div class="col-md-3"><select name="status" class="form-select"><option value="">Tất cả trạng thái</option><?php foreach ($statusOptions as $value => $label): ?><option value="<?= htmlspecialchars($value) ?>" <?= $status === $value ? 'selected' : '' ?>><?= htmlspecialchars($label) ?></option><?php endforeach; ?></select></div><div class="col-md-2"><button class="btn btn-dark w-100">Lọc</button></div></div></form>

        <div class="card"><div class="table-responsive"><table class="table table-hover align-middle mb-0">
            <?php if ($type === 'products'): ?>
                <thead><tr><th>Sản phẩm</th><th>SKU</th><th>Tồn kho</th><th>Trạng thái</th><th></th></tr></thead><tbody>
                <?php foreach ($items as $item): ?><tr><td><strong><?= htmlspecialchars($item['name']) ?></strong><div class="small text-muted"><?= htmlspecialchars($item['category_name']) ?></div></td><td><?= htmlspecialchars($item['sku'] ?? '') ?></td><td><?= (int) $item['stock_quantity'] ?></td><td><?= htmlspecialchars($item['status']) ?></td><td><?php if (in_array($item['status'], ['active', 'out_of_stock'], true)): ?><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/product/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="product_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="action" value="hide"><button class="btn btn-sm btn-outline-danger">Ẩn</button></form><?php elseif ($item['status'] === 'hidden'): ?><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/product/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="product_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="action" value="restore"><button class="btn btn-sm btn-outline-success">Khôi phục</button></form><?php endif; ?></td></tr><?php endforeach; ?>
                </tbody>
            <?php elseif ($type === 'posts'): ?>
                <thead><tr><th>Bài viết</th><th>Tác giả</th><th>Xuất bản</th><th>Trạng thái</th><th></th></tr></thead><tbody>
                <?php foreach ($items as $item): ?><tr><td><strong><?= htmlspecialchars($item['title']) ?></strong><div class="small text-muted"><?= htmlspecialchars($item['category_name'] ?? '') ?></div></td><td><?= htmlspecialchars($item['author_name']) ?></td><td><?= htmlspecialchars($item['published_at'] ?? '-') ?></td><td><?= htmlspecialchars($item['status']) ?></td><td><?php if ($item['status'] === 'published'): ?><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/post/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="post_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="action" value="hide"><button class="btn btn-sm btn-outline-danger">Ẩn</button></form><?php elseif ($item['status'] === 'hidden' && !empty($item['published_at'])): ?><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/post/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="post_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="action" value="restore"><button class="btn btn-sm btn-outline-success">Khôi phục</button></form><?php endif; ?></td></tr><?php endforeach; ?>
                </tbody>
            <?php elseif ($type === 'gallery'): ?>
                <thead><tr><th>Gallery</th><th>Danh mục</th><th>Trạng thái</th><th></th></tr></thead><tbody>
                <?php foreach ($items as $item): ?><tr><td><strong><?= htmlspecialchars($item['title'] ?? 'Không tiêu đề') ?></strong><div class="small text-muted"><?= htmlspecialchars($item['caption'] ?? '') ?></div></td><td><?= htmlspecialchars($item['category'] ?? '-') ?></td><td><?= (int) $item['is_visible'] === 1 ? 'visible' : 'hidden' ?></td><td><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/gallery/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="gallery_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="status" value="<?= (int) $item['is_visible'] === 1 ? 'hidden' : 'visible' ?>"><button class="btn btn-sm <?= (int) $item['is_visible'] === 1 ? 'btn-outline-danger' : 'btn-outline-success' ?>"><?= (int) $item['is_visible'] === 1 ? 'Ẩn' : 'Hiện' ?></button></form></td></tr><?php endforeach; ?>
                </tbody>
            <?php else: ?>
                <thead><tr><th>Đánh giá</th><th>Khách hàng</th><th>Sản phẩm</th><th>Trạng thái</th><th></th></tr></thead><tbody>
                <?php foreach ($items as $item): ?><tr><td><strong><?= (int) $item['rating'] ?>/5</strong><div class="small text-muted"><?= htmlspecialchars($item['comment'] ?? '') ?></div></td><td><?= htmlspecialchars($item['customer_name']) ?></td><td><?= htmlspecialchars($item['product_name']) ?></td><td><?= htmlspecialchars($item['status']) ?></td><td><form method="POST" action="<?= BASE_URL ?>/?url=admin/moderation/review/status"><input type="hidden" name="_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>"><input type="hidden" name="review_id" value="<?= (int) $item['id'] ?>"><input type="hidden" name="status" value="<?= $item['status'] === 'visible' ? 'hidden' : 'visible' ?>"><button class="btn btn-sm <?= $item['status'] === 'visible' ? 'btn-outline-danger' : 'btn-outline-success' ?>"><?= $item['status'] === 'visible' ? 'Ẩn' : 'Hiện' ?></button></form></td></tr><?php endforeach; ?>
                </tbody>
            <?php endif; ?>
            <?php if ($items === []): ?><tbody><tr><td colspan="5" class="text-center text-muted py-4">Không có dữ liệu.</td></tr></tbody><?php endif; ?>
        </table></div></div>

        <?php if ($totalPages > 1): ?><nav class="mt-3"><ul class="pagination"><?php for ($number = 1; $number <= $totalPages; $number++): ?><li class="page-item <?= $number === $page ? 'active' : '' ?>"><a class="page-link" href="<?= BASE_URL ?>/?url=<?= htmlspecialchars($route) ?>&<?= htmlspecialchars(http_build_query(['search' => $search, 'status' => $status, 'page' => $number])) ?>"><?= $number ?></a></li><?php endfor; ?></ul></nav><?php endif; ?>
    </div>
</div></div></main>

<?php require BASE_PATH . '/app/views/layouts/footer.php'; ?>
