@extends('layout')
@section('content')
    <!-- begin::main content -->
    <main class="main-content">
        <div class="container">
            <!-- begin::page header -->
            <div class="page-header">
                <h3>Unit 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/unit') }}" 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/unit') }}">
                                        <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-primary" data-toggle="modal" data-target="#addCategoryModal">
                                <i class="ti ti-plus fa-lg"></i> Add Unit
                            </button>
                        </div>
                    </div>
                </div>

                <div class="card-body">
                    @if ($units->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>S.NO</th>
                                        <th>NAME</th>
                                        <th>SHORT NAME</th>
                                        <th>ACTION</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($units as $key => $category)
                                        <tr>
                                            <td>{{++$key}}</td>
                                            <td>{{ $category->name }}</td>
                                            <td>{{ $category->short_name }}</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-short="{{ $category->short_name }}"
                                                        >
                                                        <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">
                            {{ $units->links('pagination::bootstrap-4') }}
                        </div>
                    @else
                        <div class="alert alert-info">No Unit found. Add your first Unit!</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 Unit</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-unit') }}" method="POST">
                        @csrf
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Unit Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Short Name</label>
                                <input name="short_name" class="form-control" >
                            </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 Unit</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 Unit</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-unit')}}" method="POST">
                        @csrf
                        <input type="hidden" name="unit_id" id="category_id">
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Unit Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" id="editCategoryName" class="form-control" required>
                            </div>
                            <div class="form-group">
                                <label>Short Name</label>
                                <input name="short_name" class="form-control" id="short_name">
                            </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 Unit</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');
                var short_name = $(this).data('short');
                
                
                $('#category_id').val(categoryId);
                $('#editCategoryName').val(categoryName);
                $('#editCategoryDescription').val(categoryDescription);
                $('#short_name').val(short_name);
                
                
                $('#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: 'Once delete you can not recover !',
                    icon: 'warning',
                    dangerMode: true,
                    buttons: ["Cancel", "Delete"],
                }).then(function(value) {
                    if (value) {
                        $.ajax({
                            url: "{{asset('account/delete-unit')}}",
                            type: 'post',
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: {
                                "category_id":category_id
                            },
                            success: function(result) {
                                swal("Success!", "Unit deleted successfully!", "success");
                                setTimeout(function() {
                                    window.location.reload();
                                }, 1000);
                            },
                            error: function(xhr) {
                                swal("Error!", "Failed to delete category!", "error");
                            }
                        });
                    }
                });
            });

            
        });
    </script>
@endpush