@extends('layouts.app-mobile-partner')
@section('title', 'Leaves — Partner App')
@section('apptitle', 'Leaves')

@section('appheaderright')
<a href="{{ route('pa.home') }}" class="ah-back" aria-label="Back">‹</a>
@endsection

@section('content')

<div class="app-section app-pad">
    <div class="leave-hero">
        <div class="lh-ic">🏖</div>
        <h3>Apply for leave</h3>
        <p>Submit a leave request. We'll auto-skip affected visits and notify customers.</p>
    </div>

    <form method="POST" action="{{ route('pa.leave.apply') }}" class="app-form">
        @csrf
        <div class="field-app">
            <label>Leave type</label>
            <select name="leave_type" required>
                <option value="full_day">Full day</option>
                <option value="half_day">Half day</option>
                <option value="multi_day">Multiple days</option>
            </select>
        </div>
        <div class="field-app">
            <label>From date</label>
            <input type="date" name="leave_date" min="{{ now()->toDateString() }}" required value="{{ old('leave_date') }}">
        </div>
        <div class="field-app">
            <label>To date <span class="optional">(for multi-day)</span></label>
            <input type="date" name="leave_end_date" min="{{ now()->toDateString() }}" value="{{ old('leave_end_date') }}">
        </div>
        <div class="field-app">
            <label>Reason <span class="optional">(optional)</span></label>
            <textarea name="reason" rows="2" placeholder="e.g. Family function, medical...">{{ old('reason') }}</textarea>
        </div>
        <button type="submit" class="btn-app btn-app-primary btn-app-full">Submit leave request</button>
    </form>

    <h3 class="app-detail-h3">My leave history</h3>
    @if($leaves->count())
        <div class="leave-list">
            @foreach($leaves as $lv)
                <div class="leave-row leave-pill-{{ $lv->status_color }}">
                    <div class="lv-dates">
                        <strong>{{ $lv->leave_date->format('d M') }}</strong>
                        @if($lv->leave_end_date && $lv->leave_end_date->ne($lv->leave_date))
                            <span>→ {{ $lv->leave_end_date->format('d M') }}</span>
                        @endif
                        <span class="lv-days">{{ $lv->days_count }} day{{ $lv->days_count > 1 ? 's' : '' }}</span>
                    </div>
                    <div class="lv-info">
                        <span class="visit-pill visit-pill-{{ $lv->status === 'approved' ? 'completed' : ($lv->status === 'rejected' ? 'cancelled' : 'scheduled') }}">
                            {{ ucfirst($lv->status) }}
                        </span>
                        <span class="muted">{{ ucfirst(str_replace('_', ' ', $lv->leave_type)) }} · {{ $lv->reason ?: 'No reason given' }}</span>
                    </div>
                    @if(in_array($lv->status, ['pending', 'approved']))
                        <form method="POST" action="{{ route('pa.leave.cancel', $lv) }}">
                            @csrf
                            <button type="submit" class="lv-cancel">Cancel</button>
                        </form>
                    @endif
                </div>
            @endforeach
        </div>
    @else
        <div class="empty-app">
            <div class="ea-ic">🏖</div>
            <h3>No leave requests</h3>
            <p>Need a day off? Apply above.</p>
        </div>
    @endif
</div>

@endsection