/**
 * Currency Helper for Frontend
 * Provides currency formatting functions for JavaScript
 */

// System currency symbol (loaded from server)
let systemCurrency = 'Rs'; // Default fallback

/**
 * Load currency from server
 */
async function loadCurrency() {
    try {
        const response = await fetch('ajax/get_currency.php');
        const data = await response.json();
        if (data.currency) {
            systemCurrency = data.currency;
        }
    } catch (error) {
        console.error('Error loading currency:', error);
        // Keep default 'Rs'
    }
}

/**
 * Format amount with currency symbol
 * @param {number|string} amount - Amount to format
 * @param {number} decimals - Number of decimal places (default: 2)
 * @returns {string} Formatted amount with currency
 */
function formatCurrency(amount, decimals = 2) {
    const numAmount = parseFloat(amount) || 0;
    return systemCurrency + ' ' + numAmount.toFixed(decimals);
}

/**
 * Format amount without currency symbol (just number formatting)
 * @param {number|string} amount - Amount to format
 * @param {number} decimals - Number of decimal places (default: 2)
 * @returns {string} Formatted amount
 */
function formatAmount(amount, decimals = 2) {
    const numAmount = parseFloat(amount) || 0;
    return numAmount.toFixed(decimals);
}

// Initialize currency on page load
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', loadCurrency);
} else {
    loadCurrency();
}

// Also load immediately if script is loaded after DOM is ready
loadCurrency();

