$('#student').on('change', function() {
    $('#studentForm').submit();
});

//get lessons according to class

$('#class_id').on('change', function() {
    var classId = $(this).val();

    $.ajax({
        url: base_url + "classdiary/getLessonsByClassId/" + classId,
        type: 'GET',
        dataType: 'json',
        success: function(response) {
            console.log(response);
            $('#lesson_id').html(
                '<option value="">Select Lesson</option>'
            );

            $.each(response, function(index, lesson) {

                $('#lesson_id').append(
                    '<option value="' + lesson.id + '">' + lesson.name + '</option>'
                );

            });
        },
        error: function(xhr, status, error) {
            console.log(error);
        }
    });

    //get the start and end time from class
    $.ajax({
        url: base_url + "class/get/" + classId,
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            $('#started_time').val(data.start_time);
            $('#ended_time').val(data.end_time);
        },
        error: function(xhr, status, error) {
            console.log(error);
        }
    });

    //get students by class
    $.ajax({
        url: base_url + "classdiary/getStudentsByClassId/" + classId,
        type: 'GET',
        dataType: 'json',
        success: function(data) {

            $('#student_table_body').html('');

            $.each(data, function(index, data) {

                $('#student_table_body').append(
                    '<tr><td>' + data.first_name + " " + data.last_name + '<input type="hidden"\n' +
                    '                                   name="student_id[]"\n' +
                    '                                   value="' + data.id + '"></td>' +
                    '<td>' + '<div class="" data-bs-toggle="buttons" role="group">\n' +
                    '                                            <label class="btn btn-outline btn-info">\n' +
                    '                                                <div class="custom-control custom-radio">\n' +
                    '                                                    <input type="radio" id="attendance1' + data.id + '" name="attendance[' + data.id + ']" value="1"\n' +
                    '                                                        class="custom-control-input" checked>\n' +
                    '                                                    <label class="custom-control-label" for="attendance1' + data.id + '"> <i\n' +
                    '                                                            class="ti-check text-active" aria-hidden="true"></i>\n' +
                    '                                                        Present</label>\n' +
                    '                                                </div>\n' +
                    '                                            </label>\n' +
                    '                                            <label class="btn btn-outline btn-danger">\n' +
                    '                                                <div class="custom-control custom-radio">\n' +
                    '                                                    <input type="radio" id="attendance2' + data.id + '" name="attendance[' + data.id + ']" value="0"\n' +
                    '                                                        class="custom-control-input">\n' +
                    '                                                    <label class="custom-control-label" for="attendance2' + data.id + '"> <i\n' +
                    '                                                            class="ti-check text-active" aria-hidden="true"></i>\n' +
                    '                                                        Absent</label>\n' +
                    '                                                </div>\n' +
                    '                                            </label>\n' +
                    '                                        </div>' + '</td>' +
                    '<td><input type="number" class="form-control" placeholder="" id="duration[' + data.id + ']" name="duration[' + data.id + ']" value="120" required>' +
                    '<td><input type="text" class="form-control" placeholder="" id="student_comment[' + data.id + ']" name="student_comment[' + data.id + ']">'
                );

            });


        },
        error: function(xhr, status, error) {
            console.log(error);
        }
    });
});

//set today date to date
$(document).ready(function () {
    var today = new Date().toISOString().split('T')[0];
    $('#date').val(today);
});


//update class status function
$(document).on('click', '#btnUpdate', function(){
    let id = $(this).data('id');

    $.ajax({
        url: base_url + "classdiary/get/" + id,
        type: "GET",
        dataType: "json",
        success: function(data){
            $('#updid').val(data.id);

            $('#updname').val(data.name);
            $('#updcode').val(data.code);
            $('#upddescription').val(data.description);
            $('#updclass_day').val(data.class_day);
            $('#updstarted_date').val(data.started_date);
            $('#updstart_time').val(data.start_time);
            $('#updend_time').val(data.end_time);
            $('#updfee').val(data.fee);
            $('#updstatus').val(data.status);
            $('#updclass_type_id').val(data.class_type_id);
            $('#updpayment_type_id').val(data.payment_type_id);
            //$('#updstudent_id').val(data.student_id);
            $('#updgrade_id').val(data.grade_id);
        },
        error:function(){
            alert('Unable to load data');
        }
    });

    //fill student list
    $.ajax({
        url: base_url + "class/getStudents/" + id,
        type: "GET",
        dataType: "json",
        success: function(data){
            $('#updstudent_id').val(data.student_ids).trigger('change');

        },
        error:function(){
            alert('Unable to load data');
        }
    });
});

//delete class status function
$(document).on('click', '#btnDelete', function(){
    let id = $(this).data('id');
    $('#delid').val(id);
});



