
$(document).on('change', '#student', function(){
    let id = $(this).val();

    loadClassDiary(id);

    $.ajax({
        url: base_url + "payment/getAttendance/" + id,
        type: "GET",
        dataType: "json",
        success: function(data){
            $('#presentCount').html(data.present_count);
            $('#absentCount').html(data.absent_count);
        },
        error:function(){
            alert('Unable to load data');
        }
    });

    $.ajax({
        url: base_url + "payment/getPendingFeeCount/" + id,
        type: "GET",
        dataType: "json",
        success: function(data){
            $('#pandingCount').html(data.pending_count);
            $('#paidCount').html(data.paid_count);
        },
        error:function(){
            alert('Unable to load data');
        }
    });
});


function loadClassDiary(studentId)
{
    if ($.fn.DataTable.isDataTable('#zero_config')) {
        $('#zero_config').DataTable().destroy();
    }
;
    $('#zero_config').DataTable({ 
        processing: true,
        serverSide: false,
        ajax: {
            url: base_url + "payment/getPaymentByStudentId/" + studentId,
            type: "GET",
            dataSrc: "data"
        },
        columns: [
            {
                data: "class_time"
            },
            {
                data: "class_name"
            },
            {
                data: "status",
                render: function(data) {
                    return data == 1 
                        ? '<span class="badge rounded-pill text-bg-success" style="font-size:100%">Paid</span>'
                        : '<span class="badge rounded-pill text-bg-warning" style="font-size:100%">Pending</span>';
                }
            },
            {
                data: "paid_date",
                defaultContent: "-"
            },
            {
                data: "payment_method_name",
                defaultContent: "-"
            },
            {
                data: "id",
                render: function(data){
                    return '<button id="btnUpdate" data-id="'+ data +'" data-bs-toggle="modal" data-bs-target="#updateModal" type="button" class="btn btn-secondary btn-rounded" style="padding: 3px 3px; width: 100%; height: 30px;">Pay / Update</button>'
                }
            }
        ],
        order: []
    });
}


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

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

            $('#updname').val(data[0].class_name);
            $('#upddate').val(data[0].class_time);
            $('#updfee').val(data[0].student_fee);

            //if paid_date empty, set today
            if(data[0].paid_date == null){
                let today = new Date().toISOString().split('T')[0];
                $('#updpaidDate').val(today);
            }else{
                $('#updpaidDate').val(data[0].paid_date);
            }
        },
        error:function(){
            alert('Unable to load data');
        }
    });
});

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

//set today date to paid date



$('#paymentForm').submit(function(e) {
    e.preventDefault();

    $.ajax({
        url: base_url + "payment/update",
        type: "POST",
        data: $(this).serialize(),
        dataType: "json",
        success: function(response) {
            if(response.status == "success") {
                 // Close modal
                var modal = bootstrap.Modal.getInstance(document.getElementById('updateModal'));
                modal.hide();

                $('#msg').html(
                    '<div class="alert alert-success">' +
                    response.message +
                    '</div>'
                );

                setTimeout(function () {
                    $('#msg').fadeOut(500);
                }, 3000);
                // Refresh DataTable if needed
                $('#zero_config').DataTable().ajax.reload(null, false);
            } else {
                $('#msg').html(
                    '<div class="alert alert-danger">' +
                    response.message +
                    '</div>'
                );
                setTimeout(function () {
                    $('#msg').fadeOut(500);
                }, 3000);
            }
        },
        error: function(xhr) {
            alert(xhr.responseText);
        }
    });
    
    $('#zero_config').DataTable().ajax.reload(null, false);
});

