<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

// ===== Handle Filters =====
$start_date = $_GET['start_date'] ?? '';
$end_date   = $_GET['end_date'] ?? '';
$search_desc = $_GET['search_desc'] ?? '';

$whereClause = [];
if($start_date && $end_date){
    $whereClause[] = "date BETWEEN '$start_date' AND '$end_date'";
}
if($search_desc){
    $whereClause[] = "description LIKE '%".$conn->real_escape_string($search_desc)."%'";
}

$whereSQL = $whereClause ? "WHERE ".implode(' AND ', $whereClause) : "";

// ===== Totals =====
$totalQuery = $conn->query("SELECT SUM(amount) AS total_amount FROM petty_cash $whereSQL");
$totalAmount = $totalQuery->fetch_assoc()['total_amount'] ?? 0;
?>

<div class="container mt-4">

    <button class="btn btn-secondary mb-3" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>

    <h3><i class="fa fa-wallet"></i> Petty Cash</h3>
    <hr>

    <!-- ===== Filters ===== -->
    <div class="card p-3 mb-3 bg-info text-white">
        <form method="get" class="row g-2 align-items-end">
            <div class="col-md-3">
                <label>Start Date:</label>
                <input type="date" name="start_date" class="form-control" value="<?= htmlspecialchars($start_date) ?>">
            </div>
            <div class="col-md-3">
                <label>End Date:</label>
                <input type="date" name="end_date" class="form-control" value="<?= htmlspecialchars($end_date) ?>">
            </div>
            <div class="col-md-4">
                <label>Description:</label>
                <input type="text" name="search_desc" class="form-control" placeholder="Search description" value="<?= htmlspecialchars($search_desc) ?>">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-light w-100"><i class="fa fa-filter"></i> Apply</button>
                <a href="petty_cash.php" class="btn btn-dark w-100 mt-1"><i class="fa fa-sync"></i> Reset</a>
            </div>
        </form>
    </div>

    <!-- ===== Summary Totals ===== -->
    <div class="row mb-3">
        <div class="col-md-4">
            <div class="card p-3 bg-success text-white">
                <h5>Total Petty Cash:</h5>
                <h3><?= number_format($totalAmount, 2) ?></h3>
            </div>
        </div>
        <div class="col-md-8 text-end">
            <a href="add_petty_cash.php" class="btn btn-success"><i class="fa fa-plus"></i> Add Entry</a>
            <button onclick="window.print()" class="btn btn-primary"><i class="fa fa-print"></i> Print</button>
        </div>
    </div>

    <!-- ===== Petty Cash Table ===== -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-sm">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Amount</th>
                    <th>Description</th>
                    <th>Recorded By</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $query = $conn->query("SELECT * FROM petty_cash $whereSQL ORDER BY date DESC, id DESC");
                $i = 1;
                while($row = $query->fetch_assoc()):
                ?>
                <tr>
                    <td><?= $i ?></td>
                    <td><?= $row['date'] ?></td>
                    <td><?= number_format($row['amount'], 2) ?></td>
                    <td><?= htmlspecialchars($row['description']) ?></td>
                    <td><?= htmlspecialchars($row['recorded_by']) ?></td>
                </tr>
                <?php $i++; endwhile; ?>
                <?php if($i==1): ?>
                    <tr>
                        <td colspan="5" class="text-center">No petty cash records found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

</div>

<?php include 'includes/footer.php'; ?>