@extends('layouts.yellow')

@section('title', 'Inventario - Stock Bajo')

@section('content_header')
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1><i class="fas fa-exclamation-triangle text-warning"></i> Productos con Stock Bajo</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 active">Stock Bajo</li>
                </ol>
            </div>
        </div>
    </div>
@endsection

@section('content')
    <div class="container-fluid inventory-container">
        <!-- Información -->
        <div class="low-stock-alert">
            <h5><i class="icon fas fa-exclamation-triangle"></i> ¡Atención!</h5>
            <p>Los siguientes productos están por debajo del nivel mínimo de stock establecido.</p>
        </div>
        
        <!-- Acciones rápidas -->
        <div class="row mb-3">
            <div class="col-md-12">
                <a href="{{ route('inventory.index') }}" class="btn-inventory btn-inventory-primary">
                    <i class="fas fa-arrow-left"></i> Volver al Inventario
                </a>
                <a href="{{ route('inventory.categories.index') }}" class="btn-inventory btn-inventory-info">
                    <i class="fas fa-tags"></i> Categorías
                </a>
                <a href="{{ route('inventory.suppliers.index') }}" class="btn-inventory btn-inventory-secondary">
                    <i class="fas fa-truck"></i> Proveedores
                </a>
            </div>
        </div>

        <!-- Listado de items con stock bajo -->
        <div class="inventory-card">
            <div class="card-header">
                <h3 class="card-title">Productos con Stock Bajo</h3>
            </div>
            <div class="card-body table-responsive p-0">
                @if(count($lowStockItems) > 0)
                    <table class="inventory-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Nombre</th>
                                <th>Categoría</th>
                                <th>Stock Actual</th>
                                <th>Stock Mínimo</th>
                                <th>Estado</th>
                                <th>Proveedor</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($lowStockItems as $item)
                                <tr>
                                    <td>{{ $item->id }}</td>
                                    <td>
                                        <strong>{{ $item->name }}</strong>
                                        @if ($item->brand)
                                            <br><small>{{ $item->brand }} {{ $item->model }}</small>
                                        @endif
                                    </td>
                                    <td>{{ $item->category->name ?? 'N/A' }}</td>
                                    <td>
                                        <span class="badge-inventory badge-inventory-danger">{{ $item->quantity }}</span>
                                    </td>
                                    <td>{{ $item->min_stock }}</td>
                                    <td>
                                        @if ($item->status == 'active')
                                            <span class="badge-inventory badge-inventory-success">Activo</span>
                                        @elseif ($item->status == 'inactive')
                                            <span class="badge-inventory badge-inventory-secondary">Inactivo</span>
                                        @elseif ($item->status == 'maintenance')
                                            <span class="badge-inventory badge-inventory-warning">Mantenimiento</span>
                                        @endif
                                    </td>
                                    <td>{{ $item->supplier->name ?? 'N/A' }}</td>
                                    <td>
                                        <div class="btn-group">
                                            <a href="{{ route('inventory.show', $item) }}" class="btn-inventory btn-inventory-info btn-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <button type="button" class="btn-inventory btn-inventory-primary btn-sm" data-toggle="modal" data-target="#adjustModal{{ $item->id }}">
                                                <i class="fas fa-plus"></i> Reponer
                                            </button>
                                        </div>

                                        <!-- Modal de ajuste de stock -->
                                        <div class="modal fade inventory-modal" id="adjustModal{{ $item->id }}" tabindex="-1" role="dialog" aria-labelledby="adjustModalLabel{{ $item->id }}" aria-hidden="true" data-backdrop="static" data-keyboard="false">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="adjustModalLabel{{ $item->id }}">Reponer stock: {{ $item->name }}</h5>
                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                            <span aria-hidden="true">&times;</span>
                                                        </button>
                                                    </div>
                                                    <form action="{{ route('inventory.adjust', $item) }}" method="POST" class="form-inventory">
                                                        @csrf
                                                        <div class="modal-body">
                                                            <div class="form-group">
                                                                <label for="transaction_type">Tipo de Transacción</label>
                                                                <select class="form-control" id="transaction_type" name="transaction_type" required>
                                                                    <option value="entry" selected>Entrada</option>
                                                                </select>
                                                            </div>
                                                            <div class="form-group">
                                                                <label for="quantity">Cantidad a reponer</label>
                                                                <input type="number" class="form-control" id="quantity" name="quantity" min="1" step="0.01" value="1" required>
                                                                <small class="form-text text-muted">
                                                                    Stock actual: {{ $item->quantity }} | Stock mínimo: {{ $item->min_stock }}
                                                                </small>
                                                            </div>
                                                            <div class="form-group">
                                                                <label for="notes">Notas</label>
                                                                <textarea class="form-control" id="notes" name="notes" rows="3" placeholder="Ej. Reposición de stock por nivel bajo">Reposición de stock por nivel bajo</textarea>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn-inventory btn-inventory-secondary" data-dismiss="modal">Cancelar</button>
                                                            <button type="submit" class="btn-inventory btn-inventory-primary">Reponer Stock</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                @else
                    <div class="alert-inventory alert-inventory-success m-3">
                        <h5><i class="icon fas fa-check"></i> ¡Todo en orden!</h5>
                        <p>No hay productos con stock bajo en este momento.</p>
                    </div>
                @endif
            </div>
            <div class="card-footer">
                <div class="d-flex justify-content-center">
                    {{ $lowStockItems->links() }}
                </div>
            </div>
        </div>
    </div>
@endsection 