<script>
    $(document).find("#employee").select2();
    $(document).on('change', "#employee", function (e) {
        var table = $(this).find(":selected").data('table');
        //    console.log(table);
        $("#user_type").val(table);
    });
    $(document).on('change', "#u_employee", function (e) {
        var table = $(this).find(":selected").data('table');
        //    console.log(table);
        $("#u_user_type").val(table);
    });
    $(document).on('keypress', '.change_focus', function (e) {
        var next_id = $(this).data('id');
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById(next_id).focus();
        }
    });

    function clear_fields() {
        $("#add_salary_form")[0].reset();
        $(".error").text('');
        $(document).find("#employee").select2();
    }

    function get_driver_vehicle(update = 'no') {
        if (update == 'yes') {
            var employee_selector = $(document).find("#u_employee");
            var vehicle_dev_selector = $(document).find("#u_vehicle_dev");
            var vehicle_selector = $(document).find("#u_vehicle");

        } else {
            var employee_selector = $(document).find("#employee");
            var vehicle_dev_selector = $(document).find("#vehicle_dev");
            var vehicle_selector = $(document).find("#vehicle");


        }
        var employee = employee_selector.val();
        if (employee != '') {
            var table = employee_selector.find(":selected").data('table');
            vehicle_dev_selector.html('');
            var url = "{{ route("get_vehicles_for_salary") }}";
            if (table == 'driver') {
                $.ajax({
                    url: url,
                    type: "POST",
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}",
                    },
                    data: {update: update},
                    success: function (data) {
                        vehicle_dev_selector.html(data.view);
                        setTimeout(() => {
                            $(document).find("#vehicle").select2();
                            $(document).find("#u_vehicle").select2();
                        }, 600);
                    }
                });
            }
        } else {
            vehicle_dev_selector.html('');
        }
    }

    var salary_table = $("#salary_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('salary_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "type",
                data: "type"
            },
            {
                name: "employee",
                data: "employee"
            },
            {
                name: "date",
                data: "date"
            },
            {
                name: "amount",
                data: "amount"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_salary_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('salary_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) {
                    clear_fields();
                    $('#date').focus();

                    alertify.success("Salary Voucher Created Successfully!");
                    salary_table.ajax.reload();
                } 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]);
                    });
                }
                $('#date').focus();

            }
        });
    });
    $(document).on('click', '.edit_voucher', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('salary_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                $("#editVoucherData").html(resonpse);
                $("#editVoucherModal").modal('toggle');
                setTimeout(() => {
                    document.getElementById('u_date').focus();
                    $(document).find('#u_employee').select2();
                    $(document).find('#u_vehicle').select2();
                }, 500);
            }
        });
    });
    $(document).on('submit', "#update_voucher_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('salary_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) {
                    salary_table.ajax.reload();
                    $("#editVoucherModal").modal('hide');
                    alertify.success("Voucher Updated Successfully!");
                    $('#date').focus();

                } 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]);
                    });
                }
                setTimeout(() => {
                    $(document).find('#u_date').focus();
                }, 400);
            }
        });
    });
    $(document).on('click', '.delete_voucher', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('salary_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Salary?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        salary_table.ajax.reload();
                        alertify.success("Salary Voucher Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                    setTimeout(() => {
                        $(document).find('#date').focus();
                    }, 400);
                }
            });
        } else {
            return false;
        }
    });
</script>
