<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
            {{ __('Nuova Banca') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h1 class="h3 mb-0 text-gray-800">
                            <i class="fas fa-university"></i>
                            Nuova Banca
                        </h1>
                        <a href="{{ route('admin.banche.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i>
                            Torna all'Elenco
                        </a>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Dati Banca</h6>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.banche.store') }}" method="POST">
                        @csrf
                        
                        <div class="row">
                            <div class="col-md-8">
                                <div class="form-group">
                                    <label for="denominazione">Denominazione <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('denominazione') is-invalid @enderror" 
                                           id="denominazione" name="denominazione" value="{{ old('denominazione') }}" required>
                                    @error('denominazione')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="filiale">Filiale</label>
                                    <input type="text" class="form-control @error('filiale') is-invalid @enderror" 
                                           id="filiale" name="filiale" value="{{ old('filiale') }}">
                                    @error('filiale')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="codice_abi">Codice ABI <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('codice_abi') is-invalid @enderror" 
                                           id="codice_abi" name="codice_abi" value="{{ old('codice_abi') }}" 
                                           maxlength="5" required>
                                    @error('codice_abi')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="codice_cab">Codice CAB <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('codice_cab') is-invalid @enderror" 
                                           id="codice_cab" name="codice_cab" value="{{ old('codice_cab') }}" 
                                           maxlength="5" required>
                                    @error('codice_cab')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="saldo_corrente">Saldo Corrente</label>
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text">€</span>
                                        </div>
                                        <input type="number" step="0.01" class="form-control @error('saldo_corrente') is-invalid @enderror" 
                                               id="saldo_corrente" name="saldo_corrente" value="{{ old('saldo_corrente', '0.00') }}">
                                        @error('saldo_corrente')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="iban">IBAN <span class="text-danger">*</span></label>
                            <input type="text" class="form-control @error('iban') is-invalid @enderror" 
                                   id="iban" name="iban" value="{{ old('iban') }}" 
                                   placeholder="IT60 X054 2811 1010 0000 0123 456" required>
                            @error('iban')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="indirizzo">Indirizzo</label>
                                    <input type="text" class="form-control @error('indirizzo') is-invalid @enderror" 
                                           id="indirizzo" name="indirizzo" value="{{ old('indirizzo') }}">
                                    @error('indirizzo')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="citta">Città</label>
                                    <input type="text" class="form-control @error('citta') is-invalid @enderror" 
                                           id="citta" name="citta" value="{{ old('citta') }}">
                                    @error('citta')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="cap">CAP</label>
                                    <input type="text" class="form-control @error('cap') is-invalid @enderror" 
                                           id="cap" name="cap" value="{{ old('cap') }}" maxlength="5">
                                    @error('cap')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="telefono">Telefono</label>
                                    <input type="text" class="form-control @error('telefono') is-invalid @enderror" 
                                           id="telefono" name="telefono" value="{{ old('telefono') }}">
                                    @error('telefono')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="email">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="form-group">
                            <label for="note">Note</label>
                            <textarea class="form-control @error('note') is-invalid @enderror" 
                                      id="note" name="note" rows="3">{{ old('note') }}</textarea>
                            @error('note')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group text-right">
                            <a href="{{ route('admin.banche.index') }}" class="btn btn-secondary">
                                <i class="fas fa-times"></i>
                                Annulla
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i>
                                Salva Banca
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-info">
                        <i class="fas fa-info-circle"></i>
                        Informazioni
                    </h6>
                </div>
                <div class="card-body">
                    <p class="small text-muted">
                        <strong>Codice ABI:</strong> Codice identificativo della banca (5 cifre).
                    </p>
                    <p class="small text-muted">
                        <strong>Codice CAB:</strong> Codice identificativo della filiale (5 cifre).
                    </p>
                    <p class="small text-muted">
                        <strong>IBAN:</strong> Inserire il codice IBAN completo del conto corrente.
                    </p>
                    <p class="small text-muted">
                        <strong>Saldo Corrente:</strong> Saldo attuale del conto corrente.
                    </p>
                </div>
            </div>
        </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
    $(document).ready(function() {
        // Formattazione automatica IBAN
        $('#iban').on('input', function() {
            let value = $(this).val().replace(/\s/g, '').toUpperCase();
            let formatted = value.match(/.{1,4}/g)?.join(' ') || value;
            $(this).val(formatted);
        });
        
        // Validazione codici numerici
        $('#codice_abi, #codice_cab').on('input', function() {
            $(this).val($(this).val().replace(/\D/g, ''));
        });
        
        // Validazione CAP
        $('#cap').on('input', function() {
            $(this).val($(this).val().replace(/\D/g, ''));
        });
    });
    </script>
    @endpush
</x-app-layout>
