<?php $__env->startSection('content'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const customerInput = document.querySelector('input[name="customer"]');
    const productInput = document.querySelector('input[name="product"]');
    
    customerInput.addEventListener('input', function() {
        if (this.value === '') {
            const form = this.closest('form');
            const url = new URL(form.action);
            url.searchParams.delete('customer');
            window.location.href = url.toString();
        }
    });

    productInput.addEventListener('input', function() {
        const form = this.closest('form');
        const url = new URL(form.action);
        if (this.value === '') {
            url.searchParams.delete('product');
        } else {
            url.searchParams.set('product', this.value);
        }
        url.searchParams.delete('page');
        window.location.href = url.toString();
    });
});
</script>
<style>
@media print {
    @page { size: landscape; margin: 10mm; }
    body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .btn, .card-header form, .no-print, .summary-badges { display: none !important; }
    .card { border: none !important; box-shadow: none !important; }
    .card-body { padding: 0 !important; }
    .table { font-size: 9px; }
    .table th, .table td { padding: 3px !important; }
    .row.mb-4 .card { page-break-inside: avoid; margin-bottom: 10px !important; }
    .print-header { display: block !important; margin-bottom: 15px; }
    .print-footer { display: flex !important; }
}
.print-header, .print-footer { display: none; }
</style>

<div class="d-flex justify-content-between align-items-center mb-4 no-print">
    <div>
        <h4>Sales Report</h4>
    </div>
    <div class="text-end summary-badges">
        <div class="badge bg-success fs-6 me-1">Cash: Kes <?php echo e(number_format($paymentMethodTotals['cash'], 2)); ?></div>
        <div class="badge bg-primary fs-6 me-1">M-Pesa: Kes <?php echo e(number_format($paymentMethodTotals['mpesa'], 2)); ?></div>
        <div class="badge bg-warning text-dark fs-6">Card: Kes <?php echo e(number_format($paymentMethodTotals['card'], 2)); ?></div>
    </div>
</div>
<div class="d-flex justify-content-end mb-4 no-print">
    <a href="<?php echo e(route('reports.sales.export', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'product' => $product ?? ''])); ?>" class="btn btn-success me-2">
        <i class="bi bi-download"></i> Export CSV
    </a>
    <button onclick="window.print()" class="btn btn-danger">
        <i class="bi bi-file-earmark-pdf"></i> Print PDF
    </button>
</div>

<div class="card mb-4">
    <div class="card-header no-print">
        <form method="GET" action="<?php echo e(route('reports.sales')); ?>" class="row g-3">
            <div class="col-md-3">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="<?php echo e($dateFrom); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="<?php echo e($dateTo); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Customer</label>
                <input type="text" name="customer" class="form-control" placeholder="Search customer or 'Walk-in'..." value="<?php echo e($customer ?? ''); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Product</label>
                <input type="text" name="product" class="form-control" placeholder="Search product..." value="<?php echo e($product ?? ''); ?>">
            </div>
            <div class="col-md-4">
                <label class="form-label">Quick Filters</label>
                <div class="d-flex gap-2">
                    <a href="<?php echo e(route('reports.sales', ['date_from' => date('Y-m-d'), 'date_to' => date('Y-m-d')])); ?>" class="btn btn-sm <?php echo e($dateFrom == date('Y-m-d') && $dateTo == date('Y-m-d') ? 'btn-primary' : 'btn-outline-primary'); ?>">Today</a>
                    <a href="<?php echo e(route('reports.sales', ['date_from' => \Carbon\Carbon::now()->startOfWeek()->toDateString(), 'date_to' => \Carbon\Carbon::now()->endOfWeek()->toDateString()])); ?>" class="btn btn-sm <?php echo e($dateFrom == \Carbon\Carbon::now()->startOfWeek()->toDateString() && $dateTo == \Carbon\Carbon::now()->endOfWeek()->toDateString() ? 'btn-primary' : 'btn-outline-primary'); ?>">This Week</a>
                    <a href="<?php echo e(route('reports.sales', ['date_from' => \Carbon\Carbon::now()->startOfMonth()->toDateString(), 'date_to' => \Carbon\Carbon::now()->endOfMonth()->toDateString()])); ?>" class="btn btn-sm <?php echo e($dateFrom == \Carbon\Carbon::now()->startOfMonth()->toDateString() && $dateTo == \Carbon\Carbon::now()->endOfMonth()->toDateString() ? 'btn-primary' : 'btn-outline-primary'); ?>">This Month</a>
                    <a href="<?php echo e(route('reports.sales', ['date_from' => \Carbon\Carbon::now()->startOfYear()->toDateString(), 'date_to' => \Carbon\Carbon::now()->endOfYear()->toDateString()])); ?>" class="btn btn-sm <?php echo e($dateFrom == \Carbon\Carbon::now()->startOfYear()->toDateString() && $dateTo == \Carbon\Carbon::now()->endOfYear()->toDateString() ? 'btn-primary' : 'btn-outline-primary'); ?>">This Year</a>
                </div>
            </div>
            <div class="col-md-2 d-flex gap-2 align-items-end">
                <button type="submit" class="btn btn-primary">Generate</button>
                <a href="<?php echo e(route('reports.sales')); ?>" class="btn btn-secondary">Reset</a>
            </div>
        </form>
    </div>
</div>

<div class="print-header mb-3">
    <h4 class="text-center mb-1">Sales Report</h4>
    <p class="text-center mb-1 text-muted">Date Range: <?php echo e(\Carbon\Carbon::parse($dateFrom)->format('M d, Y')); ?> - <?php echo e(\Carbon\Carbon::parse($dateTo)->format('M d, Y')); ?></p>
    <div class="text-center">
        <span class="me-3">Cash: Kes <?php echo e(number_format($paymentMethodTotals['cash'], 2)); ?></span>
        <span class="me-3">M-Pesa: Kes <?php echo e(number_format($paymentMethodTotals['mpesa'], 2)); ?></span>
        <span class="me-3">Card: Kes <?php echo e(number_format($paymentMethodTotals['card'], 2)); ?></span>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <h5>Total Sales</h5>
                <h3><?php echo e(number_format($totalSales, 2)); ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-secondary text-white">
            <div class="card-body">
                <h5>Total Cost</h5>
                <h3><?php echo e(number_format($totalCost, 2)); ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-success text-white">
            <div class="card-body">
                <h5>Total Profit</h5>
                <h3><?php echo e(number_format($totalProfit, 2)); ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-info text-white">
            <div class="card-body">
                <h5>Total Orders</h5>
                <h3><?php echo e($totalOrders); ?></h3>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-body table-responsive">
        <table class="table table-hover table-sm">
            <thead>
                <tr>
                    <th>Invoice #</th>
                    <th>Date</th>
                    <th>Customer</th>
                    <th>Items</th>
                    <th>Subtotal</th>
                    <th>Discount</th>
                    <th>Tax</th>
                    <th>Total</th>
                    <th>Cost</th>
                    <th>Profit</th>
                    <th>Payment</th>
                </tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <?php
                        $orderCost = 0;
                        foreach($order->items as $item) {
                            $orderCost += ($item->product->cost_price ?? 0) * $item->quantity;
                        }
                        $orderProfit = $order->total - $orderCost;
                    ?>
                    <tr>
                        <td><?php echo e($order->invoice_number); ?></td>
                        <td><?php echo e($order->client_time ? \Carbon\Carbon::parse($order->client_time)->format('M d, Y H:i') : $order->created_at->format('M d, Y H:i')); ?></td>
                        <td><?php echo e($order->customer->name ?? 'Walk-in'); ?></td>
                        <td>
                            <?php $__currentLoopData = $order->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="small"><?php echo e($item->product->name ?? 'N/A'); ?> (<?php echo e($item->quantity); ?> x <?php echo e(number_format($item->price, 2)); ?>)</div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </td>
                        <td><?php echo e(number_format($order->subtotal, 2)); ?></td>
                        <td><?php echo e(number_format($order->discount, 2)); ?></td>
                        <td><?php echo e(number_format($order->tax, 2)); ?></td>
                        <td><strong><?php echo e(number_format($order->total, 2)); ?></strong></td>
                        <td><?php echo e(number_format($orderCost, 2)); ?></td>
                        <td class="<?php echo e($orderProfit >= 0 ? 'text-success' : 'text-danger'); ?>"><strong><?php echo e(number_format($orderProfit, 2)); ?></strong></td>
                        <td>
                            <?php if($order->payment_methods): ?>
                                <?php $pmethods = json_decode($order->payment_methods, true); ?>
                                <?php $__currentLoopData = $pmethods; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $pm): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <div><span class="badge bg-<?php echo e($pm['method'] == 'cash' ? 'success' : ($pm['method'] == 'mpesa' ? 'primary' : 'warning')); ?>"><?php echo e(strtoupper($pm['method'])); ?></span> Kes <?php echo e(number_format($pm['amount'], 2)); ?></div>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php else: ?>
                                <span class="badge bg-secondary"><?php echo e(ucfirst($order->payment_method)); ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="11" class="text-center">No orders found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
        <div class="print-footer mt-3 p-2">
            <div class="row text-center fw-bold">
                <div class="col border p-2">Total Sales<br><?php echo e(number_format($totalSales, 2)); ?></div>
                <div class="col border p-2">Total Cost<br><?php echo e(number_format($totalCost, 2)); ?></div>
                <div class="col border p-2 text-success">Total Profit<br><?php echo e(number_format($totalProfit, 2)); ?></div>
                <div class="col border p-2">Total Orders<br><?php echo e($totalOrders); ?></div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.main', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\AI PROJECT POS\PosAI3\resources\views/reports/sales.blade.php ENDPATH**/ ?>