@extends('layouts.app')
@section('title', $material->title)
@section('page_title', $material->title)
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('materials.index') }}">LMS</a></li><li class="breadcrumb-item active">{{ Str::limit($material->title, 30) }}</li>
@endsection
@section('page_actions')
    @can('lms.update')<a href="{{ route('materials.edit', $material) }}" class="btn btn-outline-primary"><i class="bi bi-pencil me-1"></i>Edit</a>@endcan
@endsection

@section('content')
    <div class="row g-3">
        <div class="col-lg-{{ $material->type === 'assignment' ? '5' : '12' }}">
            <div class="card h-100">
                <div class="card-header d-flex justify-content-between"><span><i class="bi bi-journal-text me-1"></i>Details</span><span class="badge bg-soft-{{ $material->type_color }}">{{ $material->type_label }}</span></div>
                <div class="card-body">
                    <dl class="row small mb-3">
                        <dt class="col-4 text-muted">Class</dt><dd class="col-8">{{ $material->schoolClass?->name }}{{ $material->section ? ' / '.$material->section->name : '' }}</dd>
                        <dt class="col-4 text-muted">Subject</dt><dd class="col-8">{{ $material->subject?->name ?? '—' }}</dd>
                        <dt class="col-4 text-muted">Due Date</dt><dd class="col-8">{{ optional($material->due_date)->format('d M Y') ?? '—' }}</dd>
                        <dt class="col-4 text-muted">Posted By</dt><dd class="col-8">{{ $material->teacher?->name ?? '—' }}</dd>
                        <dt class="col-4 text-muted">Status</dt><dd class="col-8"><span class="badge bg-soft-{{ $material->is_published ? 'success' : 'secondary' }}">{{ $material->is_published ? 'Published' : 'Draft' }}</span></dd>
                    </dl>
                    @if($material->description)<p class="small" style="white-space:pre-line">{{ $material->description }}</p>@endif
                    @if($material->attachment_url)<a href="{{ $material->attachment_url }}" target="_blank" class="btn btn-sm btn-light border"><i class="bi bi-paperclip me-1"></i>Download Attachment</a>@endif
                </div>
            </div>
        </div>

        @if($material->type === 'assignment')
            <div class="col-lg-7">
                <div class="card h-100">
                    <div class="card-header"><i class="bi bi-inbox me-1"></i>Submissions ({{ $material->submissions->count() }})</div>
                    <div class="card-body">
                        <table class="table table-sm align-middle">
                            <thead><tr><th>Student</th><th>Submitted</th><th>Status</th><th class="text-end">Marks</th><th></th></tr></thead>
                            <tbody>
                                @forelse($material->submissions as $sub)
                                    <tr>
                                        <td class="fw-semibold">{{ $sub->student?->name }}</td>
                                        <td class="small">{{ optional($sub->submitted_at)->format('d M, H:i') ?? '—' }}</td>
                                        <td><span class="badge bg-soft-{{ $sub->status_color }} text-capitalize">{{ $sub->status }}</span></td>
                                        <td class="text-end">{{ $sub->marks ?? '—' }}</td>
                                        <td class="text-end">
                                            @if($sub->attachment_url)<a href="{{ $sub->attachment_url }}" target="_blank" class="btn btn-sm btn-light border py-0"><i class="bi bi-download"></i></a>@endif
                                            @can('lms.update')<button class="btn btn-sm btn-light border py-0" data-bs-toggle="modal" data-bs-target="#grade{{ $sub->id }}"><i class="bi bi-pencil-square"></i></button>@endcan
                                        </td>
                                    </tr>
                                    @if($sub->note)<tr><td colspan="5" class="small text-muted ps-3">↳ {{ $sub->note }}</td></tr>@endif
                                @empty
                                    <tr><td colspan="5" class="text-center text-muted py-3">No submissions yet.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        @endif
    </div>

    {{-- Grade modals rendered outside the table (valid HTML) --}}
    @can('lms.update')
        @foreach($material->submissions as $sub)
            <div class="modal fade" id="grade{{ $sub->id }}" tabindex="-1"><div class="modal-dialog"><form method="POST" action="{{ route('submissions.grade', $sub) }}" class="modal-content">
                @csrf
                <div class="modal-header"><h5 class="modal-title">Grade — {{ $sub->student?->name }}</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
                <div class="modal-body">
                    <div class="mb-2"><label class="form-label small">Marks</label><input type="number" step="0.01" name="marks" value="{{ $sub->marks }}" class="form-control"></div>
                    <div class="mb-2"><label class="form-label small">Feedback</label><input type="text" name="feedback" value="{{ $sub->feedback }}" class="form-control"></div>
                    <div class="mb-2"><label class="form-label small">Status</label><select name="status" class="form-select">@foreach(['submitted','graded','returned'] as $st)<option value="{{ $st }}" @selected($sub->status===$st)>{{ ucfirst($st) }}</option>@endforeach</select></div>
                </div>
                <div class="modal-footer"><button type="button" class="btn btn-light border" data-bs-dismiss="modal">Cancel</button><button class="btn btn-primary">Save</button></div>
            </form></div></div>
        @endforeach
    @endcan
@endsection
