<?php
require_once '../init.php';

$startDate = $_GET['start_date'] ?? date(DATE_FORMAT, strtotime('-30 days'));
$endDate = $_GET['end_date'] ?? date(DATE_FORMAT);

$transactions = $database->select(
    "SELECT * FROM transactions WHERE user_id = ? AND DATE(created_at) BETWEEN ? AND ? ORDER BY created_at DESC",
    [$_SESSION['user_id'], $startDate, $endDate]
);

$totalSales = 0;
$totalTransactions = count($transactions);
foreach ($transactions as $trans) {
    $totalSales += $trans['total_amount'];
}

$pageTitle = 'Laporan - KasirMurah.biz.id';
?>
<?php include '../layout/header.php'; ?>
    <?php include '../layout/navbar.php'; ?>
    
    <div class="container-fluid mt-4">
        <h1>Laporan Penjualan</h1>
        
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET" class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">Tanggal Mulai</label>
                        <input type="date" class="form-control" name="start_date" value="<?php echo $startDate; ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Tanggal Akhir</label>
                        <input type="date" class="form-control" name="end_date" value="<?php echo $endDate; ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary w-100">Filter</button>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- Stats -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card bg-primary text-white">
                    <div class="card-body">
                        <h5>Total Transaksi</h5>
                        <p style="font-size: 28px; font-weight: bold;"><?php echo $totalTransactions; ?></p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-success text-white">
                    <div class="card-body">
                        <h5>Total Penjualan</h5>
                        <p style="font-size: 24px; font-weight: bold;"><?php echo Helper::formatCurrency($totalSales); ?></p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-info text-white">
                    <div class="card-body">
                        <h5>Rata-rata Transaksi</h5>
                        <p style="font-size: 24px; font-weight: bold;">
                            <?php echo $totalTransactions > 0 ? Helper::formatCurrency($totalSales / $totalTransactions) : 'Rp 0'; ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Transactions Table -->
        <div class="card">
            <div class="card-header">
                <h5>Detail Transaksi</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Invoice</th>
                                <th>Tanggal</th>
                                <th>Subtotal</th>
                                <th>Tax</th>
                                <th>Diskon</th>
                                <th>Total</th>
                                <th>Metode</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($transactions as $trans): ?>
                            <tr>
                                <td><?php echo $trans['invoice_number']; ?></td>
                                <td><?php echo Helper::formatDateTime($trans['created_at']); ?></td>
                                <td><?php echo Helper::formatCurrency($trans['subtotal']); ?></td>
                                <td><?php echo Helper::formatCurrency($trans['tax_amount']); ?></td>
                                <td><?php echo Helper::formatCurrency($trans['discount_amount']); ?></td>
                                <td><strong><?php echo Helper::formatCurrency($trans['total_amount']); ?></strong></td>
                                <td><?php echo ucfirst($trans['payment_method']); ?></td>
                                <td>
                                    <a href="../pos/receipt.php?invoice=<?php echo $trans['invoice_number']; ?>" class="btn btn-sm btn-info">Struk</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <?php if (empty($transactions)): ?>
                <div class="alert alert-info">Tidak ada transaksi dalam periode ini</div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <?php include '../layout/footer.php'; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
