<div>
    <div class="row">
        <div class="col-md-6 d-flex justify-content-between mt-2 mb-2">
            <h4 class="fw-bold "><span class="text-muted fw-light"></span> Users</h4>
        </div>

        <div class="col-md-6 d-flex justify-content-end">
            <div class="col-md-9 mb-4">
                <input type="text" wire:model.live.debounce.300ms="search" value="" name="search" placeholder="Search" class="form-control search">
            </div>
        </div>
    </div>
    <!-- Striped Rows -->
    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-end mb-2 mt-0">
                <a href="{{route('admin.exportUsers')}}" class="btn btn-primary" style="color: white"><strong>Export</strong></a>
            </div>
            <div class="table-responsive text-wrap">
                <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Company Name</th>
                        <th>Email</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody class="table-border-bottom-0">
                    @forelse ($users as $user)
                        <tr  wire:key="user-row-{{ $user->id }}">
                            <td>{{$user->id}}</td>
                            <td>{{$user->name}}</td>
                            <td>{{$user->company?->name ?? "-"}}</td>
                            <td>{{$user->email}}</td>
                            <td><span class="badge
                            @if($user->is_block == 0 && !empty($user->company))
                                bg-label-success
                            @elseif(empty($user->company))
                                bg-label-warning
                            @else
                                bg-label-primary
                            @endif
                            ">
                                {{--{{$user->is_block == 0 ? 'Active' : 'Blocked'}}--}}
                                @if($user->is_block == 0 && !empty($user->company))
                                        Active
                                    @elseif(empty($user->company))
                                        Pending
                                    @else
                                        Blocked
                                @endif
                                </span>
                            </td>
                            <td style="width: 18%;">

                                <a href="javascript:;" wire:click="updateStatus({{$user->id}})" title="{{$user->is_block == 0 ? 'Active': 'Blocked'}}">
                                    @if($user->is_block == 0)
                                        <i class="bx bx-check btn btn-success btn-xs"></i>
                                    @else
                                        <i class="bx bx-x btn btn-warning btn-xs"></i>
                                    @endif
                                </a>
                                <a href="{{url('admin/users/edit/' . $user->id)}}" title="Edit">
                                    <i class="bx bx-edit btn btn-warning btn-xs"></i>
                                </a>
                                <a href="{{url('admin/users/detail/' . $user->id)}}" title="Detail">
                                    <i class="bx bx-show btn btn-primary btn-xs"></i>
                                </a>
                                <a href="javascript:void(0);" class="delete" data-id="{{$user->id}}" title="Delete">
                                    <i class="bx bx-trash btn btn-danger btn-xs"></i>
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center">No record found</td>
                        </tr>
                    @endforelse
                </tbody>

                </table>
                <div>
                    {{ $users->links('livewire.pagination') }}
                </div>
            </div>
        </div>
    </div>
</div>

@section('script')
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    $(document).ready(function(){
        function confirmDelete(id) {
            Swal.fire({
                title: 'Are you sure?',
                text: "This will also remove the linked company and thier listings",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    @this.call('delete', id)
                }
            });
        }

        $(document).on('click', '.delete', function(e) {
            e.preventDefault();

            const id = $(e.target).closest('.delete').data('id');
            confirmDelete(id);
        });
    });
</script>
@endsection
