@extends('layouts.admin')

@section('page-title')
    {{ __('Learning Management') }}
@endsection

@section('title')
    <div class="d-inline-block">
        <h5 class="h4 d-inline-block font-weight-400 mb-0 ">{{ __('Learning Management') }}</h5>
    </div>
@endsection

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('Dashboard') }}</a></li>
    <li class="breadcrumb-item">{{ __('Learning Management') }}</li>
@endsection

@section('content')
    <div class="row mb-3">
        <div class="col-md-3">
            <div class="card">
                <div class="card-body">
                    <p class="text-muted mb-1">{{ __('Courses') }}</p>
                    <h4 class="mb-0">{{ $stats['courses'] }}</h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card">
                <div class="card-body">
                    <p class="text-muted mb-1">{{ __('Lessons') }}</p>
                    <h4 class="mb-0">{{ $stats['lessons'] }}</h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card">
                <div class="card-body">
                    <p class="text-muted mb-1">{{ __('Enrollments') }}</p>
                    <h4 class="mb-0">{{ $stats['enrollments'] }}</h4>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card">
                <div class="card-body">
                    <p class="text-muted mb-1">{{ __('Completed') }}</p>
                    <h4 class="mb-0">{{ $stats['completed'] }}</h4>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-6">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">{{ __('Add Course') }}</h5></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('learning-management.courses.store') }}">
                        @csrf
                        <div class="mb-2">
                            <label class="form-label">{{ __('Title') }}</label>
                            <input type="text" name="title" class="form-control" required>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Code') }}</label>
                            <input type="text" class="form-control" value="{{ $courseCodePreview }}" readonly disabled>
                            <small class="text-muted">{{ __('Auto-generated with COURS prefix when you save.') }}</small>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Description') }}</label>
                            <textarea name="description" rows="3" class="form-control"></textarea>
                        </div>
                        <button class="btn btn-primary">{{ __('Create Course') }}</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-6">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">{{ __('Add Lesson') }}</h5></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('learning-management.lessons.store') }}">
                        @csrf
                        <div class="mb-2">
                            <label class="form-label">{{ __('Course') }}</label>
                            <select name="course_id" class="form-control" required>
                                <option value="">{{ __('Select course') }}</option>
                                @foreach($courses as $course)
                                    <option value="{{ $course->id }}">{{ $course->title }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Lesson Title') }}</label>
                            <input type="text" name="title" class="form-control" required>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Duration (minutes)') }}</label>
                            <input type="number" name="duration_minutes" class="form-control" min="0">
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Order') }}</label>
                            <input type="number" name="position" class="form-control" min="0" value="0">
                        </div>
                        <button class="btn btn-primary">{{ __('Create Lesson') }}</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-6">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">{{ __('Add Enrollment') }}</h5></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('learning-management.enrollments.store') }}">
                        @csrf
                        <div class="mb-2">
                            <label class="form-label">{{ __('Course') }}</label>
                            <select name="course_id" class="form-control" required>
                                <option value="">{{ __('Select course') }}</option>
                                @foreach($courses as $course)
                                    <option value="{{ $course->id }}">{{ $course->title }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('User') }}</label>
                            <select name="user_id" class="form-control" required>
                                <option value="">{{ __('Select user') }}</option>
                                @foreach($users as $user)
                                    <option value="{{ $user->id }}">{{ $user->name }} ({{ $user->email }})</option>
                                @endforeach
                            </select>
                        </div>
                        <button class="btn btn-primary">{{ __('Enroll User') }}</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-6">
            <div class="card">
                <div class="card-header"><h5 class="mb-0">{{ __('Track Progress') }}</h5></div>
                <div class="card-body">
                    <form method="POST" action="{{ route('learning-management.progress.store') }}">
                        @csrf
                        <div class="mb-2">
                            <label class="form-label">{{ __('Enrollment') }}</label>
                            <select name="enrollment_id" class="form-control" required>
                                <option value="">{{ __('Select enrollment') }}</option>
                                @foreach($enrollments as $enrollment)
                                    <option value="{{ $enrollment->id }}">{{ $enrollment->course->title }} - {{ $enrollment->user->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Lesson (optional)') }}</label>
                            <select name="lesson_id" class="form-control">
                                <option value="">{{ __('No specific lesson') }}</option>
                                @foreach($lessons as $lesson)
                                    <option value="{{ $lesson->id }}">{{ $lesson->course->title }} / {{ $lesson->title }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Progress %') }}</label>
                            <input type="number" name="progress_percent" class="form-control" min="0" max="100" step="0.01" required>
                        </div>
                        <div class="mb-2">
                            <label class="form-label">{{ __('Spent Minutes') }}</label>
                            <input type="number" name="spent_minutes" class="form-control" min="0" value="0">
                        </div>
                        <button class="btn btn-primary">{{ __('Save Progress') }}</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="card mt-3">
        <div class="card-header"><h5 class="mb-0">{{ __('Courses & Lessons') }}</h5></div>
        <div class="card-body table-border-style">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>{{ __('Course') }}</th>
                        <th>{{ __('Code') }}</th>
                        <th>{{ __('Lessons') }}</th>
                        <th>{{ __('Enrollments') }}</th>
                        <th>{{ __('Action') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($courses as $course)
                        <tr>
                            <td>{{ $course->title }}</td>
                            <td>{{ $course->code ?: '-' }}</td>
                            <td>{{ $course->lessons_count }}</td>
                            <td>{{ $course->enrollments_count }}</td>
                            <td>
                                <form method="POST" action="{{ route('learning-management.courses.destroy', $course->id) }}" onsubmit="return confirm('{{ __('Delete this course?') }}')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger">{{ __('Delete') }}</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center">{{ __('No courses found.') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="card mt-3">
        <div class="card-header"><h5 class="mb-0">{{ __('Enrollments & Progress') }}</h5></div>
        <div class="card-body table-border-style">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>{{ __('Course') }}</th>
                        <th>{{ __('User') }}</th>
                        <th>{{ __('Status') }}</th>
                        <th>{{ __('Completion %') }}</th>
                        <th>{{ __('Action') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($enrollments as $enrollment)
                        <tr>
                            <td>{{ $enrollment->course->title }}</td>
                            <td>{{ $enrollment->user->name }}</td>
                            <td>{{ ucfirst($enrollment->status) }}</td>
                            <td>{{ number_format((float)$enrollment->completion_percent, 2) }}</td>
                            <td>
                                <form method="POST" action="{{ route('learning-management.enrollments.destroy', $enrollment->id) }}" onsubmit="return confirm('{{ __('Delete this enrollment?') }}')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger">{{ __('Delete') }}</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center">{{ __('No enrollments found.') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="card mt-3">
        <div class="card-header"><h5 class="mb-0">{{ __('Recent Progress Logs') }}</h5></div>
        <div class="card-body table-border-style">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>{{ __('Course') }}</th>
                        <th>{{ __('User') }}</th>
                        <th>{{ __('Lesson') }}</th>
                        <th>{{ __('Progress %') }}</th>
                        <th>{{ __('Spent Minutes') }}</th>
                        <th>{{ __('Updated At') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($progresses as $progress)
                        <tr>
                            <td>{{ optional(optional($progress->enrollment)->course)->title ?: '-' }}</td>
                            <td>{{ optional(optional($progress->enrollment)->user)->name ?: '-' }}</td>
                            <td>{{ optional($progress->lesson)->title ?: '-' }}</td>
                            <td>{{ number_format((float)$progress->progress_percent, 2) }}</td>
                            <td>{{ $progress->spent_minutes }}</td>
                            <td>{{ $progress->created_at }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center">{{ __('No progress logs found.') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection


