<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit();
}
include_once "../config/database.php";
?>

<?php include_once "../includes/header.php"; ?>
<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">
<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold">Expense Report</h4>
    </div>

    <?php
    $from = $_GET['from'] ?? '';
    $to = $_GET['to'] ?? '';

    $where = "";

    if(!empty($from) && !empty($to)){
        $where = "WHERE expense_date BETWEEN '$from' AND '$to'";
    }

    $query = mysqli_query($conn, "
        SELECT * FROM expenses 
        $where
        ORDER BY id DESC
    ");

    $total = 0;
    ?>

    <!-- Filter -->
    <form class="row g-3 mb-3">
        <div class="col-md-3 col-12">
            <input type="date" name="from" class="form-control" value="<?php echo $from; ?>">
        </div>
        <div class="col-md-3 col-12">
            <input type="date" name="to" class="form-control" value="<?php echo $to; ?>">
        </div>
        <div class="col-md-2 col-6">
            <button class="btn btn-dark w-100">Filter</button>
        </div>
    </form>

    <div class="card rounded-4 shadow border-0">
        <div class="card-body">

            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Category</th>
                            <th>Amount</th>
                            <th>Date</th>
                            <th>Notes</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if(mysqli_num_rows($query) > 0){
                            $i = 1;
                            while($row = mysqli_fetch_assoc($query)){
                                $total += $row['amount'];
                        ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo htmlspecialchars($row['expense_name']); ?></td>
                            <td><?php echo htmlspecialchars($row['category']); ?></td>
                            <td>₹<?php echo number_format($row['amount'],2); ?></td>
                            <td><?php echo date("d M Y", strtotime($row['expense_date'])); ?></td>
                            <td><?php echo htmlspecialchars($row['notes']); ?></td>
                        </tr>
                        <?php }} else { ?>
                        <tr>
                            <td colspan="6" class="text-center">No data found</td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>

            <div class="text-end mt-3">
                <h5>Total Expenses: ₹<?php echo number_format($total,2); ?></h5>
            </div>

        </div>
    </div>

</div>

<?php include_once "../includes/footer.php"; ?>
</div>