<x-app-layout>
    @section('title')
    Blog Categories Management
    @endsection
    <!-- Page Heading -->
    <h1 class="h3 mb-2 text-gray-800">
        @can('BlogCategory create')
            <a href="{{ route('admin.blog-categories.create') }}" class="btn btn-primary"><i class="fas fa-plus" aria-hidden="true"></i>
                Blog Create Categories
            </a>
        @endcan
    </h1>
    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">
                Listing Blog Categories Table
            </h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>S.No.</th>
                            <th>Category Name</th>
                            <th>Category Slug</th>
                            <th>Parent Category</th>
                            <th>Status</th>
                            <th>Created At</th>
                            <th>Updated At</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>

                        @can('BlogCategory access')

                        @if(isset($categories))
                        <?php $_SESSION['i'] = 0; ?>

                            @foreach($categories as $category)
                            <?php $_SESSION['i']=$_SESSION['i']+1; ?>
                                <tr>
                                    <?php $dash=''; ?>
                                    <td>{{$_SESSION['i']}}</td>
                                    <td>{{$category->name}}</td>
                                    <td>{{$category->slug}}</td>
                                    <td>
                                        @if(isset($category->parent_id))
                                            {{$category->subcategory->name}}
                                        @else
                                            None
                                        @endif
                                    </td>
                                    <td>
                                        <a onclick="changeStatus(this)" href="{{ route('admin.blog-categories.status',$category->id) }}">
                                            <input type="checkbox"  {{ $category->status=='1' ? 'checked':'' }}
                                            data-toggle="toggle" data-on="{{ $category->status=='1' ? 'Active':'' }}" data-off="{{ $category->status=='1' ? 'Active':'InActive' }}" data-onstyle="success"
                                            data-offstyle="danger" value="{{ $category->status }}" onclick="changeStatus(this)"/>
                                        </a>
                                    </td>
                                    <td>{{ $category->created_at->diffForHumans() }}</td>
                                    <td>{{ $category->updated_at->diffForHumans() }}</td>
                                    <td class="d-flex">
                                        @can('BlogCategory edit')
                                            <a href="{{ route('admin.blog-categories.edit', $category->id) }}"
                                                class="btn btn-primary btn-sm mr-1">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        @endcan

                                        @can('BlogCategory delete')
                                            <a href="{{ route('admin.blog-categories.destroy', $category->id) }}"
                                                class="btn btn-danger btn-sm" onclick="event.preventDefault();
                                                document.getElementById('delete-{{$category->id}}').submit();"><i class="fas fa-trash"></i></a>
                                            <form id="delete-{{$category->id}}" action="{{ route('admin.blog-categories.destroy', $category->id) }}"
                                            method="POST" class="inline d-none">
                                            @csrf
                                            @method('delete')
                                            <button
                                                class="btn btn-block bg-gradient-danger">Delete</button>
                                        </form>
                                        @endcan

                                    </td>
                                </tr>
                                    @if(count($category->subcategory))
                                        @include('admin.blogs.categories.sub-category-list',['subcategories' => $category->subcategory])
                                    @endif

                                @endforeach
                                <?php unset($_SESSION['i']); ?>
                            @endif
                        @endcan
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        function changeStatus(e)
        {
            // console.log(e.href);
            location.href=e.href;
        }
    </script>
</x-app-layout>
