@extends('layouts.yellow')
@section('content')
<div class="container-fluid">
    <!-- Cabecera compacta -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card shadow-sm border-0 rounded-lg overflow-hidden">
                <div class="card-header bg-gradient-primary text-white py-2">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-hospital-alt fa-fw mr-2"></i>
                            <div>
                                <h4 class="mb-0 font-weight-bold text-white">Identidad del Consultorio</h4>
                                <small class="text-white">Gestiona la información que aparecerá en documentos y reportes médicos</small>
                            </div>
                        </div>
                        <a href="{{ route('clinic-identity.create') }}" class="btn btn-light btn-sm shadow-sm">
                            <i class="fas fa-plus-circle mr-1"></i> Nueva Identidad
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show shadow-sm" role="alert">
            <div class="d-flex align-items-center">
                <i class="fas fa-check-circle fa-fw fa-lg mr-3"></i>
                <span>{{ session('success') }}</span>
            </div>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show shadow-sm" role="alert">
            <div class="d-flex align-items-center">
                <i class="fas fa-exclamation-circle fa-fw fa-lg mr-3"></i>
                <span>{{ session('error') }}</span>
            </div>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0 rounded-lg">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover" id="identityTable">
                            <thead class="bg-light">
                                <tr>
                                    <th class="text-center border-top-0" width="10%">Logo</th>
                                    <th class="border-top-0" width="20%">Nombre</th>
                                    <th class="border-top-0" width="30%">Dirección</th>
                                    <th class="border-top-0" width="15%">Contacto</th>
                                    <th class="text-center border-top-0" width="10%">Estado</th>
                                    <th class="text-center border-top-0" width="15%">Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($identities as $identity)
                                    <tr class="{{ $identity->is_active ? 'table-active' : '' }}">
                                        <td class="text-center align-middle">
                                            @if($identity->logo_path)
                                                <img src="{{ asset('storage/' . $identity->logo_path) }}" alt="Logo" 
                                                    class="img-thumbnail shadow-sm" style="max-height: 60px">
                                            @else
                                                <div class="rounded-circle bg-light d-flex align-items-center justify-content-center mx-auto" style="width: 60px; height: 60px;">
                                                    <i class="fas fa-hospital fa-2x text-primary"></i>
                                                </div>
                                            @endif
                                        </td>
                                        <td class="align-middle">
                                            <span class="font-weight-bold d-block">{{ $identity->name }}</span>
                                            @if($identity->specialty)
                                                <small class="text-muted">{{ $identity->specialty }}</small>
                                            @endif
                                        </td>
                                        <td class="align-middle">
                                            <div class="d-flex align-items-start">
                                                <i class="fas fa-map-marker-alt fa-fw mr-2 text-secondary mt-1"></i>
                                                <span>{{ Str::limit($identity->address, 70) }}</span>
                                            </div>
                                        </td>
                                        <td class="align-middle">
                                            @if($identity->phone)
                                                <div class="d-flex align-items-center mb-1">
                                                    <i class="fas fa-phone fa-fw mr-2 text-secondary"></i>
                                                    <span>{{ $identity->phone }}</span>
                                                </div>
                                            @endif
                                            @if($identity->email)
                                                <div class="d-flex align-items-center">
                                                    <i class="fas fa-envelope fa-fw mr-2 text-secondary"></i>
                                                    <span>{{ Str::limit($identity->email, 20) }}</span>
                                                </div>
                                            @endif
                                        </td>
                                        <td class="text-center align-middle">
                                            @if($identity->is_active)
                                                <span class="badge badge-success py-2 px-3">
                                                    <i class="fas fa-check-circle mr-1"></i> Activo
                                                </span>
                                            @else
                                                <span class="badge badge-secondary py-2 px-3">
                                                    <i class="fas fa-power-off mr-1"></i> Inactivo
                                                </span>
                                            @endif
                                        </td>
                                        <td class="text-center align-middle">
                                            <div class="btn-group" role="group">
                                                <a href="{{ route('clinic-identity.edit', $identity->id) }}" 
                                                   class="btn btn-sm btn-outline-primary" 
                                                   data-toggle="tooltip" 
                                                   title="Editar">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                
                                                @if(!$identity->is_active)
                                                    <a href="{{ route('clinic-identity.set-active', $identity->id) }}" 
                                                       class="btn btn-sm btn-outline-success" 
                                                       data-toggle="tooltip"
                                                       title="Establecer como activa"
                                                       onclick="return confirm('¿Está seguro de establecer esta identidad como activa?')">
                                                        <i class="fas fa-check"></i>
                                                    </a>
                                                @endif
                                                
                                                <form action="{{ route('clinic-identity.destroy', $identity->id) }}" method="POST" style="display: inline-block;">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-sm btn-outline-danger" 
                                                            data-toggle="tooltip"
                                                            title="Eliminar"
                                                            onclick="return confirm('¿Está seguro de eliminar esta identidad? Esta acción no se puede deshacer.')">
                                                        <i class="fas fa-trash-alt"></i>
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="6" class="text-center py-5">
                                            <div class="empty-state">
                                                <i class="fas fa-hospital-user fa-4x text-muted mb-3"></i>
                                                <h5 class="text-muted mb-3">No hay identidades registradas</h5>
                                                <p class="text-muted mb-3">Crea una nueva identidad para tu consultorio haciendo clic en el botón "Nueva Identidad"</p>
                                                <a href="{{ route('clinic-identity.create') }}" class="btn btn-primary">
                                                    <i class="fas fa-plus-circle mr-1"></i> Nueva Identidad
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('javascript')
<script>
    $(function () {
        // Inicializar tooltips
        $('[data-toggle="tooltip"]').tooltip();
        
        // Inicializar datatable si hay elementos
        if ($('#identityTable tbody tr').length > 1) {
            $('#identityTable').DataTable({
                language: {
                    url: '//cdn.datatables.net/plug-ins/1.10.25/i18n/Spanish.json'
                },
                order: [[4, 'desc']], // Ordenar por estado (activo primero)
                responsive: true,
                pageLength: 10
            });
        }
    });
</script>
@endsection 