<?php
$product = $product ?? [];
$categories = $categories ?? [];
$brands = $brands ?? [];
$units = $units ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= esc(old('name', $product['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">SKU <span class="text-danger">*</span></label>
        <input type="text" name="sku" class="form-control text-uppercase" maxlength="50" value="<?= esc(old('sku', $product['sku'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Barcode</label>
        <input type="text" name="barcode" class="form-control" maxlength="50" value="<?= esc(old('barcode', $product['barcode'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Category</label>
        <select name="category_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($categories as $c): ?>
                <option value="<?= (int) $c['id'] ?>" <?= (int) old('category_id', $product['category_id'] ?? 0) === (int) $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Brand</label>
        <select name="brand_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($brands as $b): ?>
                <option value="<?= (int) $b['id'] ?>" <?= (int) old('brand_id', $product['brand_id'] ?? 0) === (int) $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Unit of Measure</label>
        <select name="unit_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($units as $u): ?>
                <option value="<?= (int) $u['id'] ?>" <?= (int) old('unit_id', $product['unit_id'] ?? 0) === (int) $u['id'] ? 'selected' : '' ?>><?= esc($u['name']) ?> (<?= esc($u['code']) ?>)</option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cost Price</label>
        <input type="number" step="0.01" min="0" name="cost_price" class="form-control" value="<?= esc(old('cost_price', $product['cost_price'] ?? '0.00')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Sale Price</label>
        <input type="number" step="0.01" min="0" name="sale_price" class="form-control" value="<?= esc(old('sale_price', $product['sale_price'] ?? '0.00')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Reorder Level</label>
        <input type="number" step="0.001" min="0" name="reorder_level" class="form-control" value="<?= esc(old('reorder_level', $product['reorder_level'] ?? '0')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['active', 'inactive'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $product['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $product['description'] ?? '')) ?></textarea>
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes', $product['notes'] ?? '')) ?></textarea>
    </div>
</div>
