<script>


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

        $(document).find("#brokerReach_id_hidden").val(id); 
        $(document).find("#vehicle_id_hidden").val(vehicleId);
        $(document).find("#trip_id_hidden").val(tripId);
        $(document).find("#payable_hidden_total_amount").val(amount);
        $(document).find("#amount").val(amount);
    }

    $(document).ready(function () {
    function getVehicleAndParty() {
        var partyId = $('#party').val();
        var partyName = $('#party option:selected').text();
        console.log('Party ID:', partyId, 'Party Name:', partyName);

        $.ajax({
                url: "{{ route('get_broker_reach_amount') }}",
                type: "POST",
                data: {
                    'party_id': partyId,
                },
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                success: function (response) {
                    console.log(response);
                    $(document).find("#broker_advance_dev").html(response);
                    setTimeout(() => {
                        $(document).find("#pump_payalbe_p").select2();

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

    // Trigger function on dropdown change
    $('#party').change(function () {
        getVehicleAndParty();
    });
});

function payment_type_change(type, update = 'no') {


if(type=='in-account'){
    $("#bank_dev").html('');
    var dataId = $(this).find(':selected').data('id');

$.ajax({
        url: "{{ route('get_banks') }}",
        type: "POST",
        headers: {
            "X-CSRF-TOKEN": "{{ csrf_token() }}"
        },
        data: {
            params:'get_workshop',
            req:'payment_vouchers' // Ensure `get_workshop` is a valid variable or replace with actual value
        },
        success: function (data) {
            $("#workshop_div").html(data);
            setTimeout(() => {
                    $(document).find("#select_account").select2({
                        tags: true
                    });
                }, 200);
        }
    });
return false;
}

if (type == 'cash') {
    $("#bank_dev").html('');
    $("#workshop_div").html('');

    $(document).find("#u_bank_dev").html('');
} else {

    $("#workshop_div").html('');
    $.ajax({
        url: "{{ route('get_banks') }}",
        type: "POST",
        headers: {
            "X-CSRF-TOKEN": "{{ csrf_token() }}"
        },
        success: function (data) {
            if (update == 'yes') {
                $(document).find("#u_bank_dev").html(data);
                setTimeout(() => {
                    $(document).find("#all_bank").select2();
                }, 200);
            } else {
                $("#bank_dev").html(data);
                setTimeout(() => {
                    $(document).find("#all_bank").select2({
                        tags: true
                    });
                }, 200);
            }
        }
    });
}
}


$("#add_broker_reach_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('broker_reach_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    alertify.success("Broker Reach Created Successfully!");
                    location.reload();
                    // driver_table.ajax.reload();
                    clear_fields();
                    setTimeout(() => {
                        document.getElementById('date').focus();
                    }, 400);
                } 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]);
                    });
                }
                $('#date').focus();

            }
        });
    });


    var driver_table = $("#driver_table").DataTable({
    processing: true,
    serverSide: true,
    ajax: "{{ route('broker_reach_index') }}",
    columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
        {
            name: "date",
            data: "date"
        },
        {
            name: "sender",
            data: "sender"
        },
        {
            name: "amount_sender",
            data: "amount_sender"
        },
        {
            name: "receiver",
            data: "receiver"
        },
        {
            name: "amount_receiver",
            data: "amount_receiver"
        },
        {
            name: "action",
            data: "action",
            orderable: false,
        },
    ]
});




function clear_fields() {
        $("#add_receipt_form")[0].reset();
        $(".error").text('');
        $("#bank_dev").html('');
        $("#workshop_div").html('');

    }



    /////////////////// edit  ///////////
    // $(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) {
    //             $(document).find("#trip_amount_payable_modal_body").html(resonpse);
    //             $(document).find("#u_received_from_id").select2();
    //         }
    //     });
    // });


    $(document).on('click', '.broker_reach_edit', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('broker_reach_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editBrokerReachData").html(resonpse);
                $("#editBrokerReachModal").modal('toggle');
                setTimeout(() => {
                    $(document).find('#u_date').focus();
                    $(document).find('#u_broker').select2();
                    $(document).find('#all_bank').select2();

                }, 500);
            }
        });
    });

    $(document).on('submit', "#update_broker_reach_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('broker_reach_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');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    location.reload();
                    $("#editVoucherModal").modal('hide');
                    alertify.success("Voucher Updated Successfully!");
                    setTimeout(() => {
                        document.getElementById('date').focus();
                    }, 300);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#update_btn").text('Update');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".u_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
                setTimeout(() => {
                    $(document).find('#u_date').focus();
                    $(document).find('#u_broker').select2();
                    $(document).find('#all_bank').select2();

                }, 500);
            }
        });
    });


    $(document).on('click', '.delete_broker_reach', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('broker_reach_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Broker Reach?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        location.reload();
                        alertify.success("Broker Reach Deleted Successfully!");
                        setTimeout(() => {
                            document.getElementById('date').focus();
                        }, 300);
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });


</script>
