@extends('layouts.yellow')

@section('title', 'Editar Categoría')

@section('content')
    <div class="container-fluid inventory-container">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1><i class="fas fa-edit"></i> Editar Categoría</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('inventory.index') }}">Inventario</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('inventory.categories.index') }}">Categorías</a></li>
                    <li class="breadcrumb-item active">Editar {{ $category->name }}</li>
                </ol>
            </div>
        </div>

        <div class="inventory-card">
            <div class="card-header">
                <h3 class="card-title">Información de la Categoría</h3>
            </div>
            <form action="{{ route('inventory.categories.update', $category) }}" method="POST" class="form-inventory">
                @csrf
                @method('PUT')
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="name">Nombre <span class="text-danger">*</span></label>
                                <input type="text" class="form-control @error('name') is-invalid @enderror" id="name" name="name" value="{{ old('name', $category->name) }}" required>
                                @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                            
                            <div class="form-group">
                                <label for="parent_id">Categoría Padre</label>
                                <select class="form-control select2 @error('parent_id') is-invalid @enderror" id="parent_id" name="parent_id">
                                    <option value="">Ninguna (Categoría Principal)</option>
                                    @foreach($parentCategories ?? [] as $parent)
                                        <option value="{{ $parent->id }}" {{ old('parent_id', $category->parent_id) == $parent->id ? 'selected' : '' }}>
                                            {{ $parent->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('parent_id')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                                <small class="form-text text-muted">No puedes seleccionar la misma categoría o una de sus subcategorías como padre.</small>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="description">Descripción</label>
                                <textarea class="form-control @error('description') is-invalid @enderror" id="description" name="description" rows="3">{{ old('description', $category->description) }}</textarea>
                                @error('description')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                            
                            <div class="form-group">
                                <label for="is_active">Estado</label>
                                <select class="form-control @error('is_active') is-invalid @enderror" id="is_active" name="is_active">
                                    <option value="1" {{ old('is_active', $category->is_active) == 1 ? 'selected' : '' }}>Activa</option>
                                    <option value="0" {{ old('is_active', $category->is_active) == 0 ? 'selected' : '' }}>Inactiva</option>
                                </select>
                                @error('is_active')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="card-footer">
                    <button type="submit" class="btn-inventory btn-inventory-primary">
                        <i class="fas fa-save"></i> Guardar Cambios
                    </button>
                    <a href="{{ route('inventory.categories.show', $category) }}" class="btn-inventory btn-inventory-secondary">
                        <i class="fas fa-times"></i> Cancelar
                    </a>
                </div>
            </form>
        </div>
        
        <!-- Subcategorías -->
        @if(count($category->children) > 0)
        <div class="inventory-card mt-4">
            <div class="card-header">
                <h3 class="card-title">Subcategorías</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="inventory-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Nombre</th>
                                <th>Estado</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($category->children as $child)
                                <tr>
                                    <td>{{ $child->id }}</td>
                                    <td>{{ $child->name }}</td>
                                    <td>
                                        @if($child->is_active)
                                            <span class="badge-inventory badge-inventory-success">Activa</span>
                                        @else
                                            <span class="badge-inventory badge-inventory-danger">Inactiva</span>
                                        @endif
                                    </td>
                                    <td>
                                        <a href="{{ route('inventory.categories.edit', $child) }}" class="btn-inventory btn-inventory-warning btn-sm">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        @endif
    </div>
@endsection

@section('javascript')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Obtener el ID de la categoría actual
        const categoryId = {{ $category->id }};
        
        // Deshabilitar selección de la misma categoría como padre
        const parentSelect = document.getElementById('parent_id');
        
        for (let i = 0; i < parentSelect.options.length; i++) {
            if (parentSelect.options[i].value == categoryId) {
                parentSelect.options[i].disabled = true;
            }
        }
    });
</script>
@endsection 