@extends('admin.layouts.main')

@section('section')
    <div class="page-body">
        <div class="container-fluid">
            <div class="row page-title">
                <div class="col-sm-6">
                    <h3>Category List</h3>
                </div>
                <div class="col-sm-6 text-end">
                    <a class="btn btn-primary" href="{{ route('category.add') }}">
                        <svg class="feather">
                            <use href="{{ asset('assets/svg/feather-icons/dist/feather-sprite.svg#plus-square') }}"></use>
                        </svg>
                        Add New Category
                    </a>
                </div>
            </div>
        </div>

        <div class="container-fluid list-products mt-3">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive theme-scrollbar product-table">
                        <table class="display table table-bordered" id="basic-3">
                            <thead>
                                <tr>
                                    <th>S.No</th>
                                    <th>Name</th>
                                    <th>Details</th>
                                    <th>Status</th>
                                    <th>Created By</th>
                                    <th>Created At</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($categories as $category)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $category->name }}</td>
                                        <td>{{ Str::limit($category->description, 50) }}</td>
                                        <td><span
                                            class="badge rounded-pill toggle-status {{ $category->status ? 'bg-success' : 'bg-danger' }}"
                                            data-id="{{ $category->id }}" style="cursor: pointer;">
                                            {{ $category->status ? 'Active' : 'Inactive' }}
                                        </span>
                                    </td>

                                        <td>{{ $category->createdBy->name ?? 'N/A' }}</td>
                                        <td>{{ $category->created_at->format('Y-m-d') }}</td>
                                        <td>
                                            <a href="{{ route('edit.category', $category->id) }}" class="btn btn-sm btn-primary"
                                                title="Edit">
                                                Edit
                                            </a>
                                            <form action="{{ route('delete.category', $category->id) }}" method="POST"
                                                class="d-inline-block"
                                                onsubmit="return confirm('Are you sure you want to delete this category?');">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-danger"
                                                    title="Delete">Delete</button>
                                            </form>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="text-center">No categories found.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="{{ asset('assets/js/vendors/simple-datatables/dist/umd/simple-datatables.js') }}"></script>
    <script src="{{ asset('assets/js/product/product-list-custom.js') }}"></script>
    <script>
       
        document.addEventListener("DOMContentLoaded", function () {
            const statusButtons = document.querySelectorAll('.toggle-status');

            statusButtons.forEach(button => {
                button.addEventListener('click', function () {
                    const categoryId = this.getAttribute('data-id');

                    fetch("{{ route('category.changeStatusAjax') }}", {
                        method: 'POST',
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token() }}',
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id: categoryId })
                    })
                        .then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                this.classList.toggle('bg-success', data.status == 1);
                                this.classList.toggle('bg-danger', data.status == 0);
                                this.textContent = data.status == 1 ? 'Active' : 'Inactive';
                            }
                        })
                        .catch(error => console.error('Error:', error));
                });
            });
        });
    </script>


    </script>
@endsection