<?php

require_once '../../../includes/modal_header.php';

$product_id = intval($_GET['id']);

// Get product name
$product_name = escapeSql(getFieldById('products', $product_id, 'product_name'));

// Generate the HTML form content using output buffering.
ob_start();
?>

<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fas fa-fw fa-box-open me-2"></i>Stocking: <strong><?= $product_name ?></strong></h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
    <input type="hidden" name="product_id" value="<?= $product_id ?>">
    <div class="modal-body">

        <div class="mb-3">
            <label>QTY <strong class="text-danger">*</strong></label>
            <div class="input-group">
                    <span class="input-group-text"><i class="fa fa-fw fa-balance-scale"></i></span>
                <input type="text" inputmode="numeric" pattern="[0-9]*" class="form-control" name="qty" placeholder="Units to add" required>
            </div>
        </div>

        <div class="mb-3">
            <label>Expense</label>
            <div class="input-group">
                    <span class="input-group-text"><i class="fa fa-fw fa-shopping-cart"></i></span>
                <select class="form-select select2" name="expense">
                    <option value="0">- Link an Expense -</option>
                    <?php

                    $expenses_sql = mysqli_query($mysqli, "SELECT expense_id, expense_description, expense_date
                        FROM expenses
                        WHERE expense_archived_at IS NULL ORDER BY expense_date DESC"
                    );

                    while ($row = mysqli_fetch_assoc($expenses_sql)) {
                        $expense_id = intval($row['expense_id']);
                        $expense_description = escapeHtml($row['expense_description']);
                        $expense_date = escapeHtml($row['expense_date']);
                        ?>
                        <option value="<?= $expense_id ?>"><?= "($expense_date) $expense_description"; ?></option>

                        <?php
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="mb-3">
            <label>Notes</label>
            <textarea class="form-control" rows="4" name="note" placeholder="Enter some notes"></textarea>
        </div>

    </div>

    <div class="modal-footer">
        <button type="submit" name="add_product_stock" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Add Stock</button>
        <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
    </div>
</form>

<?php

require_once '../../../includes/modal_footer.php';
