@extends('layouts.app')

@section('title', 'My Documents')

@section('content')
    @include('layouts.partials.page_header', [
        'title' => 'My Documents',
        'showSocial' => false,
        'breadcrumbs' => [
            ['label' => 'Home', 'url' => route('home')],
            ['label' => 'Dashboard', 'url' => route('dashboard')],
            ['label' => 'Documents', 'url' => ''],
        ],
    ])

    <section class="course-grid">
        <div class="container">
            @if(session('message'))
                <div class="row justify-content-center mb-4">
                    <div class="col-xl-10">
                        <div class="alert alert-success mb-0">{{ session('message') }}</div>
                    </div>
                </div>
            @endif

            <div class="row g-4">
                <div class="col-lg-5">
                    <div class="course-grid__left h-100">
                        <div class="course-grid__sidebar h-100">
                            <div class="course-grid__search course-grid__single h-100">
                                <div class="course-grid__title-box">
                                    <h3 class="course-grid__title">Upload New Document</h3>
                                    <div class="course-grid__title-shape-1">
                                        <img src="{{ asset('images/shapes/course-grid-title-shape-1.png') }}" alt="">
                                    </div>
                                </div>

                                <p class="course-grid__search-text mb-4">
                                    Keep your passport, transcript, CV, and letters ready for future applications.
                                </p>

                                <form method="POST" action="{{ route('portal.documents.upload') }}" enctype="multipart/form-data">
                                    @csrf

                                    <div class="mb-3">
                                        <label class="form-label fw-semibold">Document Type</label>
                                        <select name="type" class="form-select">
                                            <option value="passport">Passport</option>
                                            <option value="transcript">Transcript</option>
                                            <option value="cv">CV</option>
                                            <option value="motivation_letter">Motivation Letter</option>
                                            <option value="recommendation_letter">Recommendation Letter</option>
                                            <option value="language_certificate">Language Certificate</option>
                                            <option value="other">Other</option>
                                        </select>
                                    </div>

                                    <div class="mb-4">
                                        <label class="form-label fw-semibold">File</label>
                                        <input type="file" name="file" required class="form-control">
                                        <p class="course-grid__search-text mt-2 mb-0">
                                            PDF, JPG, or PNG files up to 2MB.
                                        </p>
                                        @error('file')
                                            <div class="text-danger small mt-1">{{ $message }}</div>
                                        @enderror
                                    </div>

                                    <button type="submit" class="thm-btn">
                                        <span>Upload</span>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-7">
                    <div class="course-grid__left h-100">
                        <div class="course-grid__sidebar h-100">
                            <div class="course-grid__search course-grid__single h-100">
                                <div class="course-grid__title-box">
                                    <h3 class="course-grid__title">Uploaded Documents</h3>
                                    <div class="course-grid__title-shape-1">
                                        <img src="{{ asset('images/shapes/course-grid-title-shape-1.png') }}" alt="">
                                    </div>
                                </div>

                                @forelse($documents as $doc)
                                    <div class="d-flex flex-column flex-md-row justify-content-between gap-3 py-3 {{ !$loop->last ? 'border-bottom' : '' }}">
                                        <div>
                                            <p class="mb-1 fw-semibold">
                                                {{ ucfirst(str_replace('_', ' ', $doc->type)) }}
                                            </p>
                                            <p class="mb-1 text-muted">
                                                {{ $doc->application?->program?->title ?? 'No linked application' }}
                                            </p>
                                            <small class="text-muted">
                                                {{ $doc->created_at->format('M d, Y') }}
                                            </small>
                                            @if($doc->verified)
                                                <span class="badge bg-success ms-2">Verified</span>
                                            @endif
                                        </div>

                                        <div class="d-flex flex-wrap gap-2 align-items-start">
                                            <a href="{{ asset('storage/'.$doc->file_path) }}" target="_blank" class="thm-btn-two">
                                                <span>View</span>
                                            </a>
                                            <form method="POST" action="{{ route('portal.documents.destroy', $doc) }}" onsubmit="return confirm('Delete this document?')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="thm-btn">
                                                    <span>Delete</span>
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                @empty
                                    <div class="text-center py-5">
                                        <h4>No documents uploaded yet.</h4>
                                        <p class="mb-0">Use the upload form to add your first file.</p>
                                    </div>
                                @endforelse
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection
