<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-boxes me-2 text-primary"></i><?= $item ? 'Edit Item' : 'New Product / Service' ?></h5>
    <a class="btn btn-sm btn-outline-secondary" href="<?= url('/ProductsServices') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<form method="post" action="<?= url('/ProductsServices/save') ?>" class="rs-card p-3">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="item_id" value="<?= (int) ($item['item_id'] ?? 0) ?>">

    <div class="row g-3">
        <div class="col-md-3">
            <label class="form-label small fw-bold">Type <span class="text-danger">*</span></label>
            <select name="type" class="form-select">
                <option value="product" <?= ($item['type'] ?? 'product') === 'product' ? 'selected' : '' ?>>Product</option>
                <option value="service" <?= ($item['type'] ?? '') === 'service' ? 'selected' : '' ?>>Service</option>
            </select>
        </div>
        <div class="col-md-6">
            <label class="form-label small fw-bold">Name <span class="text-danger">*</span></label>
            <input type="text" name="name" class="form-control" value="<?= e($item['name'] ?? '') ?>" required>
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">SKU</label>
            <input type="text" name="sku" class="form-control" value="<?= e($item['sku'] ?? '') ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Unit</label>
            <select name="unit" class="form-select">
                <?php foreach (['ea','hr','kg','g','m','cm','mm','l','ml','m2','m3','pkt','box','set','lot'] as $u): ?>
                    <option value="<?= $u ?>" <?= ($item['unit'] ?? 'ea') === $u ? 'selected' : '' ?>><?= $u ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Unit Cost</label>
            <input type="number" step="0.01" name="unit_cost" class="form-control" value="<?= e($item['unit_cost'] ?? '') ?>" placeholder="0.00">
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Unit Price</label>
            <input type="number" step="0.01" name="unit_price" class="form-control" value="<?= e($item['unit_price'] ?? '') ?>" placeholder="0.00">
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Category</label>
            <input type="text" name="category" class="form-control" value="<?= e($item['category'] ?? '') ?>" list="cat-list">
            <datalist id="cat-list">
                <?php foreach (\App\Models\ProductService::categoriesForTenant((int) ($_SESSION['tenant_id'] ?? 0)) as $cat): ?>
                    <option value="<?= e($cat) ?>">
                <?php endforeach; ?>
            </datalist>
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Tax Rate (%)</label>
            <input type="number" step="0.01" name="tax_rate" class="form-control" value="<?= e($item['tax_rate'] ?? '0') ?>" placeholder="0">
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Taxable</label>
            <select name="taxable" class="form-select">
                <option value="1" <?= !isset($item) || ($item['taxable'] ?? 1) == 1 ? 'selected' : '' ?>>Yes</option>
                <option value="0" <?= ($item['taxable'] ?? 1) == 0 ? 'selected' : '' ?>>No</option>
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label small fw-bold">Active</label>
            <select name="is_active" class="form-select">
                <option value="1" <?= !isset($item) || ($item['is_active'] ?? 1) == 1 ? 'selected' : '' ?>>Yes</option>
                <option value="0" <?= ($item['is_active'] ?? 1) == 0 ? 'selected' : '' ?>>No</option>
            </select>
        </div>
        <div class="col-md-12">
            <label class="form-label small fw-bold">Description</label>
            <textarea name="description" class="form-control" rows="2"><?= e($item['description'] ?? '') ?></textarea>
        </div>
    </div>

    <div class="d-flex gap-2 mt-3">
        <button type="submit" class="btn btn-primary"><?= $item ? 'Update' : 'Create' ?> Item</button>
        <?php if ($item): ?>
            <a href="<?= url('/ProductsServices/delete/' . (int) $item['item_id']) ?>" class="btn btn-outline-danger" onclick="return confirm('Delete this item?')"><i class="fas fa-trash me-1"></i>Delete</a>
        <?php endif; ?>
    </div>
</form>