@extends('layouts.app-shell')
@section('bottomnav')
    <a href="{{ route('partner.dashboard') }}"><span class="ic">📊</span><span>Home</span></a>
    <a href="{{ route('partner.bookings') }}"><span class="ic">📋</span><span>Bookings</span></a>
    <a href="{{ route('partner.schedule') }}" class="active"><span class="ic">📅</span><span>Schedule</span></a>
    <a href="{{ route('partner.team') }}"><span class="ic">👥</span><span>Team</span></a>
    <a href="{{ route('partner.profile') }}"><span class="ic">👤</span><span>Profile</span></a>
@endsection
@section('title', 'Schedule')
@section('sidebar')
    <a href="{{ route('partner.dashboard') }}"><span class="ic">📊</span> Dashboard</a>
    <a href="{{ route('partner.bookings') }}"><span class="ic">📋</span> My bookings</a>
    <a href="{{ route('partner.schedule') }}" class="active"><span class="ic">📅</span> Schedule</a>
    <a href="{{ route('partner.attendance') }}"><span class="ic">📍</span> Attendance</a>
    <a href="{{ route('partner.leaves') }}"><span class="ic">🏖</span> Leaves</a>
    <a href="{{ route('partner.team') }}"><span class="ic">👥</span> My team</a>
    <a href="{{ route('partner.earnings') }}"><span class="ic">💰</span> Earnings</a>
    <a href="{{ route('partner.profile') }}"><span class="ic">👤</span> Profile</a>
    <a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('lo').submit()"><span class="ic">⤴</span> Logout</a>
    <form id="lo" method="POST" action="{{ route('logout') }}" style="display:none">@csrf</form>
@endsection
@section('content')

@php
    $prev = $first->copy()->subMonth();
    $next = $first->copy()->addMonth();
    $today = now()->toDateString();
    $leaves = \App\Models\PartnerLeave::where('partner_id', $partner->id)
        ->whereBetween('leave_date', [$first->toDateString(), $first->copy()->endOfMonth()->toDateString()])
        ->where('status', 'approved')
        ->get()
        ->keyBy(fn($l) => $l->leave_date->format('Y-m-d'));
@endphp

<div class="flex-between mb-3">
    <div>
        <h2 style="font-size: 24px; font-weight: 700;">Schedule</h2>
        <p class="muted" style="font-size: 13px;">Your monthly calendar of visits, leaves and team assignments.</p>
    </div>
    <div class="flex" style="gap: 6px;">
        <a href="{{ route('partner.schedule', ['month' => $prev->month, 'year' => $prev->year]) }}" class="btn btn-outline btn-sm">‹ {{ $prev->format('M') }}</a>
        <strong style="padding: 8px 14px; background: #fff; border: 1px solid var(--bfk-border); border-radius: 10px; font-size: 14.5px;">{{ $first->format('F Y') }}</strong>
        <a href="{{ route('partner.schedule', ['month' => $next->month, 'year' => $next->year]) }}" class="btn btn-outline btn-sm">{{ $next->format('M') }} ›</a>
    </div>
</div>

<div class="kpi-row" style="margin-bottom: 18px;">
    <div class="kpi-card kpi-orange"><div class="kpi-ic">📅</div><div><strong>{{ $stats['scheduled'] }}</strong><span>Scheduled</span></div></div>
    <div class="kpi-card kpi-green"><div class="kpi-ic">✓</div><div><strong>{{ $stats['completed'] }}</strong><span>Completed</span></div></div>
    <div class="kpi-card kpi-purple"><div class="kpi-ic">🏖</div><div><strong>{{ $stats['leaves'] }}</strong><span>Leaves</span></div></div>
    <div class="kpi-card kpi-blue"><div class="kpi-ic">👥</div><div><strong>{{ $partner->activeTeamMembers()->count() }}</strong><span>Active team</span></div></div>
</div>

<div class="panel">
    <div class="cal-mini" style="grid-template-columns: repeat(7, 1fr); gap: 8px; margin-bottom: 14px;">
        @foreach(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] as $dow)
            <div class="dow">{{ $dow }}</div>
        @endforeach
    </div>
    <div class="month-grid">
        @php
            $startDow = $first->dayOfWeekIso - 1;
            $daysInMonth = (int) $first->daysInMonth;
        @endphp
        @for($i = 0; $i < $startDow; $i++)
            <div class="month-day is-empty"></div>
        @endfor
        @for($d = 1; $d <= $daysInMonth; $d++)
            @php $date = $first->copy()->day($d); $key = $date->format('Y-m-d'); @endphp
            <div class="month-day {{ $key === $today ? 'is-today' : '' }} {{ isset($leaves[$key]) ? 'is-leave' : '' }}">
                <div class="md-num">{{ $d }}</div>
                <div class="md-dow">{{ $date->format('D') }}</div>
                <div class="md-visits">
                    @if(isset($visits[$key]))
                        @php $dayVisits = $visits[$key]->take(3); @endphp
                        @foreach($dayVisits as $v)
                            <a href="{{ route('partner.visit', $v) }}" class="md-visit {{ $v->status === 'completed' ? 'is-done' : '' }} {{ $v->teamMember ? 'is-team' : '' }}" title="{{ $v->booking->service->name ?? '' }}">
                                {{ \Carbon\Carbon::parse($v->scheduled_slot)->format('H:i') }} {{ $v->teamMember ? '👥' : '' }}{{ $v->booking->service->name ?? '' }}
                            </a>
                        @endforeach
                        @if($visits[$key]->count() > 3)
                            <div class="md-more">+{{ $visits[$key]->count() - 3 }} more</div>
                        @endif
                    @endif
                    @if(isset($leaves[$key]))
                        <div class="md-visit" style="background: rgba(245, 158, 11, 0.2); color: #92400e;">🏖 On leave</div>
                    @endif
                </div>
            </div>
        @endfor
    </div>
</div>

<div class="flex mt-3" style="gap: 14px; flex-wrap: wrap; padding: 12px 0;">
    <div class="mini-stat"><span class="dot dot-orange"></span> Scheduled</div>
    <div class="mini-stat"><span class="dot dot-green"></span> Completed</div>
    <div class="mini-stat"><span class="dot" style="background: #6d28d9;"></span> With team member</div>
    <div class="mini-stat"><span class="dot" style="background: #b45309;"></span> On leave</div>
</div>

@endsection