@extends('layouts.app')

@section('title', 'Member Dashboard - LPTA Portal')

@section('head')
<style>
    /* ── Membership Card Layout ────────────────── */
    .id-card-wrap {
        perspective: 1000px;
        max-width: 400px;
        margin: 0 auto 30px;
    }
    .id-card-body {
        width: 100%;
        height: 250px;
        background: linear-gradient(135deg, var(--blue) 0%, var(--blue-mid) 50%, #0c1830 100%);
        border-radius: 16px;
        position: relative;
        box-shadow: var(--shadow-lg);
        border: 2px solid rgba(255, 255, 255, 0.12);
        color: white;
        padding: 24px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        overflow: hidden;
    }
    .id-card-body::before {
        content: '';
        position: absolute;
        inset: 0;
        background-image: repeating-linear-gradient(45deg, rgba(255,255,255,0.03) 0, rgba(255,255,255,0.03) 1px, transparent 0, transparent 50%);
        background-size: 20px 20px;
        pointer-events: none;
    }
    .id-card-glow {
        position: absolute;
        width: 250px;
        height: 250px;
        border-radius: 50%;
        background: radial-gradient(circle, rgba(192,57,43,0.3) 0%, transparent 70%);
        top: -100px;
        right: -100px;
        pointer-events: none;
    }
    .id-card-belt-strip {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 8px;
        background: var(--red); /* Fallback */
    }
    
    /* Dynamic belt rank coloring */
    .belt-white { background: #ffffff !important; box-shadow: inset 0 -1px 3px rgba(0,0,0,0.2); }
    .belt-yellow { background: #f1c40f !important; }
    .belt-green { background: #27ae60 !important; }
    .belt-blue { background: #2980b9 !important; }
    .belt-red { background: #c0392b !important; }
    .belt-black { background: #111111 !important; border-bottom: 2px solid var(--gold); }

    /* ── CSS Print Layout ──────────────────────── */
    @media print {
        body * { display: none !important; }
        .printable-card-section, .printable-card-section * { display: block !important; }
        .printable-card-section {
            position: absolute;
            left: 0;
            top: 0;
            width: 85.6mm !important;
            height: 53.98mm !important;
            padding: 0 !important;
            margin: 0 !important;
        }
        .id-card-body {
            width: 85.6mm !important;
            height: 53.98mm !important;
            border-radius: 0 !important;
            box-shadow: none !important;
            border: 1px solid #000 !important;
            box-sizing: border-box !important;
            -webkit-print-color-adjust: exact !important;
            print-color-adjust: exact !important;
        }
    }
</style>
@endsection

@section('content')

<!-- Member Header -->
<section style="background: linear-gradient(135deg, var(--blue) 0%, #0d1f45 100%); padding: 120px 0 60px; color: var(--white);">
    <div class="container" style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 20px;">
        <div>
            <span class="section-label" style="color: var(--gold-light);">MEMBER AREA</span>
            <h1 style="font-family: 'Oswald', sans-serif; font-size: clamp(2rem, 4vw, 2.6rem); font-weight: 700; margin-top: 6px;">Welcome, {{ $member->first_name }}!</h1>
            <p style="font-size: 0.88rem; color: rgba(255,255,255,0.7); margin-top: 6px;"><i class="fas fa-id-card"></i> LPTA ID: {{ $member->member_id }} | Belt Rank: {{ $member->belt_rank }}</p>
        </div>
        
        <form action="{{ route('logout') }}" method="POST">
            @csrf
            <button type="submit" class="btn btn-primary" style="padding: 10px 22px; font-size: 0.85rem;"><i class="fas fa-sign-out-alt"></i> Logout</button>
        </form>
    </div>
</section>

<!-- Dashboard Main Contents -->
<section class="section-pad" style="background: var(--white);">
    <div class="container">
        <div style="display: grid; grid-template-columns: 1.2fr 2fr; gap: 40px; align-items: start;">
            
            <!-- Left Side: Printable Digital Membership Card -->
            <div>
                <h3 style="font-family: 'Oswald', sans-serif; font-size: 1.25rem; color: var(--blue); margin-bottom: 20px;">Digital Membership Card</h3>
                
                @php
                    $beltClass = 'belt-white';
                    if (str_contains(strtolower($member->belt_rank), 'yellow')) $beltClass = 'belt-yellow';
                    elseif (str_contains(strtolower($member->belt_rank), 'green')) $beltClass = 'belt-green';
                    elseif (str_contains(strtolower($member->belt_rank), 'blue')) $beltClass = 'belt-blue';
                    elseif (str_contains(strtolower($member->belt_rank), 'red')) $beltClass = 'belt-red';
                    elseif (str_contains(strtolower($member->belt_rank), 'black')) $beltClass = 'belt-black';
                @endphp

                <!-- Card markup -->
                <div class="id-card-wrap printable-card-section">
                    <div class="id-card-body">
                        <div class="id-card-glow"></div>
                        <div class="id-card-belt-strip {{ $beltClass }}"></div>
                        
                        <!-- Top Header of Card -->
                        <div style="display: flex; justify-content: space-between; align-items: flex-start; z-index: 1;">
                            <div style="display: flex; align-items: center; gap: 8px;">
                                <div style="font-size: 1.15rem;">🥋</div>
                                <div style="line-height: 1;">
                                    <strong style="font-family: 'Oswald', sans-serif; font-size: 0.85rem; letter-spacing: 0.5px;">LPTA NEPAL</strong>
                                    <small style="display: block; font-size: 0.55rem; color: rgba(255,255,255,0.6); text-transform: uppercase;">Official Member</small>
                                </div>
                            </div>
                            <div style="font-size: 0.65rem; background: rgba(255,255,255,0.1); padding: 2px 8px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.15); font-weight: 700;">
                                {{ strtoupper($member->status) }}
                            </div>
                        </div>

                        <!-- Card Center -->
                        <div style="display: flex; gap: 14px; align-items: center; z-index: 1; margin: 10px 0;">
                            <div style="width: 65px; height: 65px; border-radius: 50%; background: linear-gradient(135deg, var(--red), var(--gold-light)); color: white; display: flex; align-items: center; justify-content: center; font-size: 1.6rem; font-weight: 700; border: 2px solid rgba(255,255,255,0.3); flex-shrink: 0;">
                                {{ substr($member->first_name, 0, 1) }}
                            </div>
                            <div>
                                <h4 style="font-family: 'Oswald', sans-serif; font-size: 1.15rem; font-weight: 600; line-height: 1.2;">{{ $member->full_name }}</h4>
                                <span style="font-size: 0.75rem; color: var(--gold-light); font-weight: 600;">{{ $member->belt_rank }}</span>
                                <small style="display: block; font-size: 0.62rem; color: rgba(255,255,255,0.7); margin-top: 2px;">Club: {{ $member->club ? $member->club->name : 'N/A' }}</small>
                            </div>
                        </div>

                        <!-- Card Footer -->
                        <div style="display: flex; justify-content: space-between; align-items: flex-end; z-index: 1; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 8px;">
                            <div>
                                <small style="display: block; font-size: 0.5rem; color: rgba(255,255,255,0.5); text-transform: uppercase;">Member ID</small>
                                <strong style="font-family: 'Oswald', sans-serif; font-size: 0.88rem;">{{ $member->member_id }}</strong>
                            </div>
                            
                            <div style="text-align: right;">
                                <small style="display: block; font-size: 0.5rem; color: rgba(255,255,255,0.5); text-transform: uppercase;">Expiry Date</small>
                                <strong style="font-family: 'Oswald', sans-serif; font-size: 0.88rem;">{{ $member->expiry_date ? $member->expiry_date->format('Y-m-d') : 'N/A' }}</strong>
                            </div>
                        </div>
                    </div>
                </div>

                <button onclick="window.print()" class="btn btn-blue" style="width: 100%; justify-content: center;"><i class="fas fa-print"></i> Print ID Card (CR80 Standard)</button>
            </div>

            <!-- Right Side: Records, Info, Logs -->
            <div>
                <!-- Personal Details Card -->
                <div style="background: var(--off-white); border-radius: var(--radius); border: 1px solid var(--gray-200); padding: 30px; box-shadow: var(--shadow-sm); margin-bottom: 30px;">
                    <h3 style="font-family: 'Oswald', sans-serif; font-size: 1.25rem; color: var(--blue); margin-bottom: 20px; border-bottom: 2px solid var(--gray-200); padding-bottom: 8px; display: flex; align-items: center; justify-content: space-between;">
                        <span><i class="fas fa-user-circle"></i> Profile Details</span>
                        <span class="badge badge-red" style="font-size: 0.65rem;">Status: {{ $member->status }}</span>
                    </h3>

                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; font-size: 0.88rem;">
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600;">Email</span>
                            <strong style="color: var(--blue);">{{ $member->email ?? 'N/A' }}</strong>
                        </div>
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600;">Phone</span>
                            <strong style="color: var(--blue);">{{ $member->phone ?? 'N/A' }}</strong>
                        </div>
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">Date of Birth</span>
                            <strong style="color: var(--blue); display: block; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">{{ $member->dob ? $member->dob->format('Y-m-d') : 'N/A' }}</strong>
                        </div>
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">Address</span>
                            <strong style="color: var(--blue); display: block; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">{{ $member->address ?? 'N/A' }}</strong>
                        </div>
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">District</span>
                            <strong style="color: var(--blue); display: block; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">{{ $member->district->name }}</strong>
                        </div>
                        <div>
                            <span style="color: var(--gray-500); display: block; font-size: 0.75rem; text-transform: uppercase; font-weight: 600; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">Dojang / Club</span>
                            <strong style="color: var(--blue); display: block; border-top: 1px solid var(--gray-200); padding-top: 8px; margin-top: 8px;">{{ $member->club ? $member->club->name : 'N/A' }}</strong>
                        </div>
                    </div>
                </div>

                <!-- Activity Log Card -->
                <div style="background: var(--off-white); border-radius: var(--radius); border: 1px solid var(--gray-200); padding: 30px; box-shadow: var(--shadow-sm);">
                    <h3 style="font-family: 'Oswald', sans-serif; font-size: 1.25rem; color: var(--blue); margin-bottom: 20px; border-bottom: 2px solid var(--gray-200); padding-bottom: 8px;"><i class="fas fa-history"></i> Account Activity Logs</h3>
                    
                    @if($logs->count() > 0)
                        <div style="display: flex; flex-direction: column; gap: 12px;">
                            @foreach($logs as $log)
                            <div style="background: white; border: 1px solid var(--gray-200); border-radius: 6px; padding: 12px 18px; display: flex; justify-content: space-between; align-items: center;">
                                <div>
                                    <span style="font-size: 0.85rem; font-weight: 600; color: var(--blue); text-transform: uppercase;">{{ str_replace('_', ' ', $log->action) }}</span>
                                    <p style="font-size: 0.78rem; color: var(--gray-500); margin-top: 2px;">{{ $log->details }}</p>
                                </div>
                                <span style="font-size: 0.75rem; color: var(--gray-500);"><i class="fas fa-clock"></i> {{ $log->created_at->diffForHumans() }}</span>
                            </div>
                            @endforeach
                        </div>
                    @else
                        <p style="font-size: 0.85rem; color: var(--gray-500); text-align: center; padding: 12px;">No activity logs found.</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</section>

@endsection
