<script>

    function p_pump_payable_change(update = 'no') {
        var amount = $(document).find("#pump_payalbe_p :selected").data('amount');
        var brokerId = $(document).find("#pump_payalbe_p :selected").data('broker-id');

        $(document).find("#broker_id_hidden").val(brokerId);
        $(document).find("#payable_hidden_total_amount").val(amount);
        $(document).find("#amount").val(amount);
        console.log(amount);
    }

    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_payable() {
        var trip_id = $(document).find("#pay_trip_id").val();
        var party = $(document).find("#payable_party").val();
        var table = $(document).find("#payable_party :selected").data('table');

        // console.log(party);

            $.ajax({
                url: "{{ route('get_pump_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_payable_dev").html(response);
                    setTimeout(() => {
                        $(document).find("#pump_payalbe_p").select2();

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


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

        show_payable_modal(trip_id, vehicle_id);
    });

    function show_payable_modal(trip_id, vehicle_id) {
        var url = "{{ route('trip_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) {
                $(document).find("#addTripAdvanceData").html('');
                $(document).find("#addTripAdvanceData").html(response);
                $(document).find("#addTripAdvanceModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#payable_party").select2();
                    $(document).find("#payable_date").focus();
                }, 500);
            }
        });
    }

    $(document).on('submit', "#add_TripPayables_Form", function (e) {
        // console.log(e);
        e.preventDefault();
        $.ajax({
            url: "{{ route('trip_payable_store_j') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(document).find(".p_error").text('');
                $(document).find("#tripPayableSubmit").text('Processing....');
                $(document).find("#tripPayableSubmit").prop('disabled', true);
            },
            success: function (data) {
                $(document).find(".p_error").text('');
                $(document).find("#tripPayableSubmit").text('Pay');
                $(document).find("#tripPayableSubmit").prop('disabled', false);
                if (data.success) {
                    // $(document).find("#addTripAdvanceModal").modal('toggle');
                    $(document).find("#trip_payables_modal_table").html(data.view);
                    alertify.success("Trip Payable Stored Successfully!");
                    location.reload();
                    setTimeout(() => {
                        $(document).find("#payable_date").focus();
                    }, 500);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                console.log(error);
                $(document).find("#tripPayableSubmit").text('Pay');
                $(document).find("#tripPayableSubmit").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("#payable_date").focus();
                }, 500);
            }
        });
    });
    $(document).on('submit', "#updateTripPayableForm", function (e) {
        e.preventDefault();
        console.log('trip udate CONSOLE');
        $.ajax({
            url: "{{ route('trip_payable_advance_update')}}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(document).find(".r_error").text('');
                $(document).find("#advancePayableUpdate").text('Processing....');
                $(document).find("#advancePayableUpdate").prop('disabled', true);
            },
            success: function (data) {
                console.log(data);
                $(document).find(".r_error").text('');
                $(document).find("#advancePayableUpdate").text('اپڈیٹ');
                $(document).find("#advancePayableUpdate").prop('disabled', false);
                if (data.success) {
                    show_payable_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) {
                console.log("Error Details:", error);
                console.log("Response:", error.responseJSON);

                $(document).find("#advancePayableUpdate").text('اپڈیٹ');
                $(document).find("#advancePayableUpdate").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_payable', function () {
        var edit_id = $(this).data('id');
        console.log(edit_id);
        var url = "{{ route('trip_payable_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $(document).find("#trip_amount_payable_modal_body").html(resonpse);
                $(document).find("#u_received_from_id").select2();
            }
        });
    });
    $(document).on('click', '.delete_trip_payable', function () {
        var del_id = $(this).data('id');
        console.log(del_id);
        var url = "{{ route('trip_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_payable_modal(response.trip_id);
                        location.reload();
                        alertify.success("Trip Payable Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
