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

requireCompanyLogin();

$companyId = $_SESSION['company_id'];

// Hizmetleri listele
$stmt = $pdo->prepare("
    SELECT s.*, sc.name as category_name 
    FROM services s
    LEFT JOIN service_categories sc ON s.service_category_id = sc.id
    WHERE s.company_id = ?
    ORDER BY s.created_at DESC
");
$stmt->execute([$companyId]);
$services = $stmt->fetchAll();

// Mevcut hizmet sayısı
$serviceCount = count($services);

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Hizmetlerim</h1>
    <?php if ($serviceCount < MAX_SERVICES): ?>
        <a href="create.php" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i>Yeni Hizmet Ekle
        </a>
    <?php else: ?>
        <button class="btn btn-secondary" disabled>
            <i class="fas fa-exclamation-triangle me-2"></i>Maksimum <?= MAX_SERVICES ?> hizmet ekleyebilirsiniz
        </button>
    <?php endif; ?>
</div>

<?php if ($serviceCount >= MAX_SERVICES): ?>
    <div class="alert alert-warning">
        <i class="fas fa-info-circle me-2"></i>Maksimum <?= MAX_SERVICES ?> hizmet ekleyebilirsiniz. Yeni hizmet eklemek için mevcut bir hizmeti silin.
    </div>
<?php endif; ?>

<div class="card">
    <div class="card-body">
        <?php if (empty($services)): ?>
            <p class="text-muted text-center py-5">Henüz hizmet eklenmemiş.</p>
        <?php else: ?>
            <div class="row">
                <?php foreach ($services as $service): ?>
                    <div class="col-md-6 mb-4">
                        <div class="card h-100">
                            <?php if ($service['image']): ?>
                                <img src="<?= e($service['image']) ?>" class="card-img-top" alt="<?= e($service['title']) ?>" style="height: 200px; object-fit: cover;">
                            <?php endif; ?>
                            <div class="card-body">
                                <h5 class="card-title"><?= e($service['title']) ?></h5>
                                <?php if ($service['category_name']): ?>
                                    <p class="text-muted small mb-2">
                                        <i class="fas fa-tag me-1"></i><?= e($service['category_name']) ?>
                                    </p>
                                <?php endif; ?>
                                <p class="card-text"><?= excerpt($service['description'], 100) ?></p>
                                <?php if ($service['price']): ?>
                                    <p class="h5 text-primary mb-3"><?= number_format($service['price'], 2) ?> ₺</p>
                                <?php endif; ?>
                                <div class="d-flex justify-content-between align-items-center">
                                    <small class="text-muted">
                                        <i class="fas fa-eye me-1"></i><?= $service['views'] ?> görüntülenme
                                    </small>
                                    <div>
                                        <a href="edit.php?id=<?= $service['id'] ?>" class="btn btn-sm btn-warning">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="delete.php?id=<?= $service['id'] ?>" class="btn btn-sm btn-danger btn-delete" onclick="return confirm('Bu hizmeti silmek istediğinize emin misiniz?')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

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





