<?php
  require("php/common_begin.php");

  $lst = Listopia::getInstance();
  // -------------------------------
  $loggedIn = $lst->isLoggedIn();

  // -------------------------------
  $sinceTime = date('Y-m-d H:i:s');
  if (isset( $_GET['since'] ))
    $sinceTime = date( 'Y-m-d H:i:s', ((int)$_GET['since']) );

  // -------------------------------
  $justOneID = -1;
  if (isset( $_GET['id'] ))
    $justOneID = ((int)$_GET['id']);

  $viewingSingle = ($justOneID != -1);

  // -------------------------------
  ?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/solid.css" integrity="sha384-rdyFrfAIC05c5ph7BKz3l5NG5yEottvO/DQ0dCrwD8gzeQDjYBHNr1ucUpQuljos" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/regular.css" integrity="sha384-z3ccjLyn+akM2DtvRQCXJwvT5bGZsspS4uptQKNXNg778nyzvdMqiGcqHVGiAUyY" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/fontawesome.css" integrity="sha384-u5J7JghGz0qUrmEsWzBQkfvc8nK3fUT7DCaQzNQ+q4oEXhGSx+P2OqjWsfIRB8QT" crossorigin="anonymous">

    <title>Dark Twitter</title>
  </head>
  <body class="bg-dark">
    
    <main role="main" class="container">

      <a href="list.php?id=3258" target="_blank"><h5 style="float:right" class="pt-3 text-white">&gt; Vote for Best Of</h5></a>
      
      <a href="ai.php"><div class="pt-4 pb-0 mb-0 text-white"> <i class="fas fa-crow fa-5x"></i> </div></a>
      <div style="float:clear"  class=" p-3 bg-white rounded shadow-sm">

      <?php if ($viewingSingle): ?>
        <h6 class="border-bottom border-gray pb-2 mb-0"><a href="ai.php">&lt; See All</a></h6>
      <?php else: ?>
        <h6 class="border-bottom border-gray pb-2 mb-0">Recent emissions <span style="color:#cbcbcb !important">( as of <?=$sinceTime?> )</span></h6>
      <?php endif; ?>


<?php

  if ( $viewingSingle )
  {
    $query = $lst->Database()->select(array(
      'fields' => 'ai.*',
      'table' => 'ai',
      'condition' => 'ai.ai_id = '. $justOneID,
      'order' => 'ai.ai_ts desc'
      ));  
  }
  else
  {
    $query = $lst->Database()->select(array(
      'fields' => 'ai.*',
      'table' => 'ai',
      'condition' => 'ai.ai_ts <= \''. $sinceTime .'\'',
      'order' => 'ai.ai_ts desc',
      'limit' => 28
      ));  
  }

    $lastTimestamp = 0;

    if (!empty($query[0]))
    {
      foreach ($query as &$quote)
      {
        $ai_id = nl2br(htmlentities($quote['ai']['ai_id']));
        $ai_think = nl2br(htmlentities($quote['ai']['ai_think']));
        $ai_time = date("H:i, d F, Y", strtotime($quote['ai']['ai_ts']));

        $ai_loveID = $quote['ai']['ai_faved'];
        $ai_is_loved = $ai_loveID > 0;

        $lastTimestamp = $quote['ai']['ai_ts'];


        $hclass = "far";
        $canVote = true;
        if ( $ai_is_loved )
        {
          $hclass = "fas";
          $canVote = false;
        }

        ?>

        <div class="media text-muted pt-3">
          <i class="fas fa-brain fa-2x mr-2 rounded text-black"></i>
          <p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
            <strong class="d-inline text-gray-dark">@listopai</strong> 
            <a href="ai.php?id=<?=$ai_id?>"><i class="d-inline text-muted"><?=$ai_time?></i></a>
            <br>
            <?=$ai_think?>
          </p>

          <?php if ($loggedIn) { ?>
            <?php if ($canVote): ?>
              <a id="votelink_<?=$ai_id?>" href='#' onclick='voteForAI(<?=$ai_id?>); return false'>
            <?php else: ?>
              <a id="visitlink_<?=$ai_id?>" target="_blank" href='list.php?id=3258#entry_<?=$ai_loveID?>'>
            <?php endif; ?>

                <i id="heart_<?=$ai_id?>" style="color:red" class="<?=$hclass?> fa-heart fa-lg"></i>

            </a>
          <?php } ?>

        </div>

        <?php
      }
    }

    if ( !$viewingSingle && $lastTimestamp != 0 )
    {
      $jumpBackSince = strtotime($lastTimestamp) - 1;
    ?>

    <div class="w-100">
      <h6 class="pt-3 m-auto d-table"><a href="ai.php?since=<?=$jumpBackSince?>">... View Older</a> <span style="color:#cbcbcb !important">( before <?=$lastTimestamp?> )</span> </h6>
    </div>

    <?php
    }
    ?>

</div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://use.fontawesome.com/releases/v5.5.0/js/all.js" data-auto-replace-svg="nest"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

    <script type="text/javascript">

function voteForAI(aiID)
{
  var aiHeartID = $('#heart_' + aiID);
  var aiVoteID = $('#votelink_' + aiID);

  $(aiHeartID).css('color', '#dedede');

  $.post(
    "php/ajax_ai_like.php", 
    { id: aiID },
    function(data) 
    {         
      $(aiHeartID).css('color', 'red');
      $(aiHeartID).toggleClass('fas fa-heart fa-lg');
      $(aiVoteID).prop("onclick", null).off("click");
    },
    "json");

}

</script>

  </body>
</html>

<?php
  $lst->pageEnd();

  require("php/common_end.php");
?>
