<?php

$pageTitle = 'Báo cáo khách hàng - PET SPA';
$pageStyles = ['owner.css'];
$activeOwnerPage = 'reports';
$reportRoute = 'owner/reports/customers';
$reportFilters = ['search' => $search];
$baseQuery = [
    'url' => $reportRoute,
    'range' => $range['key'],
    'from' => $range['from'],
    'to' => $range['to'],
    'search' => $search
];

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>Khách hàng theo doanh số</h1><p>Chỉ gồm khách có đơ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/customers"><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-9"><label class="form-label" for="customer-search">Tìm tên, số điện thoại hoặc email</label><input id="customer-search" type="text" name="search" class="form-control" value="<?= htmlspecialchars($search) ?>"></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($totalCustomers) ?> khách hàng có phát sinh</h2><a href="<?= BASE_URL ?>/?<?= htmlspecialchars(http_build_query(array_merge($baseQuery, ['url' => 'owner/reports/export/customers']))) ?>">Xuất CSV</a></div>
            <div class="table-responsive"><table class="table align-middle owner-table"><thead><tr><th>Khách hàng</th><th>Số điện thoại</th><th>Email</th><th>Đơn hoàn thành</th><th>Tổng chi tiêu</th><th>Đơn TB</th><th>Hoàn thành gần nhất</th></tr></thead><tbody>
                <?php foreach ($topCustomers as $customer): ?><tr><td><strong><?= htmlspecialchars($customer['full_name']) ?></strong></td><td><?= htmlspecialchars($customer['phone']) ?></td><td><?= htmlspecialchars($customer['email'] ?? '') ?></td><td><?= (int) $customer['completed_order_count'] ?></td><td><?= number_format((float) $customer['total_spent'], 0, ',', '.') ?> <?= htmlspecialchars($currency) ?></td><td><?= number_format((float) $customer['average_order_value'], 0, ',', '.') ?> <?= htmlspecialchars($currency) ?></td><td><?= htmlspecialchars($customer['last_completed_at']) ?></td></tr><?php endforeach; ?>
                <?php if ($topCustomers === []): ?><tr><td colspan="7" class="text-center text-muted py-5">Không có khách hàng phù hợp.</td></tr><?php endif; ?>
            </tbody></table></div>
            <small class="text-muted d-block mt-3">CSV xuất tối đa 5.000 dòng. Báo cáo không bao gồm dữ liệu xác thực hoặc bảo mật của khách hàng.</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'; ?>
