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

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

// Hizmet bilgileri
$stmt = $pdo->prepare("
    SELECT s.*, c.company_name, c.phone, c.whatsapp, c.membership_type, c.id as company_id
    FROM services s
    INNER JOIN companies c ON s.company_id = c.id
    WHERE s.id = ? AND s.status = 'active' AND c.status = 'active'
");
$stmt->execute([$id]);
$service = $stmt->fetch();

if (!$service) {
    header("HTTP/1.0 404 Not Found");
    include '404.php';
    exit;
}

// Görüntülenme sayısını artır
$stmt = $pdo->prepare("UPDATE services SET views = views + 1 WHERE id = ?");
$stmt->execute([$id]);

$page_title = $service['title'];
include 'includes/header.php';
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-8">
            <div class="card mb-4">
                <?php if ($service['image']): ?>
                    <img src="<?= e(getImageUrl($service['image'])) ?>" class="card-img-top" alt="<?= e($service['title']) ?>" style="max-height: 400px; object-fit: cover;">
                <?php endif; ?>
                <div class="card-body">
                    <h1 class="h2 mb-3"><?= e($service['title']) ?></h1>
                    
                    <div class="mb-4 service-description">
                        <?= $service['description'] ?>
                    </div>
                    
                    <?php if ($service['price']): ?>
                        <div class="mb-4">
                            <h3 class="text-primary"><?= number_format($service['price'], 2) ?> ₺</h3>
                        </div>
                    <?php endif; ?>
                    
                    <div class="mb-4">
                        <p class="mb-2"><strong>Hizmet Veren Firma:</strong></p>
                        <a href="<?= SITE_URL ?>/firma/<?= $service['company_id'] ?>" class="btn btn-outline-primary">
                            <i class="fas fa-building me-2"></i><?= e($service['company_name']) ?>
                        </a>
                    </div>
                    
                    <!-- Üyelik Butonları (Ücretli üyelik için) -->
                    <?php if ($service['membership_type'] === 'paid'): ?>
                        <div class="mb-4">
                            <h5>İletişim</h5>
                            <?php if ($service['phone']): ?>
                                <a href="tel:<?= e($service['phone']) ?>" class="btn btn-success btn-lg me-2">
                                    <i class="fas fa-phone me-2"></i>Hemen Ara
                                </a>
                            <?php endif; ?>
                            <?php if ($service['whatsapp']): ?>
                                <a href="https://wa.me/<?= preg_replace('/[^0-9]/', '', $service['whatsapp']) ?>" target="_blank" class="btn btn-success btn-lg" style="background-color: #25D366; border-color: #25D366;">
                                    <i class="fab fa-whatsapp me-2"></i>WhatsApp
                                </a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Hizmet Bilgileri</h5>
                </div>
                <div class="card-body">
                    <p class="mb-2"><strong>Görüntülenme:</strong> <?= $service['views'] ?></p>
                    <p class="mb-2"><strong>Oluşturulma:</strong> <?= formatDate($service['created_at'], 'd.m.Y') ?></p>
                </div>
            </div>
            
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="mb-0">Firma Hakkında</h5>
                </div>
                <div class="card-body">
                    <p class="mb-3">
                        <strong><?= e($service['company_name']) ?></strong>
                    </p>
                    <a href="<?= SITE_URL ?>/firma/<?= $service['company_id'] ?>" class="btn btn-primary w-100">
                        Firma Detaylarını Gör
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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



