@extends('layouts.app-mobile-partner')
@section('title', 'Booking ' . $booking->booking_code . ' — Partner')
@section('apptitle', 'Booking ' . $booking->booking_code)

@section('content')
@php
    $today = now()->toDateString();
    $upcoming = $booking->visits->where('scheduled_date', '>=', $today)->take(8);
    $completed = $booking->visits->where('status', 'completed');
    $pct = $booking->visits_per_period ? round(($booking->visits_used / $booking->visits_per_period) * 100, 0) : 0;
@endphp

@if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

{{-- Hero --}}
<div class="bk-show-hero">
    <div class="bk-show-service">{{ $booking->service->name ?? 'Service' }}</div>
    <div class="bk-show-customer">
        <div class="bk-show-avatar">{{ strtoupper(substr($booking->customer->name ?? $booking->customer_name ?? 'C', 0, 1)) }}</div>
        <div>
            <strong>{{ $booking->customer->name ?? $booking->customer_name }}</strong>
            <div class="muted">{{ $booking->customer->phone ?? $booking->customer_phone }}</div>
        </div>
    </div>
    <div class="bk-show-stats">
        <div>
            <strong>{{ $booking->visits_used }}</strong>
            <span>Done</span>
        </div>
        <div>
            <strong>{{ $booking->visits_per_period - $booking->visits_used }}</strong>
            <span>Left</span>
        </div>
        <div>
            <strong>₹{{ number_format($booking->total_paise/100, 0) }}</strong>
            <span>Monthly</span>
        </div>
    </div>
    <div class="bk-show-progress">
        <div class="bk-progress-bar"><div class="bk-progress-fill" style="width: {{ $pct }}%"></div></div>
        <span>{{ $pct }}% complete ({{ $booking->visits_used }}/{{ $booking->visits_per_period }})</span>
    </div>
</div>

{{-- Detail rows --}}
<div class="bk-detail-card">
    <div class="bk-detail-row">
        <span>Subscription</span>
        <strong class="bk-pill bk-pill-{{ $booking->subscription_status }}">{{ ucfirst($booking->subscription_status ?? 'active') }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Code</span>
        <strong>{{ $booking->booking_code }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Service area</span>
        <strong>{{ $booking->area->name ?? '—' }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Address</span>
        <strong>{{ $booking->address }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Pincode</span>
        <strong>{{ $booking->pincode ?: '—' }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Started</span>
        <strong>{{ $booking->current_period_start?->format('d M Y') ?? '—' }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Next billing</span>
        <strong>{{ $booking->next_billing_date?->format('d M Y') ?? '—' }}</strong>
    </div>
    <div class="bk-detail-row">
        <span>Auto-renew</span>
        <strong>{{ $booking->auto_renew ? 'On' : 'Off' }}</strong>
    </div>
    @if($booking->notes)
    <div class="bk-detail-row bk-detail-address">
        <span>Notes</span>
        <strong>{{ $booking->notes }}</strong>
    </div>
    @endif
</div>

{{-- Upcoming visits --}}
<div class="app-section">
    <div class="bk-section-head">
        <h3>Upcoming visits</h3>
        <span class="muted">{{ $upcoming->count() }} scheduled</span>
    </div>
    @if($upcoming->count())
        @foreach($upcoming as $v)
            <a href="{{ route('pa.visit', $v) }}" class="visit-card">
                <div class="vc-date">
                    <strong>{{ $v->scheduled_date->format('d') }}</strong>
                    <span>{{ $v->scheduled_date->format('M') }}</span>
                    <span class="vc-day">{{ $v->scheduled_date->format('D') }}</span>
                </div>
                <div class="vc-body">
                    <div class="vc-top">
                        <span class="visit-pill visit-pill-{{ $v->status }}">{{ ucfirst(str_replace('_',' ',$v->status)) }}</span>
                        <span class="vc-time">{{ $v->scheduled_slot }}</span>
                    </div>
                    <div class="vc-meta">
                        @if($v->teamMember)
                            <span>👥 Assigned: {{ $v->teamMember->name }}</span>
                        @else
                            <span>👤 Self</span>
                        @endif
                    </div>
                </div>
                <span class="vc-arrow">›</span>
            </a>
        @endforeach
    @else
        <div class="empty-app">
            <p>All visits completed.</p>
        </div>
    @endif
</div>

{{-- All visits count --}}
<div class="app-section app-pad">
    <div class="bk-stat-grid">
        <div class="bk-stat-cell">
            <span class="bsc-num">{{ $completed->count() }}</span>
            <span class="bsc-lbl">Completed</span>
        </div>
        <div class="bk-stat-cell">
            <span class="bsc-num">{{ $booking->visits->where('status', 'cancelled')->count() }}</span>
            <span class="bsc-lbl">Cancelled</span>
        </div>
        <div class="bk-stat-cell">
            <span class="bsc-num">{{ $booking->visits->count() }}</span>
            <span class="bsc-lbl">Total</span>
        </div>
    </div>
</div>

@endsection