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

requireAdminLogin();

// İstatistikler
$stats = [];

// Toplam firma sayısı
$stmt = $pdo->query("SELECT COUNT(*) as total FROM companies");
$stats['total_companies'] = $stmt->fetch()['total'];

// Aktif firma sayısı
$stmt = $pdo->query("SELECT COUNT(*) as total FROM companies WHERE status = 'active'");
$stats['active_companies'] = $stmt->fetch()['total'];

// Onay bekleyen yorumlar
$stmt = $pdo->query("SELECT COUNT(*) as total FROM reviews WHERE status = 'pending'");
$stats['pending_reviews'] = $stmt->fetch()['total'];

// Toplam kategori sayısı
$stmt = $pdo->query("SELECT COUNT(*) as total FROM company_categories WHERE status = 'active'");
$stats['total_categories'] = $stmt->fetch()['total'];

// Toplam blog yazısı
$stmt = $pdo->query("SELECT COUNT(*) as total FROM blog_posts WHERE status = 'published'");
$stats['total_blog_posts'] = $stmt->fetch()['total'];

// Son eklenen firmalar
$stmt = $pdo->query("SELECT id, company_name, email, membership_type, created_at FROM companies ORDER BY created_at DESC LIMIT 5");
$recent_companies = $stmt->fetchAll();

// Son yorumlar
$stmt = $pdo->query("SELECT r.*, c.company_name FROM reviews r LEFT JOIN companies c ON r.company_id = c.id ORDER BY r.created_at DESC LIMIT 5");
$recent_reviews = $stmt->fetchAll();

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

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h1 class="h3 mb-4">Dashboard</h1>
        </div>
    </div>
    
    <!-- İstatistik Kartları -->
    <div class="row mb-4">
        <div class="col-md-3 mb-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-subtitle mb-2">Toplam Firma</h6>
                            <h2 class="mb-0"><?= $stats['total_companies'] ?></h2>
                        </div>
                        <i class="fas fa-building fa-3x opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-subtitle mb-2">Aktif Firmalar</h6>
                            <h2 class="mb-0"><?= $stats['active_companies'] ?></h2>
                        </div>
                        <i class="fas fa-check-circle fa-3x opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card bg-warning text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-subtitle mb-2">Onay Bekleyen Yorumlar</h6>
                            <h2 class="mb-0"><?= $stats['pending_reviews'] ?></h2>
                        </div>
                        <i class="fas fa-comments fa-3x opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 mb-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="card-subtitle mb-2">Kategoriler</h6>
                            <h2 class="mb-0"><?= $stats['total_categories'] ?></h2>
                        </div>
                        <i class="fas fa-tags fa-3x opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <!-- Son Eklenen Firmalar -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Son Eklenen Firmalar</h5>
                    <a href="companies/index.php" class="btn btn-sm btn-primary">Tümünü Gör</a>
                </div>
                <div class="card-body">
                    <?php if (empty($recent_companies)): ?>
                        <p class="text-muted mb-0">Henüz firma eklenmemiş.</p>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Firma Adı</th>
                                        <th>E-posta</th>
                                        <th>Üyelik</th>
                                        <th>Tarih</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recent_companies as $company): ?>
                                        <tr>
                                            <td><?= e($company['company_name']) ?></td>
                                            <td><?= e($company['email']) ?></td>
                                            <td>
                                                <span class="badge bg-<?= $company['membership_type'] === 'paid' ? 'success' : 'secondary' ?>">
                                                    <?= $company['membership_type'] === 'paid' ? 'Ücretli' : 'Ücretsiz' ?>
                                                </span>
                                            </td>
                                            <td><?= formatDate($company['created_at'], 'd.m.Y') ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Son Yorumlar -->
        <div class="col-md-6 mb-4">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Son Yorumlar</h5>
                    <a href="reviews/index.php" class="btn btn-sm btn-primary">Tümünü Gör</a>
                </div>
                <div class="card-body">
                    <?php if (empty($recent_reviews)): ?>
                        <p class="text-muted mb-0">Henüz yorum yapılmamış.</p>
                    <?php else: ?>
                        <div class="list-group list-group-flush">
                            <?php foreach ($recent_reviews as $review): ?>
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div class="flex-grow-1">
                                            <h6 class="mb-1"><?= e($review['name'] . ' ' . $review['surname']) ?></h6>
                                            <p class="mb-1 small"><?= e($review['company_name']) ?></p>
                                            <p class="mb-1"><?= excerpt($review['comment'], 50) ?></p>
                                            <small class="text-muted"><?= formatDate($review['created_at']) ?></small>
                                        </div>
                                        <div>
                                            <?= displayStars($review['rating']) ?>
                                            <span class="badge bg-<?= $review['status'] === 'approved' ? 'success' : ($review['status'] === 'pending' ? 'warning' : 'danger') ?> ms-2">
                                                <?= $review['status'] === 'approved' ? 'Onaylı' : ($review['status'] === 'pending' ? 'Beklemede' : 'Reddedildi') ?>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

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





