$(document).ready(function() {
    $('#teacher-table').DataTable({
        "lengthChange": false,
        "info": false,
        "pageLength": 20,
        "language": {
            "emptyTable": "ไม่พบข้อมูล",
            "paginate": {
                "first": "หน้าแรก",
                "last": "หน้าสุดท้าย",
                "next": "ถัดไป",
                "previous": "ก่อนหน้า"
            },
            "search": "ค้นหา:",
            "lengthMenu": "จำนวนผู้สอนทั้งหมด _MENU_ คน",
            "info": "ทั้งหมด _TOTAL_ คน",
            "infoEmpty": "ไม่พบผู้สอน",
        }
    });

    $('.selectpicker').selectpicker();

    $('#date').datepicker({
        format: 'mm/yyyy',
        autoclose: true,
        language: 'th',
        orientation: 'bottom',
        endDate: '+0m',
        startView: 'months',
        minViewMode: 'months',
    });

    $('#payDate').datepicker({
        format: 'dd/mm/yyyy',
        autoclose: true,
        language: 'th',
        orientation: 'bottom',
    }).datepicker("setDate", 'now');

    $('#payTime').timepicker({
        showMeridian: false,
        showSeconds: true,
        minuteStep: 1,
        secondStep: 1,
        icons: {
            up: 'fa fa-angle-up',
            down: 'fa fa-angle-down'
        }
    });

    $('#wtype').on('change', function() {
        $('#other').show();
        if ($(this).val() == 'อื่นๆ') {
            $('#teachDateDiv').hide();
            $('#oilDiv').show();
            $('#wnameDiv').show();
        }
        if ($(this).val() == 'ค่าน้ำมัน') {
            $('#wnameDiv').hide();
            $('#teachDateDiv').hide();
            $('#oilDiv').show();
        }
        if ($(this).val() == 'ค่าสอน') {
            $('#wnameDiv').hide();
            $('#oilDiv').hide();
            $('#teachDateDiv').show();
        }
    });

    $('#teachDate').datepicker({
        format: 'mm-yyyy',
        autoclose: true,
        language: 'th',
        endDate: '+0m',
        orientation: 'top',
        startView: 'months',
        minViewMode: 'months',
    });

    $('#teachDate').on('change', function() {
        getTeacher($('#teachDate').val());
    });

    $('#teacher').on('change', function() {
        getTeacherEarn($(this).val(), $('#teachDate').val());
        //console.log('teacher : ' + $(this).val() + ' , Date ' + $('#teachDate').val());
    });

    $('#withdrawModal').on('hidden.bs.modal', function() {
        $("#withdrawFrm").trigger("reset");
        $('#teachDateDiv').hide();
        $('#wnameDiv').hide();
        $('#other').hide();
        $('.selectpicker').selectpicker('refresh');
    });

});

function getTeacher(date) {
    $.ajax({
        url: '/getTeacher/' + date,
        type: 'get',
        dataType: 'json',
        success: function(response) {
            //console.log('/getTeacher/' + date);
            var len = 0;
            if (response['data'] != null) {
                len = response['data'].length;
            }
            select = document.getElementById('teacher');
            var length = select.options.length;
            for (i = length - 1; i >= 0; i--) {
                select.options[i] = null;
            }
            if (len > 0) {
                for (var i = 0; i < len; i++) {
                    var id = response['data'][i].teacher_id;
                    var name = response['data'][i].title + " " + response['data'][i].firstname + " " + response['data'][i].lastname;
                    var opt = document.createElement('option');
                    opt.value = id;
                    opt.innerHTML = name;
                    select.appendChild(opt);
                    $('#teacher').selectpicker('refresh');
                }
            }
        }
    });
}

function getTeacherEarn(teacherID, date) {
    $.ajax({
        url: '/getTeacherEarn/' + teacherID + '/' + date,
        type: 'get',
        dataType: 'json',
        success: function(response) {
            document.getElementById("amount").value = response['data'].total;
        }
    });
}

// function getTeacher(date) {
//     var dateParts = date.split("/");
//     dateParts = dateParts[2] + "-" + dateParts[1] + "-" + dateParts[0];
//     $.ajax({
//         url: '/getTeacher/' + dateParts,
//         type: 'get',
//         dataType: 'json',
//         success: function (response) {
//             console.log('/getTeacher/' + dateParts);
//             var len = 0;
//             if (response['data'] != null) {
//                 len = response['data'].length;
//             }
//             select = document.getElementById('teacher');
//             var length = select.options.length;
//             for (i = length - 1; i >= 0; i--) {
//                 select.options[i] = null;
//             }
//             if (len > 0) {
//                 for (var i = 0; i < len; i++) {
//                     var id = response['data'][i].teacher_id;
//                     var name = response['data'][i].title +" "+ response['data'][i].firstname+" "+response['data'][i].lastname;
//                     var opt = document.createElement('option');
//                     opt.value = id;
//                     opt.innerHTML = name;
//                     select.appendChild(opt);
//                     $('#teacher').selectpicker('refresh');

//                 }
//             }
//         }
//     });
// }