<?php
require 'auth_check.php';
require '../config/db.php';

// 1. FETCH DAILY SALES (Completed Orders Only)
$daily_sales = $pdo->query("
    SELECT 
        DATE(created_at) as sale_date, 
        COUNT(id) as total_orders, 
        SUM(total_amount) as total_revenue 
    FROM orders 
    WHERE status = 'completed' 
    GROUP BY DATE(created_at) 
    ORDER BY sale_date DESC
")->fetchAll();

// 2. FETCH MONTHLY SALES (For Summary)
$monthly_sales = $pdo->query("
    SELECT 
        DATE_FORMAT(created_at, '%Y-%m') as sale_month, 
        COUNT(id) as total_orders, 
        SUM(total_amount) as total_revenue 
    FROM orders 
    WHERE status = 'completed' 
    GROUP BY sale_month 
    ORDER BY sale_month DESC 
    LIMIT 6
")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Sales History</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .grid-2 { display: grid; grid-template-columns: 1fr 1.5fr; gap: 20px; }
        @media(max-width:900px) { .grid-2 { grid-template-columns: 1fr; } }
        
        .card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); }
        h3 { margin-top: 0; color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; }
        
        table { width: 100%; border-collapse: collapse; }
        th { background: #f8f9fa; padding: 10px; text-align: left; color: #7f8c8d; font-size: 13px; }
        td { padding: 12px 10px; border-bottom: 1px solid #eee; color: #2c3e50; }
        
        .amount { font-weight: bold; color: #27ae60; }
        .date { font-weight: 600; color: #34495e; }
        .badge { background: #e8f6f3; color: #1abc9c; padding: 3px 8px; border-radius: 10px; font-size: 11px; }
    </style>
</head>
<body>

    <div class="sidebar">
        <?php include 'sidebar.php'; ?>
    </div>

    <div class="content">
        <h1><i class="fas fa-history"></i> Sales History</h1>
        
        <div class="grid-2">
            
            <div class="card">
                <h3>Monthly Performance</h3>
                <table>
                    <thead>
                        <tr>
                            <th>Month</th>
                            <th>Orders</th>
                            <th>Revenue</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($monthly_sales as $m): ?>
                        <tr>
                            <td>
                                <span class="date"><?php echo date('F Y', strtotime($m['sale_month'])); ?></span>
                            </td>
                            <td><?php echo number_format($m['total_orders']); ?> Orders</td>
                            <td class="amount">₹<?php echo number_format($m['total_revenue']); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <div class="card">
                <h3>Daily Breakdown</h3>
                <table>
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Sales Count</th>
                            <th>Total Earnings</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(count($daily_sales) > 0): ?>
                            <?php foreach($daily_sales as $d): ?>
                            <tr>
                                <td>
                                    <span class="date"><?php echo date('d M, Y', strtotime($d['sale_date'])); ?></span><br>
                                    <small style="color:#999;"><?php echo date('l', strtotime($d['sale_date'])); ?></small>
                                </td>
                                <td>
                                    <span class="badge"><?php echo $d['total_orders']; ?> Orders</span>
                                </td>
                                <td class="amount" style="font-size: 16px;">
                                    ₹<?php echo number_format($d['daily_revenue']); ?>
                                </td>
                                <td>
                                    <a href="orders.php?date=<?php echo $d['sale_date']; ?>" style="color:#3498db; font-size:12px; text-decoration:none;">View Orders</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr><td colspan="4" style="text-align:center; color:#ccc; padding:20px;">No sales recorded yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>

</body>
</html>