@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">
                    <i class="fa fa-cash-register text-success me-2"></i>Registrar Pago
                </h1>
                <h2 class="fs-base lh-base fw-medium text-muted mb-0">
                    Concepto: <span class="text-primary fw-bold">{{ $tipoTexto }}</span>
                </h2>
            </div>
            <div class="flex-shrink-0 mt-3 mt-sm-0">
                <a href="{{ route('pagos-tramites.index') }}" class="btn btn-alt-secondary">
                    <i class="fa fa-arrow-left me-1"></i> Cancelar
                </a>
            </div>
        </div>
    </div>
</div>

<div class="content">
    <div class="row justify-content-center">
        <div class="col-md-8 col-lg-6">
            
            @if($errors->any())
                <div class="alert alert-danger d-flex align-items-center mb-4" role="alert">
                    <i class="fa fa-exclamation-triangle me-2"></i>
                    <ul class="mb-0 fs-sm">
                        @foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach
                    </ul>
                </div>
            @endif

            <form method="POST" action="{{ route('pagos-tramites.store') }}">
                @csrf
                
                {{-- Contexto Oculto --}}
                <input type="hidden" name="urb" value="{{ $urb }}">
                <input type="hidden" name="dis" value="{{ $dis }}">
                <input type="hidden" name="man" value="{{ $man }}">
                <input type="hidden" name="lot" value="{{ $lot }}">
                <input type="hidden" name="idcliente" value="{{ $idcliente }}">
                <input type="hidden" name="idcontrato" value="{{ $idcontrato }}">
                <input type="hidden" name="tipo" value="{{ $tipo }}">

                <div class="block block-rounded block-bordered shadow-sm">
                    <div class="block-header block-header-default">
                        <h3 class="block-title">Detalles del Recibo</h3>
                        <div class="block-options text-muted fs-xs">
                            Cliente: {{ $idcliente }}
                        </div>
                    </div>
                    <div class="block-content block-content-full">
                        
                        {{-- Fila: Fecha y N° Recibo --}}
                        <div class="row g-3 mb-4">
                            <div class="col-6">
                                <label class="form-label fs-xs text-uppercase fw-bold text-muted">Fecha del Pago</label>
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fa fa-calendar-alt"></i></span>
                                    <input type="text" class="form-control form-control-alt fs-sm" 
                                           name="fecha" id="fecha" value="{{ $hoy }}" maxlength="10">
                                </div>
                            </div>
                            <div class="col-6">
                                <label class="form-label fs-xs text-uppercase fw-bold text-muted">N° de Recibo <span class="text-danger">*</span></label>
                                <input type="number" class="form-control form-control-alt fs-sm fw-bold @error('idrecibo') is-invalid @enderror" 
                                       name="idrecibo" value="{{ old('idrecibo', $nuevoId) }}" min="1">
                            </div>
                        </div>

                        {{-- Descripción --}}
                        <div class="mb-4">
                            <label class="form-label fs-xs text-uppercase fw-bold text-muted">Descripción / Concepto <span class="text-danger">*</span></label>
                            <input type="text" class="form-control form-control-alt fs-sm @error('descripcion') is-invalid @enderror" 
                                   name="descripcion" value="{{ old('descripcion', $descripcion) }}" maxlength="50" placeholder="Ej: Pago arancel de minuta">
                        </div>

                        {{-- Montos --}}
                        <div class="bg-body-light p-3 rounded-3 mb-4 border">
                            <label class="form-label fs-xs text-uppercase fw-bold text-muted mb-3 d-block">Importes Recibidos</label>
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="input-group">
                                        <span class="input-group-text fs-sm fw-bold">Bs.</span>
                                        <input type="number" step="0.01" min="0" class="form-control form-control-alt fs-sm" 
                                               name="montobs" value="{{ old('montobs') }}" placeholder="0.00">
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="input-group">
                                        <span class="input-group-text fs-sm fw-bold">Usd.</span>
                                        <input type="number" step="0.01" min="0" class="form-control form-control-alt fs-sm" 
                                               name="montosus" value="{{ old('montosus') }}" placeholder="0.00">
                                    </div>
                                </div>
                            </div>
                            <div class="fs-xs text-muted mt-2">
                                <i class="fa fa-info-circle me-1"></i> Puede ingresar montos en ambas monedas.
                            </div>
                        </div>

                        {{-- Observaciones --}}
                        <div class="mb-4">
                            <label class="form-label fs-xs text-uppercase fw-bold text-muted">Observaciones Adicionales</label>
                            <textarea class="form-control form-control-alt fs-sm" name="observaciones" 
                                      rows="3" maxlength="200" placeholder="Notas internas...">{{ old('observaciones') }}</textarea>
                        </div>
                    </div>

                    <div class="block-content block-content-full block-content-sm bg-body-light border-top">
                        <div class="row">
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary w-100 py-2 fw-bold" onclick="return confirmar(event)">
                                    <i class="fa fa-save me-1"></i> GUARDAR PAGO
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
/**
 * Validación personalizada estilo OneUI
 */
function confirmar(e) {
    const mbs  = parseFloat(document.querySelector('[name=montobs]').value  || 0);
    const msus = parseFloat(document.querySelector('[name=montosus]').value || 0);

    if (mbs <= 0 && msus <= 0) {
        // Uso de SweetAlert2 si lo tienes configurado, sino un alert limpio
        alert('Debe ingresar un monto válido en Bolivianos o Dólares.');
        e.preventDefault(); 
        return false;
    }
    
    return confirm('¿Está seguro de registrar este pago?\nEste proceso generará un recibo en el sistema.');
}
</script>
@endpush