@php
    $sections = config('web.drawer_sections', []);
    $isLoggedIn = auth()->check();
    $user = auth()->user();

    $sess = session('_sess', []);
    $isLogin = !empty($sess['_login_']);
    $name = $sess['_mname'] ?? '';
    $email = $sess['_mid'] ?? '';
    $display = $name !== '' ? $name : ($email !== '' ? $email : '내 계정');
@endphp

{{-- Overlay --}}
<div class="mobile-drawer-overlay" data-drawer-close hidden></div>

{{-- Drawer --}}
<aside class="mobile-drawer" id="mobile-drawer" aria-hidden="true">
    <div class="mobile-drawer__head">
        <div class="mobile-drawer__title">PIN FOR YOU</div>
        <button class="mobile-drawer__close" type="button" aria-label="메뉴 닫기" data-drawer-close>✕</button>
    </div>

    {{-- ✅ 트렌디한 “프로필 카드”: 로그인 전/후 UI 분기 --}}
    <div class="m-usercard2 {{ $isLogin ? 'is-login' : 'is-guest' }}">
{{--        <div class="m-usercard2__head">--}}
{{--            <div class="m-usercard2__badge">--}}
{{--                {{ $isLogin ? 'MY' : 'GUEST' }}--}}
{{--            </div>--}}

{{--            <div class="m-usercard2__title">--}}
{{--                {{ $isLogin ? '내 계정' : '로그인이 필요해요' }}--}}
{{--                <div class="m-usercard2__sub">--}}
{{--                    {{ $isLogin ? '내역/설정은 여기서 바로' : '내역 확인 · 1:1문의는 로그인 후 이용 가능' }}--}}
{{--                </div>--}}
{{--            </div>--}}

{{--            @if($isLogin)--}}
{{--                <a class="m-usercard2__icon" href="{{ route('web.mypage.info.index') }}" aria-label="내 정보">--}}
{{--                    ⚙️--}}
{{--                </a>--}}
{{--            @endif--}}
{{--        </div>--}}

        <div class="m-usercard2__body">
            @if($isLogin)
                <div class="m-usercard2__profile">
                    <div class="m-usercard2__avatar" aria-hidden="true">
                        {{ mb_substr($user->name ?? 'U', 0, 1) }}
                    </div>

                    <div class="m-usercard2__info">
                        <div class="m-usercard2__name">{{ $name }}</div>
                        <div class="m-usercard2__meta">{{ $email }}</div>
                    </div>
                </div>
            @else
                <div class="m-usercard2__profile">
                    <div class="m-usercard2__avatar m-usercard2__avatar--ghost" aria-hidden="true">🔒</div>

                    <div class="m-usercard2__info">
                        <div class="m-usercard2__name">비회원</div>
                        <div class="m-usercard2__meta">로그인하면 할인구매/거래내역을 바로 확인할 수 있어요.</div>
                    </div>
                </div>
            @endif
        </div>

        <div class="m-usercard2__actions">
            @if($isLogin)
                <a class="m-btn2" href="{{ route('web.mypage.info.index') }}">내 정보</a>
                <a class="m-btn2 m-btn2--ghost" href="{{ route('web.mypage.usage.index') }}">이용내역</a>

                <form action="{{ route('web.auth.logout') }}" method="post" class="m-usercard2__logout">
                    @csrf
                    <button type="submit" class="m-btn2 m-btn2--text">로그아웃</button>
                </form>
            @else
                <a class="m-btn2" href="{{ route('web.auth.login') }}">로그인</a>
                <a class="m-btn2 m-btn2--ghost" href="{{ route('web.auth.register') }}">회원가입</a>
            @endif
        </div>
    </div>

    {{-- ✅ 메뉴 섹션(주메뉴/CS/정책/마이페이지) --}}
    <div class="mobile-drawer__body">
        @foreach($sections as $secKey => $sec)
            @php
                $title = $sec['title'] ?? '';
                $itemsKey = $sec['items'] ?? null;
                $rawItems = $itemsKey ? config('web.' . $itemsKey, []) : [];
                $items = collect($rawItems)->map(function($it){
                  $url = '#';
                  if (!empty($it['route'])) {
                    try { $url = route($it['route']); } catch (\Throwable $e) { $url = '#'; }
                  } elseif (!empty($it['url'])) {
                    $url = $it['url'];
                  }
                  return [
                    'label' => $it['label'] ?? '',
                    'url'   => $url,
                  ];
                })->values()->all();
            @endphp

            <section class="m-navsec">
                <div class="m-navsec__title">{{ $title }}</div>
                <div class="m-grid">
                    @foreach($items as $it)
                        <a class="m-tile" href="{{ $it['url'] }}">{{ $it['label'] }}</a>
                    @endforeach
                </div>
            </section>
        @endforeach
    </div>

    {{-- Footer CTA --}}
    <div class="mobile-drawer__foot">
        <a class="mobile-drawer__cta" href="{{ route('web.cs.qna.index') }}">1:1 문의하기</a>
    </div>
</aside>
