<?php 
require_once(__DIR__ . '/include/incl.php');
head('Главная');
?>
<div class="content-wrapper">
    <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-sm-10 col-xs-10">
                                <ul class="list-inline">
                                    <li><a href="#">Последние запросы.</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="cb-content avatars">
                        <div class="row">
                            <center>
                                <?php 
                                function limit_words($string, $word_limit)
                                {
                                $words = explode(" ", $string);
                                return implode(" ", array_splice($words, 0, $word_limit));
                                }

                                $text = file_get_contents(__DIR__ . "/history.txt", NULL);

                                // $text = str_replace("-", " ", $text);

                                $text = explode('[s]:', $text);

                                foreach($text as & $value)
                                {
                                $value = rtrim($value);
                                }
                                $out = array_slice($text, -25);
                                $out = array_reverse($out);
                                $out = array_unique($out);
                                $latest = ""; foreach($out as $value)   {   $latest.= '<a href="'; $value1 = str_replace(" ", "-", $value);
                                    $value = strtolower($value);    $latest.= "/search.php?s=".urlencode($value);
                                    $value1 = str_replace("-", " ", $value);    $value1 = strtolower($value1);
                                    $value1 = limit_words('' . $value1 . '', 5);    $latest.= "\"><span style='margin-left: 10px;'></span>" . ucwords($value1) . "</a> <font size='5'> </font> ";}
                                ?>
                                <ul class="tags">
                                    <?php
                                    echo "$latest"; echo'</div>';

                                    ?>
                                </ul>
                            </center>
                        </div>
                    </div>
                </div>

                <div class="content-block head-div">
                    <div class="cb-header">
                        <div class="row">
                            <div class="col-lg-10 col-sm-10 col-xs-8">
                                <ul class="list-inline">
                                    <li>Популярное</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="cb-content videolist">
                        <div class="row">
                            <?php 
                                $query = "https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&chart=mostPopular&maxResults=16&regionCode=RU&key=$key";
                                $page = json_decode(file_get_contents($query), 1);
                                foreach ($page['items'] as $row):
                                if ($row['snippet']['title'] != "Deleted video") {
                            ?>
                            <div class="col-lg-3 col-sm-6 videoitem">
                                <div class="b-video">
                                    <div class="v-img">
                                        <a href="/video.php?video=<?= $row['id'] ?>"><img width="270" src="<?= $row['snippet']['thumbnails']['medium']['url'] ?>" alt=""></a>
                                    </div>
                                    <div class="v-desc">
                                        <a title="<?= $row['snippet']['title'] ?>" href="/video.php?video=<?= $row['id'] ?>"><?= mb_substr($row['snippet']['title'], 0, 24, 'utf-8') ?></a>
                                    </div>
                                    <div class="v-views">
                                        <?= $row['statistics']['viewCount'] ?> просмотров.
                                    </div>
                                </div>
                            </div>
                            <?php 
                                }
                                endforeach;
                            ?>
                        </div>
                    </div>
                </div>
                <div class="content-block head-div">
                    <div class="cb-header">
                        <div class="row">
                            <div class="col-lg-10 col-sm-10 col-xs-8">
                                <ul class="list-inline">
                                    <li>Комедии</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="cb-content videolist">
                        <div class="row">
                            <?php 
                                $query = "https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&chart=mostPopular&maxResults=16&regionCode=RU&videoCategoryId=23&key=$key";
                                $page = json_decode(file_get_contents($query), 1);
                                foreach ($page['items'] as $row):
                                    if ($row['snippet']['title'] != "Deleted video") {
                            ?>
                            <div class="col-lg-3 col-sm-6 videoitem">
                                <div class="b-video">
                                    <div class="v-img">
                                        <a href="/video.php?video=<?= $row['id'] ?>"><img width="270" src="<?= $row['snippet']['thumbnails']['medium']['url'] ?>" alt=""></a>
                                    </div>
                                    <div class="v-desc">
                                        <a title="<?= $row['snippet']['title'] ?>" href="/video.php?video=<?= $row['id'] ?>"><?= mb_substr($row['snippet']['title'], 0, 24, 'utf-8') ?></a>
                                    </div>
                                    <div class="v-views">
                                        <?= $row['statistics']['viewCount'] ?> просмотров.
                                    </div>
                                </div>
                            </div>
                            <?php 
                                    }
                                endforeach;
                            ?>
                        </div>
                    </div>
                </div>
                               <div class="content-block head-div">
                    <div class="cb-header">
                        <div class="row">
                            <div class="col-lg-10 col-sm-10 col-xs-8">
                                <ul class="list-inline">
                                    <li>Спорт</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="cb-content videolist">
                        <div class="row">
                            <?php 
                                $query = "https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&chart=mostPopular&maxResults=16&regionCode=RU&videoCategoryId=17&key=$key";
                                $page = json_decode(file_get_contents($query), 1);
                                foreach ($page['items'] as $row):
                                    if ($row['snippet']['title'] != "Deleted video") {
                            ?>
                            <div class="col-lg-3 col-sm-6 videoitem">
                                <div class="b-video">
                                    <div class="v-img">
                                        <a href="/video.php?video=<?= $row['id'] ?>"><img width="270" src="<?= $row['snippet']['thumbnails']['medium']['url'] ?>" alt=""></a>
                                    </div>
                                    <div class="v-desc">
                                        <a title="<?= $row['snippet']['title'] ?>" href="/video.php?video=<?= $row['id'] ?>"><?= mb_substr($row['snippet']['title'], 0, 24, 'utf-8') ?></a>
                                    </div>
                                    <div class="v-views">
                                        <?= $row['statistics']['viewCount'] ?> просмотров.
                                    </div>
                                </div>
                            </div>
                            <?php 
                                    }
                                endforeach;
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php   
footer();
?>