<?php include __DIR__ . '/layout/header.php'; ?>

<div class="admin-card">
    <div class="admin-card-header" style="margin-bottom:14px;">
        <h2 class="admin-card-title"><i class="fa fa-bookmark" style="color:var(--primary);margin-right:8px;"></i>Marka Yönetimi</h2>
    </div>

    <!-- Add Form -->
    <form action="<?php echo $baseUrl; ?>/admin/brands/store" method="POST" enctype="multipart/form-data"
          style="background:var(--surface-2);border:1px solid var(--border);border-radius:var(--r-md);padding:16px;margin-bottom:20px;">
        <div class="section-title" style="margin-bottom:12px;"><i class="fa fa-plus-circle"></i> Yeni Marka Ekle</div>
        <div style="display:flex;gap:12px;align-items:flex-end;flex-wrap:wrap;">
            <div style="flex:1;min-width:130px;">
                <label class="form-label">Marka Adı</label>
                <input type="text" name="name" required class="admin-input" placeholder="Örn: OKI">
            </div>
            <div style="flex:1;min-width:120px;">
                <label class="form-label">Slug (Opsiyonel)</label>
                <input type="text" name="slug" class="admin-input" placeholder="oto-oluşturulur">
            </div>
            <div style="flex:1;min-width:150px;">
                <label class="form-label">Marka Görseli</label>
                <input type="file" name="brand_image" accept="image/*" class="admin-input" style="padding:6px;font-size:12px;">
            </div>
            <button type="submit" class="admin-btn admin-btn-primary" style="height:40px;white-space:nowrap;flex-shrink:0;">
                <i class="fa fa-plus"></i> Ekle
            </button>
        </div>
    </form>

    <?php if (!empty($brands)): ?>
    <!-- Desktop Table -->
    <div class="admin-table-wrap desktop-only">
        <table class="admin-table" id="brands-table">
            <thead>
                <tr>
                    <th style="width:36px;">#</th>
                    <th style="width:64px;">Görsel</th>
                    <th>Marka Adı</th>
                    <th>Slug</th>
                    <th style="width:70px;">Ürünler</th>
                    <th style="text-align:right;">Düzenle / Sil</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($brands as $i => $brand):
                    $imgUrl = '';
                    if (!empty($brand['image'])) {
                        $imgUrl = $baseUrl . '/' . ltrim($brand['image'], '/');
                    } else {
                        $imgPath = __DIR__ . '/../../../public/uploads/brands/' . $brand['slug'] . '.webp';
                        if (file_exists($imgPath)) {
                            $imgUrl = $baseUrl . '/uploads/brands/' . $brand['slug'] . '.webp';
                        }
                    }
                    $prodCount = count(\App\Models\Product::all(['brand_slug' => $brand['slug']]));
                ?>
                <tr>
                    <td style="color:var(--text-3);"><?php echo $i + 1; ?></td>
                    <td>
                        <?php if ($imgUrl): ?>
                        <img src="<?php echo htmlspecialchars($imgUrl); ?>" alt="" style="width:56px;height:36px;object-fit:contain;border-radius:4px;background:var(--surface-2);border:1px solid var(--border);">
                        <?php else: ?>
                        <div style="width:56px;height:36px;background:var(--surface-2);border:1px solid var(--border);border-radius:4px;display:flex;align-items:center;justify-content:center;color:var(--text-3);font-size:18px;"><i class="fa fa-image"></i></div>
                        <?php endif; ?>
                    </td>
                    <td><strong style="color:var(--text-1);"><?php echo htmlspecialchars($brand['name']); ?></strong></td>
                    <td><code style="background:var(--surface-2);border:1px solid var(--border);padding:3px 8px;border-radius:5px;font-size:11.5px;color:var(--text-2);"><?php echo htmlspecialchars($brand['slug']); ?></code></td>
                    <td>
                        <a href="<?php echo $baseUrl; ?>/admin/products?brand=<?php echo urlencode($brand['slug']); ?>" style="color:var(--primary);font-weight:700;text-decoration:none;">
                            <?php echo $prodCount; ?>
                        </a>
                    </td>
                    <td style="text-align:right;">
                        <form method="POST" enctype="multipart/form-data" action="<?php echo $baseUrl; ?>/admin/brands/update/<?php echo $i; ?>" style="display:inline-flex;gap:5px;align-items:center;">
                            <input type="text" name="name" value="<?php echo htmlspecialchars($brand['name']); ?>" required
                                   style="width:90px;padding:4px 6px;font-size:12px;border:1px solid var(--border);border-radius:5px;background:var(--surface-2);color:var(--text-1);">
                            <input type="text" name="slug" value="<?php echo htmlspecialchars($brand['slug']); ?>"
                                   style="width:70px;padding:4px 6px;font-size:12px;border:1px solid var(--border);border-radius:5px;background:var(--surface-2);color:var(--text-1);">
                            <input type="file" name="brand_image" accept="image/*"
                                   style="max-width:100px;font-size:10px;padding:2px;">
                            <button type="submit" class="admin-btn admin-btn-sm"><i class="fa fa-save"></i></button>
                        </form>
                        <button onclick="confirmDelete('<?php echo $baseUrl; ?>/admin/brands/delete/<?php echo urlencode($brand['slug']); ?>')"
                                class="admin-action-btn delete" style="margin-left:4px;" title="Sil">
                            <i class="fa fa-trash"></i>
                        </button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Mobile Cards -->
    <div class="mobile-only">
        <?php foreach ($brands as $i => $brand):
            $imgUrl = '';
            if (!empty($brand['image'])) {
                $imgUrl = $baseUrl . '/' . ltrim($brand['image'], '/');
            } else {
                $imgPath = __DIR__ . '/../../../public/uploads/brands/' . $brand['slug'] . '.webp';
                if (file_exists($imgPath)) $imgUrl = $baseUrl . '/uploads/brands/' . $brand['slug'] . '.webp';
            }
            $prodCount = count(\App\Models\Product::all(['brand_slug' => $brand['slug']]));
        ?>
        <div class="m-card">
            <div class="m-card-header">
                <div class="m-card-title-wrap">
                    <?php if ($imgUrl): ?>
                    <img src="<?php echo htmlspecialchars($imgUrl); ?>" class="m-card-img" alt="">
                    <?php else: ?>
                    <div class="m-card-icon"><i class="fa fa-bookmark"></i></div>
                    <?php endif; ?>
                    <div style="overflow:hidden;">
                        <h3 class="m-card-title"><?php echo htmlspecialchars($brand['name']); ?></h3>
                        <p class="m-card-subtitle"><?php echo htmlspecialchars($brand['slug']); ?></p>
                    </div>
                </div>
                <div class="m-card-value" style="color:var(--primary);"><?php echo $prodCount; ?> ürün</div>
            </div>
            <div class="m-card-footer" style="flex-direction:column;align-items:stretch;gap:8px;">
                <form method="POST" enctype="multipart/form-data" action="<?php echo $baseUrl; ?>/admin/brands/update/<?php echo $i; ?>" style="display:flex;flex-direction:column;gap:6px;">
                    <input type="text" name="name" value="<?php echo htmlspecialchars($brand['name']); ?>" required
                           style="width:100%;padding:6px 8px;font-size:13px;border:1px solid var(--border);border-radius:5px;background:var(--surface-2);color:var(--text-1);">
                    <input type="file" name="brand_image" accept="image/*" style="width:100%;font-size:11px;padding:4px;border:1px dashed var(--border);border-radius:4px;background:var(--surface-hover);">
                    <div style="display:flex;gap:6px;">
                        <button type="submit" class="admin-btn admin-btn-sm admin-btn-primary" style="flex:1;"><i class="fa fa-save"></i> Güncelle</button>
                        <button type="button" onclick="confirmDelete('<?php echo $baseUrl; ?>/admin/brands/delete/<?php echo urlencode($brand['slug']); ?>')"
                                class="admin-btn admin-btn-sm admin-btn-danger">
                            <i class="fa fa-trash"></i> Sil
                        </button>
                    </div>
                </form>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <?php else: ?>
    <div style="text-align:center;padding:48px 20px;color:var(--text-3);">
        <i class="fa fa-bookmark" style="font-size:48px;opacity:0.15;display:block;margin-bottom:16px;"></i>
        <p>Henüz marka eklenmemiş.</p>
    </div>
    <?php endif; ?>
</div>

<script>
function confirmDelete(url) {
    adminConfirm('Bu markayı silmek istiyor musunuz?', function() { window.location.href = url; });
}
</script>

<?php include __DIR__ . '/layout/footer.php'; ?>