<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="//maps.google.com/maps/api/js?key=AIzaSyDa_d1tGwiwmST9lBBWRwWSdGiuECKiE4s&libraries=places%2Cdrawing&language&ver=6.2.1"></script>

<script>
  var fbReviews = 0;
  var fbNumReviews = 0;
  // Loop through all the tables on the page
  const tables = document.querySelectorAll('.woocommerce-product-attributes');
  tables.forEach(table => {
    const placeId = table.querySelector('.woocommerce-product-attributes-item--attribute_place-id .woocommerce-product-attributes-item__value p').textContent;
    /*const fbId = table.querySelector('.woocommerce-product-attributes-item--attribute_facebook-id .woocommerce-product-attributes-item__value p').textContent;*/
    fbReviews = 0;
    fbNumReviews = 0;
	getFacebookReviews('flyttama', table)
    /*getPlaceIdContent(placeId, table);*/
  });
  function getFacebookReviews(pageId, table) {
    const accessToken = 'EAARrQzUxt8gBAAZB1QsT5g80neUZA77MZCgBHyKpz96SEvuceZBaW3ZCXE0V0hRkW8Na3AZA4mUhHg29Gw49ejyOfBSj67SSYVTi3ISnKnrGs84U4muVikhIv8ZAC7ylQqRTUdobZCkPg4zjg7IeHwYvIOvVY1FlaneTKarTFf4bSqwHnO32Fu74fSFOFNfcZB994E70xyLkRbLCptDwzyCbmFujKXi5spjUZD';
    const apiVersion = 'v13.0';
    const endpoint = `/${pageId}/ratings?access_token=${accessToken}&fields=rating,review_text`;

    fetch(`https://graph.facebook.com/${apiVersion}${endpoint}`)
      .then(response => response.json())
      .then(data => {
        fbReviews = data.data;
        fbNumReviews = reviews.length;
		
        console.log('Facebook Reviews:', fbReviews);
        console.log('Total Facebook Reviews:', fbNumReviews);
		const ratingContainer = table.querySelector('.woocommerce-product-attributes-item--attribute_recensioner .woocommerce-product-attributes-item__value p');
        ratingContainer.innerHTML = `
          <div style="float:left;margin-right:10px;line-height:25px;padding:9px 4px;width:46px;height:46px;border:3px solid orange;border-radius:50%;color:orange;font-weight:bold;font-size:21px;">${(Math.round(fbReviews * 10) / 10).toFixed(1)}</div> 
          <div style="line-height:19px;text-align:left;font-size: 24px;color: #FFC107;">${getStars(fbReviews)}<br/>
            <img style="margin-right:5px;float:left;height:18px;" src="https://www.xn--jmfrflytt-v2a4r.se/wp-content/uploads/2017/11/google-1.png"> <span style="font-size: 18px;color: #444;">${fbNumReviews} recensioner</span></div>
        `;
      })
      .catch(error => {
        console.error('Error retrieving Facebook reviews:', error);
      });
  }

  function getStars(rating) {
    const starTotal = 5;
    const roundedRating = Math.round(rating * 2) / 2;
    const stars = [];
    for (let i = 0; i < starTotal; i++) {
      if (i < Math.floor(roundedRating)) {
        stars.push('<i class="fa fa-star"></i>');
      } else if (i === Math.floor(roundedRating) && roundedRating % 1 !== 0) {
        stars.push('<i class="fa fa-star-half-o"></i>');
      } else {
        stars.push('<i class="fa fa-star-o"></i>');
      }
    }
    return stars.join('');
  }
</script>