@extends('layouts.layout-2')

@section('content')

<style>
/* Statistik Cards */
.stats-card {
    transition: all 0.3s ease;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.stats-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}

.stats-number {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 0.5rem;
}

.stats-label {
    font-size: 0.9rem;
    color: #6c757d;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Log Tabelle */
.log-table {
    font-size: 0.85rem;
}

.log-level {
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 500;
}

.log-level.info { background: rgba(23, 162, 184, 0.1); color: #17a2b8; }
.log-level.warning { background: rgba(255, 193, 7, 0.1); color: #ffc107; }
.log-level.error { background: rgba(220, 53, 69, 0.1); color: #dc3545; }
.log-level.notice { background: rgba(40, 167, 69, 0.1); color: #28a745; }

/* Filter Form */
.filter-form {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
}

/* Responsive */
@media (max-width: 768px) {
    .stats-number {
        font-size: 1.5rem;
    }
    
    .log-table {
        font-size: 0.8rem;
    }
}
</style>

<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">
            <i class="fas fa-chart-line"></i> Payment Reminder Logs & Statistiken
        </h1>
        <a href="{{ route('admin_payments_reminder') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Zurück zur Übersicht
        </a>
    </div>

    <!-- Statistiken Übersicht -->
  {{--    <div class="row mb-4">
        <!-- 7 Tage -->
        <div class="col-md-4 mb-3">
            <div class="card stats-card bg-primary text-white">
                <div class="card-body text-center">
                    <div class="stats-number">{{ $stats7Days['summary']['total_logs'] }}</div>
                    <div class="stats-label">Logs (7 Tage)</div>
                    <div class="mt-2">
                        <small>
                            <i class="fas fa-envelope"></i> {{ $stats7Days['summary']['emails_sent'] }} gesendet |
                            <i class="fas fa-exclamation-triangle"></i> {{ $stats7Days['summary']['emails_failed'] }} fehlgeschlagen
                        </small>
                    </div>
                </div>
            </div>
        </div>

        <!-- 30 Tage -->
        <div class="col-md-4 mb-3">
            <div class="card stats-card bg-success text-white">
                <div class="card-body text-center">
                    <div class="stats-number">{{ $stats30Days['summary']['total_logs'] }}</div>
                    <div class="stats-label">Logs (30 Tage)</div>
                    <div class="mt-2">
                        <small>
                            <i class="fas fa-envelope"></i> {{ $stats30Days['summary']['emails_sent'] }} gesendet |
                            <i class="fas fa-exclamation-triangle"></i> {{ $stats30Days['summary']['emails_failed'] }} fehlgeschlagen
                        </small>
                    </div>
                </div>
            </div>
        </div>

        <!-- 90 Tage -->
        <div class="col-md-4 mb-3">
            <div class="card stats-card bg-info text-white">
                <div class="card-body text-center">
                    <div class="stats-number">{{ $stats90Days['summary']['total_logs'] }}</div>
                    <div class="stats-label">Logs (90 Tage)</div>
                    <div class="mt-2">
                        <small>
                            <i class="fas fa-envelope"></i> {{ $stats90Days['summary']['emails_sent'] }} gesendet |
                            <i class="fas fa-exclamation-triangle"></i> {{ $stats90Days['summary']['emails_failed'] }} fehlgeschlagen
                        </small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    --}}

    <!-- Filter -->
    <div class="card filter-form">
        <div class="card-body">
            <h5 class="card-title mb-3">
                <i class="fas fa-filter"></i> Filter
            </h5>
            <form method="GET" action="{{ route('admin_payments_reminder_logs') }}" class="row">
                <div class="col-md-3 mb-2">
                    <label for="order_id" class="form-label">Order ID</label>
                    <input type="number" class="form-control" id="order_id" name="order_id" value="{{ $orderId }}" placeholder="Order ID">
                </div>
                <div class="col-md-3 mb-2">
                    <label for="action" class="form-label">Aktion</label>
                    <select class="form-control custom-select" id="action" name="action">
                        <option value="">Alle Aktionen</option>
                        <option value="email_sent" {{ $action == 'email_sent' ? 'selected' : '' }}>E-Mail gesendet</option>
                        <option value="email_exception" {{ $action == 'email_exception' ? 'selected' : '' }}>E-Mail Fehler</option>
                        <option value="reminder_completed" {{ $action == 'reminder_completed' ? 'selected' : '' }}>Erinnerung abgeschlossen</option>
                        <option value="action_completed" {{ $action == 'action_completed' ? 'selected' : '' }}>Aktion abgeschlossen</option>
                    </select>
                </div>
                <div class="col-md-2 mb-2">
                    <label for="start_date" class="form-label">Von</label>
                    <input type="date" class="form-control" id="start_date" name="start_date" value="{{ $startDate }}">
                </div>
                <div class="col-md-2 mb-2">
                    <label for="end_date" class="form-label">Bis</label>
                    <input type="date" class="form-control" id="end_date" name="end_date" value="{{ $endDate }}">
                </div>
                <div class="col-md-2 mb-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary me-2">
                        <i class="fas fa-search"></i> Filtern
                    </button>
                    <a href="{{ route('admin_payments_reminder_logs') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i> Reset
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Logs Tabelle -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                <i class="fas fa-list"></i> 
                @if($filteredLogs)
                    Gefilterte Logs ({{ $filteredLogs->count() }} Einträge)
                @else
                    Neueste Logs ({{ $recentLogs->count() }} Einträge)
                @endif
            </h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped log-table" id="logs-table">
                    <thead>
                        <tr>
                            <th>Datum</th>
                            <th>Aktion</th>
                            <th>Level</th>
                            <th>Model</th>
                            <th>Model ID</th>
                            <th>Nachricht</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse(($filteredLogs ?: $recentLogs) as $log)
                        <tr>
                            <td>
                                <small>{{ $log->created_at->format('d.m.Y H:i:s') }}</small>
                            </td>
                            <td>
                                <span class="badge badge-secondary">{{ $log->action }}</span>
                            </td>
                            <td>
                                <span class="log-level {{ $log->getLevelType() }}">
                                    {{ ucfirst($log->getLevelType()) }}
                                </span>
                            </td>
                            <td>
                                <small>{{ $log->model ?: '-' }}</small>
                            </td>
                            <td>
                                @if($log->model_id)
                                    <a href="{{ route('admin_sales_detail', $log->model_id) }}" target="_blank" class="btn btn-sm btn-outline-primary">
                                        {{ $log->model_id }}
                                    </a>
                                @else
                                    <small>-</small>
                                @endif
                            </td>
                            <td>
                                <small>{{ Str::limit($log->message, 100) }}</small>
                                @if(strlen($log->message) > 100)
                                    <button type="button" class="btn btn-sm btn-link" data-toggle="tooltip" data-placement="top" title="{{ $log->message }}">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                @endif
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center text-muted">
                                <i class="fas fa-inbox"></i> Keine Logs gefunden
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Detaillierte Statistiken -->
    <div class="row mt-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-chart-pie"></i> Aktionen (30 Tage)
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Aktion</th>
                                    <th>Level</th>
                                    <th>Anzahl</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($stats30Days['detailed_stats'] as $stat)
                                <tr>
                                    <td><small>{{ $stat->action }}</small></td>
                                    <td>
                                        <span class="log-level {{ $stat->level == 2 ? 'info' : ($stat->level == 5 ? 'error' : 'notice') }}">
                                            {{ $stat->level == 2 ? 'Info' : ($stat->level == 5 ? 'Error' : 'Notice') }}
                                        </span>
                                    </td>
                                    <td><strong>{{ $stat->count }}</strong></td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-chart-bar"></i> Erfolgsrate (30 Tage)
                    </h5>
                </div>
                <div class="card-body">
                    @php
                        $totalEmails = $stats30Days['summary']['emails_sent'] + $stats30Days['summary']['emails_failed'];
                        $successRate = $totalEmails > 0 ? round(($stats30Days['summary']['emails_sent'] / $totalEmails) * 100, 1) : 0;
                    @endphp
                    
                    <div class="text-center">
                        <div class="display-4 text-success">{{ $successRate }}%</div>
                        <div class="text-muted">Erfolgsrate</div>
                        <div class="mt-3">
                            <div class="progress">
                                <div class="progress-bar bg-success" style="width: {{ $successRate }}%"></div>
                            </div>
                        </div>
                        <div class="mt-2">
                            <small class="text-muted">
                                {{ $stats30Days['summary']['emails_sent'] }} erfolgreich / {{ $totalEmails }} gesamt
                            </small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    // DataTable für Logs
    $('#logs-table').DataTable({
        "language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.24/i18n/German.json"
        },
        "order": [[0, "desc"]], // Sortiere nach Datum absteigend
        "pageLength": 50,
        "responsive": true
    });
    
    // Tooltips initialisieren
    $('[data-toggle="tooltip"]').tooltip();
});
</script>

@endsection 