@extends('admin.layouts.main')

@section('section')
    <div class="page-body">
        <div class="container-fluid">
            <div class="row">
                <div class="container-fluid">
                    <div class="row page-title">
                        <div class="col-sm-6">
                            <h3>Page List</h3>
                        </div>
                        <div class="col-sm-6 text-end">
                            <a class="btn btn-primary" href="{{ route('admin.pages.create') }}">
                                <svg class="feather">
                                    <use href="{{ asset('assets/svg/feather-icons/dist/feather-sprite.svg#plus-square') }}">
                                    </use>
                                </svg>
                                Create New Page</a>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <!-- Tag table -->
                    <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>Title</th>

                                            <th>Status</th>
                                            <th>Contant</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse ($pages as $page)
                                            <tr>
                                                <td>{{ $loop->iteration }}</td>
                                                <td>{{ $page->title }}</td>
                                                <td>
                                                    <span
                                                        class="badge rounded-pill toggle-status {{ $page->status == 1 ? 'bg-success' : 'bg-danger' }}"
                                                        data-id="{{ $page->id }}" style="cursor: pointer;">
                                                        {{ $page->status == 1 ? 'Active' : 'Inactive' }}
                                                    </span>
                                                </td>
                                                <td>{{ $page->content }}</td>
                                                <td>
                                                    <a href="{{ route('admin.pages.edit', $page->id) }}"
                                                        class="btn btn-primary btn-xs" title="Edit">Edit</a>
                                                    <form action="{{ route('admin.pages.destroy', $page->id) }}" method="POST"
                                                        class="d-inline-block"
                                                        onsubmit="return confirm('Are you sure you want to delete this tag?');">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="btn btn-danger btn-xs"
                                                            title="Delete">Delete</button>
                                                    </form>
                                                </td>
                                            </tr>
                                        @empty
                                            <tr>
                                                <td colspan="4" class="text-center">No tags found.</td>
                                            </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Container-fluid ends-->
    </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 tagId = this.getAttribute('data-id');

                    fetch("{{ route('tag.changeStatusAjax') }}", {
                        method: 'POST',
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token() }}',
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id: tagId })
                    })
                        .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>

@endsection