<?php
    include("header-1.php");
?>
<title>TYTD Archive - Search<?php 
if(isset($_GET['q'])) {
    echo " ".htmlspecialchars($_GET['q']);
}

?></title>
<?php
    include("header-2.php");
?>
<?php 
    include_once 'db.php';
$offset = 0;

$page_first=0;

$search="";

if(isset($_GET["q"]))
{
    $search = $_GET["q"];

  

    if(isset($_GET["page"]))
    {
        $offset = ((int)$_GET["page"])-1;
        $page_first = $offset - ($offset % 3);

        
    }

    $stmt = $pdo->prepare("select * from SavedVideo where Title like '%$search%' LIMIT ".strval($offset*20).", 20");
    $stmt->execute();
    $videos = $stmt->fetchAll();

    foreach($videos as $video)
    {
        $class = "class=\"bg-danger\"";
        if(strlen(video_exists($video["Id"],"PreMuxed")) > 0)
            $class = "";

        echo "<a ".$class." href=\"./watch.php?v=".$video["Id"]."\">".htmlspecialchars($video["Title"])."</a>";
        echo "<br>";
    }

    
}

?>

<nav aria-label="...">
  <ul class="pagination">
    <li class="page-item <?php if($offset == 0) echo "disabled"; ?>">
      <a class="page-link" href="./search.php?q=<?php echo urlencode($search); ?>&page=<?php echo $offset; ?>">Previous</a>
    </li>
    <li class="page-item <?php 
        if(($page_first) == $offset) echo "active";
    ?>"><a class="page-link" href="./search.php?q=<?php echo urlencode($search); ?>&page=<?php echo $page_first+1; ?>"><?php 
        echo $page_first+1;
    ?></a></li>
    <li class="page-item <?php 
        if(($page_first+1) == $offset) echo "active";
    ?>">
      <a class="page-link" href="./search.php?q=<?php echo urlencode($search); ?>&page=<?php echo $page_first+2; ?>"> <span class="sr-only"><?php 
        echo $page_first+2;
    ?></span></a>
    </li>
    <li class="page-item <?php 
        if(($page_first+2) == $offset) echo "active";
    ?>"><a class="page-link" href="./search.php?q=<?php echo urlencode($search); ?>&page=<?php echo $page_first+3; ?>"><?php 
        echo $page_first+3;
    ?></a></li>
    <li class="page-item">
      <a class="page-link" href="./search.php?q=<?php echo urlencode($search); ?>&page=<?php echo $offset+2; ?>">Next</a>
    </li>
  </ul>
</nav>

<?php 
    include("footer.php");
?>