@extends('layouts.yellow')

@section('title', 'Proveedores de Inventario')

@section('content')
    <div class="container-fluid inventory-container">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1><i class="fas fa-truck"></i> Proveedores de Inventario</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">Proveedores</li>
                </ol>
            </div>
        </div>

        <!-- Acciones rápidas -->
        <div class="row mb-3">
            <div class="col-md-12">
                <a href="{{ route('inventory.suppliers.create') }}" class="btn-inventory btn-inventory-success">
                    <i class="fas fa-plus"></i> Nuevo Proveedor
                </a>
                <a href="{{ route('inventory.index') }}" class="btn-inventory btn-inventory-primary">
                    <i class="fas fa-boxes"></i> Ver Inventario
                </a>
            </div>
        </div>

        <!-- Filtros -->
        <div class="filter-card mb-3">
            <div class="card-header">
                <h3 class="card-title">Filtros</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                        <i class="fas fa-minus"></i>
                    </button>
                </div>
            </div>
            <div class="card-body">
                <form action="{{ route('inventory.suppliers.index') }}" method="GET" class="form-inventory">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="search">Buscar</label>
                                <input type="text" class="form-control" id="search" name="search" placeholder="Nombre, empresa, contacto..." value="{{ request('search') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="status">Estado</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="">Todos</option>
                                    <option value="1" {{ request('status') == '1' ? 'selected' : '' }}>Activo</option>
                                    <option value="0" {{ request('status') == '0' ? 'selected' : '' }}>Inactivo</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3 d-flex align-items-end">
                            <button type="submit" class="btn-inventory btn-inventory-primary mr-2">
                                <i class="fas fa-search"></i> Filtrar
                            </button>
                            <a href="{{ route('inventory.suppliers.index') }}" class="btn-inventory btn-inventory-secondary">
                                <i class="fas fa-sync"></i> Limpiar
                            </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- Listado de proveedores -->
        <div class="inventory-card">
            <div class="card-header">
                <h3 class="card-title">Listado de Proveedores</h3>
            </div>
            <div class="card-body">
                @if(count($suppliers) > 0)
                    <div class="table-responsive">
                        <table class="inventory-table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Nombre</th>
                                    <th>Empresa</th>
                                    <th>Contacto</th>
                                    <th>Teléfono</th>
                                    <th>Email</th>
                                    <th>Productos</th>
                                    <th>Estado</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($suppliers as $supplier)
                                    <tr>
                                        <td>{{ $supplier->id }}</td>
                                        <td>{{ $supplier->name }}</td>
                                        <td>{{ $supplier->company_name ?? 'N/A' }}</td>
                                        <td>{{ $supplier->contact_person ?? 'N/A' }}</td>
                                        <td>{{ $supplier->phone ?? 'N/A' }}</td>
                                        <td>{{ $supplier->email ?? 'N/A' }}</td>
                                        <td>
                                            <a href="{{ route('inventory.suppliers.items', $supplier) }}" class="badge-inventory badge-inventory-info">
                                                {{ $supplier->inventory_count ?? 0 }} productos
                                            </a>
                                        </td>
                                        <td>
                                            @if($supplier->is_active)
                                                <span class="badge-inventory badge-inventory-success">Activo</span>
                                            @else
                                                <span class="badge-inventory badge-inventory-danger">Inactivo</span>
                                            @endif
                                        </td>
                                        <td>
                                            <div class="btn-group">
                                                <a href="{{ route('inventory.suppliers.show', $supplier) }}" class="btn-inventory btn-inventory-info btn-sm">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('inventory.suppliers.edit', $supplier) }}" class="btn-inventory btn-inventory-warning btn-sm">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn-inventory btn-inventory-danger btn-sm" data-toggle="modal" data-target="#deleteModal{{ $supplier->id }}">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>

                                            <!-- Modal de eliminación -->
                                            <div class="modal fade inventory-modal" id="deleteModal{{ $supplier->id }}" tabindex="-1" role="dialog" 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">Confirmar eliminación</h5>
                                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                <span aria-hidden="true">&times;</span>
                                                            </button>
                                                        </div>
                                                        <div class="modal-body">
                                                            ¿Estás seguro de que deseas eliminar al proveedor <strong>{{ $supplier->name }}</strong>?
                                                            @if($supplier->inventory_count > 0)
                                                                <div class="low-stock-alert mt-3">
                                                                    <i class="fas fa-exclamation-triangle"></i>
                                                                    Este proveedor tiene {{ $supplier->inventory_count }} productos asociados. 
                                                                    Eliminarlo podría afectar estos productos.
                                                                </div>
                                                            @endif
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn-inventory btn-inventory-secondary" data-dismiss="modal">Cancelar</button>
                                                            <form action="{{ route('inventory.suppliers.destroy', $supplier) }}" method="POST">
                                                                @csrf
                                                                @method('DELETE')
                                                                <button type="submit" class="btn-inventory btn-inventory-danger">Eliminar</button>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex justify-content-center mt-3">
                        {{ $suppliers->links() }}
                    </div>
                @else
                    <div class="low-stock-alert">
                        <h5><i class="icon fas fa-info"></i> ¡No hay proveedores!</h5>
                        <p>No se encontraron proveedores en el sistema. Puedes crear uno nuevo haciendo clic en el botón "Nuevo Proveedor".</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
@endsection 