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

requireAdminLogin();

// Arama ve filtreleme
$search = $_GET['search'] ?? '';
$status_filter = $_GET['status'] ?? '';
$membership_filter = $_GET['membership'] ?? '';

$where = [];
$params = [];

if ($search) {
    $where[] = "(company_name LIKE ? OR email LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if ($status_filter) {
    $where[] = "status = ?";
    $params[] = $status_filter;
}

if ($membership_filter) {
    $where[] = "membership_type = ?";
    $params[] = $membership_filter;
}

$whereClause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';

// Firmaları listele
$sql = "
    SELECT c.*, 
           (SELECT COUNT(*) FROM reviews WHERE company_id = c.id AND status = 'approved') as review_count,
           (SELECT AVG(rating) FROM reviews WHERE company_id = c.id AND status = 'approved') as avg_rating
    FROM companies c
    $whereClause
    ORDER BY c.created_at DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$companies = $stmt->fetchAll();

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

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

<!-- Filtreleme -->
<div class="card mb-4">
    <div class="card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-4">
                <input type="text" class="form-control" name="search" placeholder="Firma adı veya e-posta ara..." value="<?= e($search) ?>">
            </div>
            <div class="col-md-3">
                <select class="form-select" name="status">
                    <option value="">Tüm Durumlar</option>
                    <option value="active" <?= $status_filter === 'active' ? 'selected' : '' ?>>Aktif</option>
                    <option value="inactive" <?= $status_filter === 'inactive' ? 'selected' : '' ?>>Pasif</option>
                </select>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="membership">
                    <option value="">Tüm Üyelikler</option>
                    <option value="free" <?= $membership_filter === 'free' ? 'selected' : '' ?>>Ücretsiz</option>
                    <option value="paid" <?= $membership_filter === 'paid' ? 'selected' : '' ?>>Ücretli</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="fas fa-search me-2"></i>Filtrele
                </button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($companies)): ?>
            <p class="text-muted text-center py-5">Firma bulunamadı.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Firma Adı</th>
                            <th>E-posta</th>
                            <th>Telefon</th>
                            <th>Üyelik</th>
                            <th>Yorum</th>
                            <th>Puan</th>
                            <th>Durum</th>
                            <th>İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($companies as $company): ?>
                            <tr>
                                <td><?= $company['id'] ?></td>
                                <td><?= e($company['company_name']) ?></td>
                                <td><?= e($company['email']) ?></td>
                                <td><?= e($company['phone']) ?></td>
                                <td>
                                    <span class="badge bg-<?= $company['membership_type'] === 'paid' ? 'success' : 'secondary' ?>">
                                        <?= $company['membership_type'] === 'paid' ? 'Ücretli' : 'Ücretsiz' ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge bg-info"><?= $company['review_count'] ?></span>
                                </td>
                                <td>
                                    <?php if ($company['avg_rating']): ?>
                                        <?= number_format($company['avg_rating'], 1) ?> 
                                        <?= displayStars($company['avg_rating']) ?>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $company['status'] === 'active' ? 'success' : 'secondary' ?>">
                                        <?= $company['status'] === 'active' ? 'Aktif' : 'Pasif' ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="edit.php?id=<?= $company['id'] ?>" class="btn btn-sm btn-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="view.php?id=<?= $company['id'] ?>" class="btn btn-sm btn-info">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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





