@extends('layouts.app')

@section('title', 'Client Groups Management')

@section('content')
<div class="container-fluid">
    <!-- Navigation Bar -->
    <div class="navigation-bar">
        <div class="row align-items-center">
            <div class="col-md-6">
                <div class="page-title">
                    <h1><i class="fas fa-users me-2"></i>Client Groups Management</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Group Statistics -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="stat-card card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="stat-icon">
                            <i class="fas fa-users"></i>
                        </div>
                        <div class="ms-3">
                            <h3 class="fw-bold text-primary mb-0">{{ \App\Models\ClientGroup::count() }}</h3>
                            <p class="text-muted mb-0">Total Groups</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="stat-card card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="stat-icon stat-icon-success">
                            <i class="fas fa-user-check"></i>
                        </div>
                        <div class="ms-3">
                            <h3 class="fw-bold text-success mb-0">{{ \App\Models\ClientGroup::where('status', 'active')->count() }}</h3>
                            <p class="text-muted mb-0">Active Groups</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="stat-card card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="stat-icon stat-icon-warning">
                            <i class="fas fa-user-friends"></i>
                        </div>
                        <div class="ms-3">
                            <h3 class="fw-bold text-warning mb-0">{{ \App\Models\Client::where('type', 'group')->count() }}</h3>
                            <p class="text-muted mb-0">Group Clients</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="stat-card card border-0 shadow-sm">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <div class="stat-icon stat-icon-info">
                            <i class="fas fa-hand-holding-usd"></i>
                        </div>
                        <div class="ms-3">
                            <h3 class="fw-bold text-info mb-0">${{ number_format(\App\Models\ClientGroup::with('activeMembers')->get()->sum(function($group) {
                                return $group->activeMembers->sum(function($client) {
                                    return $client->savingsAccounts->sum('current_balance');
                                });
                            }), 2) }}</h3>
                            <p class="text-muted mb-0">Total Group Savings</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Groups Table -->
    <div class="form-card card">
        <div class="card-body">
            <!-- Search and Filters -->
            <div class="row mb-4">
                <div class="col-md-4">
                    <div class="search-box">
                        <div class="input-icon-wrapper">
                            <i class="fas fa-search"></i>
                            <input type="text" class="form-control" name="search" value="{{ request('search') }}" placeholder="Search groups by name or code...">
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="status" onchange="document.getElementById('filterForm').submit()">
                        <option value="">All Status</option>
                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
                <div class="col-md-5 text-end">
                    <div class="filter-buttons">
                        <a href="{{ route('client-groups.print-all', request()->query()) }}" class="btn btn-outline-primary" target="_blank">
                            <i class="fas fa-print me-1"></i>Print All
                        </a>
                        <a href="{{ route('client-groups.download-all', request()->query()) }}" class="btn btn-outline-success">
                            <i class="fas fa-download me-1"></i>Download PDF
                        </a>
                    </div>
                </div>
            </div>

            <div class="table-responsive">
                <table class="table table-hover client-table">
                    <thead>
                        <tr>
                            <th class="sortable" data-sort="code">
                                <span>GROUP CODE</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th class="sortable" data-sort="name">
                                <span>GROUP INFORMATION</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th class="sortable" data-sort="members_count">
                                <span>MEMBERS</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th class="sortable" data-sort="minimum_savings">
                                <span>MIN SAVINGS</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th class="sortable" data-sort="status">
                                <span>STATUS</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th class="sortable" data-sort="created_at">
                                <span>CREATED</span>
                                <i class="fas fa-sort"></i>
                            </th>
                            <th>ACTIONS</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($groups as $group)
                        <tr class="client-row">
                            <td>
                                <div class="client-id">
                                    <strong>{{ $group->code }}</strong>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="client-avatar-placeholder rounded-circle d-flex align-items-center justify-content-center me-3 bg-primary">
                                        <i class="fas fa-users text-white"></i>
                                    </div>
                                    <div>
                                        <div class="client-name fw-semibold">{{ $group->name }}</div>
                                        <small class="client-email text-muted">{{ \Illuminate\Support\Str::limit($group->description, 50) }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="client-phone">{{ $group->active_members_count }} members</div>
                            </td>
                            <td>
                                <div class="client-id-number">${{ number_format($group->minimum_savings, 2) }}</div>
                            </td>
                            <td>
                                @if($group->status == 'active')
                                    <span class="status-badge badge bg-success">
                                        <i class="fas fa-circle me-1"></i>Active
                                    </span>
                                @else
                                    <span class="status-badge badge bg-secondary">
                                        <i class="fas fa-circle me-1"></i>Inactive
                                    </span>
                                @endif
                            </td>
                            <td>
                                <div class="created-date">{{ $group->created_at->format('M d, Y') }}</div>
                                <small class="text-muted">{{ $group->created_at->diffForHumans() }}</small>
                            </td>
                            <td>
                                <div class="action-buttons-table">
                                    <a href="{{ route('client-groups.show', $group) }}" class="btn-action btn-view" title="View Group">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{{ route('client-groups.edit', $group) }}" class="btn-action btn-edit" title="Edit Group">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="{{ route('client-groups.members', $group) }}" class="btn-action btn-members" title="Manage Members">
                                        <i class="fas fa-user-plus"></i>
                                    </a>
                                    <button class="btn-action btn-more" title="More Options">
                                        <i class="fas fa-ellipsis-v"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="text-center py-5">
                                <div class="empty-state">
                                    <i class="fas fa-users fa-3x text-muted mb-3"></i>
                                    <h4 class="text-muted">No groups found</h4>
                                    <p class="text-muted mb-3">Get started by creating your first client group.</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            @if($groups->hasPages())
            <div class="d-flex justify-content-between align-items-center mt-4 pt-3 border-top">
                <div class="text-muted">
                    Showing {{ $groups->firstItem() }} to {{ $groups->lastItem() }} of {{ $groups->total() }} groups
                </div>
                <div class="pagination-custom">
                    {{ $groups->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>

<style>
    :root {
        --primary-green: #198754;
        --primary-green-dark: #146c43;
        --accent-yellow: #fbbf24;
        --accent-yellow-light: #fef3c7;
        --success-green: #198754;
        --white: #ffffff;
        --gray-50: #f9fafb;
        --gray-100: #f3f4f6;
        --gray-200: #e5e7eb;
        --gray-300: #d1d5db;
        --gray-600: #4b5563;
        --gray-700: #374151;
        --gray-900: #111827;
        --danger: #ef4444;
    }

    /* Navigation Bar Styles */
    .navigation-bar {
        background: white;
        padding: 1.5rem 0;
        margin-bottom: 2rem;
        border-bottom: 2px solid var(--gray-200);
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    }

    .page-title h1 {
        color: var(--gray-900);
        font-weight: 700;
        font-size: 1.75rem;
        margin-bottom: 0;
        display: flex;
        align-items: center;
    }

    .page-title h1 i {
        color: var(--primary-green);
    }

    .action-buttons-nav .btn {
        padding: 0.625rem 1.25rem;
        border-radius: 8px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.025em;
        font-size: 0.875rem;
        transition: all 0.3s ease;
    }

    .action-buttons-nav .btn-primary {
        background: var(--primary-green);
        border: none;
        box-shadow: 0 4px 12px rgba(25, 135, 84, 0.25);
    }

    .action-buttons-nav .btn-primary:hover {
        background: var(--primary-green-dark);
        transform: translateY(-1px);
        box-shadow: 0 6px 16px rgba(25, 135, 84, 0.35);
    }

    /* Statistics Cards */
    .stat-card {
        border-radius: 12px;
        transition: all 0.3s ease;
    }

    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1) !important;
    }

    .stat-icon {
        width: 50px;
        height: 50px;
        background: linear-gradient(135deg, var(--primary-green) 0%, var(--success-green) 100%);
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 1.25rem;
    }

    .stat-icon-success {
        background: linear-gradient(135deg, #10b981 0%, #34d399 100%);
    }

    .stat-icon-warning {
        background: linear-gradient(135deg, #f59e0b 0%, #fbbf24 100%);
    }

    .stat-icon-info {
        background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%);
    }

    /* Form Card */
    .form-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 15px 40px rgba(0, 0, 0, 0.08);
        border: none;
        overflow: hidden;
    }

    .form-card .card-body {
        padding: 2rem;
    }

    /* Search and Filters */
    .search-box .input-icon-wrapper {
        position: relative;
    }

    .search-box .input-icon-wrapper i {
        position: absolute;
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--gray-600);
        z-index: 1;
    }

    .search-box .form-control {
        padding-left: 2.75rem;
        border: 2px solid var(--gray-300);
        border-radius: 10px;
        transition: all 0.3s ease;
    }

    .search-box .form-control:focus {
        border-color: var(--primary-green);
        box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
    }

    .filter-buttons .btn {
        padding: 0.5rem 1rem;
        border: 2px solid var(--gray-300);
        border-radius: 8px;
        font-weight: 600;
        font-size: 0.875rem;
    }

    /* Table Styles */
    .client-table {
        margin-bottom: 0;
    }

    .client-table thead th {
        background: var(--gray-50);
        border-bottom: 2px solid var(--gray-200);
        padding: 1rem 0.75rem;
        font-weight: 700;
        color: var(--gray-700);
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        vertical-align: middle;
    }

    .client-table tbody td {
        padding: 1rem 0.75rem;
        vertical-align: middle;
        border-bottom: 1px solid var(--gray-100);
    }

    .client-table tbody tr {
        transition: all 0.2s ease;
    }

    .client-table tbody tr:hover {
        background: var(--gray-50);
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }

    .sortable {
        cursor: pointer;
        user-select: none;
    }

    .sortable:hover {
        background: var(--gray-100);
    }

    .sortable span {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
    }

    .sortable i {
        font-size: 0.75rem;
        color: var(--gray-400);
        transition: color 0.3s ease;
    }

    .sortable:hover i {
        color: var(--gray-600);
    }

    /* Client Avatar */
    .client-avatar {
        width: 40px;
        height: 40px;
        object-fit: cover;
    }

    .client-avatar-placeholder {
        width: 40px;
        height: 40px;
        background: var(--gray-400);
        font-size: 0.875rem;
    }

    .client-name {
        color: var(--gray-900);
        font-size: 0.9rem;
    }

    .client-email {
        font-size: 0.8rem;
    }

    .client-phone, .client-id-number, .created-date {
        font-size: 0.9rem;
        color: var(--gray-700);
    }

    /* Badges */
    .status-badge {
        font-size: 0.75rem;
        padding: 0.35rem 0.75rem;
        border-radius: 6px;
        font-weight: 600;
    }

    .status-badge i {
        font-size: 0.5rem;
    }

    /* Action Buttons */
    .action-buttons-table {
        display: flex;
        gap: 0.5rem;
    }

    .btn-action {
        width: 32px;
        height: 32px;
        border: none;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.875rem;
        transition: all 0.3s ease;
        text-decoration: none;
    }

    .btn-view {
        background: rgba(59, 130, 246, 0.1);
        color: #3b82f6;
    }

    .btn-view:hover {
        background: #3b82f6;
        color: white;
        transform: translateY(-1px);
    }

    .btn-edit {
        background: rgba(16, 185, 129, 0.1);
        color: var(--primary-green);
    }

    .btn-edit:hover {
        background: var(--primary-green);
        color: white;
        transform: translateY(-1px);
    }

    .btn-members {
        background: rgba(168, 85, 247, 0.1);
        color: #8b5cf6;
    }

    .btn-members:hover {
        background: #8b5cf6;
        color: white;
        transform: translateY(-1px);
    }

    .btn-more {
        background: rgba(107, 114, 128, 0.1);
        color: var(--gray-600);
    }

    .btn-more:hover {
        background: var(--gray-600);
        color: white;
        transform: translateY(-1px);
    }

    /* Empty State */
    .empty-state {
        padding: 2rem 0;
    }

    .empty-state i {
        opacity: 0.5;
    }

    /* Pagination */
    .pagination-custom .pagination {
        margin-bottom: 0;
    }

    .pagination-custom .page-link {
        border: 1px solid var(--gray-300);
        color: var(--gray-700);
        padding: 0.25rem 0.5rem;
        border-radius: 6px;
        margin: 0 0.25rem;
        font-weight: 400;
    }

    .pagination-custom .page-item.active .page-link {
        background: var(--primary-green);
        border-color: var(--primary-green);
    }

    .pagination-custom .page-link:hover {
        background: var(--gray-100);
        border-color: var(--gray-400);
    }

    @media (max-width: 768px) {
        .navigation-bar {
            padding: 1rem 0;
        }

        .page-title h1 {
            font-size: 1.5rem;
        }

        .form-card .card-body {
            padding: 1.5rem;
        }

        .client-table thead {
            display: none;
        }

        .client-table tbody tr {
            display: block;
            margin-bottom: 1rem;
            border: 1px solid var(--gray-200);
            border-radius: 10px;
            padding: 1rem;
        }

        .client-table tbody td {
            display: block;
            border: none;
            padding: 0.5rem 0;
        }

        .client-table tbody td::before {
            content: attr(data-label);
            font-weight: 700;
            color: var(--gray-700);
            text-transform: uppercase;
            font-size: 0.75rem;
            display: block;
            margin-bottom: 0.25rem;
        }

        .action-buttons-table {
            justify-content: center;
            margin-top: 1rem;
        }
    }
</style>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Sortable table functionality
        const sortableHeaders = document.querySelectorAll('.sortable');

        sortableHeaders.forEach(header => {
            header.addEventListener('click', function() {
                const sortBy = this.dataset.sort;
                const isAscending = !this.classList.contains('asc');

                // Reset all headers
                sortableHeaders.forEach(h => {
                    h.classList.remove('asc', 'desc');
                });

                // Set current header state
                this.classList.toggle('asc', isAscending);
                this.classList.toggle('desc', !isAscending);

                // Update sort icon
                const icon = this.querySelector('i');
                icon.className = isAscending ? 'fas fa-sort-up' : 'fas fa-sort-down';

                // Here you would typically make an AJAX request to sort the data
                console.log('Sort by:', sortBy, 'Order:', isAscending ? 'asc' : 'desc');
            });
        });

        // Search functionality
        const searchInput = document.querySelector('.search-box input');
        searchInput.addEventListener('input', function(e) {
            const searchTerm = e.target.value.toLowerCase();
            const rows = document.querySelectorAll('.client-row');

            rows.forEach(row => {
                const text = row.textContent.toLowerCase();
                row.style.display = text.includes(searchTerm) ? '' : 'none';
            });
        });
    });
</script>
@endsection
