<?php $p = $product ?? null; ?>
<form class="form-card" method="post" action="<?= e(url('admin/urun/kaydet')) ?>" enctype="multipart/form-data" id="productForm">
    <?= csrf_field() ?>
    <input type="hidden" name="id" value="<?= (int)($p['id'] ?? 0) ?>">
    <input type="hidden" id="aiUrl" value="<?= e(url('admin/ai')) ?>">
    <input type="hidden" id="aiCsrf" value="<?= e(csrf_token()) ?>">

    <div class="form-row">
        <div class="form-group">
            <label>Ürün Adı *</label>
            <input name="name" id="prodName" value="<?= e($p['name'] ?? '') ?>" required>
        </div>
        <div class="form-group">
            <label>Kategori</label>
            <select name="category_id">
                <option value="">— Seçiniz —</option>
                <?php foreach ($categories as $c): ?>
                    <option value="<?= (int)$c['id'] ?>" <?= ($p['category_id'] ?? '')==$c['id']?'selected':'' ?>><?= e($c['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group">
            <label>Fiyat (<?= e(setting('currency','₺')) ?>) *</label>
            <input name="price" type="text" inputmode="decimal" value="<?= e($p['price'] ?? '') ?>" required>
        </div>
        <div class="form-group">
            <label>SEO Adres (slug) <small>otomatik</small></label>
            <input name="slug" value="<?= e($p['slug'] ?? '') ?>" placeholder="otomatik">
        </div>
    </div>

    <div class="form-group">
        <label>Açıklama</label>
        <textarea name="description" id="prodDesc" rows="3"><?= e($p['description'] ?? '') ?></textarea>
    </div>

    <!-- YAPAY ZEKA -->
    <div class="ai-box">
        <div class="ai-head">
            <div>
                <strong>🤖 Yapay Zeka Asistanı</strong>
                <p>Ürün adına göre kalori, alerjen ve besin değerlerini otomatik doldurur (API'siz, yerel).</p>
            </div>
            <button type="button" class="btn-ai" id="aiBtn">✨ Otomatik Doldur</button>
        </div>
        <div class="ai-status" id="aiStatus"></div>
    </div>

    <fieldset class="fieldset">
        <legend>Besin Değerleri (Yönetmelik)</legend>
        <div class="form-grid-5">
            <div class="form-group"><label>Kalori (kcal)</label><input name="calories" id="f_calories" type="number" value="<?= e($p['calories'] ?? '') ?>"></div>
            <div class="form-group"><label>Protein (g)</label><input name="protein" id="f_protein" type="text" value="<?= e($p['protein'] ?? '') ?>"></div>
            <div class="form-group"><label>Karbonhidrat (g)</label><input name="carbs" id="f_carbs" type="text" value="<?= e($p['carbs'] ?? '') ?>"></div>
            <div class="form-group"><label>Yağ (g)</label><input name="fat" id="f_fat" type="text" value="<?= e($p['fat'] ?? '') ?>"></div>
            <div class="form-group"><label>Şeker (g)</label><input name="sugar" id="f_sugar" type="text" value="<?= e($p['sugar'] ?? '') ?>"></div>
        </div>
        <div class="form-row">
            <div class="form-group"><label>Kafein (mg)</label><input name="caffeine" id="f_caffeine" type="number" value="<?= e($p['caffeine'] ?? '') ?>"></div>
            <div class="form-group"><label>Alerjenler</label><input name="allergens" id="f_allergens" value="<?= e($p['allergens'] ?? '') ?>" placeholder="Süt, Gluten, ..."></div>
        </div>
        <div class="form-group"><label>Notlar / Uyarılar</label><input name="notes" id="f_notes" value="<?= e($p['notes'] ?? '') ?>"></div>
    </fieldset>

    <div class="form-row">
        <div class="form-group">
            <label>Ürün Görseli</label>
            <input name="image" type="file" accept="image/*">
            <?php if (!empty($p['image'])): ?>
                <div class="current-media"><img src="<?= e(media_url($p['image'])) ?>" alt=""></div>
                <label class="switch small"><input type="checkbox" name="remove_image"> Görseli kaldır</label>
            <?php endif; ?>
        </div>
        <div class="form-group">
            <label>Ürün Videosu (mp4/webm) <small>hareketli görünüm</small></label>
            <input name="video" type="file" accept="video/*">
            <?php if (!empty($p['video'])): ?>
                <div class="current-media"><video muted loop autoplay playsinline><source src="<?= e(media_url($p['video'])) ?>"></video></div>
                <label class="switch small"><input type="checkbox" name="remove_video"> Videoyu kaldır</label>
            <?php endif; ?>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group"><label>Sıra</label><input name="sort_order" type="number" value="<?= (int)($p['sort_order'] ?? 0) ?>"></div>
        <div class="form-group toggles">
            <label class="switch"><input type="checkbox" name="is_featured" <?= (!empty($p['is_featured']))?'checked':'' ?>> Öne çıkar (anasayfa)</label>
            <label class="switch"><input type="checkbox" name="is_active" <?= (!$p || $p['is_active'])?'checked':'' ?>> Aktif</label>
        </div>
    </div>

    <div class="form-actions">
        <button class="btn-primary" type="submit">Kaydet</button>
        <a class="btn-ghost" href="<?= e(url('admin/urunler')) ?>">İptal</a>
    </div>
</form>
