<x-app-layout>
    <div class="app-main flex-column flex-row-fluid">
        <div class="d-flex flex-column flex-column-fluid">
            <div class="app-toolbar py-3 py-lg-6">
                <div class="app-container container-xxl d-flex flex-stack">
                    <div class="page-title d-flex flex-column justify-content-center flex-wrap me-3">
                        <h1 class="page-heading d-flex text-dark fw-bold fs-3 flex-column justify-content-center my-0">
                            Daily Distribution</h1>
                        {{ Breadcrumbs::render('daily.index') }}
                    </div>
                    @can('daily-add')
                    <div class="d-flex align-items-center ml-auto gap-2 gap-lg-3">
                        <x-add-back-button onclick="window.location='{{route('admin.daily.create')}}'">
                            <i class="ki-duotone ki-plus fs-2"></i>
                            <span class="indicator-label">{{ __('Add Distribution') }}</span>
                        </x-add-back-button>
                    </div>
                    @endcan
                </div>
            </div>
            <div class="app-content flex-column-fluid">
                <div class="app-container  container-xxl ">
                    <div class="pb-0 sm:p-8 bg-white shadow sm:rounded-lg">
                        @livewire('daily-table')
                    </div>
                </div>
            </div>
        </div>
    </div>
    <x-slot name="scripts">
        <script>
        $(document).ready(function () {

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $('.delete').on('click', function () {
                var distribution_Id = $(this).data('id');
                var distributionName = $(this).data('name');

                Swal.fire({
                    title: 'Confirm Deletion',
                    text: 'Are you sure you want to delete this distribution "' + distributionName + '"?',
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes, delete it!',
                    cancelButtonText: 'Cancel',
                    reverseButtons: true,
                    customClass: {
                        confirmButton: 'btn btn-success',
                        cancelButton: 'btn btn-danger'
                    },
                }).then((result) => {
                    if (result.isConfirmed) {
                        var url = '{{ route("admin.daily.destroy", ":id") }}';
                        url = url.replace(':id', distribution_Id); // Use invoiceId here

                        $.ajax({
                            url: url,
                            type: 'POST',
                            dataType: 'json',
                            data: {
                                '_method': 'DELETE'
                            },
                            success: function(data) {
                                if (data['alert-type'] == 'success') {
                                    toastr.success(data.message);
                                    setTimeout(function () {
                                        location.reload(); // Reload the page after deletion
                                    }, 1000);
                                }
                            },
                            error: function(data) {
                                Swal.fire({
                                    title: 'Oops...',
                                    html:
                                        'Report to admin with the following message <br/>' +
                                        data.responseJSON.message,
                                    icon: 'error',
                                })
                            }
                        });
                    }
                });
            });
        });
        </script>
    </x-slot>

</x-app-layout>
