@extends('layouts.landing')

@section('title', 'Nueva Promoción')

@section('content')
<div class="container-fluid">
    <div class="card shadow mb-4" style="max-width:760px; margin:auto">
        <div class="card-header py-3 d-flex align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">
                <i class="fas fa-plus-circle mr-1"></i> Nueva Promoción
            </h6>
            <a href="{{ route('promocion.index') }}" class="btn btn-sm btn-secondary">
                <i class="fas fa-arrow-left mr-1"></i> Volver
            </a>
        </div>
        <div class="card-body">

            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $e)
                            <li>{{ $e }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form action="{{ route('promocion.store') }}" method="POST" enctype="multipart/form-data">
                @csrf

                <div class="form-group">
                    <label class="font-weight-bold">Título <span class="text-danger">*</span></label>
                    <input type="text" name="titulo" class="form-control @error('titulo') is-invalid @enderror"
                           value="{{ old('titulo') }}" maxlength="200" required>
                    @error('titulo')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>

                <div class="form-group">
                    <label class="font-weight-bold">Descripción</label>
                    <textarea name="descripcion" class="form-control @error('descripcion') is-invalid @enderror"
                              rows="4" maxlength="3000">{{ old('descripcion') }}</textarea>
                    @error('descripcion')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>

                <div class="form-group">
                    <label class="font-weight-bold">Planteles <span class="text-danger">*</span></label>
                    <div class="border rounded p-3 bg-light @error('plantels') border-danger @enderror">
                        @foreach($planteles as $plantel)
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox"
                                       id="p-{{ $plantel->id }}"
                                       name="plantels[]" value="{{ $plantel->id }}"
                                       {{ in_array($plantel->id, old('plantels', [])) ? 'checked' : '' }}>
                                <label class="form-check-label" for="p-{{ $plantel->id }}">
                                    {{ $plantel->name }}
                                </label>
                            </div>
                        @endforeach
                    </div>
                    @error('plantels')<div class="text-danger small mt-1">{{ $message }}</div>@enderror
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="font-weight-bold">Fecha inicio <span class="text-danger">*</span></label>
                            <input type="date" name="fecha_inicio"
                                   class="form-control @error('fecha_inicio') is-invalid @enderror"
                                   value="{{ old('fecha_inicio') }}" required>
                            @error('fecha_inicio')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="font-weight-bold">Fecha fin <span class="text-danger">*</span></label>
                            <input type="date" name="fecha_fin"
                                   class="form-control @error('fecha_fin') is-invalid @enderror"
                                   value="{{ old('fecha_fin') }}" required>
                            @error('fecha_fin')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="font-weight-bold">Imagen</label>
                    <div class="custom-file">
                        <input type="file" class="custom-file-input @error('imagen') is-invalid @enderror"
                               id="imagenInput" name="imagen" accept="image/*">
                        <label class="custom-file-label" for="imagenInput">Seleccionar imagen (máx. 10 MB)</label>
                    </div>
                    @error('imagen')<div class="text-danger small mt-1">{{ $message }}</div>@enderror
                    <div id="imagenPreview" class="mt-2 d-none">
                        <img id="previewImg" src="" alt="Vista previa"
                             class="img-fluid rounded" style="max-height:200px">
                    </div>
                </div>

                <div class="form-group">
                    <label class="font-weight-bold">Status</label>
                    <div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="status" id="statusActivo"
                                   value="activo" {{ old('status', 'activo') === 'activo' ? 'checked' : '' }}>
                            <label class="form-check-label" for="statusActivo">
                                <span class="badge badge-success">Activo</span>
                            </label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="status" id="statusInactivo"
                                   value="inactivo" {{ old('status') === 'inactivo' ? 'checked' : '' }}>
                            <label class="form-check-label" for="statusInactivo">
                                <span class="badge badge-secondary">Inactivo</span>
                            </label>
                        </div>
                    </div>
                </div>

                <hr>
                <div class="d-flex justify-content-end">
                    <a href="{{ route('promocion.index') }}" class="btn btn-secondary mr-2">Cancelar</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save mr-1"></i> Guardar
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    document.getElementById('imagenInput').addEventListener('change', function () {
        const file = this.files[0];
        this.nextElementSibling.textContent = file ? file.name : 'Seleccionar imagen (máx. 10 MB)';
        if (file) {
            const reader = new FileReader();
            reader.onload = e => {
                document.getElementById('previewImg').src = e.target.result;
                document.getElementById('imagenPreview').classList.remove('d-none');
            };
            reader.readAsDataURL(file);
        } else {
            document.getElementById('imagenPreview').classList.add('d-none');
        }
    });
</script>
@endsection
