<script>
    function clear_fields() {
        $("#add_driver_form")[0].reset();
        $(".error").text('');
    }

    $('#name').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('cnic').focus();
        }
    });
    $('#cnic').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('mobile_number').focus();
        }
    });
    $('#mobile_number').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('salary').focus();
        }
    });

    $('#salary').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('commission').focus();
        }
    });

    var driver_table = $("#driver_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('employee_index') }}",
        columns: [
            { name: "DT_RowIndex", data: "DT_RowIndex" },
            { name: "name", data: "name" },
            { name: "mobile", data: "mobile" },
            { name: "cnic", data: "cnic" },
            { name: "salary", data: "salary" },
            { name: "commission", data: "commission" },
            { name: "action", data: "action", orderable: false },
        ]
    });

    $("#add_driver_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('employee_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('مخفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    clear_fields();
                    alertify.success("Employee Stored Successfully!");
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#submit_btn").text('مخفوظ');
                $("#submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });

    $(document).on('click', '.edit_emp', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('employee_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                $("#editEmpData").html(resonpse);
                $("#editEmpModal").modal('toggle');
            }
        });
    });

    $(document).on('submit', "#update_emp_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('employee_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#update_btn").text('Processing....');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    $("#editEmpModal").modal('hide');
                    alertify.success("Employee Updated Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".u_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });

    $(document).on('click', '.delete_emp', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('employee_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Employee?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        driver_table.ajax.reload();
                        alertify.success("Employee Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
