<?
  $nologsessionstart=1; // log after bot checks..
  $protectedpage = true;
  require "includes/sessionhandler.php";

  // if we came here, log the entrytag when we are allowed to log
  if ($delayedsessionstartlogging)
  {
    // A newguid request and the order parameter? Scraping - do not log
    if (($newguid || isset($_SESSION['newguid'])) && $_GET['order'])
    {
      // NOP
    }
    else
    {
      $entrytag = $_SESSION['entrytag'] ?? null;
      log_session_start($entrytag);
    }
  }
  
  require "includes/markups.php";
  
  if (isset($_GET['siteid']) && is_valid($_GET['siteid']))
{
  $siteid = intval($_GET['siteid']);
  $expiretime = 600;
  // Regions
  $query = "select s.siteid, s.title, s.haslogo, s.weight, s.rating, s.rating_w, s.votes, count(ss.siteid) as totalsets, x.cheapest from sites_sets ss join sites s on ss.siteid=s.siteid left join (
  select ss.siteid , SUM(IF(s.price=ss.price, 1, 0)) as cheapest from sites_sets ss join sets_region s on ss.setid=s.setid and s.regionid=".REGION." join sites_sets_shipping sss on ss.setid=sss.setid and ss.siteid=sss.siteid and sss.regionid=".REGION." where ss.availability='A' and s.status='A' group by siteid
) x on s.siteid=x.siteid where availability='A' and s.status in ('A','F','T') and s.siteid=$siteid";
  if (isJP())
  {
    $query = "select s.siteid, s.title, s.haslogo, s.weight, s.rating, s.rating_w, s.votes, count(ss.siteid) as totalsets, x.cheapest from sites_sets ss join sites s on ss.siteid=s.siteid left join (
    select ss.siteid , SUM(IF(s.price=ss.price, 1, 0)) as cheapest from sites_sets ss join sets_region s on ss.setid=s.setid and s.regionid=".REGION." join sites_sets_shipping sss on ss.setid=sss.setid and ss.siteid=sss.siteid and sss.regionid=".REGION." where ss.availability='A' and s.status='A' group by siteid
  ) x on s.siteid=x.siteid where availability='A' and s.siteid=$siteid";
    $expiretime = 10;
  }

  $row = getCachedRow($query, $expiretime); // 10 minutes for global data is enough
  if ($row)
  {
    $title = $row['title'];
    $haslogo = ($row['haslogo'] == 'Y');
    $weight = $row['weight'];
    $totalsets = $row['totalsets'];
    $cheapest = is_null($row['cheapest']) ? t('nosets') : $row['cheapest'];
    $nolocaldelivery = (is_null($row['cheapest']));
    $siterating = $row['rating'];
    $siterating_w = $row['rating_w'];
    $votes = $row['votes'];
    $breadcrumbs = array(
      [
        'url'=>'/sites',
        'name'=>t('sites')
      ]
    );
  }

  // No title ? Redirect to overview
  if (!$title) {
    Header("Location: /" . LOCALE . "/sites?expired&src=$siteid");
    return;
  }
  
  $path= $_GET['path'];
  if ($path == 'sets') 
  {
    $breadcrumbs[] =
      [
        'url'=>'/site/'.$siteid.'/',
        'name'=>$title,
        'image'=>($haslogo ? 'https://cdn.kiobi.com/images/sites/'.$siteid.'.png' : false)
      ];
    $pagetitle = str_replace('{title}', $title, t('{title} monitored toys'));
    $tab = intval($_GET['tab']);
    if (($tab == 6) && ($_SESSION['userid'] > 0)) 
    { 
      $extratitle = ' - '.t('your speelwatch'); 
    }
    else
    {
      if ($tab <= 1 || $tab > 5) { $tab = 1; $extratitle = ' - '.t('Cheapest').' sets'; }
      if ($tab == 2) { $extratitle = ' - 100-110%'; }
      if ($tab == 3) { $extratitle = ' - 110-125%'; }
      if ($tab == 4) { $extratitle = ' - 125-150%'; }
      if ($tab == 5) { $extratitle = ' - 150%+'; }  
    }
    $pagetitle .= $extratitle;
  }
  else if ($siteid && ($weight == 0) && ($path <> 'sets')) // Redirect on weight 0
  {
      Header("Location: /".LOCALE."/site/$siteid/sets/1/".escape(strtolower(t('Cheapest'))));
      return;
  }
  else if ($path == 'reviews')
  {
    $breadcrumbs[] =
      [
        'url'=>'/site/'.$siteid.'/',
        'name'=>$title,
        'image'=>($haslogo ? 'https://cdn.kiobi.com/images/sites/'.$siteid.'.png' : false)
      ];
    $pagetitle = str_replace('{title}', $title, t('{title} ratings and reviews'));
  }
  else // default
  {
    $pagetitle = str_replace('{title}', $title, t('{title} overview'));
  }
  // site handles all for now
  $pushinject = "<img src='/images/view/site/$siteid.gif' width=1 height=1 alt=''>";
  include "includes/header.php";
  include "includes/site.php";
  $scriptinject .= "<script src='https://cdn.kiobi.com/js/tags.js'></script>";
}
else // Overall overview
{
  $pagetitle = t('Monitored Sites');
  include "includes/header.php";
  include "includes/sites.php";  
}
  include "includes/footer.php";
 ?>