@extends('layout.admin')
@section('title','Courses')
@section('content')
<div class="max-w-5xl mx-auto px-1 mt-2">
  
  <!-- Header -->
  <div class="flex justify-between items-center mb-4">
    <h1 class="text-lg md:text-xl font-bold text-slate-700 flex items-center gap-2">
      <i class="fa-solid fa-book-open text-orange-500"></i> Courses
    </h1>
    <a href="{{ route('admin.courses.create') }}"
       class="px-4 py-2 rounded-lg bg-gradient-to-r from-emerald-500 to-emerald-600 text-white font-semibold shadow hover:shadow-lg hover:scale-105 transition">
      + New Course
    </a>
  </div>

  <!-- Success Message -->
  @if(session('success'))
    <div class="mb-3 px-4 py-2 bg-emerald-50 border border-emerald-200 text-emerald-700 rounded shadow-sm">
      {{ session('success') }}
    </div>
  @endif

  <div class="mb-4 rounded-xl border border-blue-200 bg-blue-50/40 p-4">
    <h2 class="text-sm font-bold text-slate-800 mb-2">
      <i class="fa-regular fa-image text-blue-600 mr-1"></i> Courses Top Banner (Single)
    </h2>
    <form method="POST" action="{{ route('admin.courses.banner.update') }}" enctype="multipart/form-data" class="grid grid-cols-1 md:grid-cols-4 gap-3 items-end">
      @csrf
      <div class="md:col-span-3">
        <label class="block text-xs font-semibold mb-1 text-slate-700">Banner Image</label>
        <input type="file" name="banner_image" accept=".jpg,.jpeg,.png,.webp" class="w-full border rounded px-3 py-2 bg-white" required>
        <p class="text-xs text-slate-500 mt-1">Only one banner will be used for all user Courses pages.</p>
      </div>
      <div class="text-right">
        <button class="px-3 py-2 rounded bg-blue-600 text-white text-sm font-semibold">
          Save Banner
        </button>
      </div>
    </form>
    @if(!empty($coursesBanner))
      <div class="mt-3 rounded-lg border bg-white overflow-hidden">
        <img src="{{ asset(ltrim($coursesBanner, '/')) }}" alt="Courses banner" class="w-full h-auto">
      </div>
    @endif
  </div>

  <!-- Course List -->
  <div class="grid gap-3">
    @forelse($courses as $c)
      <div class="bg-white rounded-lg border shadow-sm p-4 flex flex-col md:flex-row items-start md:items-center justify-between hover:shadow-md transition">
        <!-- Left -->
        <div class="mb-3 md:mb-0">
          <div class="font-semibold text-slate-800 text-base">{{ $c->title }}</div>
          <div class="text-sm text-slate-500 mt-1">{{ Str::limit($c->description, 100) }}</div>
        </div>

        <!-- Right (Actions) -->
        <div class="flex gap-2 flex-wrap">
          <a href="{{ route('admin.courses.edit',$c) }}"
             class="px-3 py-1.5 rounded-md bg-blue-600 text-white text-sm shadow hover:bg-blue-700 transition">
            <i class="fa-solid fa-pen"></i> Edit
          </a>

          <form method="POST" action="{{ route('admin.courses.destroy',$c) }}" 
                onsubmit="return confirm('Delete course?')">
            @csrf @method('DELETE')
            <button
              class="px-3 py-1.5 rounded-md bg-rose-600 text-white text-sm shadow hover:bg-rose-700 transition">
              <i class="fa-solid fa-trash"></i> Delete
            </button>
          </form>

          <!-- Manage Course -->
          <a href="{{ route('admin.sessions.index', $c->id) }}"
             class="px-3 py-1.5 rounded-md bg-orange-500 text-white text-sm shadow hover:bg-orange-600 transition">
            <i class="fa-solid fa-layer-group"></i> Manage Course
          </a>
        </div>
      </div>
    @empty
      <div class="p-6 text-center text-slate-500">🚫 No courses available</div>
    @endforelse
  </div>

  <!-- Pagination -->
  <div class="mt-4">
    {{ $courses->links() }}
  </div>
</div>
@endsection
