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

    var cities_table = $("#cities_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('bank_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "name",
                data: "name"
            }, {
                name: "account_number",
                data: "account_number"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_city_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('bank_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    cities_table.ajax.reload();
                    clear_fields();
                    alertify.success("Bank 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_bank', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('bank_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editCityData").html(resonpse);
                $("#editCityModal").modal('show');
                setTimeout(() => {
                    $(document).find("#u_name").focus();
                }, 700);
            }
        });
    });
    $(document).on('submit', "#update_city_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('bank_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) {
                    cities_table.ajax.reload();
                    $("#editCityModal").modal('hide');

                    alertify.success("Bank Updated Successfully!");
                    $('#name').focus();
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.delete_bank', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('bank_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Bank?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        cities_table.ajax.reload();
                        $('#name').focus();

                        alertify.success("Bank Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
