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

$today = date('Y-m-d');

// ===================== FILTERS =====================
$from = $_GET['from'] ?? $today;
$to   = $_GET['to'] ?? $today;
$payment_method = $_GET['payment_method'] ?? '';
$bartender = $_GET['bartender'] ?? '';

$where = "DATE(s.sale_date) BETWEEN '$from' AND '$to'";
if(!empty($payment_method)){
    $where .= " AND s.payment_method='$payment_method'";
}
if(!empty($bartender)){
    $where .= " AND u.username LIKE '%$bartender%'";
}

// ===================== FETCH SALES =====================
$sales = $conn->query("
    SELECT s.*, u.username AS bartender_name, c.name AS customer_name
    FROM sales s
    LEFT JOIN users u ON s.user_id=u.id
    LEFT JOIN guests c ON s.customer_id=c.id
    WHERE $where
    ORDER BY s.sale_date DESC
");

// ===================== TOTALS =====================
$total_payments = $conn->query("
    SELECT IFNULL(SUM(s.total),0) AS total
    FROM sales s
    LEFT JOIN users u ON s.user_id=u.id
    LEFT JOIN guests c ON s.customer_id=c.id
    WHERE $where
")->fetch_assoc()['total'];

// ===================== LEVY CALCULATION =====================
$levy_rate = 0.01; // 1% levy
$levy_amount = $total_payments * $levy_rate;

?>

<!-- DataTables CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css">

<div class="container mt-4">
    <div class="welcome-msg mb-3">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>

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

    <h3 class="text-center">Tourism Levy</h3>
    <hr>

    <!-- ================= TOTAL SUMMARY ================= -->
    <div class="alert alert-success">
        
        <strong>Tourism Levy (<?= $levy_rate*100 ?>%):</strong> MK <?= number_format($levy_amount,2) ?>
    </div>

    <!-- ================= FILTERS ================= -->
    <form method="GET" class="row g-3 mb-3">
        <div class="col-md-2">
            <label>From</label>
            <input type="date" name="from" value="<?= $from ?>" class="form-control">
        </div>
        <div class="col-md-2">
            <label>To</label>
            <input type="date" name="to" value="<?= $to ?>" class="form-control">
        </div>
        <div class="col-md-3">
            <label>Payment Method</label>
            <select name="payment_method" class="form-control">
                <option value="">-- All --</option>
                <option <?= $payment_method=='Cash'?'selected':'' ?>>Cash</option>
                <option <?= $payment_method=='Mobile Money'?'selected':'' ?>>Mobile Money</option>
                <option <?= $payment_method=='Card'?'selected':'' ?>>Card</option>
            </select>
        </div>
       
        <div class="col-md-2 mt-4">
            <button class="btn btn-primary w-100">Filter</button>
        </div>
    </form>

    <!-- ================= SALES TABLE ================= -->
    <table id="barSalesTable" class="display nowrap table table-bordered table-striped" style="width:100%">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Date</th>
                <th>Bartender</th>
                <th>Customer</th>
                <th>Payment</th>
                <th>Total (MK)</th>
                <th>Levy (MK)</th>
            </tr>
        </thead>
        <tbody>
        <?php 
        $i=1;
        while($row = $sales->fetch_assoc()): 
            $total = $row['total'] ?? 0;
            $row_levy = $total * $levy_rate;
        ?>
            <tr>
                <td><?= $i++ ?></td>
                <td><?= $row['sale_date'] ?></td>
                <td><?= $row['bartender_name'] ?? 'Walk-in' ?></td>
                <td><?= $row['customer_name'] ?? 'Walk-in' ?></td>
                <td><?= $row['payment_method'] ?></td>
                <td><strong><?= number_format($total,2) ?></strong></td>
                <td><?= number_format($row_levy,2) ?></td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
</div>

<!-- DataTables JS -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>

<script>
$(document).ready(function() {
    $('#barSalesTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            { extend: 'excelHtml5', title: 'Bar Sales Report with Levy' },
            { extend: 'pdfHtml5', title: 'Bar Sales Report with Levy', orientation: 'portrait', pageSize: 'A4' },
            { extend: 'print', title: 'Bar Sales Report with Levy' }
        ],
        scrollX: true
    });
});
</script>

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