<?php require_once 'includes/main.php'; ?>
<?php require_once 'elements/head.php'; ?>
<?php 

$id = (int)$_GET['id'];

$sql = "SELECT * FROM listings WHERE id = '{$id}' LIMIT 1";
if(fetch_rows($sql) < 1){

   $_SESSION['messages'][] = "Listing could not be found";
   header("Location: ".URL);
   exit;

}else{

   $x = fetch_records($sql);

}

?>

<!-- META: TITLE -->
<title><?php echo $x['name']; ?> - <?php echo $set['website_name']; ?></title>

<!-- META: OTHER -->
<meta name="description" content="<?php echo $x['description']; ?>" />
<meta name="keywords" content="listing" />
<!-- SEO -->

<!-- META: GRAPH -->
<meta property="og:url"                content="<?php echo URL; ?>/listing/<?php echo $id; ?>" />
<meta property="og:type"               content="website" />
<meta property="og:title"              content="<?php echo $x['name']; ?> - <?php echo $set['website_name']; ?>" />
<meta property="og:description"        content="<?php echo $set['listing_description']; ?>" />

</head>

<body>

<?php include_once 'elements/header.php'; ?>

<!-- CONTENT -->
<section class="Content">

   <h2 class="GLOB-Title"><?php echo $x['name']; ?> <span class="FR">#<?php echo fetch_rows("SELECT id FROM listings WHERE points >= {$x['points']}"); ?></span></h2>
   <h3 class="GLOB-SubTitle">Points: <?php echo comma_number($x['points']); ?> <span class="FR"><?php echo timestamp2day($x['date_created']); ?></span></h3>
   <?php if(isset($user_id) && $user_id == $x['user_id']){ ?>
   <h3 class="GLOB-SubTitle"><a class="B" href="edit-listing/<?php echo $id; ?>">Edit Listing</a> <span class="FR"><a class="B" href="add-points/<?php echo $id; ?>">Add Points</a></span></h3>
   <?php }else{ ?>
   <?php $next = fetch_record("SELECT id FROM listings WHERE id = (SELECT min(id) FROM listings WHERE id > $id)"); ?>
   <?php $prev = fetch_record("SELECT id FROM listings WHERE id = (SELECT max(id) FROM listings WHERE id < $id)"); ?>
   <h3 class="GLOB-SubTitle">Clicks: <?php echo comma_number($x['clicks']); ?> <a class="FR B" target="_blank" href="<?php echo $x['url']; ?>">Visit Website</a></h3>
   <h3 class="GLOB-SubTitle"><?php if(!empty($prev)){ ?><a class="FL B" href="listing/<?php echo $prev; ?>">Previous Listing</a><?php } ?> <?php if(!empty($next)){ ?><a class="FR B" href="listing/<?php echo $next; ?>">Next Listing</a><?php } ?></h3>
   <?php } ?>

   <!-- LISTING -->
   <article class="Listing">
      <div class="Video"></div>
      <div class="Article RenderHTML">
         <?php echo $x['about']; ?>
      </div>
   </article>
   <!-- LISTING -->

   <section class="Stats">
      <div class="header">Pageviews: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM pageviews WHERE page = '{$uri}'")); ?></span></div>
      <div class="stat">Last 24 Hours: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM pageviews WHERE page = '{$uri}' AND date_created > '{$period['day']}'")); ?></span></div>
      <div class="stat">Last 7 Days: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM pageviews WHERE page = '{$uri}' AND date_created > '{$period['week']}'")); ?></span></div>
      <div class="stat">Last 30 Days: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM pageviews WHERE page = '{$uri}' AND date_created > '{$period['month']}'")); ?></span></div>
   </section>
   <section class="Stats">
      <div class="header">Clicks: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM clicks WHERE listing_id = '{$id}'")); ?></span></div>
      <div class="stat">Last 24 Hours: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM clicks WHERE listing_id = '{$id}' AND date_created > '{$period['day']}'")); ?></span></div>
      <div class="stat">Last 7 Days: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM clicks WHERE listing_id = '{$id}' AND date_created > '{$period['week']}'")); ?></span></div>
      <div class="stat">Last 30 Days: <span class="FR"><?php echo comma_number(fetch_record("SELECT COUNT(id) FROM clicks WHERE listing_id = '{$id}' AND date_created > '{$period['month']}'")); ?></span></div>
   </section>

</section>
<!-- CONTENT -->

<?php include_once 'elements/sidebar/main.php'; ?>

<?php include_once 'elements/footer.php'; ?>

</body>
</html>
