<?php $isEdit = !empty($part); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-boxes-stacked me-2 text-primary"></i><?= $isEdit ? 'Edit' : 'New' ?> Spare Part</h5>
    <a href="<?= url('/Parts') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Parts/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="part_id" value="<?= (int) ($part['part_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-3"><label class="form-label">SKU *</label>
            <input type="text" class="form-control form-control-sm" name="sku" required value="<?= e($part['sku'] ?? '') ?>" placeholder="e.g. BRG-6205-2RS">
        </div>
        <div class="col-md-6"><label class="form-label">Name *</label>
            <input type="text" class="form-control form-control-sm" name="name" required value="<?= e($part['name'] ?? '') ?>">
        </div>
        <div class="col-md-3"><label class="form-label">Category</label>
            <input type="text" class="form-control form-control-sm" name="category" value="<?= e($part['category'] ?? '') ?>" placeholder="e.g. Bearings">
        </div>
        <div class="col-12"><label class="form-label">Description</label>
            <textarea class="form-control form-control-sm" name="description" rows="2"><?= e($part['description'] ?? '') ?></textarea>
        </div>
        <div class="col-md-2"><label class="form-label">Unit</label>
            <select name="unit" class="form-select form-select-sm">
                <?php foreach (\App\Models\Part::UNITS as $u): ?>
                    <option value="<?= e($u) ?>" <?= ($part['unit'] ?? 'ea') === $u ? 'selected' : '' ?>><?= e($u) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2"><label class="form-label">Unit cost</label>
            <input type="number" step="0.01" class="form-control form-control-sm" name="unit_cost" value="<?= e((string) ($part['unit_cost'] ?? '0.00')) ?>">
        </div>
        <?php if (!$isEdit): ?>
            <div class="col-md-2"><label class="form-label">Opening stock</label>
                <input type="number" step="0.001" class="form-control form-control-sm" name="on_hand" value="0">
                <small class="text-muted">One-off — later use Receive.</small>
            </div>
        <?php endif; ?>
        <div class="col-md-2"><label class="form-label">Min stock</label>
            <input type="number" step="0.001" class="form-control form-control-sm" name="min_stock" value="<?= e((string) ($part['min_stock'] ?? '0')) ?>">
        </div>
        <div class="col-md-2"><label class="form-label">Max stock</label>
            <input type="number" step="0.001" class="form-control form-control-sm" name="max_stock" value="<?= e((string) ($part['max_stock'] ?? '')) ?>">
        </div>
        <div class="col-md-2"><label class="form-label">Reorder qty</label>
            <input type="number" step="0.001" class="form-control form-control-sm" name="reorder_qty" value="<?= e((string) ($part['reorder_qty'] ?? '')) ?>">
        </div>
        <div class="col-md-4"><label class="form-label">Storage location</label>
            <input type="text" class="form-control form-control-sm" name="location" value="<?= e($part['location'] ?? '') ?>" placeholder="Bin A3-04">
        </div>
        <div class="col-md-4"><label class="form-label">Preferred supplier</label>
            <input type="text" class="form-control form-control-sm" name="supplier" value="<?= e($part['supplier'] ?? '') ?>">
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="is_active" value="1" id="pActive" <?= !$isEdit || $part['is_active'] ? 'checked' : '' ?>>
                <label for="pActive" class="form-check-label">Active</label>
            </div>
        </div>
    </div>
    <div class="mt-3 d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
    <form method="post" action="<?= url('/Parts/delete/' . (int) $part['part_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete part?"
          data-confirm-body="Soft-delete <strong><?= e($part['name']) ?></strong>? Existing stock movements stay in history."
          data-confirm-ok="Delete">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete part</button>
    </form>
<?php endif; ?>
