@extends('layouts.admin')

@section('content')
<div class="pt-3 pb-2 mb-3 border-bottom d-flex justify-content-between align-items-center">
    <div>
        <h1 class="h3 fw-bold text-dark">Editar Contenido</h1>
        <p class="text-muted mb-0">Curso: <span class="text-primary fw-semibold">{{ $course->title }}</span></p>
    </div>
    <a href="{{ route('admin.courses.contents.index', $course->id) }}" class="btn btn-outline-secondary btn-sm rounded-pill">
        <i class="bi bi-arrow-left me-1"></i> Volver al Temario
    </a>
</div>

<div class="card border-0 shadow-sm p-4 max-w-2xl bg-white rounded-3">
    <form action="{{ route('admin.courses.contents.update', $content->id) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <div class="mb-3">
            <label class="form-label fw-semibold">Título de la Lección o Material</label>
            <input type="text" name="title" class="form-control" value="{{ old('title', $content->title) }}" required>
        </div>

        <div class="mb-3">
            <label class="form-label fw-semibold">Tipo de Formato</label>
            <select name="type" id="contentTypeSelect" class="form-select" required onchange="toggleFileInputs()">
                <option value="document" {{ $content->type == 'document' ? 'selected' : '' }}>📄 Documento Privado (PDF)</option>
                <option value="audio" {{ $content->type == 'audio' ? 'selected' : '' }}>🎵 Podcast / Audio Privado</option>
                <option value="video" {{ $content->type == 'video' ? 'selected' : '' }}>🎥 Videoclase (.mp4 / Streaming)</option>
            </select>
        </div>

        <div id="localFileGroup" class="mb-3 {{ $content->type == 'video' && !str_contains($content->file_path, '/') ? 'd-none' : '' }}">
            <label class="form-label fw-semibold">Reemplazar Archivo (Opcional)</label>
            <input type="file" name="file" class="form-control">
            <div class="form-text text-muted" style="font-size: 0.75rem;">Sube un archivo nuevo solo si deseas reemplazar el actual.</div>
        </div>

        <div id="videoUrlGroup" class="mb-3 {{ $content->type == 'video' && !str_contains($content->file_path, '/') ? '' : 'd-none' }}">
            <label class="form-label fw-semibold">ID o Ruta del Video</label>
            <input type="text" name="video_id" class="form-control" value="{{ $content->type == 'video' ? $content->file_path : '' }}" placeholder="Ej: ID de YouTube o ruta .mp4">
        </div>

        <div class="mb-4">
            <label class="form-label fw-semibold">Instrucciones o Descripción para el Alumno</label>
            <textarea name="description" rows="4" class="form-control" placeholder="Escribe detalles complementarios o indicaciones sobre este material...">{{ old('description', $content->description) }}</textarea>
        </div>

        <button type="submit" class="btn btn-warning fw-bold px-4 rounded-pill shadow-sm">
            <i class="bi bi-journal-check me-1"></i> Actualizar Elemento
        </form>
</div>

<script>
function toggleFileInputs() {
    const type = document.getElementById('contentTypeSelect').value;
    const localFileGroup = document.getElementById('localFileGroup');
    const videoUrlGroup = document.getElementById('videoUrlGroup');

    if (type === 'video') {
        localFileGroup.classList.add('d-none');
        videoUrlGroup.classList.remove('d-none');
    } else {
        localFileGroup.classList.remove('d-none');
        videoUrlGroup.classList.add('d-none');
    }
}
</script>
@endsection