<script>
    $("#city").select2();

    $('#city_name').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('phone').focus();
        }
    });
    $('#phone').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('city').focus();
        }
    });
$('#city').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('note').focus();
        }
    });

    function clear_fields() {
        $("#add_pump_form")[0].reset();
        $(".error").text('');
        $("#city").select2();
    }

    var pump_table = $("#pump_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('pump_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "name",
                data: "name"
            },
            {
                name: "city",
                data: "city"
            },
            {
                name: "phone",
                data: "phone"
            },
            {
                name: "note",
                data: "note"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_pump_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('pump_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#submit_btn").text('پراسسنگ۔۔۔۔۔');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    pump_table.ajax.reload();
                    clear_fields();
                    alertify.success("Pump Added 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_pump', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('pump_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editPumpData").html(resonpse);
                $("#editPumpModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_city").select2();
                }, 300);
            }
        });
    });
    $(document).on('submit', "#update_pump_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('pump_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#update_btn").text('پراسسنگ۔۔۔۔۔');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    pump_table.ajax.reload();
                    $("#editPumpModal").modal('hide');
                    alertify.success("Pump 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_pump', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('pump_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('کیا آپ کو پمپ کو حذف کرنے کا یقین ہے؟?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        pump_table.ajax.reload();
                        alertify.success("Pump Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
