<script>
    $("#party").select2();
    $('#party').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('amount').focus();
        }
    });
    $('#amount').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('description').focus();
        }
    });
    $('#description').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('submit_btn').focus();
        }
    });

    function is_negative_change(update = 'no') {
        if (update == 'no') {
            if ($("#is_negative").is(":checked")) {
                $("#is_negative").val('yes');
            } else {
                $("#is_negative").val('no');

            }
        }
        if (update == 'yes') {
            if ($(document).find("#u_is_negative").is(":checked")) {
                $(document).find("#u_is_negative").val('yes');
            } else {
                $(document).find("#u_is_negative").val('no');
            }
        }
    }
    function party_change(update = 'no') {
        if (update == 'yes') {
            var party = $(document).find("#u_party").val();
            var table = $(document).find("#u_party").find(":selected").data('table');
            $(document).find("#u_hidden_party_table").val(table);
        }
        if (update == 'no') {
            var party = $("#party").val();
            var table = $("#party").find(":selected").data('table');
            $("#hidden_party_table").val(table);
        }
    }

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

    var balance_table = $("#cities_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('opening_balance_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "date",
                data: "date"
            },
            {
                name: "party",
                data: "party"
            },
            {
                name: "amount",
                data: "amount"
            },

            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_opening_balance_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('opening_balance_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');

                $("#submit_btn").text('پراسسنگ۔۔۔۔۔');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    balance_table.ajax.reload();
                    clear_fields();
                    $("#date").focus();
                    alertify.success("Opening Balance 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]);
                    });
                }
                $("#amount").focus();

            }
        });
    });
    $(document).on('click', '.edit_balance', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('opening_balance_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editBrokerData").html(resonpse);
                $("#editBrokerModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_party").select2();
                    $(document).find("#u_date").focus();
                }, 500);
            }
        });
    });
    $(document).on('submit', "#update_balance_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('opening_balance_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#update_btn").text('پراسسنگ۔۔۔۔۔');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    balance_table.ajax.reload();
                    $("#editBrokerModal").modal('hide');
                    alertify.success("Balance Updated Successfully!");

                } 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_balance', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('opening_balance_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('کیا آپ کو اوپننگ بیلنس کو حذف کرنے کا یقین ہے؟')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        balance_table.ajax.reload();
                        alertify.success("Balance Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
