@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">Nueva Identidad de Consultorio</h4>
                                <small class="text-white">Registra los datos que identifican a tu consultorio médico</small>
                            </div>
                        </div>
                        <a href="{{ route('clinic-identity.index') }}" class="btn btn-light btn-sm shadow-sm">
                            <i class="fas fa-arrow-left mr-1"></i> Volver a la lista
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <form action="{{ route('clinic-identity.store') }}" method="POST" enctype="multipart/form-data" class="mb-4" id="clinicIdentityForm">
                @csrf
                
                <div class="row">
                    <!-- Panel izquierdo: Logo e imagen -->
                    <div class="col-md-4 mb-4 mb-md-0">
                        <div class="card shadow-sm border-0 h-100 rounded-lg">
                            <div class="card-header bg-light">
                                <h5 class="mb-0"><i class="fas fa-image mr-2"></i> Imagen del Consultorio</h5>
                            </div>
                            <div class="card-body d-flex flex-column justify-content-center align-items-center p-4">
                                <div class="logo-container mb-4 text-center">
                                    <div id="logo-preview" class="mb-3 d-none">
                                        <img src="" alt="Vista previa" class="img-fluid rounded shadow-sm" style="max-height: 150px; max-width: 100%;">
                                    </div>
                                    <div id="default-logo" class="bg-light rounded-lg p-5 text-center">
                                        <i class="fas fa-hospital-user fa-5x text-primary opacity-50"></i>
                                        <h6 class="mt-3 text-muted">Seleccione un logo<br>para su consultorio</h6>
                                    </div>
                                </div>
                                
                                <div class="input-group">
                                    <div class="custom-file">
                                        <input type="file" class="custom-file-input @error('logo') is-invalid @enderror" 
                                               id="logo" name="logo" accept="image/*">
                                        <label class="custom-file-label" for="logo">Seleccionar archivo</label>
                                    </div>
                                </div>
                                @error('logo')
                                    <div class="text-danger small mt-2">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted mt-2">
                                    <i class="fas fa-info-circle mr-1"></i> Formatos permitidos: JPG, PNG, GIF. Tamaño máximo: 2MB
                                </small>
                                
                                <div class="form-group mt-4 w-100">
                                    <div class="custom-control custom-switch">
                                        <input type="checkbox" class="custom-control-input" id="is_active" name="is_active" value="1" 
                                               {{ old('is_active') ? 'checked' : '' }}>
                                        <label class="custom-control-label" for="is_active">Establecer como Identidad Activa</label>
                                    </div>
                                    <small class="form-text text-muted mt-1">
                                        <i class="fas fa-info-circle mr-1"></i> Esta identidad se usará en todos los documentos generados
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Panel derecho: Formulario principal -->
                    <div class="col-md-8">
                        <div class="card shadow-sm border-0 rounded-lg">
                            <div class="card-header bg-light">
                                <ul class="nav nav-tabs card-header-tabs" id="myTab" role="tablist">
                                    <li class="nav-item">
                                        <a class="nav-link active" id="basic-tab" data-toggle="tab" href="#basic" role="tab">
                                            <i class="fas fa-info-circle mr-1"></i> <strong>Información Básica</strong>
                                        </a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" id="additional-tab" data-toggle="tab" href="#additional" role="tab">
                                            <i class="fas fa-cogs mr-1"></i> <strong>Información Adicional</strong>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                            <div class="card-body p-4">
                                <div class="tab-content" id="myTabContent">
                                    <!-- Pestaña 1: Información básica -->
                                    <div class="tab-pane fade show active" id="basic" role="tabpanel">
                                        <div class="row">
                                            <div class="col-md-12 mb-3">
                                                <div class="form-group">
                                                    <label for="name">
                                                        <i class="fas fa-hospital mr-1 text-primary"></i>
                                                        Nombre del Consultorio <span class="text-danger">*</span>
                                                    </label>
                                                    <input type="text" class="form-control @error('name') is-invalid @enderror" 
                                                           id="name" name="name" value="{{ old('name') }}" required>
                                                    @error('name')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-12 mb-3">
                                                <div class="form-group">
                                                    <label for="address">
                                                        <i class="fas fa-map-marker-alt mr-1 text-primary"></i>
                                                        Dirección <span class="text-danger">*</span>
                                                    </label>
                                                    <textarea class="form-control @error('address') is-invalid @enderror" 
                                                              id="address" name="address" rows="3" required>{{ old('address') }}</textarea>
                                                    @error('address')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="phone">
                                                        <i class="fas fa-phone mr-1 text-primary"></i>
                                                        Teléfono
                                                    </label>
                                                    <input type="text" class="form-control @error('phone') is-invalid @enderror" 
                                                           id="phone" name="phone" value="{{ old('phone') }}">
                                                    @error('phone')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="email">
                                                        <i class="fas fa-envelope mr-1 text-primary"></i>
                                                        Email
                                                    </label>
                                                    <input type="email" class="form-control @error('email') is-invalid @enderror" 
                                                           id="email" name="email" value="{{ old('email') }}">
                                                    @error('email')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="tax_id">
                                                        <i class="fas fa-id-card mr-1 text-primary"></i>
                                                        Identificación Fiscal
                                                    </label>
                                                    <input type="text" class="form-control @error('tax_id') is-invalid @enderror" 
                                                           id="tax_id" name="tax_id" value="{{ old('tax_id') }}">
                                                    @error('tax_id')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                            <div class="col-md-6 mb-3">
                                                <div class="form-group">
                                                    <label for="specialty">
                                                        <i class="fas fa-stethoscope mr-1 text-primary"></i>
                                                        Especialidad
                                                    </label>
                                                    <input type="text" class="form-control @error('specialty') is-invalid @enderror" 
                                                           id="specialty" name="specialty" value="{{ old('specialty') }}"
                                                           placeholder="Ej. Medicina General, Pediatría, etc.">
                                                    @error('specialty')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group">
                                                    <label for="website">
                                                        <i class="fas fa-globe mr-1 text-primary"></i>
                                                        Sitio Web
                                                    </label>
                                                    <input type="url" class="form-control @error('website') is-invalid @enderror" 
                                                           id="website" name="website" value="{{ old('website') }}" 
                                                           placeholder="https://www.ejemplo.com">
                                                    @error('website')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="d-flex justify-content-end mt-3">
                                            <button type="button" class="btn btn-primary" onclick="$('#additional-tab').tab('show')">
                                                Siguiente <i class="fas fa-arrow-right ml-1"></i>
                                            </button>
                                        </div>
                                    </div>
                                    
                                    <!-- Pestaña 2: Información adicional -->
                                    <div class="tab-pane fade" id="additional" role="tabpanel">
                                        <div class="row">
                                            <div class="col-md-12 mb-3">
                                                <div class="form-group">
                                                    <label for="description">
                                                        <i class="fas fa-align-left mr-1 text-primary"></i>
                                                        Descripción del Consultorio
                                                    </label>
                                                    <textarea class="form-control @error('description') is-invalid @enderror" 
                                                              id="description" name="description" rows="3">{{ old('description') }}</textarea>
                                                    @error('description')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-12 mb-3">
                                                <div class="form-group">
                                                    <label for="social_media_links">
                                                        <i class="fas fa-share-alt mr-1 text-primary"></i>
                                                        Enlaces de Redes Sociales
                                                    </label>
                                                    <input type="text" class="form-control @error('social_media_links') is-invalid @enderror" 
                                                           id="social_media_links" name="social_media_links" value="{{ old('social_media_links') }}"
                                                           placeholder="Facebook, Instagram, Twitter, etc.">
                                                    @error('social_media_links')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-12 mb-3">
                                                <div class="form-group">
                                                    <label for="working_hours">
                                                        <i class="fas fa-clock mr-1 text-primary"></i>
                                                        Horario de Atención
                                                    </label>
                                                    <input type="text" class="form-control @error('working_hours') is-invalid @enderror" 
                                                           id="working_hours" name="working_hours" value="{{ old('working_hours') }}"
                                                           placeholder="Lun-Vie: 8am-5pm, Sáb: 9am-1pm">
                                                    @error('working_hours')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-12">
                                                <div class="form-group">
                                                    <label for="emergency_contact">
                                                        <i class="fas fa-ambulance mr-1 text-primary"></i>
                                                        Contacto de Emergencia
                                                    </label>
                                                    <input type="text" class="form-control @error('emergency_contact') is-invalid @enderror" 
                                                           id="emergency_contact" name="emergency_contact" value="{{ old('emergency_contact') }}">
                                                    @error('emergency_contact')
                                                        <div class="invalid-feedback">{{ $message }}</div>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="d-flex justify-content-between mt-3">
                                            <button type="button" class="btn btn-outline-secondary" onclick="$('#basic-tab').tab('show')">
                                                <i class="fas fa-arrow-left mr-1"></i> Anterior
                                            </button>
                                            <button type="submit" class="btn btn-success px-4">
                                                <i class="fas fa-save mr-1"></i> Guardar Identidad
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('javascript')
<script>
    $(function() {
        // Inicializar las pestañas de Bootstrap
        $('#myTab a').on('click', function (e) {
            e.preventDefault();
            $(this).tab('show');
        });
        
        // Vista previa de la imagen
        document.getElementById('logo').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.getElementById('logo-preview');
                    preview.querySelector('img').src = e.target.result;
                    preview.classList.remove('d-none');
                    $('#default-logo').hide();
                }
                reader.readAsDataURL(file);
                
                // Actualizar el label con el nombre del archivo
                const label = document.querySelector('label.custom-file-label');
                label.textContent = file.name;
            }
        });
        
        // Validación del formulario
        $('#clinicIdentityForm').on('submit', function(e) {
            let isValid = true;
            
            // Validar campos requeridos
            if ($('#name').val().trim() === '') {
                $('#name').addClass('is-invalid');
                isValid = false;
            }
            
            if ($('#address').val().trim() === '') {
                $('#address').addClass('is-invalid');
                isValid = false;
            }
            
            if (!isValid) {
                e.preventDefault();
                $('#basic-tab').tab('show');
                return false;
            }
            
            return true;
        });
        
        // Quitar la clase is-invalid al escribir
        $('input, textarea').on('input', function() {
            $(this).removeClass('is-invalid');
        });
    });
</script>
@endsection 