<?php

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST['name']);
    $category_id = $_POST['category_id'];
    $description = trim($_POST['description']);
    $quantity = (int)$_POST['quantity'];
    $min_quantity = (int)$_POST['min_quantity'];

    $stmt = $connection->prepare("INSERT INTO inventory_items (name, category_id, description, quantity, min_quantity) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sisii", $name, $category_id, $description, $quantity, $min_quantity);
    if ($stmt->execute()) {
        $_SESSION['msg'] = "Item added successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Error adding item.";
        $_SESSION['type'] = "danger";
    }
    header("Location: inventory_create");
    exit;
}

// Fetch categories and items
$categories = $connection->query("SELECT * FROM inventory_categories");
$items = $connection->query("SELECT i.*, c.name AS category FROM inventory_items i LEFT JOIN inventory_categories c ON i.category_id = c.id");
?>


<div class='card'>
    <div class='panel panel-primary'>
        <div class='panel-heading d-flex justify-content-between align-items-center'>
            <h4 class='text-center'>Inventory Items</h4>
            <button class="btn btn-success btn-sm" data-toggle="modal" data-target="#addItemModal">
                <i class="fa fa-plus"></i> New Item
            </button>
        </div>

        <div class='panel-body' style='padding: 20px;'>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Item</th>
                            <th>Category</th>
                            <th>Description</th>
                            <th>Qty</th>
                            <th>Min Qty</th>
                            <th>Status</th>
                            <th>Date Added</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $items->fetch_assoc()): ?>
                            <tr>
                                <td class = 'text-center'><?= $row['name'] ?></td>
                                <td class = 'text-center'><?= $row['category'] ?></td>
                                <td class = 'text-center'><?= $row['description'] ?></td>
                                <td class = 'text-center'><?= $row['quantity'] ?></td>
                                <td class = 'text-center'><?= $row['min_quantity'] ?></td>
                                <td class = 'text-center'>
                                    <?php if ($row['quantity'] < $row['min_quantity']): ?>
                                        <span class="badge badge-danger">Low Stock</span>
                                    <?php else: ?>
                                        <span class="badge badge-success">OK</span>
                                    <?php endif; ?>
                                </td>
                                <td class = 'text-center'><?= date("d M, Y", strtotime($row['date_added'])) ?></td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- ✅ Add Item Modal -->
<div class="modal fade" id="addItemModal" tabindex="-1" role="dialog" aria-labelledby="addItemModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
        <form method="POST" action="">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="addItemModalLabel">Add New Inventory Item</h5>
                <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Item Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" required>
                        </div>

                        <div class="form-group">
                            <label>Category <span class="text-danger">*</span></label>
                            <select name="category_id" class="form-control" required>
                                <option value="">-- Select Category --</option>
                                <?php while ($cat = $categories->fetch_assoc()): ?>
                                    <option value="<?= $cat['id'] ?>"><?= $cat['name'] ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>Description</label>
                            <textarea name="description" class="form-control" rows="4"></textarea>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Quantity <span class="text-danger">*</span></label>
                            <input type="number" name="quantity" class="form-control" min="0" required>
                        </div>

                        <div class="form-group">
                            <label>Minimum Quantity <span class="text-danger">*</span></label>
                            <input type="number" name="min_quantity" class="form-control" min="0" required>
                        </div>

                        <div class="form-group mt-4">
                            <label>&nbsp;</label><br>
                            <button type="submit" class="btn btn-success btn-block">
                                <i class="fa fa-save"></i> Save Item
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
  </div>
</div>
