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

requireAdminLogin();

$id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("SELECT * FROM companies WHERE id = ?");
$stmt->execute([$id]);
$company = $stmt->fetch();

if (!$company) {
    setFlashMessage('error', 'Firma bulunamadı.');
    redirect('index.php');
}

// Firma resimleri
$stmt = $pdo->prepare("SELECT * FROM company_images WHERE company_id = ? ORDER BY sort_order");
$stmt->execute([$id]);
$images = $stmt->fetchAll();

// Firma kategorileri
$stmt = $pdo->prepare("
    SELECT cc.* FROM company_categories cc
    INNER JOIN company_category_relations ccr ON cc.id = ccr.category_id
    WHERE ccr.company_id = ?
");
$stmt->execute([$id]);
$categories = $stmt->fetchAll();

// Firma hizmetleri
$stmt = $pdo->prepare("SELECT * FROM services WHERE company_id = ? ORDER BY created_at DESC");
$stmt->execute([$id]);
$services = $stmt->fetchAll();

// Yorumlar
$stmt = $pdo->prepare("SELECT * FROM reviews WHERE company_id = ? ORDER BY created_at DESC");
$stmt->execute([$id]);
$reviews = $stmt->fetchAll();

// İstatistikler
$ratingStats = calculateAverageRating($id);
$ratingDistribution = getRatingDistribution($id);

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Firma Detayı</h1>
    <div>
        <a href="edit.php?id=<?= $id ?>" class="btn btn-warning">
            <i class="fas fa-edit me-2"></i>Düzenle
        </a>
        <a href="index.php" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-2"></i>Geri Dön
        </a>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">Firma Bilgileri</h5>
            </div>
            <div class="card-body">
                <table class="table">
                    <tr>
                        <th width="200">Firma Adı:</th>
                        <td><?= e($company['company_name']) ?></td>
                    </tr>
                    <tr>
                        <th>E-posta:</th>
                        <td><?= e($company['email']) ?></td>
                    </tr>
                    <tr>
                        <th>Telefon:</th>
                        <td><?= e($company['phone']) ?></td>
                    </tr>
                    <tr>
                        <th>WhatsApp:</th>
                        <td><?= e($company['whatsapp']) ?></td>
                    </tr>
                    <tr>
                        <th>Adres:</th>
                        <td><?= e($company['address']) ?></td>
                    </tr>
                    <tr>
                        <th>Üyelik Tipi:</th>
                        <td>
                            <span class="badge bg-<?= $company['membership_type'] === 'paid' ? 'success' : 'secondary' ?>">
                                <?= $company['membership_type'] === 'paid' ? 'Ücretli' : 'Ücretsiz' ?>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th>Durum:</th>
                        <td>
                            <span class="badge bg-<?= $company['status'] === 'active' ? 'success' : 'secondary' ?>">
                                <?= $company['status'] === 'active' ? 'Aktif' : 'Pasif' ?>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th>Görüntülenme:</th>
                        <td><?= $company['views'] ?></td>
                    </tr>
                    <tr>
                        <th>Firma Hakkında:</th>
                        <td><?= nl2br(e($company['about'])) ?></td>
                    </tr>
                </table>
            </div>
        </div>
        
        <?php if (!empty($images)): ?>
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Firma Resimleri</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <?php foreach ($images as $image): ?>
                            <div class="col-md-3 mb-3">
                                <img src="<?= e($image['image_url']) ?>" alt="" class="img-thumbnail w-100">
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        
        <?php if (!empty($services)): ?>
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Hizmetler</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Başlık</th>
                                    <th>Fiyat</th>
                                    <th>Görüntülenme</th>
                                    <th>Durum</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($services as $service): ?>
                                    <tr>
                                        <td><?= e($service['title']) ?></td>
                                        <td><?= $service['price'] ? number_format($service['price'], 2) . ' ₺' : '-' ?></td>
                                        <td><?= $service['views'] ?></td>
                                        <td>
                                            <span class="badge bg-<?= $service['status'] === 'active' ? 'success' : 'secondary' ?>">
                                                <?= $service['status'] === 'active' ? 'Aktif' : 'Pasif' ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>
    
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">İstatistikler</h5>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <strong>Ortalama Puan:</strong>
                    <div class="mt-2">
                        <?= $ratingStats['average'] ?> / 5.0
                        <?= displayStars($ratingStats['average']) ?>
                    </div>
                </div>
                <div class="mb-3">
                    <strong>Toplam Yorum:</strong> <?= $ratingStats['total'] ?>
                </div>
                <div>
                    <strong>Puanlama Dağılımı:</strong>
                    <ul class="list-unstyled mt-2">
                        <?php for ($i = 5; $i >= 1; $i--): ?>
                            <li>
                                <?= $i ?> Yıldız: <?= $ratingDistribution[$i] ?>
                            </li>
                        <?php endfor; ?>
                    </ul>
                </div>
            </div>
        </div>
        
        <?php if (!empty($categories)): ?>
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Kategoriler</h5>
                </div>
                <div class="card-body">
                    <?php foreach ($categories as $category): ?>
                        <span class="badge bg-primary me-1 mb-1"><?= e($category['name']) ?></span>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
        
        <?php if (!empty($reviews)): ?>
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Son Yorumlar</h5>
                </div>
                <div class="card-body">
                    <?php foreach (array_slice($reviews, 0, 5) as $review): ?>
                        <div class="mb-3 pb-3 border-bottom">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <strong><?= e($review['name'] . ' ' . $review['surname']) ?></strong>
                                <?= displayStars($review['rating']) ?>
                            </div>
                            <p class="mb-1 small"><?= excerpt($review['comment'], 100) ?></p>
                            <small class="text-muted"><?= formatDate($review['created_at']) ?></small>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

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





