<?php
use function App\Core\{e, url};

$section = (string) ($section ?? 'kategoriler');
$groups = $groups ?? [];
$activeGroup = (string) ($activeGroup ?? ($_GET['group'] ?? 'mod'));
$labels = ['mod' => 'Mod Kategorileri', 'music' => 'Muzik Kategorileri', 'music_genre' => 'Muzik Turleri'];
$activeItems = $groups[$activeGroup === 'music_genre' ? 'music_genres' : $activeGroup] ?? [];
$parentOptions = array_merge($groups['mod'] ?? [], $groups['music'] ?? []);
?>
<div class="panel zpanel zpanel-app">
    <?php require BASE_PATH . '/app/Views/zpanel/_sidebar.php'; ?>
    <section class="panel-body zpanel-main" data-zpanel-page>
        <div class="zpanel-head">
            <div>
                <p class="eyebrow">Icerik</p>
                <h1>Kategori Yonetimi</h1>
                <p class="muted">Sol taraftan yeni kategori ekle, sag taraftan mevcut kayitlari duzenle.</p>
            </div>
        </div>

        <nav class="category-tabs">
            <?php foreach ($labels as $key => $label): ?>
                <a class="<?= $activeGroup === $key ? 'is-active' : '' ?>" href="/zpanel/kategoriler?group=<?= e($key) ?>"><?= e($label) ?></a>
            <?php endforeach; ?>
        </nav>

        <div class="zpanel-grid category-manager">
            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2>Yeni kategori</h2></div>
                <form class="zpanel-form" data-zpanel-form action="/api/zpanel/categories/create" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                    <label>Tur
                        <select name="group">
                            <?php foreach ($labels as $key => $label): ?>
                                <option value="<?= e($key) ?>" <?= $activeGroup === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>Ad<input name="name" required maxlength="120"></label>
                    <label>Slug<input name="slug" maxlength="160" placeholder="bos birakirsan otomatik uretilir"></label>
                    <label>Ust kategori
                        <select name="parent_id">
                            <option value="0">Yok</option>
                            <?php foreach ($parentOptions as $parent): ?>
                                <option value="<?= e($parent['id'] ?? '') ?>"><?= e(($parent['type'] ?? 'mod') . ' - ' . ($parent['name'] ?? '')) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>Icon resmi<input type="file" name="icon_file" accept="image/jpeg,image/png,image/webp,image/gif"><small class="muted">jpg, png, webp veya gif yukle.</small></label>
                    <label>Banner resmi<input type="file" name="banner_file" accept="image/jpeg,image/png,image/webp,image/gif"><small class="muted">Kategori kapagi icin gorsel yukle.</small></label>
                    <label>Siralama<input name="sort_order" type="number" value="0"></label>
                    <label class="check-row"><input name="is_active" value="1" type="checkbox" checked> Aktif</label>
                    <label>Aciklama<textarea name="description" rows="4"></textarea></label>
                    <button class="pill primary" type="submit">Ekle</button>
                </form>
            </section>

            <section class="zpanel-card">
                <div class="zpanel-card-head"><h2><?= e($labels[$activeGroup] ?? 'Kategoriler') ?></h2><span><?= count($activeItems) ?> kayit</span></div>
                <div class="table-scroll">
                    <table class="zpanel-table">
                        <thead><tr><th>Ad</th><th>Slug</th><th>Ust</th><th>Sira</th><th>Durum</th><th>Aksiyon</th></tr></thead>
                        <tbody>
                        <?php foreach ($activeItems as $category): ?>
                            <tr>
                                <td><strong><?= e($category['name'] ?? '') ?></strong><span class="muted block"><?= e($category['description'] ?? '') ?></span></td>
                                <td><?= e($category['slug'] ?? '') ?></td>
                                <td><?= e($category['parent_name'] ?? '-') ?></td>
                                <td><?= e((string) ($category['sort_order'] ?? 0)) ?></td>
                                <td><span class="status-pill <?= !empty($category['is_active']) ? 'status-active' : 'status-archived' ?>"><?= !empty($category['is_active']) ? 'active' : 'hidden' ?></span></td>
                                <td>
                                    <details class="category-edit">
                                        <summary>Duzenle</summary>
                                        <form class="inline-admin-form category-admin-form" data-zpanel-form action="/api/zpanel/categories/update" method="post" enctype="multipart/form-data">
                                            <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                                            <input type="hidden" name="id" value="<?= e($category['id'] ?? '') ?>">
                                            <input type="hidden" name="group" value="<?= e($activeGroup) ?>">
                                            <input type="hidden" name="icon" value="<?= e($category['icon'] ?? '') ?>">
                                            <input type="hidden" name="banner" value="<?= e($category['banner'] ?? '') ?>">
                                            <label>Ad<input name="name" value="<?= e($category['name'] ?? '') ?>"></label>
                                            <label>Slug<input name="slug" value="<?= e($category['slug'] ?? '') ?>"></label>
                                            <label>Ust kategori<select name="parent_id"><option value="0">Yok</option><?php foreach ($parentOptions as $parent): ?><option value="<?= e($parent['id'] ?? '') ?>" <?= (string) ($category['parent_id'] ?? '') === (string) ($parent['id'] ?? '') ? 'selected' : '' ?>><?= e(($parent['type'] ?? 'mod') . ' - ' . ($parent['name'] ?? '')) ?></option><?php endforeach; ?></select></label>
                                            <label>Icon resmi
                                                <?php if (!empty($category['icon']) && str_starts_with((string) $category['icon'], '/')): ?><img class="category-media-preview" src="<?= e(url((string) $category['icon'])) ?>" alt=""><?php endif; ?>
                                                <input type="file" name="icon_file" accept="image/jpeg,image/png,image/webp,image/gif">
                                            </label>
                                            <label>Banner resmi
                                                <?php if (!empty($category['banner']) && str_starts_with((string) $category['banner'], '/')): ?><img class="category-media-preview banner" src="<?= e(url((string) $category['banner'])) ?>" alt=""><?php endif; ?>
                                                <input type="file" name="banner_file" accept="image/jpeg,image/png,image/webp,image/gif">
                                            </label>
                                            <label>Sira<input name="sort_order" type="number" value="<?= e((string) ($category['sort_order'] ?? 0)) ?>"></label>
                                            <label class="check-row"><input name="is_active" value="1" type="checkbox" <?= !isset($category['is_active']) || !empty($category['is_active']) ? 'checked' : '' ?>> Aktif</label>
                                            <label class="wide">Aciklama<textarea name="description"><?= e($category['description'] ?? '') ?></textarea></label>
                                            <button type="submit">Kaydet</button>
                                        </form>
                                    </details>
                                    <form data-zpanel-form action="/api/zpanel/categories/delete" method="post" data-confirm="Kategori pasife alinsin mi?">
                                        <input type="hidden" name="_csrf" value="<?= e($csrfToken ?? '') ?>">
                                        <input type="hidden" name="id" value="<?= e($category['id'] ?? '') ?>">
                                        <input type="hidden" name="group" value="<?= e($activeGroup) ?>">
                                        <button class="danger" type="submit">Pasif yap</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($activeItems)): ?><tr><td colspan="6">Kayit yok.</td></tr><?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </div>
    </section>
</div>
