@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <div class="mb-4">
        <a href="{{ route('categories.index') }}" class="text-decoration-none text-muted small">
            <i class="bi bi-arrow-left me-2"></i>Volver a Categorías
        </a>
    </div>

    <div class="row g-4">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-warning bg-opacity-10 border-0 py-3 px-4">
                    <h5 class="mb-0 text-warning-emphasis fw-bold">
                        <i class="bi bi-pencil-square me-2"></i>Editar Categoría: {{ $category->name }}
                    </h5>
                </div>
                <div class="card-body p-4">
                    <form action="{{ route('categories.update', $category) }}" method="POST" enctype="multipart/form-data">
                        @csrf @method('PUT')

                        <div class="mb-4">
                            <label class="form-label fw-bold">Nombre de la Categoría <span class="text-danger">*</span></label>
                            <input type="text"
                                   name="name"
                                   class="form-control form-control-lg @error('name') is-invalid @enderror"
                                   value="{{ old('name', $category->name) }}"
                                   required
                                   maxlength="50">
                            @error('name')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold">Descripción</label>
                            <textarea name="description"
                                      class="form-control @error('description') is-invalid @enderror"
                                      rows="3"
                                      maxlength="255">{{ old('description', $category->description) }}</textarea>
                            @error('description')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold">Imagen Actual</label>
                            <div class="mb-2">
                                @if($category->image)
                                    <div class="d-inline-block position-relative">
                                        <img src="{{ asset('storage/' . $category->image) }}"
                                             class="rounded shadow-sm border"
                                             width="180" height="180"
                                             style="object-fit: cover;">
                                        <div class="form-check mt-2">
                                            <input class="form-check-input"
                                                   type="checkbox"
                                                   name="remove_image"
                                                   id="removeImage"
                                                   value="1">
                                            <label class="form-check-label text-danger small fw-bold" for="removeImage">
                                                <i class="bi bi-trash me-1"></i>Quitar imagen actual
                                            </label>
                                        </div>
                                    </div>
                                @else
                                    <div class="bg-light text-secondary d-inline-flex justify-content-center align-items-center rounded border"
                                         style="width: 180px; height: 180px;">
                                        <div class="text-center">
                                            <i class="bi bi-image fs-1 d-block opacity-25"></i>
                                            <span class="small">Sin imagen</span>
                                        </div>
                                    </div>
                                @endif
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold">Cambiar Imagen</label>
                            <input type="file"
                                   name="image"
                                   accept="image/*"
                                   class="form-control @error('image') is-invalid @enderror"
                                   onchange="previewImage(event)">
                            <div class="form-text small">Dejar vacío para mantener la imagen actual.</div>
                            @error('image')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <div id="imagePreviewWrapper" class="mt-3 d-none">
                                <p class="small text-muted mb-2">Nueva vista previa:</p>
                                <img id="imagePreview"
                                     class="rounded shadow-sm border"
                                     style="max-height: 180px; object-fit: cover;">
                            </div>
                        </div>

                        <div class="mb-4">
                            <div class="form-check form-switch">
                                <input class="form-check-input"
                                       type="checkbox"
                                       role="switch"
                                       id="isActiveSwitch"
                                       name="is_active"
                                       value="1"
                                       {{ old('is_active', $category->is_active) ? 'checked' : '' }}>
                                <label class="form-check-label fw-bold" for="isActiveSwitch">
                                    Categoría activa
                                </label>
                            </div>
                        </div>

                        <div class="d-flex justify-content-end gap-2 pt-3 border-top">
                            <a href="{{ route('categories.index') }}" class="btn btn-secondary fw-bold px-4">
                                Cancelar
                            </a>
                            <button type="submit" class="btn btn-warning fw-bold px-4 shadow-sm">
                                <i class="bi bi-save2-fill me-2"></i>Guardar Cambios
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <h6 class="fw-bold mb-3"><i class="bi bi-info-circle-fill me-2 text-primary"></i>Información</h6>
                    <div class="mb-3">
                        <div class="small text-muted">Fecha de creación</div>
                        <div class="fw-bold">{{ $category->created_at->format('d/m/Y H:i') }}</div>
                    </div>
                    <div class="mb-3">
                        <div class="small text-muted">Última actualización</div>
                        <div class="fw-bold">{{ $category->updated_at->format('d/m/Y H:i') }}</div>
                    </div>
                    <div class="mb-3">
                        <div class="small text-muted">Productos asociados</div>
                        <div class="fw-bold">
                            <span class="badge bg-light text-dark border px-3 py-2">
                                {{ $category->products->count() }} producto(s)
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function previewImage(event) {
        var input = event.target;
        var wrapper = document.getElementById('imagePreviewWrapper');
        var preview = document.getElementById('imagePreview');

        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function(e) {
                preview.src = e.target.result;
                wrapper.classList.remove('d-none');
            };
            reader.readAsDataURL(input.files[0]);
        } else {
            wrapper.classList.add('d-none');
        }
    }
</script>
@endsection
