<?php
$row     = $row ?? [];
$lookups = $lookups ?? [];
$products = $lookups['products'] ?? [];
$employees = $lookups['employees'] ?? [];
$workOrders = $lookups['workOrders'] ?? [];
$serviceVisits = $lookups['serviceVisits'] ?? [];

$usedAt = old('used_at', isset($row['used_at']) ? substr((string) $row['used_at'], 0, 10) : date('Y-m-d'));
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Product <span class="text-danger">*</span></label>
        <select name="product_id" class="form-select" required>
            <option value="">Select product</option>
            <?php foreach ($products as $p): ?>
                <option value="<?= (int) $p['id'] ?>"
                    <?= (string) old('product_id', $row['product_id'] ?? '') === (string) $p['id'] ? 'selected' : '' ?>>
                    <?= esc($p['name']) ?> (<?= esc($p['sku']) ?>)
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Quantity <span class="text-danger">*</span></label>
        <input type="number" name="quantity" class="form-control" step="0.001" min="0.001" required
               value="<?= esc(old('quantity', $row['quantity'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Unit Label</label>
        <input type="text" name="unit_label" class="form-control" maxlength="30"
               value="<?= esc(old('unit_label', $row['unit_label'] ?? '')) ?>" placeholder="e.g. pcs, kg">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Used At</label>
        <input type="date" name="used_at" class="form-control" value="<?= esc($usedAt) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Used By</label>
        <select name="used_by_employee_id" class="form-select">
            <option value="">Unspecified</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('used_by_employee_id', $row['used_by_employee_id'] ?? '') === (string) $e['id'] ? 'selected' : '' ?>>
                    <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Work Order</label>
        <select name="work_order_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($workOrders as $wo): ?>
                <option value="<?= (int) $wo['id'] ?>"
                    <?= (string) old('work_order_id', $row['work_order_id'] ?? '') === (string) $wo['id'] ? 'selected' : '' ?>>
                    <?= esc($wo['number']) ?> — <?= esc($wo['title']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Service Visit</label>
        <select name="service_visit_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($serviceVisits as $sv): ?>
                <option value="<?= (int) $sv['id'] ?>"
                    <?= (string) old('service_visit_id', $row['service_visit_id'] ?? '') === (string) $sv['id'] ? 'selected' : '' ?>>
                    <?= esc($sv['number']) ?><?= ! empty($sv['title']) ? ' — ' . esc($sv['title']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3" maxlength="500"><?= esc(old('notes', $row['notes'] ?? '')) ?></textarea>
    </div>
</div>
