@extends('layouts.app')
@section('title','Request Leave')
@section('page-title','Leave Management')

@section('content')
<div class="page-header">
    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div>
            <h1>Request Leave</h1>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('leaves.index') }}">Leaves</a></li>
                    <li class="breadcrumb-item active">New Request</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('leaves.index') }}" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-3">
    <div class="col-12 col-lg-8">
        <div class="card">
            <div class="card-header"><i class="bi bi-calendar-plus me-2 text-primary"></i>Leave Application Form</div>
            <div class="card-body">
                <form method="POST" action="{{ route('leaves.store') }}">
                    @csrf

                    @if(auth()->user()->isHrOrAdmin() && $employees)
                    <div class="mb-3">
                        <label class="form-label">Employee <span class="text-danger">*</span></label>
                        <select name="employee_id" class="form-select" required>
                            <option value="">— Select Employee —</option>
                            @foreach($employees as $emp)
                            <option value="{{ $emp->id }}" {{ old('employee_id') == $emp->id ? 'selected' : '' }}>{{ $emp->full_name }} ({{ $emp->employee_number }})</option>
                            @endforeach
                        </select>
                    </div>
                    @endif

                    <div class="mb-3">
                        <label class="form-label">Leave Type <span class="text-danger">*</span></label>
                        <div class="row g-2">
                            @foreach($leaveTypes as $lt)
                            <div class="col-12 col-sm-6">
                                <label class="d-flex align-items-center gap-2 p-3 border rounded-3 cursor-pointer leave-type-option {{ old('leave_type_id') == $lt->id ? 'selected' : '' }}" style="cursor:pointer;transition:all .15s;">
                                    <input type="radio" name="leave_type_id" value="{{ $lt->id }}" class="form-check-input leave-type-radio" {{ old('leave_type_id') == $lt->id ? 'checked' : '' }} required style="flex-shrink:0;">
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="rounded-2" style="width:12px;height:12px;background:{{ $lt->color }};flex-shrink:0;"></div>
                                        <div>
                                            <div style="font-size:.85rem;font-weight:600;">{{ $lt->name }}</div>
                                            <div class="text-muted-sm">{{ $lt->days_allowed_per_year }} days/year</div>
                                        </div>
                                    </div>
                                </label>
                            </div>
                            @endforeach
                        </div>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-12 col-sm-6">
                            <label class="form-label">From Date <span class="text-danger">*</span></label>
                            <input type="date" name="date_from" class="form-control" value="{{ old('date_from') }}" min="{{ today()->format('Y-m-d') }}" required id="dateFrom">
                        </div>
                        <div class="col-12 col-sm-6">
                            <label class="form-label">To Date <span class="text-danger">*</span></label>
                            <input type="date" name="date_to" class="form-control" value="{{ old('date_to') }}" min="{{ today()->format('Y-m-d') }}" required id="dateTo">
                        </div>
                    </div>

                    <!-- Duration display -->
                    <div class="alert alert-info d-none mb-3" id="durationInfo">
                        <i class="bi bi-calendar-range me-2"></i>
                        Duration: <strong id="durationText"></strong>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-6">
                            <label class="form-label">Duration (Weeks)</label>
                            <input type="number" name="duration_weeks" class="form-control" min="0" value="{{ old('duration_weeks') }}" placeholder="0">
                        </div>
                        <div class="col-6">
                            <label class="form-label">Duration (Months)</label>
                            <input type="number" name="duration_months" class="form-control" min="0" value="{{ old('duration_months') }}" placeholder="0">
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Forwarding Address</label>
                        <input type="text" name="forwarding_address" class="form-control" value="{{ old('forwarding_address') }}" placeholder="Where can you be reached during leave?">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Arrangement for Duties While Away</label>
                        <textarea name="duty_arrangement" class="form-control" rows="3" placeholder="Describe how your duties will be covered while you are on leave…">{{ old('duty_arrangement') }}</textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Reason / Remarks</label>
                        <textarea name="reason" class="form-control" rows="3" placeholder="Optional — provide additional context for your leave request…">{{ old('reason') }}</textarea>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary px-4">
                            <i class="bi bi-send me-2"></i>Submit Request
                        </button>
                        <a href="{{ route('leaves.index') }}" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Sidebar info -->
    <div class="col-12 col-lg-4">
        <div class="card mb-3">
            <div class="card-header"><i class="bi bi-info-circle me-2 text-primary"></i>Approval Process</div>
            <div class="card-body">
                <div class="timeline">
                    <div class="timeline-item">
                        <div class="timeline-icon bg-warning text-white"><i class="bi bi-person"></i></div>
                        <div class="timeline-body"><div class="fw-semibold" style="font-size:.85rem;">Step 1: Employee Submits</div><div class="text-muted-sm">You submit the request</div></div>
                    </div>
                    <div class="timeline-item">
                        <div class="timeline-icon bg-info text-white"><i class="bi bi-person-workspace"></i></div>
                        <div class="timeline-body"><div class="fw-semibold" style="font-size:.85rem;">Step 2: Dept. Head Review</div><div class="text-muted-sm">Your college/division head reviews</div></div>
                    </div>
                    <div class="timeline-item">
                        <div class="timeline-icon bg-primary text-white"><i class="bi bi-people"></i></div>
                        <div class="timeline-body"><div class="fw-semibold" style="font-size:.85rem;">Step 3: HR Review</div><div class="text-muted-sm">Associate VP / HR approves</div></div>
                    </div>
                    <div class="timeline-item" style="margin-bottom:0;">
                        <div class="timeline-icon bg-success text-white"><i class="bi bi-award"></i></div>
                        <div class="timeline-body"><div class="fw-semibold" style="font-size:.85rem;">Step 4: Final Approval</div><div class="text-muted-sm">VPAA / President grants leave</div></div>
                    </div>
                </div>
            </div>
        </div>

        @if($employee)
        <div class="card">
            <div class="card-header"><i class="bi bi-calendar-heart me-2 text-primary"></i>Your Leave Balances</div>
            <div class="card-body">
                @foreach($employee->leaveBalances()->with('leaveType')->get() as $bal)
                <div class="d-flex justify-content-between py-2 border-bottom" style="font-size:.83rem;">
                    <span class="d-flex align-items-center gap-2">
                        <span class="rounded-1" style="width:10px;height:10px;background:{{ $bal->leaveType->color }};display:inline-block;"></span>
                        {{ $bal->leaveType->name }}
                    </span>
                    <span class="fw-semibold">{{ $bal->remaining }} days left</span>
                </div>
                @endforeach
            </div>
        </div>
        @endif
    </div>
</div>

<style>
.leave-type-option:hover { border-color: #003366!important; background:#f8fafc; }
.leave-type-option.selected { border-color: #003366!important; background:#eff6ff; }
</style>

@push('scripts')
<script>
// Highlight selected leave type
document.querySelectorAll('.leave-type-radio').forEach(r => {
    r.addEventListener('change', function() {
        document.querySelectorAll('.leave-type-option').forEach(o => o.classList.remove('selected'));
        this.closest('.leave-type-option').classList.add('selected');
    });
});

// Duration calculator
function calcDuration() {
    const from = document.getElementById('dateFrom').value;
    const to   = document.getElementById('dateTo').value;
    if (from && to) {
        const d1 = new Date(from), d2 = new Date(to);
        if (d2 >= d1) {
            let days = 0, cur = new Date(d1);
            while (cur <= d2) {
                const day = cur.getDay();
                if (day !== 0 && day !== 6) days++;
                cur.setDate(cur.getDate()+1);
            }
            document.getElementById('durationInfo').classList.remove('d-none');
            document.getElementById('durationText').textContent = days + ' working day(s)';
        }
    }
}
document.getElementById('dateFrom').addEventListener('change', calcDuration);
document.getElementById('dateTo').addEventListener('change', calcDuration);
</script>
@endpush
@endsection
