<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> Forums</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.createForum')}}" class="btn btn-primary" 
                    ><strong><i class="bx bx-plus"></i>
                    Post on Forum</strong></a>
                
                <a href="{{route('admin.reports.forums')}}" class="btn btn-primary" style="margin-left: 10px;" 
                    ><strong><i class="bx bx-down-arrow-alt"></i>
                    Download</strong></a>
            </div>
            <div class="table-responsive text-nowrap">
                <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Company</th>
                        <th>Description</th>
                        <th>Likes</th>
                        <th>Comments</th>
                        <th>Clicks</th>
                        <th>Views</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody class="table-border-bottom-0">
                    @forelse ($forums as $forum)
                        <tr wire:key="forum-row-{{ $forum->id }}">
                            <td>{{$forum->id}}</td>
                            <td>{{$forum->title}}</td>
                            <td>{{$forum->company?->name ?? "-"}}</td>
                            <td><p style="width: 150px; text-wrap:auto;">{{Str::limit($forum->description ?? "", 50, '...')}}</p></td>
                            <td>{{$forum->likes_count}}</td>
                            <td>{{$forum->comments_count}}</td>
                            <td>{{$forum->clicks_count ?? 0}}</td>
                            <td>{{$forum->views_count ?? 0}}</td>
                            <td><span class="badge {{$forum->status == 1 ? "bg-label-success": 'bg-label-primary'}}">
                                {{$forum->status == 1 ? 'Enabled' : 'Disabled'}}</span>
                            </td>
                            <td style="width: 17%;">
                                <a href="javascript:;" wire:click="updateStatus({{$forum->id}})" title="Change Status">
                                    @if($forum->status == 1)
                                        <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/forums/detail/' . $forum->id)}}" title="View Detail">
                                    <i class="bx bx-show btn btn-primary btn-xs"></i>
                                </a>
                                <a href="{{url('admin/forums/edit/' . $forum->id)}}" title="Edit">
                                    <i class="bx bx-edit btn btn-warning btn-xs"></i>
                                </a>
                                <a href="javascript:void(0);" class="delete" data-id="{{$forum->id}}" title="Delete">
                                    <i class="bx bx-trash btn btn-danger btn-xs"></i>
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center">No record found</td>
                        </tr>
                    @endforelse
                </tbody>
                </table>
                <div>
                    {{ $forums->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: "",
                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