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

// Filters
$start_date = $_POST['start_date'] ?? '';
$end_date = $_POST['end_date'] ?? '';
$product_id = $_POST['product_id'] ?? '';

// Build WHERE clause
$where = "WHERE s.archived=0 ";
if($start_date && $end_date){
    $where .= "AND DATE(s.sale_date) BETWEEN '$start_date' AND '$end_date' ";
}
if($product_id){
    $where .= "AND si.product_id = $product_id ";
}

// Fetch sales with product filter
$sql = "
SELECT s.*, GROUP_CONCAT(p.name SEPARATOR ', ') AS products
FROM sales s
JOIN sale_items si ON s.id = si.sale_id
JOIN products p ON si.product_id = p.id
$where
GROUP BY s.id
ORDER BY s.sale_date DESC
";
$result = $conn->query($sql);

// Fetch all products for filter
$products_list = $conn->query("SELECT * FROM products ORDER BY name ASC");
?>

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

    <!-- Filter Form -->
    <form method="POST" class="row g-3 mb-4">
        <div class="col-md-3">
            <input type="date" name="start_date" value="<?= htmlspecialchars($start_date) ?>" class="form-control" placeholder="Start Date">
        </div>
        <div class="col-md-3">
            <input type="date" name="end_date" value="<?= htmlspecialchars($end_date) ?>" class="form-control" placeholder="End Date">
        </div>
        <div class="col-md-3">
            <select name="product_id" class="form-control">
                <option value="">-- Filter by Product --</option>
                <?php while($p = $products_list->fetch_assoc()): ?>
                    <option value="<?= $p['id'] ?>" <?= $p['id']==$product_id?'selected':'' ?>><?= $p['name'] ?></option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-3">
            <button type="submit" class="btn btn-primary w-100">Filter</button>
        </div>
    </form>

    <!-- Export buttons -->
    <div class="mb-3">
        <a href="export_sales.php?type=pdf" class="btn btn-danger btn-sm">Export PDF</a>
        <a href="export_sales.php?type=excel" class="btn btn-success btn-sm">Export Excel</a>
    </div>

    <!-- Sales Table -->
    <table class="table table-bordered table-striped">
        <thead class="bg-dark text-white">
            <tr>
                <th>#</th>
                <th>Sale ID</th>
                <th>Date</th>
                <th>Products</th>
                <th>Payment Method</th>
                <th>Discount (MK)</th>
                <th>VAT (MK)</th>
                <th>Total (MK)</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i=1;
            while($row = $result->fetch_assoc()):
            ?>
                <tr>
                    <td><?= $i ?></td>
                    <td><?= $row['id'] ?></td>
                    <td><?= $row['sale_date'] ?></td>
                    <td><?= $row['products'] ?></td>
                    <td><?= $row['payment_method'] ?></td>
                    <td><?= number_format($row['discount'],2) ?></td>
                    <td><?= number_format($row['vat_total'],2) ?></td>
                    <td><?= number_format($row['total'],2) ?></td>
                    <td>
                        <a href="receipt.php?sale_id=<?= $row['id'] ?>" target="_blank" class="btn btn-primary btn-sm">Receipt</a>
                        <a href="edit_sale.php?sale_id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">Edit</a>
                        <a href="delete_sale.php?sale_id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Archive this sale?');">Archive</a>
                    </td>
                </tr>
            <?php
            $i++;
            endwhile;
            ?>
        </tbody>
    </table>
</div>

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