@extends('layouts.dashboard')

@section('title', 'Admin Management')
@section('page-title', 'Admin Management')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h5 class="mb-1">Manage Administrators</h5>
        <p class="text-muted mb-0">Create and manage admin accounts with different access levels</p>
    </div>
    <a href="{{ route('dashboard.admins.create') }}" class="btn btn-primary">
        <i class="bi bi-plus-lg me-2"></i>Add Admin
    </a>
</div>

<div class="table-container">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>Admin</th>
                    <th>Email</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th>Managed Users</th>
                    <th>Last Login</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($admins as $admin)
                <tr>
                    <td>
                        <div class="d-flex align-items-center gap-3">
                            <div class="admin-avatar" style="width: 40px; height: 40px; border-radius: 10px; background: {{ $admin->isSuperAdmin() ? 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)' : 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)' }}; display: flex; align-items: center; justify-content: center; color: white; font-weight: 600; font-size: 0.8rem;">
                                {{ strtoupper(substr($admin->name, 0, 2)) }}
                            </div>
                            <div>
                                <div class="fw-semibold">{{ $admin->name }}</div>
                                @if($admin->created_by)
                                <small class="text-muted">Created by: {{ $admin->createdBy->name ?? 'Unknown' }}</small>
                                @endif
                            </div>
                        </div>
                    </td>
                    <td>{{ $admin->email }}</td>
                    <td>
                        @if($admin->isSuperAdmin())
                        <span class="badge badge-superadmin">
                            <i class="bi bi-shield-check me-1"></i>Super Admin
                        </span>
                        @else
                        <span class="badge badge-admin-role">
                            <i class="bi bi-person-badge me-1"></i>Admin
                        </span>
                        @endif
                    </td>
                    <td>
                        @if($admin->is_active)
                        <span class="badge bg-success">Active</span>
                        @else
                        <span class="badge bg-secondary">Inactive</span>
                        @endif
                    </td>
                    <td>
                        @if($admin->isSuperAdmin())
                        <span class="text-muted">All Users</span>
                        @else
                        <span class="badge bg-light text-dark">
                            {{ $admin->getManagedUserCount() }} / {{ $admin->limits->max_users ?? 10 }}
                        </span>
                        @endif
                    </td>
                    <td>
                        @if($admin->last_login_at)
                        {{ $admin->last_login_at->diffForHumans() }}
                        @else
                        <span class="text-muted">Never</span>
                        @endif
                    </td>
                    <td class="text-end">
                        <div class="dropdown">
                            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                Actions
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end">
                                <li>
                                    <a class="dropdown-item" href="{{ route('dashboard.admins.edit', $admin->id) }}">
                                        <i class="bi bi-pencil me-2"></i>Edit
                                    </a>
                                </li>
                                @if(!$admin->isSuperAdmin())
                                <li>
                                    <a class="dropdown-item" href="{{ route('dashboard.admins.permissions', $admin->id) }}">
                                        <i class="bi bi-key me-2"></i>Permissions
                                    </a>
                                </li>
                                <li>
                                    <a class="dropdown-item" href="{{ route('dashboard.admins.limits', $admin->id) }}">
                                        <i class="bi bi-sliders me-2"></i>Limits
                                    </a>
                                </li>
                                @endif
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li>
                                    <form action="{{ route('dashboard.admins.toggle-status', $admin->id) }}" method="POST" class="d-inline">
                                        @csrf
                                        <button type="submit" class="dropdown-item {{ $admin->is_active ? 'text-warning' : 'text-success' }}">
                                            @if($admin->is_active)
                                            <i class="bi bi-pause-circle me-2"></i>Disable
                                            @else
                                            <i class="bi bi-play-circle me-2"></i>Enable
                                            @endif
                                        </button>
                                    </form>
                                </li>
                                @if($admin->id !== auth()->guard('admin')->id())
                                <li>
                                    <form action="{{ route('dashboard.admins.delete', $admin->id) }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="dropdown-item text-danger"
                                            data-confirm="Are you sure you want to delete this admin? This cannot be undone."
                                            data-confirm-title="Delete Admin"
                                            data-confirm-btn="Delete"
                                            data-confirm-class="btn-danger">
                                            <i class="bi bi-trash me-2"></i>Delete
                                        </button>
                                    </form>
                                </li>
                                @endif
                            </ul>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="text-center py-4 text-muted">
                        <i class="bi bi-person-x fs-1 d-block mb-2"></i>
                        No admins found
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @if($admins->hasPages())
    <div class="d-flex justify-content-center mt-4">
        {{ $admins->links() }}
    </div>
    @endif
</div>

<!-- Quick Stats -->
<div class="row mt-4">
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: linear-gradient(135deg, rgba(240, 147, 251, 0.15) 0%, rgba(245, 87, 108, 0.15) 100%); color: #f5576c;">
                    <i class="bi bi-shield-check"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\Admin::where('role', 'superadmin')->count() }}</div>
                    <div class="label">Super Admins</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: linear-gradient(135deg, rgba(79, 172, 254, 0.15) 0%, rgba(0, 242, 254, 0.15) 100%); color: #4facfe;">
                    <i class="bi bi-person-badge"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\Admin::where('role', 'admin')->count() }}</div>
                    <div class="label">Regular Admins</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="stat-card">
            <div class="d-flex align-items-center gap-3">
                <div class="icon" style="background: linear-gradient(135deg, rgba(40, 167, 69, 0.15) 0%, rgba(32, 201, 151, 0.15) 100%); color: #28a745;">
                    <i class="bi bi-person-check"></i>
                </div>
                <div>
                    <div class="value">{{ App\Models\Admin::where('is_active', true)->count() }}</div>
                    <div class="label">Active Admins</div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection