@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
  <div class="card">

    <div class="card-header d-flex justify-content-between align-items-center">
      <div>
        <h5 class="mb-0">Edit Submission #{{ $submission->id }}</h5>
        <small class="text-muted">
          Assignment: {{ $submission->assignment->title ?? '-' }} •
          Student: {{ $submission->student->name ?? '-' }} (ID: {{ $submission->student_id }})
        </small>
      </div>
      <a href="{{ route('assignment-submissions.show', $submission) }}" class="btn btn-outline-secondary btn-sm">Back</a>
    </div>
    <hr class="mb-0"/>

    <div class="card-body">

      {{-- Flash Messages --}}
      @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
      @endif
      @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
      @endif

      {{-- Global Errors --}}
      @if($errors->any())
        <div class="alert alert-danger">
          <strong>Please fix the following errors:</strong>
          <ul class="mb-0 mt-1">
            @foreach($errors->all() as $e)
              <li>{{ $e }}</li>
            @endforeach
          </ul>
        </div>
      @endif

      <form action="{{ route('assignment-submissions.update', $submission) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        {{-- Student/Admin: file replace --}}
        @if(auth()->user()->hasRole('student') || auth()->user()->hasRole('master-admin'))
          <div class="mb-3">
            <label class="form-label">Replace File</label>

            <input type="file"
                   name="file"
                   class="form-control @error('file') is-invalid @enderror">

            @error('file')
              <div class="invalid-feedback">{{ $message }}</div>
            @enderror

            <small class="text-muted d-block mt-1">
              Current:
              @if($submission->file_path)
                <a href="{{ asset($submission->file_path) }}" target="_blank">View / Download</a>
              @else
                -
              @endif
              <span class="ms-2">Max 10MB</span>
            </small>
          </div>
        @endif

        {{-- Teacher/Admin: grade --}}
        @if(auth()->user()->hasRole('teacher') || auth()->user()->hasRole('master-admin'))
          <div class="mb-3">
            <label class="form-label">Grade</label>

            <input type="text"
                   name="grade"
                   class="form-control @error('grade') is-invalid @enderror"
                   value="{{ old('grade', $submission->grade) }}"
                   placeholder="A / B / 90">

            @error('grade')
              <div class="invalid-feedback">{{ $message }}</div>
            @enderror
          </div>
        @endif

        <div class="d-flex gap-2">
          <button class="btn btn-primary">Save</button>
          <a href="{{ route('assignment-submissions.show', $submission) }}" class="btn btn-outline-secondary">Cancel</a>
        </div>

      </form>

    </div>
  </div>
</div>
@endsection