<?php
require 'config.php';
require_login();

if (!function_exists('h')) {
    function h($s){ return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8'); }
}

$service_id = (int)($_GET['service_id'] ?? 0);
if ($service_id <= 0) redirect('index.php');

// সার্ভিস তথ্য আনা (মেইন সার্ভিস অ্যাক্টিভ থাকতে হবে)
$stmt = $pdo->prepare("SELECT name FROM services WHERE id=? AND is_active=1");
$stmt->execute([$service_id]);
$service = $stmt->fetch();

if (!$service) redirect("index.php");

// সাব-ক্যাটাগরি আনা
$stmt2 = $pdo->prepare("SELECT id, name, price, status FROM subcategories WHERE service_id=? ORDER BY id ASC");
$stmt2->execute([$service_id]);
$subcategories = $stmt2->fetchAll();

require_once 'header.php';
?>

<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">

<style>
    :root {
        --primary: #6C3FAA;
        --primary-dark: #4B2A7A;
        --accent: #FF9F43;
        --bg-light: #F8F5FD;
        --card-bg: #FFFFFF;
        --text-dark: #1E1135;
        --text-muted: #6C6A7A;
        --border: #E2DEF0;
        --shadow-sm: 0 4px 15px rgba(108, 63, 170, 0.08);
        --shadow-md: 0 8px 25px rgba(108, 63, 170, 0.12);
        --shadow-lg: 0 12px 40px rgba(108, 63, 170, 0.2);
        --radius: 20px;
        --transition: all 0.3s cubic-bezier(0.165, 0.84, 0.44, 1);
    }

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        background: linear-gradient(135deg, #F5F0FF 0%, #EDE4FB 100%);
        font-family: 'Hind Siliguri', sans-serif;
        color: var(--text-dark);
        min-height: 100vh;
    }

    .premium-container {
        max-width: 1100px;
        margin: 0 auto;
        padding: 2rem 1.5rem;
    }

    /* Hero Header */
    .hero-header {
        background: linear-gradient(135deg, #7B4DC7 0%, #542E90 100%);
        border-radius: var(--radius);
        padding: 2.5rem 2rem;
        text-align: center;
        box-shadow: var(--shadow-lg);
        margin-bottom: 2.5rem;
        position: relative;
        overflow: hidden;
    }
    .hero-header::after {
        content: '';
        position: absolute;
        width: 200px;
        height: 200px;
        background: rgba(255,255,255,0.05);
        border-radius: 50%;
        top: -60px;
        right: -60px;
    }
    .hero-header::before {
        content: '';
        position: absolute;
        width: 150px;
        height: 150px;
        background: rgba(255,255,255,0.03);
        border-radius: 50%;
        bottom: -40px;
        left: -40px;
    }
    .hero-icon {
        font-size: 3rem;
        color: var(--accent);
        margin-bottom: 1rem;
        position: relative;
        z-index: 1;
        display: inline-block;
        animation: float 3s ease-in-out infinite;
    }
    @keyframes float {
        0%, 100% { transform: translateY(0); }
        50% { transform: translateY(-10px); }
    }
    .breadcrumb {
        font-size: 0.9rem;
        color: rgba(255,255,255,0.7);
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        margin-bottom: 0.8rem;
        position: relative;
        z-index: 1;
    }
    .breadcrumb a {
        text-decoration: none;
        color: var(--accent);
        font-weight: 600;
        transition: var(--transition);
    }
    .breadcrumb a:hover {
        color: #FFF;
    }
    .hero-title {
        font-size: 2.2rem;
        color: white;
        font-weight: 700;
        margin: 0;
        position: relative;
        z-index: 1;
        text-shadow: 0 2px 10px rgba(0,0,0,0.2);
    }

    /* Service Grid */
    .service-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 25px;
    }

    /* Premium Card */
    .premium-card {
        background: var(--card-bg);
        border-radius: var(--radius);
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        text-decoration: none;
        position: relative;
        overflow: hidden;
        box-shadow: var(--shadow-sm);
        border: 1px solid var(--border);
        transition: var(--transition);
    }
    .premium-card:not(.disabled-card):hover {
        transform: translateY(-8px);
        box-shadow: var(--shadow-lg);
        border-color: var(--primary);
    }
    .premium-card.disabled-card {
        background: #F9F7FC;
        cursor: not-allowed;
    }

    .card-body {
        padding: 2rem 1.8rem;
        flex-grow: 1;
        text-align: center;
    }
    .card-icon {
        width: 70px;
        height: 70px;
        background: linear-gradient(135deg, #F1ECFB, #E6DEF7);
        color: var(--primary);
        border-radius: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 2rem;
        margin: 0 auto 1.2rem auto;
        transition: var(--transition);
    }
    .premium-card:not(.disabled-card):hover .card-icon {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        color: white;
        transform: rotateY(180deg);
    }
    .card-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: var(--text-dark);
        margin-bottom: 1rem;
        line-height: 1.4;
    }
    .price-badge {
        display: inline-block;
        background: linear-gradient(135deg, #FFF8E7, #FFF3D1);
        color: #B45309;
        padding: 8px 24px;
        border-radius: 50px;
        font-weight: 800;
        font-size: 1.3rem;
        border: 1px solid #FDE68A;
    }
    .disabled-card .price-badge {
        background: #EDE8F2;
        color: #8A7E9B;
        border-color: #D1CAE0;
    }

    .card-footer {
        padding: 1.2rem 1.8rem;
        background: #FAF8FE;
        border-top: 1px solid var(--border);
        border-radius: 0 0 var(--radius) var(--radius);
    }
    .btn-apply {
        display: block;
        width: 100%;
        background: linear-gradient(135deg, #7B4DC7, #542E90);
        color: white;
        padding: 12px;
        border-radius: 12px;
        text-align: center;
        font-weight: 700;
        transition: var(--transition);
        border: none;
        text-decoration: none;
        box-shadow: 0 4px 15px rgba(108,63,170,0.25);
    }
    .premium-card:not(.disabled-card):hover .btn-apply {
        background: linear-gradient(135deg, #8A5ED4, #6337A0);
        box-shadow: 0 8px 20px rgba(108,63,170,0.4);
    }
    .btn-closed {
        display: block;
        width: 100%;
        background: #D1CAE0;
        color: #6C6A7A;
        padding: 12px;
        border-radius: 12px;
        text-align: center;
        font-weight: 600;
        border: none;
        cursor: not-allowed;
    }

    /* Empty State */
    .empty-state {
        grid-column: 1 / -1;
        background: var(--card-bg);
        padding: 4rem 2rem;
        text-align: center;
        border-radius: var(--radius);
        border: 2px dashed var(--border);
        color: var(--text-muted);
        box-shadow: var(--shadow-sm);
    }
    .empty-state i {
        font-size: 3.5rem;
        color: #D1CAE5;
        display: block;
        margin-bottom: 1rem;
    }

    @media (max-width: 576px) {
        .hero-title { font-size: 1.5rem; }
        .premium-container { padding: 1rem; }
    }
</style>

<div class="premium-container">
    <!-- Hero Header -->
    <div class="hero-header">
        <div class="hero-icon">
            <i class="bi bi-rocket-takeoff-fill"></i>
        </div>
        <div class="breadcrumb">
            <a href="index.php"><i class="bi bi-house-fill"></i> হোম</a>
            <i class="bi bi-chevron-right small"></i>
            <span>সেবাসমূহ</span>
        </div>
        <h1 class="hero-title"><?= h($service['name']); ?></h1>
    </div>

    <!-- Service Grid -->
    <div class="service-grid">
        <?php if($subcategories): ?>
            <?php foreach($subcategories as $s):
                $id = $s['id']; 
                $name = h($s['name']);
                $price = number_format($s['price'], 2);
                $isActive = (isset($s['status']) && $s['status'] == 1);
                $link = $isActive ? "order.php?subcategory_id=$id" : "javascript:void(0)";
                $cardClass = $isActive ? "" : "disabled-card";
            ?>
            
            <a href="<?=$link?>" class="premium-card <?=$cardClass?>">
                <div class="card-body">
                    <div class="card-icon">
                        <?php if($isActive): ?>
                            <i class="bi bi-lightning-charge-fill"></i>
                        <?php else: ?>
                            <i class="bi bi-pause-circle-fill"></i>
                        <?php endif; ?>
                    </div>
                    <div class="card-title"><?=$name?></div>
                    <div class="price-badge">৳ <?=$price?></div>
                </div>
                <div class="card-footer">
                    <?php if ($isActive): ?>
                        <div class="btn-apply">অর্ডার করুন <i class="bi bi-arrow-right-short"></i></div>
                    <?php else: ?>
                        <div class="btn-closed">সাময়িকভাবে বন্ধ <i class="bi bi-lock-fill ms-1"></i></div>
                    <?php endif; ?>
                </div>
            </a>

            <?php endforeach; ?>
        <?php else: ?>
            <div class="empty-state">
                <i class="bi bi-inbox"></i>
                <h5 class="fw-bold">কোনো সেবা পাওয়া যায়নি</h5>
                <p class="mb-0">দুঃখিত, এই ক্যাটাগরিতে বর্তমানে কোনো প্যাকেজ উপলব্ধ নেই।</p>
            </div>
        <?php endif;?>
    </div>
</div>

<?php require_once 'footer.php'; ?>