@extends('layouts.app')

@section('title', $student->full_name)

@section('content')
    <x-page-head :title="$student->full_name" :subtitle="$student->admission_number">
        <x-slot:actions>
            <a href="{{ route('school.students.edit', $student) }}" class="btn btn-primary">Edit</a>
            <a href="{{ route('school.students.index') }}" class="btn btn-outline">Back</a>
        </x-slot:actions>
    </x-page-head>

    <div style="display:grid; grid-template-columns: 1fr 1fr; gap:16px;">
        <x-panel title="Profile">
            <dl style="display:grid; grid-template-columns: 140px 1fr; gap:8px; margin:0;">
                <dt>Status</dt><dd><x-badge variant="blue">{{ $student->status }}</x-badge></dd>
                @if ($student->leadership_role)
                    <dt>Leadership</dt><dd><x-badge variant="green">{{ config('southend.leadership_roles.'.$student->leadership_role, $student->leadership_role) }}</x-badge></dd>
                @endif
                <dt>Gender</dt><dd>{{ ucfirst($student->gender ?? '—') }}</dd>
                <dt>Date of Birth</dt><dd>{{ optional($student->date_of_birth)->format('d M Y') ?? '—' }}</dd>
                <dt>Admission Date</dt><dd>{{ optional($student->admission_date)->format('d M Y') ?? '—' }}</dd>
                <dt>Academic Year</dt><dd>{{ $student->academicYear?->name ?? '—' }}</dd>
                <dt>Email</dt><dd>{{ $student->email ?? '—' }}</dd>
                <dt>Phone</dt><dd>{{ $student->phone ?? '—' }}</dd>
                <dt>Address</dt><dd>{{ $student->address ?? '—' }}</dd>
            </dl>
        </x-panel>

        <x-panel title="Guardians">
            @if ($student->guardians->isEmpty())
                <x-empty-state title="No guardians linked" message="Edit this student to link guardians." />
            @else
                <div class="table-wrap">
                    <table>
                        <thead><tr><th>Name</th><th>Relationship</th><th>Contact</th></tr></thead>
                        <tbody>
                            @foreach ($student->guardians as $guardian)
                                <tr>
                                    <td>{{ $guardian->full_name }} @if($guardian->pivot->is_primary)<x-badge variant="green">Primary</x-badge>@endif</td>
                                    <td>{{ $guardian->pivot->relationship ?? '—' }}</td>
                                    <td>{{ $guardian->phone ?? $guardian->email ?? '—' }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </x-panel>
    </div>

    <x-panel title="Classes">
        @if ($student->classes->isEmpty())
            <p style="color:var(--text2);">Not assigned to any class yet.</p>
        @else
            <div class="table-wrap"><table><thead><tr><th>Class</th><th>Level</th><th>Enrolled</th></tr></thead><tbody>
                @foreach ($student->classes as $class)
                    <tr><td>{{ $class->name }}</td><td>{{ $class->level ?? '—' }}</td><td>{{ $class->pivot->enrolled_on ?? '—' }}</td></tr>
                @endforeach
            </tbody></table></div>
        @endif
    </x-panel>

    <x-panel title="Documents" style="margin-top:16px;">
        <form method="POST" action="{{ route('school.students.documents.store', $student) }}" enctype="multipart/form-data" style="display:flex; gap:12px; align-items:end; margin-bottom:16px;">
            @csrf
            <div class="form-group"><label class="form-label" for="title">Title</label><input class="input" type="text" name="title" id="title" required></div>
            <div class="form-group"><label class="form-label" for="file">File</label><input class="input" type="file" name="file" id="file" required></div>
            <button type="submit" class="btn btn-primary btn-sm">Upload</button>
        </form>
        @if ($student->documents->isEmpty())
            <p style="color:var(--text2);">No documents uploaded.</p>
        @else
            <ul>@foreach ($student->documents as $doc)<li><a href="{{ asset('storage/'.$doc->file_path) }}" target="_blank">{{ $doc->title }}</a> ({{ $doc->file_name }})</li>@endforeach</ul>
        @endif
    </x-panel>
@endsection
