@extends('layouts.admin')

@section('admin-content')

    <div class="admin-page-header">
        <div class="admin-page-header-content">
            <div class="admin-page-title">
                <h1>{{ trans('app.Request Logs') }}</h1>
                <p class="admin-page-subtitle">{{ trans('app.All incoming product requests') }}</p>
            </div>
        </div>
    </div>

    <!-- Stats Section -->
    <div class="row g-4 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="card border-0 shadow-sm stats-card">
                <div class="card-body p-4 d-flex align-items-center">
                    <div class="stats-icon bg-primary bg-opacity-10 rounded-circle p-3 me-3">
                        <i class="fas fa-database text-primary fs-4"></i>
                    </div>
                    <div>
                        <p class="text-muted small mb-1 fw-medium">{{ trans('app.Total Logs') }}</p>
                        <h3 class="fw-bold text-dark">{{ $requestlogs->total() }}</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- FILTERS -->
    <div class="card mb-4 border-0 shadow-sm">
        <div class="card-body">

            <form method="GET" action="" class="row g-3">

                <!-- Search -->
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="Search domain / purchase code"
                        value="{{ request('search') }}">
                </div>

                <!-- Domain Filter -->
                <div class="col-md-2">
                    <select name="domain" class="form-select">
                        <option value="">Filter by Domain</option>
                        @foreach ($domains as $d)
                            <option value="{{ $d }}" {{ request('domain') == $d ? 'selected' : '' }}>
                                {{ $d }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- Product Filter -->
                <div class="col-md-2">
                    <select name="product" class="form-select">
                        <option value="">Filter by Product</option>
                        @foreach ($products as $p)
                            <option value="{{ $p }}" {{ request('product') == $p ? 'selected' : '' }}>
                                {{ $p }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- IP Filter -->
                <div class="col-md-2">
                    <select name="ip_address" class="form-select">
                        <option value="">Filter by IP</option>
                        @foreach ($ips as $ip)
                            <option value="{{ $ip }}" {{ request('ip_address') == $ip ? 'selected' : '' }}>
                                {{ $ip }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- License Filter -->
                <div class="col-md-2">
                    <select name="license" class="form-select">
                        <option value="">Filter by License</option>
                        @foreach ($licenses as $l)
                            <option value="{{ $l }}" {{ request('license') == $l ? 'selected' : '' }}>
                                {{ $l }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <!-- Sorting -->
                <div class="col-md-2">
                    <select name="sort_by" class="form-select">
                        <option value="id" {{ request('sort_by') == 'id' ? 'selected' : '' }}>Sort by ID</option>
                        <option value="created_at" {{ request('sort_by') == 'created_at' ? 'selected' : '' }}>Sort by Date
                        </option>
                    </select>
                </div>

                <div class="col-md-2">
                    <select name="sort_dir" class="form-select">
                        <option value="asc" {{ request('sort_dir') == 'asc' ? 'selected' : '' }}>Ascending</option>
                        <option value="desc" {{ request('sort_dir') == 'desc' ? 'selected' : '' }}>Descending</option>
                    </select>
                </div>

                <!-- Submit -->
                <div class="col-md-2">
                    <button class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i> Apply
                    </button>
                </div>

                <!-- Reset -->
                <div class="col-md-2">
                    <a href="{{ route('admin.requestlogs.index') }}" class="btn btn-secondary w-100">
                        Reset
                    </a>
                </div>

            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header bg-light">
    <div class="d-flex align-items-center justify-content-between">

        <div class="d-flex align-items-center">
            <i class="fas fa-list me-3 text-primary"></i>
            <div>
                <h5 class="card-title mb-0">{{ trans('app.Request Logs') }}</h5>
                <small class="text-muted">
                    {{ trans('app.All API/license validation requests') }}
                </small>
            </div>
        </div>

        <button class="btn btn-outline-primary"
                data-bs-toggle="modal"
                data-bs-target="#csvModal">
            <i class="fas fa-file-csv me-1"></i>
            Archived Logs (CSV)
        </button>

    </div>
</div>

       


        <div class="card-body p-0">
            @if ($requestlogs->count() > 0)
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th class="text-center">ID</th>
                                <th>Product</th>
                                <th>Purchase Code</th>
                                <th>Domain</th>
                                <th>User Agent</th>
                                <th>Client IP</th>
                                <th>Server IP</th>
                                <th>Created At</th>
                                <th class="text-center">Actions</th>
                            </tr>
                        </thead>

                        <tbody>
                            @foreach ($requestlogs as $log)
                                <tr>
                                    <td class="text-center">{{ $log->id }}</td>
                                    <td>{{ $log->product_slug }}</td>
                                    <td><span class="badge bg-info">{{ $log->purchase_code }}</span></td>
                                    <td>{{ $log->domain }}</td>

                                    <td><small class="text-muted">{{ Str::limit($log->user_agent, 40) }}</small></td>

                                    <td><span class="badge bg-secondary">{{ $log->end_user_ip }}</span></td>

                                    <td><span class="badge bg-dark">{{ $log->host_server_ip }}</span></td>

                                    <td>{{ $log->created_at ? $log->created_at->format('Y-m-d H:i') : '—' }}</td>

                                    <td class="text-center">
                                        <a href="{{ route('admin.requestlogs.show', $log->id) }}"
                                            class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="card-footer">
                    <div class="d-flex justify-content-center">
                        {{ $requestlogs->links() }}
                    </div>
                </div>
            @else
                <div class="text-center py-5">
                    <div class="mb-4">
                        <i class="fas fa-database text-muted empty-state-icon"></i>
                    </div>
                    <h4 class="text-muted">No Request Logs Found</h4>
                    <p class="text-muted mb-4">You will see logs when API requests are made</p>
                </div>
            @endif
        </div>
    </div>

     <div class="modal fade" id="csvModal" tabindex="-1">
            <div class="modal-dialog modal-lg modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title text-black">Archived Request Logs</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        @if ($csvFiles->count())
                            <ul class="list-group">
                                @foreach ($csvFiles as $file)
                                    <li class="list-group-item d-flex justify-content-between">
                                        <span>{{ $file }}</span>
                                        <a href="{{ route('admin.requestlogs.download', $file) }}"
                                            class="btn btn-sm btn-success">
                                            Download
                                        </a>
                                    </li>
                                @endforeach
                            </ul>
                        @else
                            <p class="text-muted text-center">No archived CSV files found</p>
                        @endif
                    </div>
                </div>
            </div>
        </div>

@endsection
