@extends('layouts.app')

@section('title', 'Admin Activity Logs')

@section('content')
<div class="container-fluid">
    <div class="page-header d-print-none">
        <div class="row align-items-center">
            <div class="col">
                <h2 class="page-title">
                    <span class="text-muted">Admin Activity Logs</span>
                </h2>
            </div>
        </div>
    </div>
</div>

<div class="page-wrapper">
    <div class="container-fluid">
        <!-- Activity Summary Cards -->
        <div class="row mb-3">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="subheader">Total Activities</div>
                            <div class="ms-auto">
                                <div class="h2 m-0">{{ $summary['total'] }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="subheader">Created</div>
                            <div class="ms-auto">
                                <div class="h2 m-0 text-success">{{ $summary['created'] }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="subheader">Updated</div>
                            <div class="ms-auto">
                                <div class="h2 m-0 text-info">{{ $summary['updated'] }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex align-items-center">
                            <div class="subheader">Deleted</div>
                            <div class="ms-auto">
                                <div class="h2 m-0 text-danger">{{ $summary['deleted'] }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters Card -->
        <div class="card mb-3">
            <div class="card-header">
                <h3 class="card-title">Filter Activity Logs</h3>
            </div>
            <div class="card-body">
                <form method="GET" action="{{ route('super-admin.activity-logs.index') }}" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">Admin User</label>
                        <select name="user_id" class="form-select">
                            <option value="">All Admins</option>
                            @foreach($admins as $admin)
                                <option value="{{ $admin->id }}" {{ request('user_id') == $admin->id ? 'selected' : '' }}>
                                    {{ $admin->name }} ({{ $admin->email }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Action</label>
                        <select name="action" class="form-select">
                            <option value="">All Actions</option>
                            <option value="created" {{ request('action') == 'created' ? 'selected' : '' }}>Created</option>
                            <option value="updated" {{ request('action') == 'updated' ? 'selected' : '' }}>Updated</option>
                            <option value="deleted" {{ request('action') == 'deleted' ? 'selected' : '' }}>Deleted</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Model Type</label>
                        <select name="model_type" class="form-select">
                            <option value="">All Types</option>
                            @foreach($modelTypes as $type)
                                <option value="{{ $type }}" {{ request('model_type') == $type ? 'selected' : '' }}>
                                    {{ $type }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Start Date</label>
                        <input type="date" name="start_date" class="form-control" value="{{ request('start_date') }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">End Date</label>
                        <input type="date" name="end_date" class="form-control" value="{{ request('end_date') }}">
                    </div>
                    <div class="col-md-3 d-flex gap-2 align-items-end">
                        <button type="submit" class="btn btn-primary">Filter</button>
                        <a href="{{ route('super-admin.activity-logs.index') }}" class="btn btn-secondary">Reset</a>
                        <a href="{{ route('super-admin.activity-logs.export', request()->query()) }}" class="btn btn-success" title="Export to CSV">
                            <i class="ti ti-download"></i> Export
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- Activity Logs Table -->
        <div class="card">
            <div class="table-responsive">
                <table class="table table-vcenter card-table">
                    <thead>
                        <tr>
                            <th>Admin</th>
                            <th>Action</th>
                            <th>Model Type</th>
                            <th>Model ID</th>
                            <th>Description</th>
                            <th>Date & Time</th>
                            <th>IP Address</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($activityLogs as $log)
                            <tr>
                                <td class="text-nowrap">
                                    <div class="d-flex align-items-center">
                                        <div>
                                            <div class="font-weight-medium">{{ $log->user->name }}</div>
                                            <small class="text-muted">{{ $log->user->email }}</small>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    @if($log->action == 'created')
                                        <span class="badge bg-success">Created</span>
                                    @elseif($log->action == 'updated')
                                        <span class="badge bg-info">Updated</span>
                                    @elseif($log->action == 'deleted')
                                        <span class="badge bg-danger">Deleted</span>
                                    @else
                                        <span class="badge bg-secondary">{{ ucfirst($log->action) }}</span>
                                    @endif
                                </td>
                                <td class="text-nowrap">{{ $log->model_type ?? 'N/A' }}</td>
                                <td class="text-nowrap">{{ $log->model_id ?? '-' }}</td>
                                <td>{{ $log->description }}</td>
                                <td class="text-nowrap">
                                    <div>{{ $log->created_at->format('M d, Y') }}</div>
                                    <small class="text-muted">{{ $log->created_at->format('H:i:s') }}</small>
                                </td>
                                <td class="text-nowrap" title="{{ $log->user_agent }}">
                                    <code class="text-xs">{{ $log->ip_address }}</code>
                                </td>
                                <td>
                                    <a href="{{ route('super-admin.activity-logs.admin-activity', $log->user_id) }}"
                                       class="btn btn-sm btn-info"
                                       title="View all activities of this admin">
                                        <i class="ti ti-eye"></i> View Admin
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center py-4">No activity logs found</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($activityLogs->count() > 0)
                <div class="card-footer d-flex align-items-center">
                    <div class="ms-auto">
                        {{ $activityLogs->links() }}
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
