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

// Set timezone untuk pastikan ia selari
date_default_timezone_set('Asia/Kuala_Lumpur');

echo "<pre style='font-family: monospace; font-size: 14px; line-height: 1.6;'>";

// 1. Paparkan Waktu Semasa
echo "<h2>Pemeriksaan Waktu Server</h2>";
echo "Waktu Server (UTC):        " . gmdate('Y-m-d H:i:s') . "\n";
echo "Waktu Server (Malaysia):   " . date('Y-m-d H:i:s') . "\n";
echo "Tarikh 'hari ini' SQLite:  " . db()->query("SELECT date('now', '+8 hours') as d")->fetch()['d'] . "\n\n";

// 2. Jalankan Query yang Sama Seperti Dashboard
echo "<h2>Query Jualan Hari Ini (Status 'Paid' atau 'Shipped')</h2>";
$query_text = "SELECT id, created_at, amount, status FROM orders WHERE status IN ('Paid','Shipped') AND date(created_at, '+8 hours') = date('now', '+8 hours')";
echo "<strong>Query yang dijalankan:</strong>\n$query_text\n\n";

try {
    $st = db()->query($query_text);
    $rows = $st->fetchAll(PDO::FETCH_ASSOC);

    if (empty($rows)) {
        echo "<strong>Hasil: TIADA PESANAN 'Paid' atau 'Shipped' DIJUMPAI UNTUK HARI INI.</strong>\n";
    } else {
        echo "<strong>Hasil (" . count($rows) . " pesanan dijumpai):</strong>\n";
        print_r($rows);
    }

    // 3. Kira jumlah secara manual dari hasil
    $total_amount = 0;
    foreach ($rows as $row) {
        $total_amount += (float)$row['amount'];
    }
    echo "\n<strong>Jumlah Jualan (dikira dari hasil di atas): " . money($total_amount) . "</strong>\n";

} catch (PDOException $e) {
    echo "RALAT SEMASA MENJALANKAN QUERY: " . $e->getMessage();
}

echo "</pre>";