<script>

function p_pump_reach_payable_change(update = 'no') {
    var selected = $(document).find("#pump_reach_payalbe_p :selected");

    var amount = selected.data('amount');
    var brokerId = selected.data('broker-id');
    var tripId = selected.data('trip-id');
    var tripDetailId = selected.data('trip-detail-id'); // یہ field بھی dropdown میں ہونا چاہیے
    var tripReachId = selected.data('trip-reach-id');   // یہ بھی dropdown میں ہونا چاہیے

    $(document).find("#broker_payReach_id_hidden").val(brokerId);
    $(document).find("#pay_trip_id_hidden").val(tripId);
    $(document).find("#reach_payable_hidden_total_amount").val(amount);
    $(document).find("#amount").val(amount);

    $("#tripReach_id_hidden").val(tripReachId);
    $("#trip_detail_id").val(tripDetailId);
}


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


        }

    }

    function adv_payable_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('advance_payable_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 adv_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('advance_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 party_change_for_reach_payable() {
        var trip_id = $(document).find("#pay_trip_id").val();
        var party = $(document).find("#reach_payable_party").val();
        var table = $(document).find("#reach_payable_party :selected").data('table');

        // console.log(party);

            $.ajax({
                url: "{{ route('get_pump_reach_payable_amount') }}",
                type: "POST",
                data: {
                    'party': table,
                    "broker_id": party,
                    "trip_id": trip_id,
                },
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                success: function (response) {
                    // console.log(response);
                    $(document).find("#pump_reach_payable_dev").html(response);
                    setTimeout(() => {
                        $(document).find("#pump_reach_payalbe_p").select2();

                    }, 500);
                }
            });
            $(document).find("#payable_party_type").val(table);
 }


    $(document).on('click', '.show_reach_payable_account_modal', function () {
        var trip_id = $(this).data('reach-id');
        var vehicle_id = $(this).data('vehicle-id');
        show_reach_payable_modal(trip_id, vehicle_id);
    });

    function show_reach_payable_modal(trip_id, vehicle_id) {
        var url = "{{ route('trip_reach_payable_index', ':id') }}";
        url = url.replace(':id', trip_id);
        $.ajax({
            url: url,
            type: "POST",
            data: {
                trip_id: trip_id,
                vehicle_id : vehicle_id
            },
            headers: {
                "X-CSRF-TOKEN": "{{ csrf_token() }}"
            },
            success: function (response) {
                // console.log(response);
                $(document).find("#addTripReachData").html('');
                $(document).find("#addTripReachData").html(response);
                $(document).find("#addTripReachModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#payable_party").select2();
                    $(document).find("#payable_date").focus();
                }, 500);

            }
        });
    }

    $(document).on('submit', "#add_reach_TripPayables_Form", function (e) {
        // console.log(e);
        e.preventDefault();
        $.ajax({
            url: "{{ route('trip_reach_payable_store_j') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(document).find(".p_error").text('');
                $(document).find("#tripReachPayableSubmit").text('Processing....');
                $(document).find("#tripReachPayableSubmit").prop('disabled', true);
            },
            success: function (data) {
                $(document).find(".p_error").text('');
                $(document).find("#tripReachPayableSubmit").text('Pay');
                $(document).find("#tripReachPayableSubmit").prop('disabled', false);
                if (data.success) {
                    // $(document).find("#addTripReachModal").modal('toggle');
                    alertify.success("Trip Reach Stored Successfully!");
                    location.reload();
                    $(document).find("#trip_reach_payables_modal_table").html(data.view);

                    setTimeout(() => {
                        $(document).find("#reach_payable_date").focus();
                    }, 500);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                console.log(error);
                $(document).find("#tripReachPayableSubmit").text('Pay');
                $(document).find("#tripReachPayableSubmit").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(document).find(".p_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $(document).find("#p_" + index + "_error").text(value[0]);
                    });
                }
                setTimeout(() => {
                    $(document).find("#reach_payable_date").focus();
                }, 500);
            }
        });
    });
    $(document).on('submit', "#updateReachTripPayableForm", function (e) {
        e.preventDefault();
        console.log('update');
        $.ajax({
            url: "{{ route('trip_reach_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) {
                console.log(data);
                $(document).find(".r_error").text('');
                $(document).find("#receivedAcUpdate").text('اپڈیٹ');
                $(document).find("#receivedAcUpdate").prop('disabled', false);
                if (data.success) {
                    alertify.success("Amount Received Updated Successfully!");
                    location.reload();
                    // show_reach_payable_modal(data.trip_id);
                    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_reach_trip_payable', function () {
        var edit_id = $(this).data('id');
        console.log(edit_id);
        var url = "{{ route('trip_reach_payable_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $(document).find("#trip_amount_reach_payable_modal_body").html(resonpse);
                $(document).find("#u_received_from_id").select2();
            }
        });
    });
    $(document).on('click', '.delete_reach_trip_payable', function () {
        var del_id = $(this).data('id');
        console.log(del_id);
        var url = "{{ route('trip_reach_payable_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Trip Payable?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        // show_reach_payable_modal(response.trip_id);
                        location.reload();
                        alertify.success("Trip Payable Deleted Successfully!");

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