@extends('layouts.layout-2')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">
                        <i class="fas fa-clock"></i> Payment Reminders
                    </h3>
                    <div class="card-tools">
                        <span class="badge badge-primary">{{ $totalPayments }} offene Zahlungen</span>
                        <span class="badge badge-warning">{{ number_format($totalAmount, 2, ',', '.') }} € Gesamtbetrag</span>
                    </div>
                </div>
                <div class="card-body">
                    
                    <!-- Statistiken -->
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="info-box">
                                <span class="info-box-icon bg-info"><i class="fas fa-list"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Offene Zahlungen</span>
                                    <span class="info-box-number">{{ $totalPayments }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="info-box">
                                <span class="info-box-icon bg-warning"><i class="fas fa-euro-sign"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Gesamtbetrag</span>
                                    <span class="info-box-number">{{ number_format($totalAmount, 2, ',', '.') }} €</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="info-box">
                                <span class="info-box-icon bg-success"><i class="fas fa-credit-card"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Zahlungsarten</span>
                                    <span class="info-box-number">{{ count($clearingTypes) }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="info-box">
                                <span class="info-box-icon bg-danger"><i class="fas fa-calendar"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Letzte Aktualisierung</span>
                                    <span class="info-box-number">{{ now()->format('d.m.Y H:i') }}</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Übersicht nach Zahlungsarten -->
                    <div class="row mb-4">
                        @foreach($summaryData as $clearingtype => $data)
                        <div class="col-md-4">
                            <div class="small-box bg-info">
                                <div class="inner">
                                    <h3>{{ $data['count'] }}</h3>
                                    <p>{{ strtoupper($clearingtype) }} - {{ $data['interval'] }} Tage</p>
                                </div>
                                <div class="icon">
                                    <i class="fas fa-credit-card"></i>
                                </div>
                            </div>
                        </div>
                        @endforeach
                    </div>

                    <!-- Detaillierte Tabelle -->
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped" id="payment-reminders-table">
                            <thead>
                                <tr>
                                    <th>Zahlungsart</th>
                                    <th>Intervall (Tage)</th>
                                    <th>Bestell-ID</th>
                                    <th>Zahlungs-ID</th>
                                    <th>Betrag</th>
                                    <th>Erstellt am</th>
                                    <th>Tage alt</th>
                                    <th>Grenzdatum</th>
                                    <th>Aktionen</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($detailedData as $payment)
                                <tr>
                                    <td>
                                        <span class="badge badge-primary">{{ strtoupper($payment['clearingtype']) }}</span>
                                    </td>
                                    <td>{{ $payment['interval_days'] }}</td>
                                    <td>
                                        <a href="{{ route('admin_sales_detail', $payment['order_id']) }}" target="_blank">
                                            #{{ $payment['order_id'] }}
                                        </a>
                                    </td>
                                    <td>{{ $payment['payment_id'] }}</td>
                                    <td>
                                        <span class="text-danger font-weight-bold">
                                            {{ number_format($payment['amount'], 2, ',', '.') }} €
                                        </span>
                                    </td>
                                    <td>{{ $payment['created_at'] }}</td>
                                    <td>
                                        @if($payment['days_old'] > 30)
                                            <span class="badge badge-danger">{{ $payment['days_old'] }} Tage</span>
                                        @elseif($payment['days_old'] > 14)
                                            <span class="badge badge-warning">{{ $payment['days_old'] }} Tage</span>
                                        @else
                                            <span class="badge badge-info">{{ $payment['days_old'] }} Tage</span>
                                        @endif
                                    </td>
                                    <td>{{ $payment['date_limit'] }}</td>
                                    <td>
                                        <div class="btn-group">
                                            <button type="button" class="btn btn-sm btn-outline-primary" 
                                                    onclick="sendReminder({{ $payment['payment_id'] }})">
                                                <i class="fas fa-envelope"></i> Erinnerung senden
                                            </button>
                                            <button type="button" class="btn btn-sm btn-outline-info" 
                                                    onclick="viewDetails({{ $payment['payment_id'] }})">
                                                <i class="fas fa-eye"></i> Details
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="9" class="text-center text-muted">
                                        <i class="fas fa-check-circle"></i> Keine offenen Zahlungen gefunden
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    $('#payment-reminders-table').DataTable({
        "language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.24/i18n/German.json"
        },
        "order": [[6, "desc"]], // Sortiere nach "Tage alt" absteigend
        "pageLength": 25,
        "responsive": true
    });
});

function sendReminder(paymentId) {
    if (confirm('Möchten Sie wirklich eine Zahlungserinnerung senden?')) {
        // TODO: Implementiere Erinnerung senden
        alert('Erinnerung für Payment ID ' + paymentId + ' würde gesendet werden');
    }
}

function viewDetails(paymentId) {
    // TODO: Implementiere Detail-Ansicht
    alert('Details für Payment ID ' + paymentId + ' würden angezeigt werden');
}
</script>
@endsection


