@extends('back.layout.pages-layout')
@section('pageTitle', isset($pageTitle) ? $pageTitle : 'Page Title here')
@section('content')
    @livewire('admin.slides')
@endsection

@push('scripts')
    <script>
        var modal = $('#slide_modal');

        window.addEventListener('showSlideModalForm', function(e) {
            modal.modal("show");
        });

        window.addEventListener('hideSlideModalForm', function(e) {
            modal.modal("hide");
        });

        $('table tbody#sortable_slides').sortable({
            cursor: "move",
            update: function(event, ui) {
                $(this).children().each(function(index) {
                    if ($(this).attr('data-ordering') != (index + 1)) {
                        $(this).attr('data-ordering', (index + 1)).addClass('updated');
                    }
                });
                var positions = [];
                $('.updated').each(function() {
                    positions.push([$(this).attr('data-index'), $(this).attr('data-ordering')]);
                    $(this).removeClass('updated');
                });
                Livewire.dispatch('updateSlidesOrdering', [positions]);
            }
        });

        window.addEventListener('deleteSlide', function(event) {
            var id = event.detail.id;
            Swal.fire({
                title: "Bist du dir sicher?",
                text: "Das du den Slide löschen möchtest!",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Ja, löschen!"
            }).then((result) => {
                if (result.isConfirmed) {
                    Livewire.dispatch('deleteSlideAction', [id]);
                }
            });
        });
    </script>
@endpush
