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

$username = $_SESSION['username'] ?? 'Guest';

// ===== DELETE =====
if(isset($_GET['delete'])){
    $id = intval($_GET['delete']);
    $conn->query("DELETE FROM perishables WHERE id=$id");
    echo "<div class='alert alert-success'>Item deleted successfully</div>";
}
?>

<div class="container mt-4">
 <button class="btn btn-secondary btn-custom mb-3" onclick="history.back()">
     <i class="fa fa-arrow-left"></i> Back
 </button>
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4><i class="fa fa-leaf text-success"></i> Perishable Items</h4>
        <a href="add_perishable_item.php" class="btn btn-success">
            <i class="fa fa-plus"></i> Add New Item
        </a>
    </div>

    <!-- Search -->
    <div class="row mb-3">
        <div class="col-md-4">
            <input type="text" id="searchBox" class="form-control"
                   placeholder="Search item...">
        </div>
    </div>

    <!-- Table -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-hover">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Item Name</th>
                    <th>Unit</th>
                    <th>Stock Qty</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th width="140">Action</th>
                </tr>
            </thead>
            <tbody id="perishablesTable">
                <?php
                $res = $conn->query("
                    SELECT *
                    FROM perishables
                    ORDER BY name ASC
                ");

                $i=1;
                while($row = $res->fetch_assoc()):

                    $stock = floatval($row['quantity']);
                    $status = $stock > 0 ? 'Available' : 'Out of Stock';
                    $badge  = $stock > 0 ? 'success' : 'danger';
                ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><?= htmlspecialchars($row['name']) ?></td>
                    <td><?= htmlspecialchars($row['unit'] ?? '-') ?></td>
                    <td><strong><?= number_format($stock,2) ?></strong></td>

                    <!-- STATUS -->
                    <td>
                        <span class="badge bg-<?= $badge ?>">
                            <?= $status ?>
                        </span>
                    </td>

                    <td><?= $row['created_at'] ?? '-' ?></td>

                    <td>
                        <a href="edit_perishable_item.php?id=<?= $row['id'] ?>"
                           class="btn btn-sm btn-warning mb-1">
                           <i class="fa fa-edit"></i>
                        </a>

                        <a href="?delete=<?= $row['id'] ?>"
                           class="btn btn-sm btn-danger mb-1"
                           onclick="return confirm('Delete this item?')">
                           <i class="fa fa-trash"></i>
                        </a>
                    </td>
                </tr>
                <?php endwhile; ?>

                <?php if($res->num_rows==0): ?>
                <tr>
                    <td colspan="7" class="text-center text-muted">
                        No perishable items found
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- ===== SEARCH SCRIPT ===== -->
<script>
document.getElementById('searchBox').addEventListener('keyup', function(){
    let value = this.value.toLowerCase();
    document.querySelectorAll('#perishablesTable tr').forEach(row=>{
        row.style.display =
            row.innerText.toLowerCase().includes(value) ? '' : 'none';
    });
});
</script>