<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
let lastPrices = {};

async function fetchGoldInPKR() {
    try {
        // 1️⃣ Fetch gold price per ounce in USD (example free API)
        const goldResponse = await fetch('https://www.metals-api.com/api/latest?access_key=YOUR_API_KEY&base=USD&symbols=XAU');
        const goldData = await goldResponse.json();
        const goldPriceUSD = goldData.rates.XAU; // USD per 1 ounce

        // 2️⃣ Convert ounce to gram: 1 oz = 31.1035 grams
        const goldPriceUSDPerGram = goldPriceUSD / 31.1035;

        // 3️⃣ Fetch USD to PKR exchange rate
        const forexResponse = await fetch('https://open.er-api.com/v6/latest/USD');
        const forexData = await forexResponse.json();
        const usdToPkr = forexData.rates.PKR;

        // 4️⃣ Convert gold price to PKR
        const goldPricePKRPerGram = goldPriceUSDPerGram * usdToPkr;
        const goldPricePKRPerTola = goldPricePKRPerGram * 11.664; // 1 Tola = 11.664 grams

        // 5️⃣ Calculate 22K and 21K
        const gold22Gram = goldPricePKRPerGram * 0.9166;
        const gold22Tola = gold22Gram * 11.664;
        const gold21Gram = goldPricePKRPerGram * 0.875;
        const gold21Tola = gold21Gram * 11.664;

        // 6️⃣ Update DOM with arrows
        updateValue('gold24Gram', goldPricePKRPerGram, 'arrow24Gram');
        updateValue('gold24Tola', goldPricePKRPerTola, 'arrow24Tola');
        updateValue('gold22Gram', gold22Gram, 'arrow22Gram');
        updateValue('gold22Tola', gold22Tola, 'arrow22Tola');
        updateValue('gold21Gram', gold21Gram, 'arrow21Gram');
        updateValue('gold21Tola', gold21Tola, 'arrow21Tola');

        $('#lastUpdate').text(new Date().toLocaleTimeString());

    } catch (err) {
        console.error('Error fetching gold price:', err);
    }
}

function updateValue(id, value, arrowId) {
    const rounded = Math.round(value);
    $('#' + id).text(rounded);

    const last = lastPrices[id] || 0;
    if (rounded > last) {
        $('#' + arrowId).text('▲').attr('class', 'arrow-up');
    } else if (rounded < last) {
        $('#' + arrowId).text('▼').attr('class', 'arrow-down');
    } else {
        $('#' + arrowId).text('');
    }
    lastPrices[id] = rounded;
}

// Initial fetch
fetchGoldInPKR();
setInterval(fetchGoldInPKR, 10000); // update every 10 seconds
</script>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://videos.exandnext.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://videos.exandnext.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://videos.exandnext.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://videos.exandnext.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://videos.exandnext.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
