<?php
require_once __DIR__.'/../inc/config.php';
require_once __DIR__.'/../inc/db.php';
require_once __DIR__.'/../inc/helpers.php';

// Sekuriti: Pastikan hanya admin yang log masuk boleh akses
if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; }
if ($_SESSION['admin_role'] !== 'admin') {
    exit('Akses disekat. Anda tiada kebenaran untuk melihat halaman ini.');
}

// Query untuk mengumpul dan mengagregat data UTM dari pesanan yang telah dibayar
$utm_data = db()->query("
    SELECT
        utm_source,
        SUM(amount) as total_sales,
        COUNT(id) as total_orders,
        AVG(amount) as average_order_value
    FROM
        orders
    WHERE
        status IN ('Paid', 'Shipped')
        AND utm_source IS NOT NULL
        AND utm_source != ''
    GROUP BY
        utm_source
    ORDER BY
        total_sales DESC
")->fetchAll();
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../public/assets/css/style.css">
    <title>Laporan Prestasi UTM</title>
    <style>
        .admin-wrapper { display: flex; align-items: flex-start; gap: 24px; }
        .admin-sidebar { width: 220px; flex-shrink: 0; }
        .admin-content { flex-grow: 1; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 12px; text-align: left; border: 1px solid #e5e7eb; }
        th { background-color: #f8fafc; }
        td:not(:first-child) { text-align: right; }
    </style>
</head>
<body>
<div class="container">
    <div class="admin-wrapper">
        <aside class="admin-sidebar"><?php require_once '_menu.php'; ?></aside>
        <main class="admin-content">
            <h1>Laporan Prestasi UTM</h1>

            <div class="card">
                <h2>Pecahan Jualan Mengikut Sumber Trafik</h2>
                <p>Laporan ini menganalisis pesanan yang telah dibayar (`Paid` & `Shipped`) untuk menunjukkan sumber trafik mana yang paling menguntungkan.</p>
                <table>
                    <tr>
                        <th>Sumber (utm_source)</th>
                        <th style="text-align: right;">Jumlah Jualan</th>
                        <th style="text-align: right;">Bilangan Pesanan</th>
                        <th style="text-align: right;">Nilai Purata Pesanan (AOV)</th>
                    </tr>
                    <?php if (empty($utm_data)): ?>
                        <tr>
                            <td colspan="4" style="text-align: center;">Tiada data UTM direkodkan untuk pesanan berbayar.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($utm_data as $row): ?>
                        <tr>
                            <td><strong><?= e($row['utm_source']) ?></strong></td>
                            <td><?= money($row['total_sales']) ?></td>
                            <td><?= number_format($row['total_orders']) ?></td>
                            <td><?= money($row['average_order_value']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </table>
            </div>

        </main>
    </div>
</div>
</body>
</html>