//all js 

"use strict";

function change_employee_status(statusObj, employee_id) {
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/change_employee_status";
    var status = statusObj.value;

    $.ajax({
        type: "POST",
        url: req_url,
        cache: false,
        data: {
            'employee_id': employee_id,
            'status': status
        },
        success: function (data) {
            $(".pace-inactive").css("display", "none");
            $(".se-pre-con").css("display", "none");
            $('.alert').addClass("show");
            $('.alert').removeClass("hide");
            $('.alert').addClass("showAlert");
            setTimeout(function(){
                $('.alert').removeClass("show");
                $('.alert').addClass("hide");
            },3000);
            $('.close-btn').click(function(){
                $('.alert').removeClass("show");
                $('.alert').addClass("hide");
            });
        }
    });
}

function myFunction(id, salary, warning, pid, pname, num, first_name, last_name, email) {

    if (num.value === '1') {
        $('#ConfirmationModal').modal('show');//load modal
        $('#hidden_id_model_conformation').val(id);
        $('#salary').val(salary);
        $('#fname_modal_sal').val(first_name);
        $('#lname_modal_sal').val(last_name);
        $('#email_modal_sal').val(email);
        $('#position_modal_sal').val(pname);
        $('#current_salary').val(salary);

        // $('#designation').val(pid);
        $('#designation').find(":selected").value(pid);
    } else if (num.value === '2') {
        // alert(num.value + id);
        $('#TerminationModal').modal('show');//load modal
        $('#hidden_id_model_termination').val(id);
        if (warning === 0) {
            $('#warning').val("Warning " + 1);
            $('#hidden_warning_no_model').val(1);
            $('#fname_modal_ter').val(first_name);
            $('#lname_modal_ter').val(last_name);
            $('#email_modal_ter').val(email);
            $('#position_modal_ter').val(pname);
            document.getElementById("btn_save_termination").style.display = "block";
            document.getElementById("ter_div_1").style.display = "block";
            document.getElementById("ter_div_2").style.display = "block";
            document.getElementById("ter_div_3").style.display = "block";
            document.getElementById("ter_div_4").style.display = "none";
        } else if (warning >= 1 && warning <= 2) {
            warning = warning + 1;
            $('#warning').val("Warning " + warning);
            $('#hidden_warning_no_model').val(warning);
            $('#fname_modal_ter').val(first_name);
            $('#lname_modal_ter').val(last_name);
            $('#email_modal_ter').val(email);
            $('#position_modal_ter').val(pname);
            document.getElementById("btn_save_termination").style.display = "block";
            document.getElementById("ter_div_1").style.display = "block";
            document.getElementById("ter_div_2").style.display = "block";
            document.getElementById("ter_div_3").style.display = "block";
            document.getElementById("ter_div_4").style.display = "none";
        } else if (warning >= 3) {
            $('#warning').val("Warning Max");
            $('#hidden_warning_no_model').val(4);
            // $('#btn_save_termination').style.visibility="hidden";
            document.getElementById("btn_save_termination").style.display = "none";
            document.getElementById("ter_div_1").style.display = "none";
            document.getElementById("ter_div_2").style.display = "none";
            document.getElementById("ter_div_3").style.display = "none";
            document.getElementById("ter_div_4").style.display = "block";

        }
    }else if (num.value === '3'){
        $('#terminateModal').modal('show');//load modal
        $('#ter_emp_id').val(id);
        $('#ter_first_name').val(first_name);
        $('#ter_last_name').val(last_name);
        $('#ter_email').val(email);
    }
}

//Vacation Time JS
$(document).ready(function () {
    var base_url = $("#base_url").val();
    $("#vacation_record_table").DataTable({
        destroy: true,
        "ajax": base_url + "employee/Employees/get_vacation_time_list",
        "order": [],
    });

    $(".emp_status_option select").select2();
});

$("button#vacation_save").unbind().click(function (e) {
    e.preventDefault();
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var vacation_date = $("input[name='vacation_date']").val();
    var vacation_hours = $("input[name='vacation_hours']").val();
    var vacation_description = $('textarea#vacation_description').val();
    var employee_id = $("input[name='employee_id']").val();
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/create_vacation_time";

    if (vacation_date == "" || vacation_hours == "") {
        alert("Required fields are empty");
    } else {
        $.ajax({
            type: "POST",
            url: req_url,
            cache: false,
            data: {
                'vacation_date': vacation_date,
                'vacation_hours': vacation_hours,
                'vacation_description': vacation_description,
                'employee_id': employee_id,
            },
            success: function (response) {
                $("#vacationadd").modal('hide');
                if (response == 1){
                    $("#vacation_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Failed");
                }
            }
        });
    }
});

function edit_vacation_ajax(vacation_id) {
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/get_edit_vacation";
    $.ajax({
        type: "POST",
        url: req_url,
        cache: false,
        dataType: "json",
        data: {
            'vacation_id': vacation_id,
        },
        success: function (response) {
            $("input[name='vacation_date_update']").val(response.vacation_date);
            $("input[name='vacation_hours_update']").val(response.vacation_hours);
            $("textarea#vacation_description_update").val(response.vacation_description);
            $('#vacationupdate').modal({
                backdrop: "static",
                keyboard: false,
            });

            $("button#vacation_update").unbind().click(function (e) {
                $(".pace-inactive").css("display", "block");
                $(".se-pre-con").css("display", "block");
                e.preventDefault();
                var base_url = $("#base_url").val();
                var req_url = base_url + "employee/Employees/update_vacation_time";

                var vacation_date = $("input[name='vacation_date_update']").val();
                var vacation_hours = $("input[name='vacation_hours_update']").val();
                var vacation_description = $('textarea#vacation_description_update').val();
                var employee_id = $("input[name='employee_id_update']").val();

                if (vacation_date == "" || vacation_hours == "") {
                    alert("Required fields are empty");
                } else {
                    $.ajax({
                        type: "POST",
                        url: req_url,
                        cache: false,
                        data: {
                            'vacation_id': vacation_id,
                            'vacation_date': vacation_date,
                            'vacation_hours': vacation_hours,
                            'vacation_description': vacation_description,
                            'employee_id': employee_id,
                        },
                        success: function (response) {
                            $("#vacationupdate").modal('hide');
                            if (response == 1){
                                $("#vacation_record_table").DataTable().ajax.reload();
                                $(".pace-inactive").css("display", "none");
                                $(".se-pre-con").css("display", "none");
                            }else{
                                alert("Failed");
                            }
                        }
                    });
                }
            });
        }
    });
}

function delete_vacation_ajax(vacation_id) {
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var vacationId = vacation_id;
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/delete_vacation_time";
    if (confirm('Are you sure?') == true) {
        $.ajax({
            type: "POST",
            url: req_url,
            dataType: "json",
            cache: false,
            data: {
                'vacation_id': vacationId,
            },
            success: function (response) {

                if (response==1){
                    $("#vacation_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Deletion failed");
                }
            }
        });
    }
}

//Personal Time JS

$(document).ready(function () {
    var base_url = $("#base_url").val();
    $("#personal_record_table").DataTable({
        destroy: true,
        "ajax": base_url + "employee/Employees/get_personal_time_list",
        "order": [],
    });
});

$("button#personal_save").unbind().click(function (e) {
    e.preventDefault();
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var personal_date = $("input[name='personal_date']").val();
    var personal_hours = $("input[name='personal_hours']").val();
    var personal_description = $('textarea#personal_description').val();
    var employee_id = $("input[name='employee_id']").val();
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/create_personal_time";

    if (personal_date == "" || personal_hours == "") {
        alert("Required fields are empty");
    } else {
        $.ajax({
            type: "POST",
            url: req_url,
            cache: false,
            data: {
                'personal_date': personal_date,
                'personal_hours': personal_hours,
                'personal_description': personal_description,
                'employee_id': employee_id,
            },
            success: function (response) {
                $("#personaladd").modal('hide');
                if (response == 1){
                    $("#personal_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Failed");
                }
            }
        });
    }
});

function edit_personal_ajax(personal_id) {
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/get_edit_personal";
    $.ajax({
        type: "POST",
        url: req_url,
        cache: false,
        dataType: "json",
        data: {
            'personal_id': personal_id,
        },
        success: function (response) {
            $("input[name='personal_date_update']").val(response.personal_date);
            $("input[name='personal_hours_update']").val(response.personal_hours);
            $("textarea#personal_description_update").val(response.personal_description);
            $('#personalupdate').modal({
                backdrop: "static",
                keyboard: false,
            });

            $("button#personal_update").unbind().click(function (e) {
                $(".pace-inactive").css("display", "block");
                $(".se-pre-con").css("display", "block");
                e.preventDefault();
                var base_url = $("#base_url").val();
                var req_url = base_url + "employee/Employees/update_personal_time";

                var personal_date = $("input[name='personal_date_update']").val();
                var personal_hours = $("input[name='personal_hours_update']").val();
                var personal_description = $('textarea#personal_description_update').val();
                var employee_id = $("input[name='employee_id_update']").val();

                if (personal_date == "" || personal_hours == "") {
                    alert("Required fields are empty");
                } else {
                    $.ajax({
                        type: "POST",
                        url: req_url,
                        cache: false,
                        data: {
                            'personal_id': personal_id,
                            'personal_date': personal_date,
                            'personal_hours': personal_hours,
                            'personal_description': personal_description,
                            'employee_id': employee_id,
                        },
                        success: function (response) {
                            $("#personalupdate").modal('hide');
                            if (response == 1){
                                $("#personal_record_table").DataTable().ajax.reload();
                                $(".pace-inactive").css("display", "none");
                                $(".se-pre-con").css("display", "none");
                            }else{
                                alert("Failed");
                            }
                        }
                    });
                }
            });
        }
    });
}

function delete_personal_ajax(personal_id) {
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var personalId = personal_id;
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/delete_personal_time";
    if (confirm('Are you sure?') == true) {
        $.ajax({
            type: "POST",
            url: req_url,
            dataType: "json",
            cache: false,
            data: {
                'personal_id': personalId,
            },
            success: function (response) {

                if (response==1){
                    $("#personal_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Deletion failed");
                }
            }
        });
    }
}

//Disciplinary Action JS

$(document).ready(function () {
    var base_url = $("#base_url").val();
    $("#disciplinary_record_table").DataTable({
        destroy: true,
        "ajax": base_url + "employee/Employees/get_disciplinary_action_list",
        "order": [],
    });
});

$("button#disciplinary_save").unbind().click(function (e) {
    e.preventDefault();
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var disciplinary_date = $("input[name='disciplinary_date']").val();
    var disciplinary_comments = $('textarea#disciplinary_comments').val();
    var employee_id = $("input[name='employee_id']").val();
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/create_disciplinary_action";

    if (disciplinary_date == "" || disciplinary_comments == "") {
        alert("Required fields are empty");
    } else {
        $.ajax({
            type: "POST",
            url: req_url,
            cache: false,
            data: {
                'disciplinary_date': disciplinary_date,
                'disciplinary_comments': disciplinary_comments,
                'employee_id': employee_id,
            },
            success: function (response) {
                $("#disciplinaryadd").modal('hide');
                if (response == 1){
                    $("#disciplinary_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Failed");
                }
            }
        });
    }
});

function edit_disciplinary_ajax(da_id) {
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/get_edit_disciplinary";
    $.ajax({
        type: "POST",
        url: req_url,
        cache: false,
        dataType: "json",
        data: {
            'da_id': da_id,
        },
        success: function (response) {
            $("input[name='disciplinary_date_update']").val(response.disciplinary_date);
            $("textarea#disciplinary_comments_update").val(response.disciplinary_comments);
            $('#disciplinaryupdate').modal({
                backdrop: "static",
                keyboard: false,
            });

            $("button#disciplinary_update").unbind().click(function (e) {
                $(".pace-inactive").css("display", "block");
                $(".se-pre-con").css("display", "block");
                e.preventDefault();
                var base_url = $("#base_url").val();
                var req_url = base_url + "employee/Employees/update_disciplinary_time";

                var disciplinary_date = $("input[name='disciplinary_date_update']").val();
                var disciplinary_comments = $('textarea#disciplinary_comments_update').val();
                var employee_id = $("input[name='employee_id_update']").val();

                if (disciplinary_date == "" || disciplinary_comments == "") {
                    alert("Required fields are empty");
                } else {
                    $.ajax({
                        type: "POST",
                        url: req_url,
                        cache: false,
                        data: {
                            'da_id': da_id,
                            'disciplinary_date': disciplinary_date,
                            'disciplinary_comments': disciplinary_comments,
                            'employee_id': employee_id,
                        },
                        success: function (response) {
                            $("#disciplinaryupdate").modal('hide');
                            if (response == 1){
                                $("#disciplinary_record_table").DataTable().ajax.reload();
                                $(".pace-inactive").css("display", "none");
                                $(".se-pre-con").css("display", "none");
                            }else{
                                alert("Failed");
                            }
                        }
                    });
                }
            });
        }
    });
}

function delete_disciplinary_ajax(da_id) {
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var daId = da_id;
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/delete_disciplinary_action";
    if (confirm('Are you sure?') == true) {
        $.ajax({
            type: "POST",
            url: req_url,
            dataType: "json",
            cache: false,
            data: {
                'da_id': daId,
            },
            success: function (response) {

                if (response==1){
                    $("#disciplinary_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Deletion failed");
                }
            }
        });
    }
}

//Wage Time JS

$(document).ready(function () {
    var base_url = $("#base_url").val();
    $("#wage_record_table").DataTable({
        destroy: true,
        "ajax": base_url + "employee/Employees/get_wage_history_list",
        "order": [],
    });
});

$("button#wage_save").unbind().click(function (e) {
    e.preventDefault();
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var wage_job_title = $("input[name='wage_job_title']").val();
    var wage_name = $("input[name='wage_name']").val();
    var wage_type = $("input[name='wage_type']").val();
    var wage_band = $("input[name='wage_band']").val();
    var wage_step = $("input[name='wage_step']").val();
    var wage_start_date = $("input[name='wage_start_date']").val();
    var wage_end_date = $("input[name='wage_end_date']").val();
    var wage_department = $("input[name='wage_department']").val();
    var wage_manager = $("input[name='wage_manager']").val();
    var wage_comments = $('textarea#wage_comments').val();
    var employee_id = $("input[name='employee_id']").val();
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/create_wage_history";

    if (wage_job_title == "" || wage_type == "" || wage_department == "" || wage_manager == "") {
        alert("Required fields are empty");
    } else {
        $.ajax({
            type: "POST",
            url: req_url,
            cache: false,
            data: {
                'wage_job_title': wage_job_title,
                'wage_name': wage_name,
                'wage_type': wage_type,
                'wage_band': wage_band,
                'wage_step': wage_step,
                'wage_start_date': wage_start_date,
                'wage_end_date': wage_end_date,
                'wage_department': wage_department,
                'wage_manager': wage_manager,
                'wage_comments': wage_comments,
                'employee_id': employee_id,
            },
            success: function (response) {
                $("#wageadd").modal('hide');
                if (response == 1){
                    $("#wage_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Failed");
                }
            }
        });
    }
});

function edit_wage_ajax(wage_id) {
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/get_edit_wage";
    $.ajax({
        type: "POST",
        url: req_url,
        cache: false,
        dataType: "json",
        data: {
            'wage_id': wage_id,
        },
        success: function (response) {
            $("input[name='wage_job_title_update']").val(response.wage_job_title);
            $("input[name='wage_name_update']").val(response.wage_name);
            $("input[name='wage_type_update']").val(response.wage_type);
            $("input[name='wage_band_update']").val(response.wage_band);
            $("input[name='wage_step_update']").val(response.wage_step);
            $("input[name='wage_start_date_update']").val(response.wage_start_date);
            $("input[name='wage_end_date_update']").val(response.wage_end_date);
            $("input[name='wage_department_update']").val(response.wage_department);
            $("input[name='wage_manager_update']").val(response.wage_manager);
            $("textarea#wage_comments_update").val(response.wage_comments);
            $('#wageupdate').modal({
                backdrop: "static",
                keyboard: false,
            });

            $("button#wage_update").unbind().click(function (e) {
                $(".pace-inactive").css("display", "block");
                $(".se-pre-con").css("display", "block");
                e.preventDefault();
                var base_url = $("#base_url").val();
                var req_url = base_url + "employee/Employees/update_wage_history";

                var wage_job_title = $("input[name='wage_job_title_update']").val();
                var wage_name = $("input[name='wage_name_update']").val();
                var wage_type = $("input[name='wage_type_update']").val();
                var wage_band = $("input[name='wage_band_update']").val();
                var wage_step = $("input[name='wage_step_update']").val();
                var wage_start_date = $("input[name='wage_start_date_update']").val();
                var wage_end_date = $("input[name='wage_end_date_update']").val();
                var wage_department = $("input[name='wage_department_update']").val();
                var wage_manager = $("input[name='wage_manager_update']").val();
                var wage_comments = $('textarea#wage_comments_update').val();
                var employee_id = $("input[name='employee_id_update']").val();

                if (wage_job_title == "" || wage_type == "" || wage_department == "" || wage_manager == "") {
                    alert("Required fields are empty");
                } else {
                    $.ajax({
                        type: "POST",
                        url: req_url,
                        cache: false,
                        data: {
                            'wage_id': wage_id,
                            'wage_job_title': wage_job_title,
                            'wage_name': wage_name,
                            'wage_type': wage_type,
                            'wage_band': wage_band,
                            'wage_step': wage_step,
                            'wage_start_date': wage_start_date,
                            'wage_end_date': wage_end_date,
                            'wage_department': wage_department,
                            'wage_manager': wage_manager,
                            'wage_comments': wage_comments,
                            'employee_id': employee_id,
                        },
                        success: function (response) {
                            $("#wageupdate").modal('hide');
                            if (response == 1){
                                $("#wage_record_table").DataTable().ajax.reload();
                                $(".pace-inactive").css("display", "none");
                                $(".se-pre-con").css("display", "none");
                            }else{
                                alert("Failed");
                            }
                        }
                    });
                }
            });
        }
    });
}

function delete_wage_ajax(wage_id) {
    $(".pace-inactive").css("display", "block");
    $(".se-pre-con").css("display", "block");
    var wageId = wage_id;
    var base_url = $("#base_url").val();
    var req_url = base_url + "employee/Employees/delete_wage_history";
    if (confirm('Are you sure?') == true) {
        $.ajax({
            type: "POST",
            url: req_url,
            dataType: "json",
            cache: false,
            data: {
                'wage_id': wageId,
            },
            success: function (response) {

                if (response==1){
                    $("#wage_record_table").DataTable().ajax.reload();
                    $(".pace-inactive").css("display", "none");
                    $(".se-pre-con").css("display", "none");
                }else{
                    alert("Deletion failed");
                }
            }
        });
    }
}