@extends('layouts.student')
@section('title', 'Homework')
@section('page_title', 'Homework & Assignments')

@section('content')
    @if($materials->isNotEmpty())
        <div class="row g-3">
            @foreach($materials as $m)
                @php $sub = $submissions->get($m->id); @endphp
                <div class="col-md-6">
                    <div class="card h-100"><div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-1">
                            <h6 class="mb-0">{{ $m->title }}</h6>
                            <span class="badge bg-soft-{{ $m->type_color }}">{{ $m->type_label }}</span>
                        </div>
                        <div class="small text-muted mb-2">
                            {{ $m->subject?->name ? $m->subject->name.' · ' : '' }}{{ $m->teacher?->name }}
                            @if($m->due_date) · <span class="text-danger">Due {{ $m->due_date->format('d M Y') }}</span>@endif
                        </div>
                        @if($m->description)<p class="small mb-2" style="white-space:pre-line">{{ $m->description }}</p>@endif
                        @if($m->attachment_url)<a href="{{ $m->attachment_url }}" target="_blank" class="btn btn-sm btn-light border mb-2"><i class="bi bi-paperclip me-1"></i>Attachment</a>@endif

                        @if($m->type === 'assignment')
                            <hr class="my-2">
                            @if($sub)
                                <div class="small">
                                    <span class="badge bg-soft-{{ $sub->status_color }} text-capitalize">{{ $sub->status }}</span>
                                    @if($sub->marks !== null) · Marks: <strong>{{ $sub->marks }}</strong>@endif
                                    @if($sub->feedback)<div class="text-muted mt-1">Feedback: {{ $sub->feedback }}</div>@endif
                                    <div class="text-muted">Submitted {{ optional($sub->submitted_at)->format('d M, H:i') }}</div>
                                </div>
                                <button class="btn btn-sm btn-light border mt-2" data-bs-toggle="collapse" data-bs-target="#sub{{ $m->id }}">Re-submit</button>
                            @endif
                            <div class="{{ $sub ? 'collapse' : '' }} mt-2" id="sub{{ $m->id }}">
                                <form method="POST" action="{{ route('student.homework.submit', $m) }}" enctype="multipart/form-data">
                                    @csrf
                                    <textarea name="note" rows="2" class="form-control form-control-sm mb-2" placeholder="Note (optional)">{{ $sub?->note }}</textarea>
                                    <input type="file" name="attachment" class="form-control form-control-sm mb-2">
                                    <button class="btn btn-sm btn-primary"><i class="bi bi-upload me-1"></i>Submit Assignment</button>
                                </form>
                            </div>
                        @endif
                    </div></div>
                </div>
            @endforeach
        </div>
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">No homework posted for your class yet.</div></div>
    @endif
@endsection
