<script>
    function cancel_update_form(trip_id) {
        show_received_modal(trip_id)

    }

    $(document).on('change', '#received_from_id', function () {
        var table = $(this).find(":selected").data('table');
        $(document).find('#received_party_table').val(table);
    });
    $(document).on('change', '#u_received_from_id', function () {
        var table = $(this).find(":selected").data('table');
        $(document).find('#u_received_party_table').val(table);
    });

    function receiving_amount_change(update) {
        if (update == 'no') {
            var table = $(document).find("#adv_broker").find(':selected').data('table');
            $(document).find("#received_party_table").val(table);
        } else {
            var table = $(document).find("#u_adv_broker").find(':selected').data('table');
            $(document).find("#u_received_party_table").val(table);
        }
    }

    function receiving_payment_change(value,update='no') {
        if (value == 'cash') {
            $(document).find("#adv_bank_dev").html('');
            $(document).find("#u_adv_bank_dev").html('');
        } else {
            console.log({
                'type': value,
                'update_value': update
            });
            $.ajax({
                url: "{{ route('receive_account_payment_type_change') }}",
                type: "POST",
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                data: {
                    'type': value,
                    'update_value': update
                },
                success: function (resonpse) {
                    // console.log(resonpse);
                    if (update == 'no') {
                        $(document).find("#adv_bank_dev").html(resonpse);
                    } else {
                        $(document).find("#u_adv_bank_dev").html(resonpse);

                    }
                    setTimeout(() => {
                        $(document).find("#adv_bank").select2();
                        $(document).find("#adv_broker").select2();
                        $(document).find("#u_adv_broker").select2();
                        $(document).find("#u_adv_bank").select2();
                    }, 500);
                }
            });
        }
        }

        function show_received_modal(trip_id) {
            var url = "{{ route('receive_account_index') }}";
            // url = url.replace(':id', trip_id);
            $.ajax({
                url: url,
                type: "POST",
                data: {
                    trip_id: trip_id
                },
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                success: function (resonpse) {
                    // console.log(resonpse);
                    $("#addTripAdvanceData").html(resonpse);
                    $("#addTripAdvanceModal").modal('show');
                    setTimeout(() => {
                        $(document).find('#received_date').focus();
                        $(document).find('#received_from_id').select2();
                    }, 500);
                }
            });
        }

        $(document).on('click', '.show_received_account_modal', function () {
            var trip_id = $(this).data('id');
            show_received_modal(trip_id);
        });
        $(document).on('submit', "#addReceivevAccountFormRandomId", function (e) {
            console.log('id');
            e.preventDefault();
            $.ajax({
                url: "{{ route('receive_account_store') }}",
                type: "POST",
                contentType: false,
                processData: false,
                data: new FormData(this),
                beforeSend: function () {
                    $(document).find(".r_error").text('');
                    $(document).find("#receivedAcSubmit").text('Processing....');
                    $(document).find("#receivedAcSubmit").prop('disabled', true);
                },
                success: function (data) {
                    $(document).find(".r_error").text('');
                    $(document).find("#receivedAcSubmit").text('محفوظ');
                    $(document).find("#receivedAcSubmit").prop('disabled', false);
                    if (data.success) {
                        $(document).find("#received_accounts_table").html(data.view);
                        alertify.success("Amount Received To Trip Successfully!");
                        setTimeout(() => {
                            $(document).find('#received_from_id').select2();
                        }, 500);
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                },
                error: function (error) {
                    $(document).find("#receivedAcSubmit").text('محفوظ');
                    $(document).find("#receivedAcSubmit").prop('disabled', false);
                    if ('errors' in error.responseJSON) {
                        $(document).find(".r_error").text('');
                        $.each(error.responseJSON.errors, (index, value) => {
                            $(document).find("#" + index + "_error").text(value[0]);
                        });
                    }
                    setTimeout(() => {
                        $(document).find('#received_amount').focus();
                    }, 450);
                }
            });
        });
        $(document).on('submit', "#updateReceivevAccountForm", function (e) {
            e.preventDefault();
            $.ajax({
                url: "{{ route('receive_account_update') }}",
                type: "POST",
                contentType: false,
                processData: false,
                data: new FormData(this),
                beforeSend: function () {
                    $(document).find(".r_error").text('');
                    $(document).find("#receivedAcUpdate").text('Processing....');
                    $(document).find("#receivedAcUpdate").prop('disabled', true);
                },
                success: function (data) {
                    $(document).find(".r_error").text('');
                    $(document).find("#receivedAcUpdate").text('اپڈیٹ');
                    $(document).find("#receivedAcUpdate").prop('disabled', false);
                    if (data.success) {
                        show_received_modal(data.trip_id);
                        alertify.success("Amount Received Updated Successfully!");
                        setTimeout(() => {
                            $(document).find('#u_received_from_id').select2();
                        }, 500);
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                },
                error: function (error) {
                    $(document).find("#receivedAcUpdate").text('اپڈیٹ');
                    $(document).find("#receivedAcUpdate").prop('disabled', false);
                    if ('errors' in error.responseJSON) {
                        $(document).find(".r_error").text('');
                        $.each(error.responseJSON.errors, (index, value) => {
                            $(document).find("#u_" + index + "_error").text(value[0]);
                        });
                    }
                    setTimeout(() => {
                        $(document).find('#u_received_amount').focus();
                    }, 450);
                }
            });
        });
        $(document).on('click', '.edit_trip_receivings', function () {
            var edit_id = $(this).data('id');
            console.log(edit_id);
            var url = "{{ route('receive_account_edit', ':id') }}";
            url = url.replace(':id', edit_id);
            $.ajax({
                url: url,
                type: "GET",
                success: function (resonpse) {
                    // console.log(resonpse);
                    $(document).find("#trip_amount_receiving_modal_body").html(resonpse);
                    $(document).find("#u_received_from_id").select2();
                }
            });
        });
        $(document).on('click', '.delete_trip_receivings', function () {
            var del_id = $(this).data('id');
            console.log(del_id);
            var url = "{{ route('receive_account_delete', ':id') }}";
            url = url.replace(':id', del_id);
            if (confirm('Are You Sure To Delete The Trip Receiving?')) {
                $.ajax({
                    url: url,
                    type: "GET",
                    success: function (response) {
                        if (response.success) {
                            show_received_modal(response.trip_id);
                            alertify.success("Trip Receiving Deleted Successfully!");
                        } else {
                            alertify.error("Something Went Wrong!");
                        }
                    }
                });
            } else {
                return false;
            }
        });

</script>
