<?php

$pageTitle = 'Báo cáo sản phẩm - PET SPA';
$pageStyles = ['owner.css'];
$activeOwnerPage = 'reports';
$reportRoute = 'owner/reports/products';
$reportFilters = ['search' => $search, 'sort' => $sort];
$baseQuery = [
    'url' => $reportRoute,
    'range' => $range['key'],
    'from' => $range['from'],
    'to' => $range['to'],
    'search' => $search,
    'sort' => $sort
];

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

?>

<main class="owner-area"><div class="container-fluid"><div class="row g-4">
    <div class="col-xl-2 col-lg-3"><?php require BASE_PATH . '/app/views/layouts/owner-sidebar.php'; ?></div>
    <div class="col-xl-10 col-lg-9">
        <div class="owner-page-header"><div><h1>Chi tiết doanh số sản phẩm</h1><p>Chỉ tính đơn hoàn thành: <?= htmlspecialchars($range['label']) ?></p></div></div>

        <?php require BASE_PATH . '/app/views/owner/reports/_date-filter.php'; ?>
        <?php if ($range['error']): ?><div class="alert alert-danger"><?= htmlspecialchars($range['error']) ?></div><?php endif; ?>

        <section class="owner-card mb-4">
            <form method="GET" class="row g-3 align-items-end">
                <input type="hidden" name="url" value="owner/reports/products">
                <input type="hidden" name="range" value="<?= htmlspecialchars($range['key']) ?>">
                <input type="hidden" name="from" value="<?= htmlspecialchars($range['from']) ?>">
                <input type="hidden" name="to" value="<?= htmlspecialchars($range['to']) ?>">
                <div class="col-md-6"><label class="form-label" for="product-search">Tìm sản phẩm/SKU</label><input id="product-search" type="text" name="search" class="form-control" value="<?= htmlspecialchars($search) ?>"></div>
                <div class="col-md-3"><label class="form-label" for="product-sort">Sắp xếp</label><select id="product-sort" name="sort" class="form-select"><option value="quantity" <?= $sort === 'quantity' ? 'selected' : '' ?>>Số lượng bán giảm dần</option><option value="revenue" <?= $sort === 'revenue' ? 'selected' : '' ?>>Doanh thu giảm dần</option><option value="name" <?= $sort === 'name' ? 'selected' : '' ?>>Tên sản phẩm</option></select></div>
                <div class="col-md-3"><button class="btn btn-dark w-100">Lọc dữ liệu</button></div>
            </form>
        </section>

        <section class="owner-card">
            <div class="owner-card-header"><h2><?= number_format($totalProducts) ?> sản phẩm có doanh số</h2><a href="<?= BASE_URL ?>/?<?= htmlspecialchars(http_build_query(array_merge($baseQuery, ['url' => 'owner/reports/export/products']))) ?>">Xuất CSV</a></div>
            <div class="table-responsive"><table class="table align-middle owner-table"><thead><tr><th>Sản phẩm</th><th>SKU</th><th>Đã bán</th><th>Đơn hoàn thành</th><th>Doanh thu snapshot</th></tr></thead><tbody>
                <?php foreach ($productSales as $product): ?><tr><td><strong><?= htmlspecialchars($product['product_name']) ?></strong></td><td><?= htmlspecialchars($product['sku'] ?? '') ?></td><td><?= (int) $product['quantity_sold'] ?></td><td><?= (int) $product['order_count'] ?></td><td><?= number_format((float) $product['revenue'], 0, ',', '.') ?> <?= htmlspecialchars($currency) ?></td></tr><?php endforeach; ?>
                <?php if ($productSales === []): ?><tr><td colspan="5" class="text-center text-muted py-5">Không có dữ liệu bán hàng cho khoảng đã chọn.</td></tr><?php endif; ?>
            </tbody></table></div>
            <small class="text-muted d-block mt-3">Doanh thu dùng `order_items.line_total` tại thời điểm đặt hàng. CSV xuất tối đa 5.000 dòng theo bộ lọc hiện tại.</small>
        </section>

        <?php if ($totalPages > 1): ?><nav class="mt-4"><ul class="pagination justify-content-center"><?php for ($i = 1; $i <= $totalPages; $i++): ?><li class="page-item <?= $i === $page ? 'active' : '' ?>"><a class="page-link" href="<?= BASE_URL ?>/?<?= htmlspecialchars(http_build_query(array_merge($baseQuery, ['page' => $i]))) ?>"><?= $i ?></a></li><?php endfor; ?></ul></nav><?php endif; ?>
    </div>
</div></div></main>

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