<?php 
require_once(__DIR__ . '/include/incl.php');
if (!empty($_GET['s'])) {
    $words = htmlspecialchars($_GET['s']);
    $wordsh = urlencode($_GET['s']);

    head('Поиск: ' . $words);
    if (!empty($_GET['token'])) {
        $token = htmlspecialchars($_GET['token']);
        $query = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=24&q=$wordsh&key=$key&pageToken=$token&type=video";
    } else {
        $query = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=24&q=$wordsh&key=$key&type=video";
    }
    $videos = json_decode(file_get_contents($query), 1);
    $searchlog = "[s]:".$words."\n";
    $ip_file = __DIR__ . "/history.txt";
    $nam = $searchlog;
    $query = file_get_contents($ip_file);
    if(false === strpos($query,$nam)) {
        file_put_contents($ip_file,"$nam\n",FILE_APPEND);
    }
    ?>
    <div class="content-wrapper head-div">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">

                    <div class="content-block">
                        <div class="cb-header">
                            <div class="row">
                                <div class="col-lg-10 col-xs-10">
                                    <ul class="list-inline">
                                        <li>Результаты поиска: "<?= $words ?>"</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <div class="cb-content videolist">
                            <div class="row">
                                <?php foreach($videos['items'] as $video): ?>
                                <div class="col-lg-3 col-sm-6 videoitem">
                                    <div class="b-video">
                                        <div class="v-img">
                                            <a href="/video.php?video=<?= $video['id']['videoId'] ?>"><img width="270" src="<?= $video['snippet']['thumbnails']['medium']['url'] ?>" alt=""></a>
                                        </div>
                                        <div class="v-desc">
                                            <a href="/video.php?video=<?= $video['id']['videoId'] ?>"><?= mb_substr($video['snippet']['title'], 0, 24, 'utf-8') ?></a>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>

                    <div class="v-pagination">
                        <ul class="list-inline">
                            <?php if (!empty($videos['prevPageToken'])): ?>
                            <li><a href="#">
                                <div class="pages"><i class="cv cvicon-cv-previous"></i></div>
                            </a></li>
                            <?php endif; ?>
                            <li><a href="/search.php/?s=<?= $wordsh ?>&token=<?= $videos['nextPageToken'] ?>">
                                <div class="pages"><i class="cv cvicon-cv-next"></i></div>
                            </a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php 
    footer();
}
?>