@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    <div class="col-md-12">
        <!-- Page Heading & Search Area -->
        <div class="row d-flex justify-content-end mb-3">
            <div class="col-md-9">
                <h4 class="c-grey-900">{{ $title }} <a class="btn btn-primary" href="{{ route('ch-admin.kb.faqs.create') }}"><i class="ti-plus"></i></a></h4>
            </div>

            <div class="col-md-3">
                <form method="GET" action="{{ route('ch-admin.kb.faqs.index') }}">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Search…" aria-label="Search"
                               value="{{ request('search') }}">
                        <button class="btn btn-info" type="submit"><i class="ti ti-search"></i></button>
                    </div>
                </form>
            </div>
        </div>

        @include('admin.layouts.notifications')

        <!-- Main Content -->
        <div class="row">
            <div class="bgc-white bd bdrs-3 p-20 mB-20">
                <!-- Bulk Delete Button -->
                <div class="row mb-3">
                    <div class="col-md-12">
                        <button class="btn btn-danger" id="delete-faqs" disabled><i class="ti-trash"></i> Delete
                        </button>
                    </div>
                </div>

                <!-- Products Table -->
                <div class="row">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="select-all" class="form-check-input"></th>
                            <th>Product</th>
                            <th>FAQs count</th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse ($faqs as $productId => $faqGroup)
                            <tr>
                                <td><input type="checkbox" class="form-check-input select-product" value="{{ $productId }}"></td>
                                <td>
                                    @if ($faqGroup->first()->product_id != 0)
                                        {{ $faqGroup->first()->product->name }}
                                    @else
                                        Global
                                    @endif
                                </td>
                                <td>{{ $faqGroup->count() }}</td>
                                <td class="d-flex flex-row">
                                    <a href="{{ route('ch-admin.kb.faqs.edit', $productId) }}" class="btn btn-sm btn-primary me-1">
                                        <i class="ti-pencil"></i>
                                    </a>
                                    <form action="{{ route('ch-admin.kb.faqs.destroy', $productId) }}" method="POST" style="display: inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete these Faqs?')">
                                            <i class="ti-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="text-center">No records found.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script src="{{ url('assets/custom/js/axios.min.js') }}"></script>
    <script>
        const selectAllCheckbox = document.getElementById('select-all');
        const productCheckboxes = document.querySelectorAll('.select-product');
        const bulkDeleteButton = document.getElementById('delete-faqs');

        function toggleBulkDeleteButton() {
            bulkDeleteButton.disabled = !Array.from(productCheckboxes).some(checkbox => checkbox.checked);
        }

        selectAllCheckbox.addEventListener('click', function () {
            const isChecked = this.checked;
            productCheckboxes.forEach(checkbox => {
                checkbox.checked = isChecked;
            });
            toggleBulkDeleteButton();
        });

        productCheckboxes.forEach(checkbox => {
            checkbox.addEventListener('change', function () {
                const allChecked = [...productCheckboxes].every(checkbox => checkbox.checked);
                selectAllCheckbox.checked = allChecked;
                toggleBulkDeleteButton();
            });
        });

        toggleBulkDeleteButton();

        // Function to get selected products for bulk delete
        function getSelectedProducts() {
            const selectedProducts = [];
            document.querySelectorAll('.select-product:checked').forEach(function (checkbox) {
                selectedProducts.push(checkbox.value);
            });
            return selectedProducts;
        }

        bulkDeleteButton.addEventListener('click', function () {
            const product_ids = getSelectedProducts();

            axios.delete('{{ route("ch-admin.kb.faqs.bulk.destroy") }}', {
                data: {
                    product_ids: product_ids,
                }
            })
                .then((response) => {
                    if (response.data.success) {
                        window.location.href = '{{ route("ch-admin.kb.faqs.index") }}';
                    } else {
                        alert('Failed to delete product FAQs.');
                    }
                })
                .catch((error) => {
                    console.error(error);
                    alert('An error occurred. Please try again.');
                });
        });
    </script>
@endpush
