<?php
require_once '../../config/config.php';
require_once '../../includes/auth.php';

requireAdminLogin();

// Kategorileri listele
$stmt = $pdo->query("
    SELECT cc.*, 
           (SELECT COUNT(*) FROM company_category_relations WHERE category_id = cc.id) as company_count
    FROM company_categories cc
    ORDER BY cc.created_at DESC
");
$categories = $stmt->fetchAll();

$page_title = 'Firma Kategorileri';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Firma Kategorileri</h1>
    <a href="create.php" class="btn btn-primary">
        <i class="fas fa-plus me-2"></i>Yeni Kategori Ekle
    </a>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($categories)): ?>
            <p class="text-muted text-center py-5">Henüz kategori eklenmemiş.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Kategori Adı</th>
                            <th>Slug</th>
                            <th>Firma Sayısı</th>
                            <th>Durum</th>
                            <th>Oluşturulma</th>
                            <th>İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($categories as $category): ?>
                            <tr>
                                <td><?= $category['id'] ?></td>
                                <td>
                                    <?php if ($category['icon']): ?>
                                        <img src="<?= e(getImageUrl($category['icon'])) ?>" alt="" style="width: 30px; height: 30px; object-fit: cover; border-radius: 4px;" class="me-2">
                                    <?php endif; ?>
                                    <?= e($category['name']) ?>
                                </td>
                                <td><code><?= e($category['slug']) ?></code></td>
                                <td>
                                    <span class="badge bg-info"><?= $category['company_count'] ?></span>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $category['status'] === 'active' ? 'success' : 'secondary' ?>">
                                        <?= $category['status'] === 'active' ? 'Aktif' : 'Pasif' ?>
                                    </span>
                                </td>
                                <td><?= formatDate($category['created_at'], 'd.m.Y H:i') ?></td>
                                <td>
                                    <a href="edit.php?id=<?= $category['id'] ?>" class="btn btn-sm btn-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="delete.php?id=<?= $category['id'] ?>" class="btn btn-sm btn-danger btn-delete" onclick="return confirm('Bu kategoriyi silmek istediğinize emin misiniz?')">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>




