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

requireCompanyLogin();

$companyId = $_SESSION['company_id'];

// Tüm kategorileri listele
$stmt = $pdo->query("SELECT * FROM company_categories WHERE status = 'active' ORDER BY name");
$allCategories = $stmt->fetchAll();

// Firma'nın seçtiği kategorileri al
$stmt = $pdo->prepare("SELECT category_id FROM company_category_relations WHERE company_id = ?");
$stmt->execute([$companyId]);
$selectedCategories = [];
while ($row = $stmt->fetch()) {
    $selectedCategories[] = $row['category_id'];
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $categories = $_POST['categories'] ?? [];
    
    try {
        // Önce mevcut ilişkileri sil
        $stmt = $pdo->prepare("DELETE FROM company_category_relations WHERE company_id = ?");
        $stmt->execute([$companyId]);
        
        // Yeni kategorileri ekle
        if (!empty($categories)) {
            $stmt = $pdo->prepare("INSERT INTO company_category_relations (company_id, category_id) VALUES (?, ?)");
            foreach ($categories as $categoryId) {
                $categoryId = intval($categoryId);
                if ($categoryId > 0) {
                    $stmt->execute([$companyId, $categoryId]);
                }
            }
        }
        
        setFlashMessage('success', 'Kategoriler başarıyla güncellendi.');
        redirect('categories.php');
    } catch (PDOException $e) {
        $error = 'Kategoriler güncellenirken bir hata oluştu.';
    }
}

$page_title = 'Kategori Seçimi';
include 'includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Hizmet Verdiğiniz Kategoriler</h1>
</div>

<?php if ($error): ?>
    <div class="alert alert-danger"><?= e($error) ?></div>
<?php endif; ?>

<div class="card">
    <div class="card-header">
        <h5 class="mb-0">Kategorileri Seçin</h5>
        <small class="text-muted">Hangi kayak merkezlerinde hizmet verdiğinizi seçin. Seçtiğiniz kategorilerde arama sonuçlarında görüneceksiniz.</small>
    </div>
    <div class="card-body">
        <form method="POST">
            <?php if (empty($allCategories)): ?>
                <p class="text-muted">Henüz kategori eklenmemiş.</p>
            <?php else: ?>
                <div class="row">
                    <?php foreach ($allCategories as $category): ?>
                        <div class="col-md-4 mb-3">
                            <div class="card">
                                <div class="card-body">
                                    <?php if ($category['icon']): ?>
                                        <div class="text-center mb-3">
                                            <img src="<?= e($category['icon']) ?>" alt="<?= e($category['name']) ?>" style="max-width: 100px; max-height: 100px; object-fit: cover;">
                                        </div>
                                    <?php endif; ?>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="categories[]" value="<?= $category['id'] ?>" id="category_<?= $category['id'] ?>" <?= in_array($category['id'], $selectedCategories) ? 'checked' : '' ?>>
                                        <label class="form-check-label" for="category_<?= $category['id'] ?>">
                                            <strong><?= e($category['name']) ?></strong>
                                        </label>
                                    </div>
                                    <?php if ($category['description']): ?>
                                        <p class="text-muted small mt-2 mb-0"><?= excerpt($category['description'], 80) ?></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <div class="mt-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>Kaydet
                </button>
                <a href="dashboard.php" class="btn btn-secondary">İptal</a>
            </div>
        </form>
    </div>
</div>

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





