@extends('web.layouts.layout')

@section('content')
    <div class="subpage-wrap">
        <div class="container">

            {{-- Breadcrumb (optional) --}}
            @isset($breadcrumbs)
                @include('web.partials.breadcrumb', ['items' => $breadcrumbs])
            @endisset

            @php
                $heroSlides = config('web.hero_slides', []);

                // 섹션 감지
                $isCsPage = request()->is('cs') || request()->is('cs/*') || request()->routeIs('web.cs.*');
                $isMypagePage = request()->is('mypage') || request()->is('mypage/*') || request()->routeIs('web.mypage.*');
                $isPolicyPage = request()->is('policy') || request()->is('policy/*') || request()->routeIs('web.policy.*');

                // CS subnav 자동 주입
                if ($isCsPage && empty($subnavItems)) {
                    $subnavItems = collect(config('web.cs_tabs', []))
                        ->map(function ($t) {
                            $url = '#';
                            if (!empty($t['route']) && \Illuminate\Support\Facades\Route::has($t['route'])) {
                                $url = route($t['route']);
                            }
                            return [
                                'label' => $t['label'] ?? '',
                                'url'   => $url,
                                'key'   => $t['key'] ?? null,
                            ];
                        })
                        ->values()
                        ->all();
                }

                // MYPAGE subnav 자동 주입
                if ($isMypagePage && empty($subnavItems)) {
                    $subnavItems = collect(config('web.mypage_tabs', []))
                        ->map(function ($t) {
                            $url = '#';
                            if (!empty($t['route']) && \Illuminate\Support\Facades\Route::has($t['route'])) {
                                $url = route($t['route']);
                            }
                            return [
                                'label' => $t['label'] ?? '',
                                'url'   => $url,
                                'key'   => $t['key'] ?? null,
                            ];
                        })
                        ->values()
                        ->all();
                }

                // PolicyPage subnav 자동 주입
                if ($isPolicyPage && empty($subnavItems)) {
                    $subnavItems = collect(config('web.policy_tabs', []))
                        ->map(function ($t) {
                            $url = '#';
                            if (!empty($t['route']) && \Illuminate\Support\Facades\Route::has($t['route'])) {
                                $url = route($t['route']);
                            }
                            return [
                                'label' => $t['label'] ?? '',
                                'url'   => $url,
                                'key'   => $t['key'] ?? null,
                            ];
                        })
                        ->values()
                        ->all();
                }

                // active key 우선순위: 각 섹션 전용 변수 -> 기존 subnavActive
                $subnavActive = $csActive ?? $mypageActive ?? ($subnavActive ?? null);
            @endphp

            @if(!empty($heroSlides))
                @include('web.partials.hero-slider', [
                    'slides' => $heroSlides,
                    'variant' => 'compact',
                    'id' => 'hero-sub'
                ])
            @endif

            {{-- Body --}}
            <div class="subpage-grid">

                {{-- 고객센터 cs-tabs --}}
                @if($isCsPage)
                    @include('web.partials.cs-tabs', [
                        'activeKey' => $subnavActive
                    ])

                    {{-- 마이페이지 mypage-tabs --}}
                @elseif($isMypagePage)
                    @include('web.partials.mypage-tabs', [
                        'activeKey' => $subnavActive
                    ])

                    {{-- pinforyou mypage-tabs --}}
                @elseif($isPolicyPage)
                    @include('web.partials.policy-tabs', [
                        'activeKey' => $subnavActive
                    ])

                    {{-- 그 외 일반 서브페이지는 기존 방식 유지 --}}
                @else
                    {{-- Mobile Tabs --}}
                    <div class="subpage-tabs">
                        @include('web.partials.subpage-sidenav', [
                            'items' => $subnavItems ?? [],
                            'active' => $subnavActive ?? null,
                            'mode' => 'tabs'
                        ])
                    </div>

                    {{-- Desktop Side --}}
                    <aside class="subpage-side" aria-label="서브메뉴">
                        @include('web.partials.subpage-sidenav', [
                            'items' => $subnavItems ?? [],
                            'active' => $subnavActive ?? null,
                            'mode' => 'side'
                        ])
                    </aside>
                @endif

                {{-- Main --}}
                <main class="subpage-main" id="main-content">
                    <div class="content-card">
                        {{-- Header --}}
                        @include('web.partials.subpage-header', [
                            'title' => $pageTitle ?? '페이지 제목',
                            'desc'  => $pageDesc ?? null
                        ])
                        @yield('subcontent')
                    </div>
                </main>

            </div>
        </div>
    </div>
@endsection
