@extends('layouts.app')

@section('title', 'Users')

@section('content')
    <!-- [ page-header ] start -->
    <div class="page-header">
        <div class="page-header-left d-flex align-items-center">
            <div class="page-header-title">
                <h5 class="m-b-10">Users</h5>
            </div>
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('index') }}">Home</a></li>
                <li class="breadcrumb-item">Users</li>
            </ul>
        </div>
        <div class="page-header-right ms-auto">
            <div class="page-header-right-items">
                <div class="d-flex d-md-none">
                    <a href="javascript:void(0)" class="page-header-right-close-toggle">
                        <i class="feather-arrow-left me-2"></i>
                        <span>Back</span>
                    </a>
                </div>
                <div class="d-flex align-items-center gap-2 page-header-right-items-wrapper">
                
                    <div class="dropdown">
                        <a class="btn btn-icon btn-light-brand" data-bs-toggle="dropdown" data-bs-offset="0, 10"
                            data-bs-auto-close="outside">
                            <i class="feather-paperclip"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end">
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-pdf me-3"></i>
                                <span>PDF</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-csv me-3"></i>
                                <span>CSV</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-xml me-3"></i>
                                <span>XML</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-txt me-3"></i>
                                <span>Text</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-exe me-3"></i>
                                <span>Excel</span>
                            </a>
                            <div class="dropdown-divider"></div>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-printer me-3"></i>
                                <span>Print</span>
                            </a>
                        </div>
                    </div>
                    @can('create_user')
                    <a href="{{ route('user.create') }}" class="btn btn-primary">
                        <i class="feather-plus me-2"></i>
                        <span>Create User</span>
                    </a>
                    @endcan
                </div>
            </div>
            <div class="d-md-none d-flex align-items-center">
                <a href="javascript:void(0)" class="page-header-right-open-toggle">
                    <i class="feather-align-right fs-20"></i>
                </a>
            </div>
        </div>
    </div>
    <div class="main-content">
        <div class="row">
            <div class="col-lg-12">
                <div class="card stretch stretch-full">
                    <div class="card-body p-0">
                        <x-server-list-toolbar form-id="userFilters" apply-label="Apply Filters" reset-label="Reset Filters"
                            actions-class="w-100 justify-content-center" placeholder="Search users by name or email" :filters="[
                                'status'=>['label'=>'Status','options'=>['active'=>'Active','inactive'=>'Inactive']],
                                'role_id'=>['label'=>'Role','options'=>$roles->pluck('name','id')->all()],
                                'team_name'=>['label'=>'Team','options'=>$teams->mapWithKeys(fn($team)=>[$team=>$team])->all()],
                                'reporting_manager_id'=>['label'=>'Reporting manager','options'=>$managers->pluck('name','id')->all()],
                            ]" />
                        <div class="table-responsive">
                            <table class="table table-hover" id="userServerList">
                                <thead>
                                    <tr>
                                        <th class="wd-30">
                                            <div class="btn-group mb-1">
                                                <div class="custom-control custom-checkbox ms-1">
                                                    <input type="checkbox" class="custom-control-input"
                                                        id="checkAllCustomer">
                                                    <label class="custom-control-label" for="checkAllCustomer"></label>
                                                </div>
                                            </div>
                                        </th>
                                        <th>Name</th>
                                        <th>Business ID</th>
                                        <th>Email</th>
                                        <th>Team</th>
                                        <th>Reporting Manager</th>
                                        <th>Roles</th>
                                        <th>Access Type</th>
                                        <th>Status</th>
                                        <th class="text-end">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($users as $user)
                                    <tr>
                                        <td>
                                            <div class="item-checkbox ms-1">
                                                <div class="custom-control custom-checkbox">
                                                    <input type="checkbox" class="custom-control-input checkbox"
                                                        id="checkBox_{{ $user->id }}">
                                                    <label class="custom-control-label"
                                                        for="checkBox_{{ $user->id }}"></label>
                                                </div>
                                            </div>
                                        </td>
                                        <td>{{ $user->name }}</td>
                                        <td>{{ $user->business_id }}</td>
                                        <td>{{ $user->email }}</td>
                                        <td>{{ $user->team_name ?: 'Not Assigned' }}</td>
                                        <td>{{ $user->reportingManager?->name ?? 'Management' }}</td>
                                        <td>
                                            @forelse($user->roles as $role)
                                                <span class="badge bg-primary me-1">{{ $role->name }}</span>
                                            @empty
                                                <span class="text-muted">No roles assigned</span>
                                            @endforelse
                                        </td>
                                        <td>{{ $user->accessTypes->first()->name ?? 'N/A' }}</td>
                                        <td><span class="badge {{ $user->email_verified_at ? 'bg-soft-success text-success' : 'bg-soft-secondary text-secondary' }}">{{ $user->email_verified_at ? 'Active' : 'Inactive' }}</span></td>
                                        <td>
                                            <div class="hstack gap-2 justify-content-end user-row-actions">
                                                @can('update_user')
                                                <a href="{{ route('user-roles.edit', $user) }}"
                                                    class="avatar-text avatar-md bg-soft-warning text-warning"
                                                    title="Edit User" aria-label="Edit User">
                                                    <i class="feather feather-edit-3"></i>
                                                </a>
                                                @endcan
                                                @can('delete_user')
                                                    @if(auth()->id() !== $user->id)
                                                    <form action="{{ route('user-roles.destroy', $user) }}" method="POST"
                                                        class="d-inline-flex m-0" onsubmit="return confirm('Are you sure you want to delete this user?');">
                                                        @csrf @method('DELETE')
                                                        <button type="submit" class="avatar-text avatar-md border-0 bg-soft-danger text-danger"
                                                            title="Delete User" aria-label="Delete User"><i class="feather feather-trash-2"></i></button>
                                                    </form>
                                                    @endif
                                                @endcan
                                            </div>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>

                        </div>
                    </div>
                    <div class="card-footer d-flex flex-wrap align-items-center justify-content-between gap-2">
                        <small class="text-muted">Showing {{ $users->firstItem() ?? 0 }}–{{ $users->lastItem() ?? 0 }} of {{ $users->total() }} users</small>
                        {{ $users->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- [ Main Content ] end -->
@endsection
