<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requirePermission('report_view');

$pdo = getDB();
$from = $_GET['from'] ?? date('Y-m-01');
$to = $_GET['to'] ?? date('Y-m-d');

$stmt = $pdo->prepare("
    SELECT si.*, c.name as customer_name, u.name as created_by_name
    FROM sales_invoices si
    LEFT JOIN customers c ON si.customer_id = c.id
    LEFT JOIN users u ON si.created_by = u.id
    WHERE DATE(si.invoice_date) BETWEEN ? AND ? AND si.invoice_status != 'Cancelled'
    ORDER BY si.id DESC
");
$stmt->execute([$from, $to]);
$sales = $stmt->fetchAll();

$stmt = $pdo->prepare("SELECT COALESCE(SUM(total_amount),0) as total, COALESCE(SUM(paid_amount),0) as paid, COALESCE(SUM(due_amount),0) as due FROM sales_invoices WHERE DATE(invoice_date) BETWEEN ? AND ? AND invoice_status != 'Cancelled'");
$stmt->execute([$from, $to]);
$summary = $stmt->fetch();

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <h4 class="page-title"><i class="fas fa-chart-line me-2"></i>Sales Report</h4>

        <div class="card">
            <div class="card-body">
                <form method="GET" class="row g-3 mb-3">
                    <div class="col-md-4">
                        <label class="form-label">From Date</label>
                        <input type="date" name="from" class="form-control" value="<?= $from ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">To Date</label>
                        <input type="date" name="to" class="form-control" value="<?= $to ?>">
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2"><i class="fas fa-filter me-1"></i>Filter</button>
                        <button type="button" class="btn btn-success" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
                    </div>
                </form>

                <div class="row mb-3">
                    <div class="col-md-4"><div class="stat-card bg-primary-gradient"><div class="stat-icon"><i class="fas fa-money-bill"></i></div><div class="stat-value"><?= formatCurrency($summary['total']) ?></div><div class="stat-label">Total Sales</div></div></div>
                    <div class="col-md-4"><div class="stat-card bg-success-gradient"><div class="stat-icon"><i class="fas fa-check-circle"></i></div><div class="stat-value"><?= formatCurrency($summary['paid']) ?></div><div class="stat-label">Total Collected</div></div></div>
                    <div class="col-md-4"><div class="stat-card bg-warning-gradient"><div class="stat-icon"><i class="fas fa-exclamation-circle"></i></div><div class="stat-value"><?= formatCurrency($summary['due']) ?></div><div class="stat-label">Total Due</div></div></div>
                </div>

                <div class="table-responsive">
                    <table id="salesReportTable" class="table table-hover datatable">
                        <thead><tr><th>Invoice</th><th>Date</th><th>Customer</th><th>Subtotal</th><th>Discount</th><th>Tax</th><th>Total</th><th>Paid</th><th>Due</th><th>Status</th></tr></thead>
                        <tbody>
                            <?php foreach ($sales as $s): ?>
                            <tr>
                                <td><a href="<?= BASE_URL ?>pos/invoice.php?id=<?= $s['id'] ?>"><?= htmlspecialchars($s['invoice_no']) ?></a></td>
                                <td><?= formatDate($s['invoice_date']) ?></td>
                                <td><?= htmlspecialchars($s['customer_name'] ?? 'Walk-in') ?></td>
                                <td><?= formatCurrency($s['subtotal']) ?></td>
                                <td><?= formatCurrency($s['discount_amount']) ?></td>
                                <td><?= formatCurrency($s['tax_amount']) ?></td>
                                <td><strong><?= formatCurrency($s['total_amount']) ?></strong></td>
                                <td><?= formatCurrency($s['paid_amount']) ?></td>
                                <td><?= formatCurrency($s['due_amount']) ?></td>
                                <td><?= getStatusBadge($s['invoice_status']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
