$(document).ready(function () {

    // $('.price').addClass('d-none')
    $(document).on('change', '#course_id', function () {
        const type= $(this).find('option:selected').data('type')
        if('free' == type){
            $('.price').addClass('d-none')
        }else{
            $('.price').removeClass('d-none')
        }
    });
    // if($('#course_id').val()){
        // getParts($('#course_id').val())
        $('#course_id').trigger('change')
    // }
 
    
    // alert(courses_route)
    if($( "#course" ).length && $( "#course" ).attr('type') == 'text'){
        $( "#course" ).removeAttr('disabled')
        $( "#course" ).change(handleCourseChange)
        $( "#course" ).keyup(handleCourseChange)
        $( "#course" ).autocomplete({
            source: async function( request, response ) {
                // get courses
                const data= await ajax({
                    url: courses_route,
                    data: {
                        name: request.term
                    },
                    method: 'get',
                    no_load: true
                })
                // console.log(data);
                // response( data );
                response($.map(data, function (item) {
                    return {
                        // label:(item.id || '') +" - "+ (item.user.name || '') +" - "+ (item.current_description.name || ''),
                        label:(item.id || '') +" - "+ (item.current_description.name || ''),
                        // value: item.id
                        course_id: item.id,
                        course_type: item.type,
                        course_name: (item.current_description.name || ''),
                    };
                    course_ids.push(item.id);
                }));
            },
            minLength: 1,
            select: function( event, ui ) {
                // console.log(this.value)
                console.log(ui.item.course_id, ui.item.course_name, ui.item.course_type)
                // $( "#course" ).val(this.value)
                if ($('#course_part_id').length && (ui.item.course_id || null) != $('#course_part_id').val()) {
                    $('#course_part_id').html('')
                    getParts(ui.item.course_id )
                }
                setTimeout(() => {
                    console.log(ui.item.course_name);
                    $( "#course" ).val( ui.item.course_name )
                }, 500);
                $( "#course_id" ).val( ui.item.course_id )
                if($('.types').length){
                    $('.types').hide()
                    const type= ui?.item?.course_type || 'paid'
                    $('.type_'+type).show()
                }
                // console.log( ui.item ?
                // "Selected: " + ui.item.label +" VALUE: " + ui.item.value :
                // "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    }
});

const handleCourseChange= function(){
    if (!$(this).val()?.trim().length) {
        $('#course_id').val('')
    }
}