function formatRupiah(amount) {
    // Convert the amount to a string and round it to the nearest whole number
    let roundedAmount = Math.round(amount);
    let numberString = roundedAmount.toString();

    // Format the integer part with dots as thousand separators
    let formattedInteger = numberString.replace(/\B(?=(\d{3})+(?!\d))/g, ".");

    return formattedInteger;
}

function initializeDinamisSelect2(element, options = {}) {
    const defaultOptions = {
      placeholder: 'Select an option', // Default placeholder
      url: '', // Default URL (should be provided in options)
      idField: 'id', // Default field for the ID
      textField: 'text', // Default field for the display text
      delay: 250, // Default delay for AJAX requests
      onSelect: null, // Callback function for select event
    };

    // Merge default options with user-provided options
    const config = {
      ...defaultOptions,
      ...options
    };

    element.select2({
      placeholder: config.placeholder,
      ajax: {
        url: config.url,
        dataType: 'json',
        delay: config.delay,
        data: function(params) {
          return {
            q: params.term, // Search term
            page: params.page || 1,
          };
        },
        processResults: function(data, params) {
          params.page = params.page || 1;

          return {
            results: data.map(function(item) {
              return {
                id: item[config.idField], // Dynamically use the ID field
                text: item[config.textField], // Dynamically use the text field
                item: item,
              };
            }),
            pagination: {
              more: data.length > 0, // Determine if more results are available
            },
          };
        },
      },
    });

    // Add event listener for select2:select
    element.on('select2:select', function(e) {
      const selectedData = e.params.data;

      // If a callback function is provided, call it with the selected data
      if (typeof config.onSelect === 'function') {
        config.onSelect(selectedData);
      }
    });
  }