@extends('admin.layouts.netgescon')

@section('title', 'Voci di Spesa')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title">
                        <i class="fas fa-receipt"></i> Voci di Spesa
                    </h3>
                    <a href="{{ route('admin.voci-spesa.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> Nuova Voce di Spesa
                    </a>
                </div>

                <div class="card-body">
                    <!-- Filtri -->
                    <div class="row mb-3">
                        <div class="col-12">
                            <form method="GET" action="{{ route('admin.voci-spesa.index') }}" class="row g-3">
                                <div class="col-md-3">
                                    <label for="stabile_id" class="form-label">Stabile</label>
                                    <select name="stabile_id" id="stabile_id" class="form-control">
                                        <option value="">Tutti gli stabili</option>
                                        @foreach($stabili as $stabile)
                                            <option value="{{ $stabile->id }}" {{ request('stabile_id') == $stabile->id ? 'selected' : '' }}>
                                                {{ $stabile->denominazione }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label for="categoria" class="form-label">Categoria</label>
                                    <select name="categoria" id="categoria" class="form-control">
                                        <option value="">Tutte le categorie</option>
                                        @foreach($categorie as $categoria)
                                            <option value="{{ $categoria }}" {{ request('categoria') == $categoria ? 'selected' : '' }}>
                                                {{ $categoria }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label for="stato" class="form-label">Stato</label>
                                    <select name="stato" id="stato" class="form-control">
                                        <option value="">Tutti gli stati</option>
                                        <option value="attiva" {{ request('stato') == 'attiva' ? 'selected' : '' }}>Attiva</option>
                                        <option value="inattiva" {{ request('stato') == 'inattiva' ? 'selected' : '' }}>Inattiva</option>
                                        <option value="archiviata" {{ request('stato') == 'archiviata' ? 'selected' : '' }}>Archiviata</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label for="search" class="form-label">Cerca</label>
                                    <input type="text" name="search" id="search" class="form-control" 
                                           value="{{ request('search') }}" placeholder="Cerca per denominazione...">
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">&nbsp;</label>
                                    <div class="btn-group d-block">
                                        <button type="submit" class="btn btn-outline-primary">
                                            <i class="fas fa-search"></i> Filtra
                                        </button>
                                        <a href="{{ route('admin.voci-spesa.index') }}" class="btn btn-outline-secondary">
                                            <i class="fas fa-times"></i> Reset
                                        </a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                    <!-- Tabella Voci di Spesa -->
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Codice</th>
                                    <th>Denominazione</th>
                                    <th>Stabile</th>
                                    <th>Categoria</th>
                                    <th>Importo Previsto</th>
                                    <th>Periodicità</th>
                                    <th>Stato</th>
                                    <th>Azioni</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($vociSpesa as $voceSpesa)
                                    <tr>
                                        <td>
                                            <code>{{ $voceSpesa->codice_spesa }}</code>
                                        </td>
                                        <td>
                                            <strong>{{ $voceSpesa->denominazione }}</strong>
                                            @if($voceSpesa->sottocategoria)
                                                <br><small class="text-muted">{{ $voceSpesa->sottocategoria }}</small>
                                            @endif
                                        </td>
                                        <td>{{ $voceSpesa->stabile->denominazione }}</td>
                                        <td>
                                            <span class="badge bg-info">{{ $voceSpesa->categoria }}</span>
                                        </td>
                                        <td>
                                            @if($voceSpesa->importo_previsto)
                                                <span class="text-success">€ {{ number_format($voceSpesa->importo_previsto, 2, ',', '.') }}</span>
                                            @else
                                                <span class="text-muted">Non specificato</span>
                                            @endif
                                        </td>
                                        <td>
                                            @if($voceSpesa->periodicita)
                                                <span class="badge bg-secondary">{{ ucfirst(str_replace('_', ' ', $voceSpesa->periodicita)) }}</span>
                                            @else
                                                <span class="text-muted">-</span>
                                            @endif
                                        </td>
                                        <td>
                                            @switch($voceSpesa->stato)
                                                @case('attiva')
                                                    <span class="badge bg-success">Attiva</span>
                                                    @break
                                                @case('inattiva')
                                                    <span class="badge bg-warning">Inattiva</span>
                                                    @break
                                                @case('archiviata')
                                                    <span class="badge bg-secondary">Archiviata</span>
                                                    @break
                                            @endswitch
                                        </td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="{{ route('admin.voci-spesa.show', $voceSpesa) }}" 
                                                   class="btn btn-sm btn-outline-primary" title="Visualizza">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('admin.voci-spesa.edit', $voceSpesa) }}" 
                                                   class="btn btn-sm btn-outline-warning" title="Modifica">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <form method="POST" action="{{ route('admin.voci-spesa.duplicate', $voceSpesa) }}" 
                                                      style="display: inline;">
                                                    @csrf
                                                    <button type="submit" class="btn btn-sm btn-outline-info" title="Duplica">
                                                        <i class="fas fa-copy"></i>
                                                    </button>
                                                </form>
                                                <form method="POST" action="{{ route('admin.voci-spesa.destroy', $voceSpesa) }}" 
                                                      style="display: inline;" 
                                                      onsubmit="return confirm('Sei sicuro di voler eliminare questa voce di spesa?')">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Elimina">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="8" class="text-center">
                                            <div class="py-4">
                                                <i class="fas fa-receipt fa-3x text-muted mb-3"></i>
                                                <p class="text-muted">Nessuna voce di spesa trovata</p>
                                                <a href="{{ route('admin.voci-spesa.create') }}" class="btn btn-primary">
                                                    <i class="fas fa-plus"></i> Crea la prima voce di spesa
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <!-- Paginazione -->
                    @if($vociSpesa->hasPages())
                        <div class="d-flex justify-content-center">
                            {{ $vociSpesa->appends(request()->query())->links() }}
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    // Auto-submit form when select changes
    document.getElementById('stabile_id').addEventListener('change', function() {
        this.form.submit();
    });
    
    document.getElementById('categoria').addEventListener('change', function() {
        this.form.submit();
    });
    
    document.getElementById('stato').addEventListener('change', function() {
        this.form.submit();
    });
</script>
@endsection
