var allLabel= "---"
$(document).ready(function () {
    $(document).on('click', '.add_file', function () {
        $('.file_group').before($('.file_group').html())
    });
    $(document).on('click', '.delete_file', function () {
        $(this).closest('.input-group').remove()
    });
    //
    $(document).on('change', '#course_id', function () {
        $('.lesson_types').hide()
        const type= $(this).find('option:selected').data('type')
        $('.lesson_type_'+type).show()

    });
    if($('#course_id').val()){
        $('#course_id').trigger('change')
    }
    //
    $(document).on('change', '#course_part_id', function () {
        getUnits($(this).val())
    });
    if($('#course_part_id').val()){
        // getUnits($('#course_part_id').val())
        $('#course_part_id').trigger('change')
    }
});

async function getUnits(course_part_id) {
    console.log({course_part_id, course_unit_id});
    $('#course_unit_id').html('')
    if (!course_part_id) {
        return
    }
    const response= await ajax({
        url: course_units_route,
        method: 'get',
        data: {
            course_part_id,
        }
    })
    let html=allLabel ? "<option value='' >"+ allLabel +"</option>" : ''
    response.forEach(row => {
        const selected= course_unit_id == row.id ? 'selected' : ''
        html+=`<option value="${row.id}" ${selected}>${row.current_description?.title}</option>`
    });
    $('#course_unit_id').html(html)
}