<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<h5 class="mb-4"><?= isset($product) ? 'Modifier le Produit' : 'Nouveau Produit' ?></h5>

<div class="row">
    <div class="col-lg-8">
        <div class="table-card">
            <form method="POST" action="<?= isset($product) ? '/products/edit/' . $product['id'] : '/products/create' ?>">
                <?= csrf_field() ?>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Reference *</label>
                        <input type="text" name="reference" class="form-control" value="<?= $product['reference'] ?? '' ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Designation *</label>
                        <input type="text" name="designation" class="form-control" value="<?= $product['designation'] ?? '' ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Categorie</label>
                        <input type="text" name="category" class="form-control" value="<?= $product['category'] ?? '' ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Prix HT *</label>
                        <input type="number" step="0.001" name="unit_price" class="form-control" value="<?= $product['unit_price'] ?? 0 ?>" required>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">TVA (%)</label>
                        <input type="number" step="0.01" name="vat_rate" class="form-control" value="<?= ($product['vat_rate'] ?? 0.19) * 100 ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Unite</label>
                        <select name="unit" class="form-select">
                            <option value="U" <?= ($product['unit'] ?? '') === 'U' ? 'selected' : '' ?>>Unite</option>
                            <option value="H" <?= ($product['unit'] ?? '') === 'H' ? 'selected' : '' ?>>Heure</option>
                            <option value="J" <?= ($product['unit'] ?? '') === 'J' ? 'selected' : '' ?>>Jour</option>
                            <option value="KG" <?= ($product['unit'] ?? '') === 'KG' ? 'selected' : '' ?>>Kg</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Stock</label>
                        <input type="number" name="stock_quantity" class="form-control" value="<?= $product['stock_quantity'] ?? 0 ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control" rows="3"><?= $product['description'] ?? '' ?></textarea>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Statut</label>
                        <select name="is_active" class="form-select">
                            <option value="1" <?= ($product['is_active'] ?? 1) == 1 ? 'selected' : '' ?>>Actif</option>
                            <option value="0" <?= ($product['is_active'] ?? 1) == 0 ? 'selected' : '' ?>>Inactif</option>
                        </select>
                    </div>
                </div>
                <div class="d-flex justify-content-between mt-4">
                    <a href="/products" class="btn btn-outline-secondary">Annuler</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg"></i> <?= isset($product) ? 'Mettre a jour' : 'Creer' ?>
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
