<?php
require_once 'config.php';

$selected_category = isset($_GET['category']) ? trim($_GET['category']) : '';
$categories = getAllCategories($pdo);

if($selected_category && $selected_category !== '') {
    $stmt = $pdo->prepare("SELECT * FROM services WHERE is_active = 1 AND category = ? ORDER BY price_from");
    $stmt->execute([$selected_category]);
} else {
    $stmt = $pdo->prepare("SELECT * FROM services WHERE is_active = 1 ORDER BY category, price_from");
    $stmt->execute();
}
$services = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Юридические услуги - профессиональная помощь</title>
    <meta name="description" content="Полный спектр юридических услуг: консультации, составление документов, представительство в суде. Удобная навигация по категориям.">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Arial, sans-serif; background: #f0f4f8; line-height: 1.6; }
        header, footer { background: #0f172a; color: #f8fafc; padding: 1rem; text-align: center; }
        nav a { color: #f8fafc; margin: 0 15px; text-decoration: none; transition: 0.3s; }
        nav a:hover { color: #3b82f6; }
        main { max-width: 1300px; margin: 30px auto; padding: 0 20px; min-height: 60vh; }
        h1 { text-align: center; color: #0f172a; margin-bottom: 10px; }
        .subtitle { text-align: center; color: #475569; margin-bottom: 30px; }
        
        .categories { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-bottom: 35px; }
        .category-btn { background: #fff; color: #0f172a; padding: 10px 24px; border-radius: 40px; text-decoration: none; font-weight: bold; transition: 0.3s; box-shadow: 0 2px 5px rgba(0,0,0,0.05); }
        .category-btn:hover { background: #e2e8f0; transform: translateY(-2px); }
        .category-btn.active { background: #0f3b5f; color: #fff; box-shadow: 0 4px 12px rgba(15,59,95,0.3); }
        
        .services-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); gap: 25px; }
        .service-card { background: #fff; border-radius: 20px; padding: 25px; box-shadow: 0 5px 20px rgba(0,0,0,0.05); transition: transform 0.3s; }
        .service-card:hover { transform: translateY(-5px); box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .service-icon { font-size: 2.5rem; margin-bottom: 15px; }
        .service-name { font-size: 1.3rem; font-weight: bold; color: #0f172a; margin-bottom: 10px; }
        .service-category { display: inline-block; background: #e2e8f0; color: #475569; padding: 4px 12px; border-radius: 20px; font-size: 0.75rem; margin-bottom: 12px; }
        .service-desc { color: #475569; margin: 12px 0; line-height: 1.5; }
        .service-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 15px; padding-top: 15px; border-top: 1px solid #e2e8f0; }
        .service-price { font-size: 1.3rem; font-weight: bold; color: #0f3b5f; }
        .service-duration { color: #64748b; font-size: 0.85rem; }
        .btn { display: inline-block; background: #0f3b5f; color: #fff; padding: 8px 20px; text-decoration: none; border-radius: 30px; transition: 0.3s; font-weight: bold; font-size: 0.9rem; }
        .btn:hover { background: #1e5a7a; }
        footer { margin-top: 50px; }
        .empty-message { text-align: center; padding: 50px; background: #fff; border-radius: 16px; }
        @media (max-width: 768px) { .services-grid { grid-template-columns: 1fr; } .categories { gap: 8px; } .category-btn { padding: 6px 16px; font-size: 0.9rem; } }
    </style>
</head>
<body>
<header>
    <div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap;">
        <h1 style="margin: 0;">⚖️ Юридическая компания "Правовед"</h1>
        <nav>
            <a href="index.php">📋 Услуги</a>
            <a href="team.php">👨‍⚖️ Команда</a>
            <a href="consultation.php">📝 Консультация</a>
            <a href="my_consultations.php">📋 Мои заявки</a>
            <?php if(isLoggedIn()): ?>
                <a href="logout.php">🚪 Выйти (<?= h($_SESSION['username']) ?>)</a>
            <?php else: ?>
                <a href="login.php">🔐 Вход</a>
                <a href="register.php">📝 Регистрация</a>
            <?php endif; ?>
        </nav>
    </div>
</header>

<main>
    <h1>⚖️ Юридические услуги</h1>
    <p class="subtitle">Профессиональная правовая помощь по всем направлениям</p>
    
    <div class="categories">
        <a href="index.php" class="category-btn <?= empty($selected_category) ? 'active' : '' ?>">Все услуги</a>
        <?php foreach($categories as $cat): ?>
            <a href="?category=<?= urlencode($cat) ?>" class="category-btn <?= $selected_category == $cat ? 'active' : '' ?>">
                <?= h($cat) ?>
            </a>
        <?php endforeach; ?>
    </div>
    
    <?php if(count($services) > 0): ?>
        <div class="services-grid">
            <?php foreach($services as $service): ?>
                <div class="service-card">
                    <div class="service-icon"><?= h($service['icon'] ?: '⚖️') ?></div>
                    <div class="service-name"><?= h($service['name']) ?></div>
                    <div><span class="service-category">📁 <?= h($service['category']) ?></span></div>
                    <div class="service-desc"><?= h(mb_substr($service['description'], 0, 120)) ?>...</div>
                    <div class="service-footer">
                        <div>
                            <div class="service-price">от <?= number_format($service['price_from'], 0) ?> ₽</div>
                            <div class="service-duration">⏱️ <?= h($service['duration']) ?></div>
                        </div>
                        <a href="consultation.php?service=<?= $service['id'] ?>" class="btn">📝 Заказать</a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="empty-message">
            <p>😕 Услуг в выбранной категории не найдено.</p>
            <a href="index.php" class="btn">Показать все услуги</a>
        </div>
    <?php endif; ?>
</main>

<footer>
    <p>&copy; 2025 Юридическая компания "Правовед". Надёжная защита ваших прав.</p>
    <p>📍 Москва, ул. Правовая, 15 | 📞 +7 (999) 123-45-67 | ✉️ info@pravoved.ru</p>
</footer>
</body>
</html>