@extends('layouts.app')

@section('title', 'Piani di Rateizzazione')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Piani di Rateizzazione</h3>
                    <div class="card-tools">
                        <a href="{{ route('admin.piani-rateizzazione.create') }}" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> Nuovo Piano
                        </a>
                    </div>
                </div>

                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped" id="piani-table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Denominazione</th>
                                    <th>Ripartizione</th>
                                    <th>Stabile</th>
                                    <th>Numero Rate</th>
                                    <th>Importo Totale</th>
                                    <th>Stato</th>
                                    <th>Data Inizio</th>
                                    <th>Azioni</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($piani as $piano)
                                <tr>
                                    <td>{{ $piano->id }}</td>
                                    <td>{{ $piano->denominazione }}</td>
                                    <td>
                                        <a href="{{ route('admin.ripartizioni-spesa.show', $piano->ripartizione->id) }}" class="text-info">
                                            {{ $piano->ripartizione->voceSpesa->codice }}
                                        </a>
                                    </td>
                                    <td>{{ $piano->ripartizione->stabile->denominazione }}</td>
                                    <td>{{ $piano->numero_rate }}</td>
                                    <td>€ {{ number_format($piano->importo_totale, 2, ',', '.') }}</td>
                                    <td>
                                        <span class="badge badge-{{ $piano->stato == 'attivo' ? 'success' : ($piano->stato == 'completato' ? 'primary' : ($piano->stato == 'sospeso' ? 'warning' : 'secondary')) }}">
                                            {{ ucfirst($piano->stato) }}
                                        </span>
                                    </td>
                                    <td>{{ $piano->data_inizio->format('d/m/Y') }}</td>
                                    <td>
                                        <div class="btn-group">
                                            <a href="{{ route('admin.piani-rateizzazione.show', $piano->id) }}" class="btn btn-sm btn-info">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            @can('update', $piano)
                                            <a href="{{ route('admin.piani-rateizzazione.edit', $piano->id) }}" class="btn btn-sm btn-primary">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            @endcan
                                            @can('delete', $piano)
                                            <form action="{{ route('admin.piani-rateizzazione.destroy', $piano->id) }}" method="POST" style="display: inline;">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Sei sicuro di voler eliminare questo piano?')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Statistiche -->
    <div class="row">
        <div class="col-lg-3 col-6">
            <div class="small-box bg-info">
                <div class="inner">
                    <h3>{{ $piani->count() }}</h3>
                    <p>Piani Totali</p>
                </div>
                <div class="icon">
                    <i class="fas fa-calendar-alt"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-6">
            <div class="small-box bg-success">
                <div class="inner">
                    <h3>{{ $piani->where('stato', 'attivo')->count() }}</h3>
                    <p>Piani Attivi</p>
                </div>
                <div class="icon">
                    <i class="fas fa-play"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-6">
            <div class="small-box bg-primary">
                <div class="inner">
                    <h3>{{ $piani->where('stato', 'completato')->count() }}</h3>
                    <p>Piani Completati</p>
                </div>
                <div class="icon">
                    <i class="fas fa-check"></i>
                </div>
            </div>
        </div>

        <div class="col-lg-3 col-6">
            <div class="small-box bg-warning">
                <div class="inner">
                    <h3>€ {{ number_format($piani->sum('importo_totale'), 0, ',', '.') }}</h3>
                    <p>Importo Totale</p>
                </div>
                <div class="icon">
                    <i class="fas fa-euro-sign"></i>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    $('#piani-table').DataTable({
        responsive: true,
        lengthChange: false,
        autoWidth: false,
        order: [[0, 'desc']],
        language: {
            url: '//cdn.datatables.net/plug-ins/1.10.25/i18n/Italian.json'
        }
    });
});
</script>
@endsection
