@extends('backend.layouts.app')

@section('content')
<div class="row page-titles">
   <div class="col-md-5 align-self-center">
      <h4 class="text-themecolor">Category</h4>
   </div>
   <div class="col-md-7 align-self-center text-right">
      <div class="d-flex justify-content-end align-items-center">
         <ol class="breadcrumb">
                       <li class="breadcrumb-item"><a href="{{route('dashboard')}}">Dashboard</a></li>
            <li class="breadcrumb-item active">Category</li>
         </ol>
         {{-- @can('EmploymentType-create') --}}
         <a href="{{ route('category.create') }}"> <button type="button" class="btn btn-info d-none d-lg-block m-l-15"><i class="fa fa-plus-circle"></i> Create New</button></a>
         {{-- @endcan --}}
      </div>
   </div>
</div>
<div class="container-fluid">
    <div id="deleteModal" class="modal fade" role='dialog'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Are you sure you want to delete this?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <span id='deleteButton'></span>
                </div>
            </div>
        </div>
    </div>
    {{-- Status model --}}
    <div id="statusModal" class="modal fade" role='dialog'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <p>Are you sure you want to change the status?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <span id='changestatus'></span>
                </div>
            </div>
        </div>
    </div>
    {{-- end --}}

    <div class="content-wrapper">
        @if (session('status'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="far fa-check-circle"></i> {{ session('status') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">

                        <div class="form-inline">
                            <form action="{{ route('category') }}" method="get" id="form">
                                <div class="input-group">
                                    <input type="text" name="search" value="{{ $search }}" class="form-control" placeholder="Search...">
                                    <div class="input-group-append">
                                        <button class="btn btn-sidebar btn btn-secondary" type="submit"><i class="fas fa-search fa-fw"></i></button>
                                        <a href="{{ route('category') }}" class="btn btn-secondary" title="Reload"><i class="fas fa-sync"></i></a>
                                    </div>
                                </div>
                            </form>
                        </div>
                                <br>
                                <div class="table-responsive">
                            <table id="pages" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>S. No.</th>
                                        <th>@sortablelink ('title','Title')</th>
                                            <th>@sortablelink ('created_at','Created At')</th>
                                            <th>@sortablelink ('updated_at','Updated At')</th>
                                            <th>@sortablelink ('status','Status')</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if(count($categories) > 0)
                                        @foreach ($categories as $key => $category)
                                            <tr>
                                                <td>{{ ++$key }}</td>
                                                <td>{{ $category->title ?? '-'}}</td>

                                                <td>{{ ($category->created_at)?$category->created_at->format(config('GET.admin_date_time_format') ?? 'Y-m-d '):'-' }}</td>
                                                <td>{{ ($category->updated_at)? $category->updated_at->format(config('GET.admin_date_time_format') ?? 'Y-m-d '):'-' }}</td>
                                                <td onclick="myFunction({{ $category->id }});">
                                                    @if ($category->status == 1)
                                                        <button class="btn btn-success btn-sm"><span>Active</span></button>
                                                    @else
                                                        <button class="btn btn-danger btn-sm"><span>Inactive</span></button>
                                                    @endif
                                                </td>
                                                <td>
                                                    {{-- @can('Blog-list') --}}
                                                    <a href="{{ route('category.view',['id' => $category->id]) }}">
                                                        <button class="btn btn-info btn-sm" title="view"><i class="fas fa-eye"></i></button>
                                                    </a>
                                                    {{-- @endcan
                                                    @can('Blog-edit') --}}
                                                    <a href="{{ route('category.edit',['id' => $category->id] + request()->query()) }}">
                                                        <button class="btn btn-primary btn-sm" title="edit"><i class="fas fa-edit"></i></button>
                                                    </a>
                                                    {{-- @endcan
                                                    @can('Blog-delete') --}}

                                                        <button class="btn btn-danger btn-sm" title="delete"
                                                        onclick="confirmDeleteModal(@php echo $category->id; @endphp)"><i
                                                            class="fas fa-trash"></i>
                                                        </button>
                                                    {{-- @endcan --}}
                                                </td>
                                            </tr>
                                        @endforeach
                                    @else
                                        <tr>
                                            <td colspan="12">
                                                <h6><center>No Records Found</center></h6>
                                            </td>
                                        </tr>
                                    @endif
                                </tbody>
                            </table>
</div>
                            @if ($categories->total() > 0)
                                <div class="d-flex justify-content-between align-items-center">
                                    <!-- Display custom number of entries -->
                                    <div class="entries-info">
                                        Showing {{ $categories->firstItem() }} to {{ $categories->lastItem() }} of {{ $categories->total() }} entries
                                    </div>

                                    <!-- Pagination -->
                                    <div class="pagination-wrapper float-right">
                                        {{ $categories->links('vendor.pagination.bootstrap-4') }}
                                    </div>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>

{{-- Delete modal --}}
<script type="text/javascript">
    function confirmDeleteModal(id) {
        $('#deleteModal').modal();
        $('#deleteButton').html('<a id="delete-button" class="btn btn-danger" style="color:white;" onclick="deleteData(' + id + ')">Delete</a>');
    }

    function deleteData(id) {
        $('#delete-button').addClass('disabled');
        $('#delete-button').css('pointer-events', 'none'); // Disable click events
        $('#delete-button').css('opacity', '0.6'); // Make it look disabled
            location.replace("{{ route('category.delete') }}?id=" + id  + "{{ request()->getQueryString() ? '&' . request()->getQueryString() : '' }}");
        }
</script>
{{-- end --}}

{{-- change status --}}
<script>
    function myFunction(id) {

        $('#statusModal').modal();
        $('#changestatus').html('<a class="btn btn-danger" style="color:white;" onclick="changedataData(' + id + ')">Ok</a>')
    }

    function changedataData(id) {

            $('#statusModal').modal('hide');
            location.replace("{{ route('category.changestatus') }}?id=" + id  + "{{ request()->getQueryString() ? '&' . request()->getQueryString() : '' }}");
        }
</script>
{{-- end --}}
@stop
