<script>
$(document).on('click', '.lesson-link', function (e) {
    e.preventDefault();

    let url = $(this).attr('href');
    let lessonId = $(this).data('lesson-id');
    let courseId = $(this).data('course-id');

    $.ajax({
        url: "{{ route('set.watch.history') }}",
        type: "POST",
        data: {
            lesson_id: lessonId,
            course_id: courseId
        },
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success: function (response) {
            console.log('Watch history updated:', response);
            
            // Refresh sidebar jika di halaman course player
            if (typeof reloadSidebar === 'function') {
                reloadSidebar();
            }
            
            setTimeout(function () {
                window.location.href = url;
            }, 200);
        },
        error: function (xhr, status, error) {
            console.error('AJAX Error:', error);
            window.location.href = url;
        }
    });
});
</script>

<script type="text/javascript">
    // Hanya definisikan fungsi jika di halaman yang punya course_details
    @if(isset($course_details) && isset($lesson_details))
    function reloadSidebar() {
        let courseId = '{{ $course_details->id ?? 0 }}';
        let lessonId = '{{ $lesson_details->id ?? 0 }}';
        
        // Pastikan lessonId valid
        if (!lessonId || lessonId == 0) {
            console.log('No lesson ID available for sidebar reload');
            return;
        }
        
        let url = "{{ route('course.sidebar.reload', ['course_id' => '__COURSE__', 'lesson_id' => '__LESSON__']) }}";
        url = url.replace('__COURSE__', courseId)
                 .replace('__LESSON__', lessonId);
        
        $('#player_side_bar').load(url, function () {
            console.log('Sidebar updated');
        });
    }
    @else
    // Placeholder function untuk halaman lain
    function reloadSidebar() {
        console.log('Sidebar reload not available on this page');
    }
    @endif
</script>


<script>
    "use strict";

    $(document).ready(function() {
        $.ajaxSetup({
            headers: {
                "X-CSRF-TOKEN": '{{ csrf_token() }}'
            }
        });

        $('.flexCheckChecked').on('change', function(e) {
            const id = $(this).attr('id');
            const Elem = $(this);

            Elem.attr('disabled', true);

            $.ajax({
                url: "{{ route('set.watch.history') }}", // Your route
                type: "post",
                data: {
                    lesson_id: id,
                    course_id: "{{ $course_details->id }}"
                },
                success: function(response) {
                    Elem.attr('disabled', false);
                },
                error: function(xhr, status, error) {
                    Elem.attr('disabled', false);
                    console.error("Error updating watch history:", xhr.responseText);
                }
            });
        });

        $('#fullscreen').on('click', function(e) {
            e.preventDefault();
            $('#player_content').toggleClass('col-lg-8 col-12');
            $('#player_side_bar').toggleClass('col-lg-4 col-12');
        });

        function initializeSummernote() {
            $('textarea#summernote').summernote({
                height: 180, // set editor height
                minHeight: null, // set minimum height of editor
                maxHeight: null, // set maximum height of editor
                focus: true, // set focus to editable area after initializing summernote
                toolbar: [
                    ['color', ['color']],
                    ['font', ['bold', 'italic', 'underline', 'clear']],
                    ['fontsize', ['fontsize']],
                    ['para', ['ul', 'ol']],
                    ['table', ['table']],
                    ['insert', ['link']]
                ]
            });
        }

        initializeSummernote();
    });

    var formElement;
    if ($('.ajaxForm:not(.initialized)').length > 0) {
        $('.ajaxForm:not(.initialized)').ajaxForm({
            beforeSend: function(data, form) {
                var formElement = $(form);
            },
            uploadProgress: function(event, position, total, percentComplete) {},
            complete: function(xhr) {

            },
            error: function(e) {
                console.log(e);
            }
        });
        $('.ajaxForm:not(.initialized)').addClass('initialized');
    }

    $('.tagify:not(.inited)').each(function(index, element) {
        var tagify = new Tagify(element, {
            placeholder: '{{ get_phrase('Enter your keywords') }}',
            delimiters: "~",
        });
        $(element).addClass('inited');
    });

    $(document).ready(function() {
        var iframeWidth = $('.embed-responsive-item').width();
        console.log(iframeWidth)
        var iframeHeight = (iframeWidth/100)*56;
        console.log(iframeHeight)
        $('.embed-responsive-item').height(iframeHeight+'px');
    });
</script>

@if (get_player_settings('watermark_type') == 'js')
    <script>
        // append watermark in player
        function prependWatermark() {
            $.ajax({
                type: "get",
                url: "{{ route('player.prepend.watermark') }}",
                success: function(response) {
                    if (response) {
                        $('.plyr__video-wrapper').prepend(response);
                    }
                }
            });
        }

        setInterval(() => {
            if($('.plyr__video-wrapper .watermark-container').length == 0){
                prependWatermark();
            }
        }, 5000);
    </script>
@endif
