@extends('layouts.app')

@section('title', 'Proveedores | Veltrix v2')

@section('content')
    <div class="row g-3">
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <h2 class="h6 mb-3">Nuevo proveedor</h2>

                    @if ($errors->any())
                        <div class="alert alert-danger py-2">{{ $errors->first() }}</div>
                    @endif

                    @if (has_permission('proveedores.crear'))
                        <form action="{{ route('proveedores.store') }}" method="post">
                            @csrf
                            <div class="mb-2">
                                <label class="form-label">Nombre</label>
                                <input class="form-control" name="nombre" value="{{ old('nombre') }}" required>
                            </div>
                            <div class="mb-2">
                                <label class="form-label">Documento</label>
                                <input class="form-control" name="documento" value="{{ old('documento') }}">
                            </div>
                            <div class="mb-2">
                                <label class="form-label">Correo</label>
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                            <div class="mb-2">
                                <label class="form-label">Teléfono</label>
                                <input class="form-control" name="telefono" value="{{ old('telefono') }}">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Dirección</label>
                                <textarea class="form-control" name="direccion" rows="2">{{ old('direccion') }}</textarea>
                            </div>
                            <button class="btn btn-primary w-100" type="submit">Guardar proveedor</button>
                        </form>
                    @else
                        <div class="alert alert-warning mb-0">No tienes permiso para crear proveedores.</div>
                    @endif
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3 gap-2 flex-wrap">
                        <h1 class="h5 mb-0">Proveedores</h1>
                        <a href="{{ route('dashboard') }}" class="btn btn-sm btn-outline-secondary">Volver al dashboard</a>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-striped table-hover align-middle mb-0">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Nombre</th>
                                    <th>Documento</th>
                                    <th>Contacto</th>
                                    <th>Estado</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($proveedores as $proveedor)
                                    <tr>
                                        <td>{{ $proveedor->id }}</td>
                                        <td>{{ $proveedor->nombre }}</td>
                                        <td>{{ $proveedor->documento ?: 'N/A' }}</td>
                                        <td>
                                            <div>{{ $proveedor->email ?: 'Sin correo' }}</div>
                                            <small class="text-muted">{{ $proveedor->telefono ?: 'Sin teléfono' }}</small>
                                        </td>
                                        <td>
                                            <span class="badge {{ $proveedor->activo ? 'text-bg-success' : 'text-bg-secondary' }}">
                                                {{ $proveedor->activo ? 'Activo' : 'Inactivo' }}
                                            </span>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="text-center text-muted py-4">No hay proveedores registrados.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="mt-3">
                {{ $proveedores->links() }}
            </div>
        </div>
    </div>
@endsection
