@extends('layouts.app')

@section('title', 'Courses & Domains')
@section('page_header', 'Internship Courses & Domains')

@section('content')
<div class="space-y-6" x-data="{ addModalOpen: false, editModalOpen: false, editCourseData: {} }">

    <!-- Top Action Bar -->
    <div class="flex flex-col md:flex-row items-stretch md:items-center justify-between gap-4 bg-white p-6 rounded-3xl border border-slate-200/80 shadow-sm">
        <div>
            <h2 class="text-xl font-extrabold text-slate-900">Internship Domains & Courses</h2>
            <p class="text-xs text-slate-500">Manage internship topics and subjects available for students</p>
        </div>

        <button @click="addModalOpen = true" 
                class="inline-flex items-center gap-2 bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs px-5 py-3 rounded-xl shadow-lg shadow-emerald-600/20 transition-all">
            <i data-lucide="plus-circle" class="w-4 h-4"></i>
            <span>Add Internship Course</span>
        </button>
    </div>

    <!-- Courses Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        @forelse($courses as $crs)
        <div class="bg-white rounded-3xl p-6 border border-slate-200/80 shadow-sm hover:shadow-md transition-all flex flex-col justify-between space-y-4">
            
            <div class="flex items-start justify-between gap-4">
                <div class="w-12 h-12 rounded-2xl bg-amber-50 text-amber-600 flex items-center justify-center font-bold flex-shrink-0">
                    <i data-lucide="book-open" class="w-6 h-6"></i>
                </div>

                <div class="flex items-center gap-1">
                    <button @click="editCourseData = @js($crs); editModalOpen = true" 
                            class="p-2 text-slate-400 hover:text-emerald-600 hover:bg-slate-100 rounded-xl" title="Edit">
                        <i data-lucide="edit-3" class="w-4 h-4"></i>
                    </button>
                    
                    <form action="{{ route('admin.courses.destroy', $crs->id) }}" method="POST" onsubmit="return confirm('Delete {{ $crs->title }}?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="p-2 text-slate-400 hover:text-rose-600 hover:bg-rose-50 rounded-xl" title="Delete">
                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                        </button>
                    </form>
                </div>
            </div>

            <div>
                <div class="flex items-center justify-between gap-2">
                    <h3 class="font-bold text-slate-900 text-base leading-tight">{{ $crs->title }}</h3>
                    @if($crs->code)
                    <span class="font-mono text-[10px] font-bold text-amber-700 bg-amber-100 px-2 py-0.5 rounded border border-amber-200 flex-shrink-0">
                        {{ $crs->code }}
                    </span>
                    @endif
                </div>
                <p class="text-xs text-slate-500 mt-2 line-clamp-2">{{ $crs->description ?? 'No description provided.' }}</p>
            </div>

            <div class="pt-3 border-t border-slate-100 flex items-center justify-between text-xs">
                <span class="text-slate-400 font-medium">Certificates Issued</span>
                <span class="font-extrabold text-amber-700 bg-amber-50 px-3 py-1 rounded-full border border-amber-200">
                    {{ $crs->certificates_count }} Certificates
                </span>
            </div>
        </div>
        @empty
        <div class="col-span-full bg-white p-12 rounded-3xl text-center text-slate-400">
            No internship courses added yet. Click <strong>Add Internship Course</strong> to create your first domain.
        </div>
        @endforelse
    </div>

    @if($courses->hasPages())
    <div class="p-4 bg-white rounded-2xl">
        {{ $courses->links() }}
    </div>
    @endif

    <!-- ADD COURSE MODAL -->
    <div x-show="addModalOpen" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-900/60 backdrop-blur-sm">
        <div class="bg-white rounded-3xl p-8 max-w-md w-full space-y-6 shadow-2xl">
            <div class="flex items-center justify-between border-b border-slate-100 pb-4">
                <h3 class="text-lg font-bold text-slate-900">Add Internship Course</h3>
                <button @click="addModalOpen = false" class="text-slate-400 hover:text-slate-600">
                    <i data-lucide="x" class="w-5 h-5"></i>
                </button>
            </div>

            <form action="{{ route('admin.courses.store') }}" method="POST" class="space-y-4">
                @csrf
                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Course / Domain Title <span class="text-rose-500">*</span></label>
                    <input type="text" name="title" required placeholder="e.g. Web Development & UI/UX"
                           class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none">
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Course Code (Optional)</label>
                    <input type="text" name="code" placeholder="e.g. WEB-101"
                           class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none">
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Description</label>
                    <textarea name="description" rows="3" placeholder="Brief summary of internship course curriculum..."
                              class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none"></textarea>
                </div>

                <div class="flex items-center justify-end gap-3 pt-4 border-t border-slate-100">
                    <button type="button" @click="addModalOpen = false" class="px-4 py-2.5 rounded-xl border border-slate-200 text-xs font-bold text-slate-600 hover:bg-slate-50">
                        Cancel
                    </button>
                    <button type="submit" class="px-5 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold shadow-lg shadow-emerald-600/20">
                        Save Course
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- EDIT COURSE MODAL -->
    <div x-show="editModalOpen" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-900/60 backdrop-blur-sm">
        <div class="bg-white rounded-3xl p-8 max-w-md w-full space-y-6 shadow-2xl">
            <div class="flex items-center justify-between border-b border-slate-100 pb-4">
                <h3 class="text-lg font-bold text-slate-900">Edit Course</h3>
                <button @click="editModalOpen = false" class="text-slate-400 hover:text-slate-600">
                    <i data-lucide="x" class="w-5 h-5"></i>
                </button>
            </div>

            <form :action="'{{ url('admin/courses') }}/' + editCourseData.id" method="POST" class="space-y-4">
                @csrf
                @method('PUT')
                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Course Title</label>
                    <input type="text" name="title" x-model="editCourseData.title" required
                           class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none">
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Course Code</label>
                    <input type="text" name="code" x-model="editCourseData.code"
                           class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none">
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-700 uppercase tracking-wider mb-2">Description</label>
                    <textarea name="description" x-model="editCourseData.description" rows="3"
                              class="w-full px-4 py-3 rounded-xl border border-slate-200 bg-slate-50 text-sm font-semibold focus:bg-white focus:border-emerald-600 outline-none"></textarea>
                </div>

                <div class="flex items-center justify-end gap-3 pt-4 border-t border-slate-100">
                    <button type="button" @click="editModalOpen = false" class="px-4 py-2.5 rounded-xl border border-slate-200 text-xs font-bold text-slate-600 hover:bg-slate-50">
                        Cancel
                    </button>
                    <button type="submit" class="px-5 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold shadow-lg shadow-emerald-600/20">
                        Update Changes
                    </button>
                </div>
            </form>
        </div>
    </div>

</div>
@endsection
