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

<div class="container-split" style="display: grid; grid-template-columns: 2fr 1fr; gap: 20px;">
    <div class="admin-card" style="padding:0;overflow:hidden;">
        <div class="admin-table-wrap">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Marka Adı</th>
                        <th>Slug</th>
                        <th style="text-align:right;width:120px;">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($brands as $b): ?>
                    <tr>
                        <td style="font-weight:600;color:var(--text-1);"><?php echo htmlspecialchars($b->name); ?></td>
                        <td><code><?php echo htmlspecialchars($b->slug); ?></code></td>
                        <td style="text-align:right;">
                            <div style="display:flex;justify-content:flex-end;gap:4px;">
                                <a href="javascript:void(0)" onclick="editBrand('<?php echo $b->id; ?>', '<?php echo htmlspecialchars($b->name); ?>', '<?php echo htmlspecialchars($b->slug); ?>')" class="admin-action-btn edit"><i class="fa fa-pencil"></i></a>
                                <a href="<?php echo $baseUrl; ?>/admin/brands/delete/<?php echo $b->id; ?>" onclick="return confirm('Bu markayı silmek istediğinize emin misiniz?')" class="admin-action-btn delete"><i class="fa fa-trash"></i></a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="admin-card">
        <h3 id="form-title">Yeni Marka Ekle</h3>
        <form id="brand-form" action="<?php echo $baseUrl; ?>/admin/brands/store" method="POST" style="margin-top:16px;">
            <div class="form-group">
                <label for="name">Marka Adı</label>
                <input type="text" name="name" id="brand-name" class="admin-input" required placeholder="Örn: Brother">
            </div>
            <div class="form-group" style="margin-top:12px;">
                <label for="slug">Slug (Opsiyonel)</label>
                <input type="text" name="slug" id="brand-slug" class="admin-input" placeholder="brother">
            </div>
            <div style="margin-top:20px;display:flex;gap:10px;">
                <button type="submit" class="admin-btn admin-btn-primary" id="btn-submit">Ekle</button>
                <button type="button" class="admin-btn" id="btn-cancel" style="display:none;" onclick="cancelEdit()">İptal</button>
            </div>
        </form>
    </div>
</div>

<script>
function editBrand(id, name, slug) {
    document.getElementById('form-title').innerText = 'Marka Düzenle';
    document.getElementById('brand-name').value = name;
    document.getElementById('brand-slug').value = slug;
    document.getElementById('brand-form').action = '<?php echo $baseUrl; ?>/admin/brands/update/' + id;
    document.getElementById('btn-submit').innerText = 'Güncelle';
    document.getElementById('btn-cancel').style.display = 'block';
}

function cancelEdit() {
    document.getElementById('form-title').innerText = 'Yeni Marka Ekle';
    document.getElementById('brand-name').value = '';
    document.getElementById('brand-slug').value = '';
    document.getElementById('brand-form').action = '<?php echo $baseUrl; ?>/admin/brands/store';
    document.getElementById('btn-submit').innerText = 'Ekle';
    document.getElementById('btn-cancel').style.display = 'none';
}
</script>

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