<?php
    require_once( __DIR__ . '/include/incl.php' );
    if (!empty($_GET['c_id'])) {
    $id = htmlspecialchars($_GET['c_id']);
    $query = "https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=$id&maxResults=40&key=$key";
    $channel = json_decode(file_get_contents($query), 1);
    head($channel['items'][0]['snippet']['title']);
?>
<div class="content-wrapper">
    <div class="container">
        <div class="row">
            <div class="col-lg-12">

                <div class="channel-details">
                    <div class="row">
                        <div class="col-lg-10 col-lg-offset-2 col-xs-12">
                            <div class="c-details">
                                <div class="c-avatar">
                                    <div style="width: 100px; float: left; height: 100px; background: url(<?= $channel['items'][0]['snippet']['thumbnails']['high']['url'] ?>); border-radius: 50%; background-size: cover" alt="">
                                </div>
                                <div class="c-name">
                                    <h1 style="float: right;"><?= $channel['items'][0]['snippet']['title'] ?></h1>
                                </div>
                                <div class="clearfix"></div>
                                <div class="c-sub pull-right">
                                    <div class="c-sub-wrap">
                                        <div style="
                                        float: left;
                                        width: 86px;
                                        padding: 5px 10px 0px 10px;
                                        border: solid 1px #ea2c5a;
                                        border-top-left-radius: 3px;
                                        border-bottom-left-radius: 3px;
                                        background-color: #ea2c5a;
                                        color: #ffffff;
                                        ">
                                            Фанатов
                                        </div>
                                        <div style="
                                            float: left;
                                            width: 92px;
                                            padding: 5px 10px 0px 10px;
                                            border: solid 1px #eceff0;
                                            border-left: none;
                                            border-top-right-radius: 3px;
                                            border-bottom-right-radius: 3px;
                                            color: #7e7e7e;
                                        ">
                                            <?= $channel['items'][0]['statistics']['subscriberCount'] ?>
                                        </div>
                                        <div class="clearfix"></div>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="content-block">
                    <div class="cb-header">
                        <div class="row">
                            <div class="col-lg-8 col-xs-12 col-sm-6">
                                <div class="btn-group bg-clean">
                                    Видео канала:
                                </div>
                                <div class="clearfix"></div>
                            </div>
                        </div>
                    </div>
                    <div class="cb-content videolist">
                        <div class="row">
                            <?php 
                            if (!empty($_GET['token'])) {
                                $tok = htmlspecialchars($_GET['token']);
                                $query = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=$id&maxResults=40&type=video&key=$key&pageToken=$tok";
                            } else {
                                 $query = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=$id&maxResults=40&type=video&key=$key";
                            }
                            $videos = json_decode(file_get_contents($query), 1);
                            foreach ($videos['items'] as $video):
                            ?>
                            <div class=" col-lg-3 col-xs-6 videoitem">
                                <div class="b-video">
                                    <div class="v-img">
                                        <a href="/video.php?video=<?= $video['id']['videoId'] ?>" title="<?= $video['snippet']['title'] ?>"><img width="270px" src="<?= $video['snippet']['thumbnails']['high']['url'] ?>" alt=""></a>
                                    </div>
                                    <div class="v-desc">
                                        <a href="/video.php?video=<?= $video['id']['videoId'] ?>" title="<?= $video['snippet']['title'] ?>"><?= mb_substr($video['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="v-pagination">
                    <ul class="list-inline">
                        <?php if (isset($videos['prevPageToken'])) { ?>
                        <li><a href="/channel.php?c_id=<?= $id ?>&token=<?= $videos['prevPageToken'] ?>">
                            <div class="pages"><i class="cv cvicon-cv-previous"></i></div>
                        </a></li>
                        <?php } ?>
                        <?php if (isset($videos['nextPageToken'])) { ?>
                        <li><a href="/channel.php?c_id=<?= $id ?>&token=<?= $videos['nextPageToken'] ?>">
                            <div class="pages"><i class="cv cvicon-cv-next"></i></div>
                        </a></li>
                        <?php } ?>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</div>
<?php 
footer();
}
?>