@extends('layouts.app')

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

@section('content')
    <div class="container py-4">
        <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
            <h1 class="h4 mb-0">Empresas</h1>
            <a href="{{ route('dashboard') }}" class="btn btn-outline-secondary">Volver al dashboard</a>
        </div>

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

        @if (session('status'))
            <div class="alert alert-success">
                {{ session('status') }}
            </div>
        @endif

        @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif

        @if (session('error'))
            <div class="alert alert-danger">
                {{ session('error') }}
            </div>
        @endif

        @if (has_permission('empresas.crear'))
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <h2 class="h6 mb-3">Crear empresa</h2>
                    <form action="{{ route('empresas.store') }}" method="POST" class="row g-3">
                        @csrf
                        <div class="col-12 col-md-6">
                            <label class="form-label" for="nombre">Nombre</label>
                            <input class="form-control" id="nombre" name="nombre" required>
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label" for="slug">Slug (opcional)</label>
                            <input class="form-control" id="slug" name="slug">
                        </div>
                        <div class="col-12 col-md-4">
                            <label class="form-label" for="nit">NIT</label>
                            <input class="form-control" id="nit" name="nit">
                        </div>
                        <div class="col-12 col-md-4">
                            <label class="form-label" for="email">Email</label>
                            <input class="form-control" id="email" name="email" type="email">
                        </div>
                        <div class="col-12 col-md-4">
                            <label class="form-label" for="telefono">Telefono</label>
                            <input class="form-control" id="telefono" name="telefono">
                        </div>
                        <div class="col-12">
                            <label class="form-label" for="direccion">Direccion</label>
                            <input class="form-control" id="direccion" name="direccion">
                        </div>
                        <div class="col-12">
                            <button class="btn btn-primary">Guardar empresa</button>
                        </div>
                    </form>
                </div>
            </div>
        @endif

        <div class="card border-0 shadow-sm">
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Nombre</th>
                            <th>Slug</th>
                            <th>Estado</th>
                            <th>Email</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($empresas as $empresa)
                            <tr>
                                <td>{{ $empresa->id }}</td>
                                <td>{{ $empresa->nombre }}</td>
                                <td>{{ $empresa->slug }}</td>
                                <td>{{ $empresa->estado }}</td>
                                <td>{{ $empresa->email ?? 'N/A' }}</td>
                                <td class="text-end">
                                    <div class="d-inline-flex gap-1">
                                        <a href="{{ route('empresas.edit', $empresa) }}" class="btn btn-sm btn-outline-primary">Editar</a>

                                        @if (has_permission('empresas.desactivar') && $empresa->estado === 'activa')
                                            <form action="{{ route('empresas.deactivate', $empresa) }}" method="POST">
                                                @csrf
                                                @method('PATCH')
                                                <button class="btn btn-sm btn-outline-warning" onclick="return confirm('Confirmar desactivacion de empresa?')">Desactivar</button>
                                            </form>
                                        @endif

                                        @if (has_permission('empresas.reactivar') && $empresa->estado !== 'activa')
                                            <form action="{{ route('empresas.reactivate', $empresa) }}" method="POST">
                                                @csrf
                                                @method('PATCH')
                                                <button class="btn btn-sm btn-outline-success" onclick="return confirm('Confirmar reactivacion de empresa?')">Reactivar</button>
                                            </form>
                                        @endif

                                        @if (has_permission('empresas.eliminar'))
                                            <form action="{{ route('empresas.destroy', $empresa) }}" method="POST">
                                                @csrf
                                                @method('DELETE')
                                                <button class="btn btn-sm btn-outline-danger" onclick="return confirm('Confirmar eliminacion de empresa?')">Eliminar</button>
                                            </form>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-4">No hay empresas disponibles.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

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