<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$basePath = '../../';
include '../../includes/header.php';

$db = (new Database())->connect();

/*
    Load items with stock + unit
*/
$stmt = $db->prepare("
    SELECT
        i.id,
        i.name,
        i.barcode,
        i.stock,
        i.unit_id,
        u.name AS unit_name
    FROM items i
    LEFT JOIN units u ON i.unit_id = u.id
    WHERE i.stock > 0
    ORDER BY i.name ASC
");

$stmt->execute();
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Stock OUT</h3>

        <a href="index.php" class="btn btn-secondary">
            Back to Stock
        </a>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

            <form action="save_stock.php" method="POST">

                <input type="hidden" name="type" value="OUT">

                <!-- ITEM -->
                <div class="mb-3">
                    <label class="form-label">Select Item</label>

                    <select
                        name="item_id"
                        class="form-select"
                        required
                    >
                        <option value="">Select Item</option>

                        <?php foreach ($items as $item): ?>
                            <option
                                value="<?= $item['id'] ?>"
                                data-stock="<?= $item['stock'] ?>"
                                data-unit="<?= $item['unit_id'] ?>"
                            >
                                <?= htmlspecialchars($item['name']) ?>
                                (Stock: <?= number_format($item['stock'], 2) ?>
                                <?= htmlspecialchars($item['unit_name'] ?? '') ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <!-- STOCK DATE -->
                <div class="mb-3">
                    <label class="form-label">Stock Date</label>
                    <input
                        type="date"
                        name="stock_date"
                        class="form-control"
                        value="<?= date('Y-m-d') ?>"
                        required
                    >
                </div>

                <!-- QTY -->
                <div class="mb-3">
                    <label class="form-label">Quantity</label>
                    <input
                        type="number"
                        step="0.01"
                        min="0.01"
                        name="qty"
                        id="qty"
                        class="form-control"
                        required
                        oninput="calculateTotal()"
                    >
                </div>

                <!-- RATE -->
                <div class="mb-3">
                    <label class="form-label">Rate</label>
                    <input
                        type="number"
                        step="0.01"
                        min="0"
                        name="rate"
                        id="rate"
                        class="form-control"
                        value="0"
                        oninput="calculateTotal()"
                    >
                </div>

                <!-- TOTAL -->
                <div class="mb-3">
                    <label class="form-label">Total</label>
                    <input
                        type="number"
                        step="0.01"
                        name="total"
                        id="total"
                        class="form-control"
                        readonly
                    >
                </div>

                <!-- REFERENCE -->
                <div class="mb-3">
                    <label class="form-label">Reference No.</label>
                    <input
                        type="text"
                        name="reference"
                        class="form-control"
                        placeholder="Issue Reference"
                    >
                </div>

                <!-- REMARKS -->
                <div class="mb-3">
                    <label class="form-label">Remarks</label>
                    <textarea
                        name="remarks"
                        class="form-control"
                        rows="3"
                    ></textarea>
                </div>

                <button type="submit" class="btn btn-warning">
                    Save Stock OUT
                </button>

                <a href="index.php" class="btn btn-secondary">
                    Cancel
                </a>

            </form>

        </div>
    </div>

</div>

<script>
function calculateTotal()
{
    let qty = parseFloat(document.getElementById('qty').value) || 0;
    let rate = parseFloat(document.getElementById('rate').value) || 0;

    document.getElementById('total').value = (qty * rate).toFixed(2);
}
</script>

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