<?php

$pageTitle = 'Hiệu quả khuyến mãi - PET SPA';
$pageStyles = ['owner.css'];
$activeOwnerPage = 'reports';
$reportRoute = 'owner/reports/promotions';
$reportFilters = [];

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>Hiệu quả khuyến mãi</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">
            <div class="owner-card-header"><h2>Khuyến mãi đã được sử dụng</h2></div>
            <div class="table-responsive"><table class="table align-middle owner-table"><thead><tr><th>Khuyến mãi</th><th>Mã</th><th>Đơn hoàn thành</th><th>Tổng giảm giá</th><th>Doanh thu tạo ra</th></tr></thead><tbody>
                <?php foreach ($promotionPerformance as $promotion): ?><tr><td><strong><?= htmlspecialchars($promotion['promotion_name']) ?></strong></td><td><?= htmlspecialchars($promotion['promotion_code'] ?? '') ?></td><td><?= (int) $promotion['completed_order_count'] ?></td><td><?= number_format((float) $promotion['total_discount'], 0, ',', '.') ?> <?= htmlspecialchars($currency) ?></td><td><?= number_format((float) $promotion['generated_revenue'], 0, ',', '.') ?> <?= htmlspecialchars($currency) ?></td></tr><?php endforeach; ?>
                <?php if ($promotionPerformance === []): ?><tr><td colspan="5" class="text-center text-muted py-5">Không có khuyến mãi nào được dùng trong đơn hoàn thành.</td></tr><?php endif; ?>
            </tbody></table></div>
            <small class="text-muted d-block mt-3">Số liệu lấy từ đơn hàng thực tế, không dùng bộ đếm vận hành `promotions.used_count`.</small>
        </section>
    </div>
</div></div></main>

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