@extends('layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>Category Management</h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ asset('dashboard') }}">{{ __('message.dashboard') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Categories</li>
                    </ol>
                </nav>
            </div>
            <!-- end::page header -->
            
            <div class="card">
            <x-flashMessage />
                <div class="card-header">
                    <div class="row mt-3">
                        <div class="col-md-5">
                            <form action="{{ asset('account/category') }}" method="get">
                                <div class="input-group">
                                    <input type="search" value="{{ request('search') }}" name="search" placeholder="Search categories..." class="form-control border-right-0 seach-input">
                                    <button class="btn border border-left-0" type="submit">
                                        <i class="ti ti-search fa-lg"></i>
                                    </button>
                                    <a class="btn btn-white border" href="{{ asset('account/category') }}">
                                        <i class="ti ti-reload fa-lg"></i>
                                    </a>
                                </div>
                            </form>
                        </div>
                        <div class="col-md-4"></div>
                        <div class="col-md-3 d-flex justify-content-end">
                            <button class="btn btn-outline-secondary mr-2" data-toggle="modal" data-target="#addCategoryModal">
                                <i class="ti ti-plus fa-lg"></i>
                            </button>
                            <button class="btn btn-outline-secondary all_unit_delete">
                                <i class="ti ti-trash fa-lg"></i> 
                            </button>
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    @if ($categories->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th><input type="checkbox" id="checkboxAll"></th>
                                        <th>S.NO</th>
                                        <th>NAME</th>
                                        <th>DESCRIPTION</th>
                                        <th>ACTION</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($categories as $key => $category)
                                        <tr>
                                            <td><input type="checkbox" class="check_invoice" value="{{$category->id}}"></td>
                                            <td>{{++$key}}</td>
                                            <td>{{ $category->name }}</td>
                                            <td>{{ Str::limit($category->description, 50) }}</td>
                                                                                        
                                            <td>
                                                <div class="btn-group">
                                                    <button class="btn btn-sm btn-outline-primary edit-category-btn" 
                                                        data-id="{{ $category->id }}"
                                                        data-name="{{ $category->name }}"
                                                        data-description="{{ $category->description }}"
                                                        >
                                                        <i class="fa fa-edit"></i>
                                                    </button>
                                                    <button class="btn btn-sm btn-outline-danger delete-category-btn" 
                                                        cat_id="{{ $category->id }}">
                                                        <i class="ti ti-trash fa-lg"></i>
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        
                        <div class="mt-3 d-flex justify-content-center">
                            {{ $categories->links('pagination::bootstrap-4') }}
                        </div>
                    @else
                        <div class="alert alert-info">No categories found. Add your first category!</div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Add Category Modal -->
        <div class="modal fade" id="addCategoryModal" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add New Category</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="{{ asset('account/add-category') }}" method="POST">
                        @csrf
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Category Name *</label>
                                <input type="text" name="name" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Description</label>
                                <textarea name="description" class="form-control" rows="3"></textarea>
                            </div>
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Save Category</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Edit Category Modal -->
        <div class="modal fade" id="editCategoryModal" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit Category</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="{{asset('account/add-category')}}" method="POST">
                        @csrf
                        <input type="hidden" name="category_id" id="category_id">
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Category Name *</label>
                                <input type="text" name="name" id="editCategoryName" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Description</label>
                                <textarea name="description" id="editCategoryDescription" class="form-control" rows="3"></textarea>
                            </div>
                            
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Update Category</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
    <!-- end::main content -->
@endsection

@push('footer-script')
    <script>
        $(document).ready(function() {
            // Edit Category Button Click
            $('.edit-category-btn').click(function() {
                var categoryId = $(this).data('id');
                var categoryName = $(this).data('name');
                var categoryDescription = $(this).data('description');
                
                
                $('#category_id').val(categoryId);
                $('#editCategoryName').val(categoryName);
                $('#editCategoryDescription').val(categoryDescription);
                
                
                $('#editCategoryModal').modal('show');
            });

            // Delete Category Button Click
            $('.delete-category-btn').click(function() {
                var category_id = $(this).attr('cat_id');
                
                
                swal({
                    title: 'Are you sure?',
                    text: 'All items in this category will be moved to uncategorized!',
                    icon: 'warning',
                    dangerMode: true,
                    buttons: ["Cancel", "Delete"],
                }).then(function(value) {
                    if (value) {
                        $.ajax({
                            url: "{{asset('account/delete-category')}}",
                            type: 'post',
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: {
                                "category_id":category_id
                            },
                            success: function(result) {
                                swal("Success!", "Category deleted successfully!", "success");
                                setTimeout(function() {
                                    window.location.reload();
                                }, 1000);
                            },
                            error: function(xhr) {
                                swal("Error!", "Failed to delete category!", "error");
                            }
                        });
                    }
                });
            });

            $("#checkAll").click(function() {
                $('.check_invoice:checkbox').not(this).prop('checked', this.checked);
            });

            $(".all_unit_delete").click(function() {
                var URL = "{{ asset('account/multiple-delete-category') }}";
                var parent_ids = [];
                $('.check_invoice:checkbox:checked').each(function() {
                    parent_ids.push($(this).val());
                });
                swal({
                    title: 'Are you sure?',
                    text: 'Once deleted, you will not be recover!',
                    icon: 'warning',
                    dangerMode: true,
                    buttons: ["Cancel", "Yes!"],
                }).then(function(value) {
                    if (value) {
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                        });
                        $.ajax({
                            type: "post",
                            data: {
                                "ids": parent_ids
                            },
                            url: URL,
                            dataType: "html",
                            success: function(result) {
                                        
                                swal("success!", "Successfully deleted!", "success");
                                window.location.reload();
                            },
                            error: function(xhr)
                            {
                                swal("failed!", "failed !", "error");
                                window.location.reload();
                            }
                        });
                    }
                });
            });

            
        });
    </script>
@endpush