@extends('layouts.app')

@section('title', 'New Application')

@section('content')
    @include('layouts.partials.page_header', [
        'title' => 'New Application',
        'showSocial' => false,
        'breadcrumbs' => [
            ['label' => 'Home', 'url' => route('home')],
            ['label' => 'Applications', 'url' => route('portal.applications')],
            ['label' => 'Apply', 'url' => ''],
        ],
    ])

    @php
        $selectedScholarship = old('scholarship_id') ? $scholarships->firstWhere('id', (int) old('scholarship_id')) : null;
    @endphp

    @push('styles')
        <style>
            [x-cloak] {
                display: none !important;
            }
        </style>
    @endpush

    <section class="course-grid">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-xl-10">
                    <div class="course-grid__left">
                        <div class="course-grid__sidebar">
                            <div class="course-grid__search course-grid__single"
                                 x-data="{
                                     step: 1,
                                     program_id: '{{ old('program_id', $selectedProgram?->id ?? $program ?? '') }}',
                                     scholarship_id: '{{ old('scholarship_id', '') }}',
                                     programLabel: @js($selectedProgram?->title ?? 'Select a program'),
                                     scholarshipLabel: @js($selectedScholarship?->title ?? 'No scholarship selected'),
                                     documents: [],
                                     updateProgramLabel() {
                                         const option = this.$refs.programSelect?.selectedOptions?.[0];
                                         this.programLabel = option ? option.text : 'Select a program';
                                     },
                                     updateScholarshipLabel() {
                                         const option = this.$refs.scholarshipSelect?.selectedOptions?.[0];
                                         this.scholarshipLabel = option && option.value ? option.text : 'No scholarship selected';
                                     },
                                     setDocuments(event) {
                                         this.documents = Array.from(event.target.files || []).map(file => file.name);
                                     }
                                 }"
                                 x-init="updateProgramLabel(); updateScholarshipLabel()">
                                <div class="course-grid__title-box">
                                    <h3 class="course-grid__title">Application Wizard</h3>
                                    <div class="course-grid__title-shape-1">
                                        <img src="{{ asset('images/shapes/course-grid-title-shape-1.png') }}" alt="">
                                    </div>
                                </div>

                                <div class="course-grid__search-text mb-4">
                                    Complete your profile, upload your documents, then review everything before you submit.
                                </div>

                                <div class="d-flex flex-wrap gap-3 mb-4">
                                    <div class="badge rounded-pill px-3 py-2" :class="step >= 1 ? 'bg-primary' : 'bg-light text-dark'">1. Select Program</div>
                                    <div class="badge rounded-pill px-3 py-2" :class="step >= 2 ? 'bg-primary' : 'bg-light text-dark'">2. Documents</div>
                                    <div class="badge rounded-pill px-3 py-2" :class="step >= 3 ? 'bg-primary' : 'bg-light text-dark'">3. Review</div>
                                </div>

                                <form method="POST" action="{{ route('portal.apply.submit', $program) }}" enctype="multipart/form-data">
                                    @csrf

                                    <div x-show="step === 1" x-cloak>
                                        <div class="row g-4">
                                            <div class="col-lg-8">
                                                <div class="mb-4">
                                                    <label class="form-label fw-semibold">Program</label>
                                                    <select name="program_id"
                                                            x-ref="programSelect"
                                                            x-model="program_id"
                                                            @change="updateProgramLabel()"
                                                            class="form-select"
                                                            required>
                                                        <option value="">-- Select Program --</option>
                                                        @foreach($programs as $prog)
                                                            <option value="{{ $prog->id }}" {{ (string) old('program_id', $selectedProgram?->id ?? $program ?? '') === (string) $prog->id ? 'selected' : '' }}>
                                                                {{ $prog->title }} ({{ $prog->university->name }})
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                    @error('program_id')
                                                        <div class="text-danger small mt-1">{{ $message }}</div>
                                                    @enderror
                                                </div>

                                                <div class="mb-0">
                                                    <label class="form-label fw-semibold">Scholarship (optional)</label>
                                                    <select name="scholarship_id"
                                                            x-ref="scholarshipSelect"
                                                            x-model="scholarship_id"
                                                            @change="updateScholarshipLabel()"
                                                            class="form-select">
                                                        <option value="">No scholarship</option>
                                                        @foreach($scholarships as $sch)
                                                            <option value="{{ $sch->id }}" {{ (string) old('scholarship_id') === (string) $sch->id ? 'selected' : '' }}>
                                                                {{ $sch->title }} - {{ optional($sch->university)->name ?? optional($sch->country)->name ?? 'Scholarship' }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="col-lg-4">
                                                <div class="rounded-4 bg-light p-4 h-100">
                                                    <p class="mb-2 text-uppercase small text-muted">Selected Program</p>
                                                    @if($selectedProgram)
                                                        <h4 class="mb-2">{{ $selectedProgram->title }}</h4>
                                                        <p class="mb-3">{{ $selectedProgram->university->name }} &middot; {{ $selectedProgram->university->country?->name }}</p>
                                                        <ul class="list-unstyled mb-0 small">
                                                            <li class="d-flex justify-content-between py-2 border-bottom">
                                                                <span>Deadline</span>
                                                                <strong>{{ $selectedProgram->application_deadline?->format('M d, Y') ?? 'N/A' }}</strong>
                                                            </li>
                                                            <li class="d-flex justify-content-between py-2 border-bottom">
                                                                <span>Duration</span>
                                                                <strong>{{ $selectedProgram->duration }}</strong>
                                                            </li>
                                                            <li class="d-flex justify-content-between py-2">
                                                                <span>Fee</span>
                                                                <strong>{{ number_format($selectedProgram->fee) }}</strong>
                                                            </li>
                                                        </ul>
                                                    @else
                                                        <p class="mb-0">
                                                            Pick a program to preview the deadline, university, and other application details here.
                                                        </p>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>

                                        <div class="d-flex justify-content-between align-items-center mt-4">
                                            <a href="{{ route('programs.index') }}" class="thm-btn-two">
                                                <span>Browse Programs</span>
                                                <i class="icon-angles-right"></i>
                                            </a>
                                            <button type="button" @click="step = 2" class="thm-btn">
                                                <span>Next Step</span>
                                            </button>
                                        </div>
                                    </div>

                                    <div x-show="step === 2" x-cloak>
                                        <div class="row g-4">
                                            <div class="col-lg-8">
                                                <label class="form-label fw-semibold">Upload Required Documents</label>
                                                <input type="file"
                                                       name="documents[]"
                                                       multiple
                                                       class="form-control"
                                                       required
                                                       @change="setDocuments($event)">
                                                <p class="course-grid__search-text mt-2 mb-3">
                                                    Upload at least one document. Accepted formats: PDF, JPG, PNG. Maximum size is 2MB per file.
                                                </p>
                                                @error('documents.*')
                                                    <div class="text-danger small mt-1">{{ $message }}</div>
                                                @enderror

                                                <div class="rounded-4 bg-light p-4">
                                                    <p class="mb-2 fw-semibold">Selected Files</p>
                                                    <template x-if="documents.length">
                                                        <ul class="list-unstyled mb-0">
                                                            <template x-for="file in documents" :key="file">
                                                                <li class="py-1" x-text="file"></li>
                                                            </template>
                                                        </ul>
                                                    </template>
                                                    <p class="mb-0 text-muted" x-show="!documents.length">No files selected yet.</p>
                                                </div>
                                            </div>

                                            <div class="col-lg-4">
                                                <div class="rounded-4 bg-light p-4 h-100">
                                                    <p class="mb-2 text-uppercase small text-muted">Upload Tips</p>
                                                    <ul class="list-unstyled mb-0">
                                                        <li class="py-2 border-bottom">Use clear file names for faster review.</li>
                                                        <li class="py-2 border-bottom">Make sure passport and transcript scans are readable.</li>
                                                        <li class="py-2">You can upload multiple files in one step.</li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="d-flex justify-content-between align-items-center mt-4">
                                            <button type="button" @click="step = 1" class="thm-btn-two">
                                                <span>Back</span>
                                                <i class="icon-angles-right"></i>
                                            </button>
                                            <button type="button" @click="step = 3" class="thm-btn">
                                                <span>Review Application</span>
                                            </button>
                                        </div>
                                    </div>

                                    <div x-show="step === 3" x-cloak>
                                        <div class="row g-4">
                                            <div class="col-lg-8">
                                                <div class="rounded-4 bg-light p-4">
                                                    <p class="mb-3 text-uppercase small text-muted">Review & Confirm</p>
                                                    <div class="row g-3">
                                                        <div class="col-md-6">
                                                            <div class="rounded-3 bg-white p-3 h-100">
                                                                <small class="text-muted d-block">Program</small>
                                                                <strong x-text="programLabel"></strong>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="rounded-3 bg-white p-3 h-100">
                                                                <small class="text-muted d-block">Scholarship</small>
                                                                <strong x-text="scholarshipLabel"></strong>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="rounded-3 bg-white p-3 h-100">
                                                                <small class="text-muted d-block">Documents</small>
                                                                <strong x-text="documents.length + ' file(s) selected'"></strong>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="rounded-3 bg-white p-3 h-100">
                                                                <small class="text-muted d-block">Submission</small>
                                                                <strong>Ready to submit</strong>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-lg-4">
                                                <div class="rounded-4 bg-light p-4 h-100">
                                                    <p class="mb-2 text-uppercase small text-muted">Final Checklist</p>
                                                    <ul class="list-unstyled mb-0">
                                                        <li class="py-2 border-bottom">Program and scholarship selected.</li>
                                                        <li class="py-2 border-bottom">All required documents uploaded.</li>
                                                        <li class="py-2">You can submit now and track the status from your dashboard.</li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="d-flex justify-content-between align-items-center mt-4">
                                            <button type="button" @click="step = 2" class="thm-btn-two">
                                                <span>Back</span>
                                                <i class="icon-angles-right"></i>
                                            </button>
                                            <button type="submit" class="thm-btn">
                                                <span>Submit Application</span>
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection
