@extends('layouts.app')

@section('content')
<div class="bg-body-light">
    <div class="content content-full">
        <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
            <div class="flex-grow-1">
                <h1 class="h3 fw-bold mb-2">
                    Modificar Cliente
                </h1>
                <h2 class="fs-base lh-base fw-medium text-muted mb-0">
                    Editando perfil de <span class="text-warning fw-bold">{{ $cliente->nombre }}</span> (ID: #{{ $cliente->id }})
                </h2>
            </div>
            <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                <ol class="breadcrumb breadcrumb-alt">
                    <li class="breadcrumb-item">
                        <a class="link-fx" href="{{ route('clientes.index') }}">Clientes</a>
                    </li>
                    <li class="breadcrumb-item" aria-current="page">
                        Editar
                    </li>
                </ol>
            </nav>
        </div>
    </div>
</div>

<div class="content">
    <div class="row justify-content-center">
        <div class="col-md-18 col-lg-18">
            <form method="POST" action="{{ route('clientes.update', $cliente->id) }}">
                @csrf 
                @method('PUT')

                <div class="block block-rounded">
                    <div class="block-header block-header-default">
                        <h3 class="block-title">
                            <i class="fa fa-user-edit me-1 text-muted"></i> Información del Cliente
                        </h3>
                        <div class="block-options">
                            <span class="badge bg-{{ $cliente->tipo_texto == 'Titular' ? 'primary' : 'secondary' }} px-3 py-2">
                                {{ $cliente->tipo_texto }}
                            </span>
                        </div>
                    </div>
                    <div class="block-content">
                        {{-- Alertas de Error --}}
                        @if($errors->any())
                            <div class="alert alert-danger alert-dismissible" role="alert">
                                <p class="mb-0 fs-sm fw-medium">Por favor, corrija los errores en el formulario.</p>
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        @endif

                        <div class="row mb-4">
                            <div class="col-md-4">
                                <label class="form-label" for="id_display">Código de Sistema</label>
                                <input type="text" class="form-control form-control-alt" id="id_display" value="{{ $cliente->id }}" readonly>
                            </div>
                            <div class="col-md-8">
                                <label class="form-label" for="fecha">Fecha de Registro</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-body-extra-light"><i class="fa fa-calendar-alt"></i></span>
                                    <input type="text" name="fecha" id="fecha" class="form-control form-control-alt"
                                           value="{{ $cliente->fecha && $cliente->fecha !== '00-00-0000' 
                                             ? \Carbon\Carbon::createFromFormat('d/m/Y', $cliente->fecha)->format('d/m/Y') 
                                             : '' }}"
                                           placeholder="dd/mm/aaaa">
                                </div>
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label" for="nombre">Nombre Completo o Razón Social <span class="text-danger">*</span></label>
                            <input type="text" name="nombre" id="nombre"
                                   class="form-control form-control-alt @error('nombre') is-invalid @enderror"
                                   value="{{ old('nombre', $cliente->nombre) }}"
                                   style="text-transform:uppercase" maxlength="45" required>
                            @error('nombre')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-4">
                                <label class="form-label" for="ci">CI / NIT <span class="text-danger">*</span></label>
                                <input type="text" name="ci" id="ci"
                                       class="form-control form-control-alt @error('ci') is-invalid @enderror"
                                       value="{{ old('ci', $cliente->ci) }}" maxlength="15" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label" for="sexo">Sexo</label>
                                <select name="sexo" id="sexo" class="form-select form-control-alt">
                                    <option value="0">Seleccione</option>
                                    <option value="1" {{ old('sexo',$cliente->sexo)==1?'selected':'' }}>Femenino</option>
                                    <option value="2" {{ old('sexo',$cliente->sexo)==2?'selected':'' }}>Masculino</option>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label" for="estadocivil">Estado Civil</label>
                                <select name="estadocivil" id="estadocivil" class="form-select form-control-alt">
                                    <option value="0">Seleccione</option>
                                    <option value="1" {{ old('estadocivil',$cliente->estadocivil)==1?'selected':'' }}>Soltero/a</option>
                                    <option value="2" {{ old('estadocivil',$cliente->estadocivil)==2?'selected':'' }}>Casado/a</option>
                                </select>
                            </div>
                        </div>

                        <h4 class="content-heading border-bottom-0 pt-0">Ubicación y Contacto</h4>
                        
                        <div class="mb-4">
                            <label class="form-label" for="direccion">Dirección de Domicilio <span class="text-danger">*</span></label>
                            <div class="input-group">
                                <span class="input-group-text bg-body-extra-light"><i class="fa fa-map-marker-alt"></i></span>
                                <input type="text" name="direccion" id="direccion"
                                       class="form-control form-control-alt @error('direccion') is-invalid @enderror"
                                       value="{{ old('direccion', $cliente->direccion) }}"
                                       style="text-transform:uppercase" maxlength="60" required>
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-6">
                                <label class="form-label" for="cboCiudades">Departamento / Ciudad</label>
                                <select name="cboCiudades" id="cboCiudades" class="form-select form-control-alt">
                                    <option value="0">Seleccione</option>
                                    @foreach($ciudades as $c)
                                        <option value="{{ $c->codigociu }}"
                                          {{ old('cboCiudades', $cliente->ciudad) == $c->codigociu ? 'selected' : '' }}>
                                          {{ $c->nombreciu }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label" for="celular">Celular <span class="text-danger">*</span></label>
                                <input type="text" name="celular" id="celular"
                                       class="form-control form-control-alt @error('celular') is-invalid @enderror"
                                       value="{{ old('celular', $cliente->celular) }}" maxlength="8">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label" for="telefono">Teléfono</label>
                                <input type="text" name="telefono" id="telefono" class="form-control form-control-alt"
                                       value="{{ old('telefono', $cliente->telefono) }}" maxlength="7">
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-4">
                                <label class="form-label" for="fax">Fax</label>
                                <input type="text" name="fax" id="fax" class="form-control form-control-alt"
                                       value="{{ old('fax', $cliente->fax) }}" maxlength="7">
                            </div>
                            <div class="col-md-5">
                                <label class="form-label" for="email">Correo electrónico</label>
                                <input type="email" name="email" id="email" class="form-control form-control-alt"
                                       value="{{ old('email', $cliente->email) }}" maxlength="35" placeholder="ejemplo@correo.com">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label" for="casilla">Casilla</label>
                                <input type="text" name="casilla" id="casilla" class="form-control form-control-alt"
                                       value="{{ old('casilla', $cliente->casilla) }}" maxlength="10">
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label" for="observaciones">Observaciones Internas</label>
                            <textarea name="observaciones" id="observaciones" class="form-control form-control-alt" rows="3"
                                      style="text-transform:uppercase" placeholder="Notas adicionales sobre el cliente..."
                                      >{{ old('observaciones', $cliente->observaciones) }}</textarea>
                        </div>
                    </div>
                    <div class="block-content block-content-full block-content-sm bg-body-light text-end rounded-bottom">
                        <a href="{{ route('clientes.show', $cliente->id) }}" class="btn btn-alt-secondary">
                            <i class="fa fa-times me-1"></i> Cancelar
                        </a>
                        <button type="submit" class="btn btn-alt-warning"
                                onclick="return confirm('¿Está seguro de guardar los cambios?')">
                            <i class="fa fa-save me-1"></i> Guardar Cambios
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection