@extends('layouts.admin')

@section('title', 'Edit '.$programme->name)
@section('heading', 'Edit programme')

@section('content')

    <div class="mb-6">
        <a href="{{ route('admin.programmes.index') }}"
           class="inline-flex items-center gap-1.5 text-sm text-slate-500 transition hover:text-slate-900">
            <svg class="h-4 w-4" fill="currentColor" viewBox="0 0 20 20" aria-hidden="true">
                <path fill-rule="evenodd" d="M17 10a1 1 0 0 1-1 1H6.4l3.3 3.3a1 1 0 1 1-1.4 1.4l-5-5a1 1 0 0 1 0-1.4l5-5a1 1 0 0 1 1.4 1.4L6.4 9H16a1 1 0 0 1 1 1Z" clip-rule="evenodd"/>
            </svg>
            All programmes
        </a>
    </div>

    <form method="POST" action="{{ route('admin.programmes.update', $programme) }}">
        @csrf
        @method('PUT')

        <div class="grid gap-6 lg:grid-cols-3">
            <div class="space-y-6 lg:col-span-2">

                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Identity</h2>

                    <div class="mt-5 space-y-5">
                        <x-input name="name" label="Programme name" required
                                 :value="old('name', $programme->name)" />

                        <div class="grid gap-5 sm:grid-cols-2">
                            <x-input name="code" label="Code" required :value="old('code', $programme->code)" />
                            <x-select name="level" label="Level" required
                                      :selected="old('level', $programme->level)"
                                      :options="[
                                          'undergraduate' => 'Undergraduate',
                                          'diploma' => 'Diploma',
                                          'certificate' => 'Certificate',
                                          'postgraduate' => 'Postgraduate',
                                      ]" />
                        </div>

                        <x-input name="award" label="Award" required :value="old('award', $programme->award)" />

                        <div class="grid gap-5 sm:grid-cols-2">
                            <x-select name="school_id" label="School" required
                                      :selected="old('school_id', $programme->school_id)"
                                      :options="$schools->pluck('name', 'id')->all()" />

                            <x-select name="department_id" label="Department" placeholder="None"
                                      :selected="old('department_id', $programme->department_id)"
                                      :options="$departments->pluck('name', 'id')->all()" />
                        </div>
                    </div>
                </x-card>

                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Content</h2>

                    <div class="mt-5 space-y-5">
                        <x-textarea name="description" label="Short description" rows="2"
                                    hint="Shown on programme cards.">{{ old('description', $programme->description) }}</x-textarea>

                        <x-textarea name="overview" label="Overview" rows="6"
                                    hint="Shown on the programme page.">{{ old('overview', $programme->overview) }}</x-textarea>

                        <x-textarea name="career_opportunities" label="Career opportunities" rows="4">{{ old('career_opportunities', $programme->career_opportunities) }}</x-textarea>
                    </div>
                </x-card>

                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Entry requirements</h2>
                    <p class="mt-2 text-xs text-slate-500">
                        Requirements drive the advisory eligibility check shown to applicants.
                    </p>

                    @if ($programme->requirements->isEmpty())
                        <p class="mt-4 text-sm text-slate-500">No requirements defined.</p>
                    @else
                        <ul class="mt-4 space-y-2">
                            @foreach ($programme->requirements as $requirement)
                                <li class="rounded-lg border border-slate-200 p-3 text-sm">
                                    <p class="text-slate-800">{{ $requirement->description }}</p>
                                    <p class="mt-1 text-xs text-slate-500">
                                        {{ $requirement->typeLabel() }}
                                        @if ($requirement->minimum_points) &middot; min {{ $requirement->minimum_points }} points @endif
                                        @if ($requirement->required_subject) &middot; {{ $requirement->required_subject }} @endif
                                        @if ($requirement->minimum_grade) &middot; grade {{ $requirement->minimum_grade }} @endif
                                        @unless ($requirement->is_required) &middot; desirable @endunless
                                    </p>
                                </li>
                            @endforeach
                        </ul>
                    @endif
                </x-card>
            </div>

            <aside class="space-y-6">
                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Delivery</h2>

                    <div class="mt-5 space-y-5">
                        <div class="grid gap-4 sm:grid-cols-2">
                            <x-input name="duration" type="number" min="1" max="10" label="Duration" required
                                     :value="old('duration', $programme->duration)" />

                            <x-select name="duration_unit" label="Unit" required
                                      :selected="old('duration_unit', $programme->duration_unit)"
                                      :options="['years' => 'Years', 'semesters' => 'Semesters', 'months' => 'Months']" />
                        </div>

                        <fieldset>
                            <legend class="mb-2 text-sm font-medium text-slate-800">Study modes</legend>
                            <div class="space-y-2">
                                @foreach ($modes as $mode)
                                    <label class="flex items-center gap-2 text-sm text-slate-700">
                                        <input type="checkbox" name="study_modes[]" value="{{ $mode['value'] }}"
                                               @checked(in_array($mode['value'], $programme->study_modes ?? [], true))
                                               class="h-4 w-4 rounded border-slate-300 text-brand-700 focus:ring-brand-700">
                                        {{ $mode['label'] }}
                                    </label>
                                @endforeach
                            </div>
                        </fieldset>

                        <fieldset>
                            <legend class="mb-2 text-sm font-medium text-slate-800">Campuses</legend>
                            <div class="space-y-2">
                                @foreach (['Bunga Campus', 'Mulago Campus', 'Namugongo Campus', 'Kisoro Campus'] as $campus)
                                    <label class="flex items-center gap-2 text-sm text-slate-700">
                                        <input type="checkbox" name="campuses[]" value="{{ $campus }}"
                                               @checked(in_array($campus, $programme->campuses ?? [], true))
                                               class="h-4 w-4 rounded border-slate-300 text-brand-700 focus:ring-brand-700">
                                        {{ $campus }}
                                    </label>
                                @endforeach
                            </div>
                        </fieldset>
                    </div>
                </x-card>

                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Fees</h2>

                    <div class="mt-5 space-y-5">
                        <x-input name="tuition_amount" type="number" step="1000" min="0"
                                 label="Tuition per year ({{ $programme->currency }})"
                                 :value="old('tuition_amount', $programme->tuition_amount)" />

                        <x-input name="application_fee" type="number" step="1000" min="0"
                                 label="Application fee ({{ $programme->currency }})"
                                 hint="Leave blank to use the platform default."
                                 :value="old('application_fee', $programme->application_fee)" />
                    </div>
                </x-card>

                <x-card>
                    <h2 class="text-sm font-semibold uppercase tracking-wider text-slate-900">Publishing</h2>

                    <div class="mt-5 space-y-5">
                        <x-select name="status" label="Status" required
                                  :selected="old('status', $programme->status->value)"
                                  :options="['active' => 'Active', 'inactive' => 'Inactive']" />

                        <label class="flex items-center gap-2 text-sm text-slate-700">
                            <input type="checkbox" name="is_featured" value="1"
                                   @checked(old('is_featured', $programme->is_featured))
                                   class="h-4 w-4 rounded border-slate-300 text-brand-700 focus:ring-brand-700">
                            Feature on the homepage
                        </label>
                    </div>

                    <div class="mt-6 space-y-2">
                        <x-button type="submit" class="w-full">Save changes</x-button>
                        <x-button :href="route('programmes.show', $programme)" variant="outline" class="w-full">
                            View public page
                        </x-button>
                    </div>
                </x-card>
            </aside>
        </div>
    </form>

@endsection
