<?php
/**
 * @var $this \yii\web\View
 * @var $model \app\models\News
 */

use yii\helpers\ArrayHelper;
use app\modules\ldjson\jsonLD;
use yii\web\UrlManager;
use app\models\Authors;

jsonLD::addAmicORG();
jsonLD::news( $model->title, $model->lid, $model->image->url, $model->dt_pub, $model->getUrl());
$this->title = $model->title;
$this->params['breadcrumbs'] = [
    [
        "text"=>"НОВОСТИ",
        "url"=>"/news"
    ]
];
$this->params['context'] = "post-view";
$topic = ArrayHelper::getValue($model->topics,0);
if($topic instanceof \app\models\base\NewsTopic){
    $this->params['breadcrumbs'][] =
        [
            "text"=>$topic->title,
            "url"=>mb_strtoupper($topic->url)
        ];
}
$i = 1;
foreach($this->params['breadcrumbs'] as $el){
	jsonLD::addBreadcrumb($i, $el['url'], $el['text']);
	$i++;
}
if( $model->show_author == 'Y' )
{
	$author = Authors::getAuthor( $model->author );
	$authorName = $author->name;
	jsonLD::author( $authorName );
}else{
	$authorName = 'ИА Амител';
	jsonLD::author( $authorName, false );
}
$GLOBALS['type_long'] = ($model->type == 6);

?>
<div class="row">
    <div class="<?=($model->type == 2 || $model->type == 6)?'col-md-12':'col-md-8'?>">
        <div id="post" class="post">
<?
	if( Yii::$app->user->can('admin') ){
?>
<span style="position: absolute;background-color: #a9a9a924;right: 30px;top: 10px; box-shadow: 2px 5px 5px 3px #888;"><img onclick="location='/manager/news/update?id=<?=$model['id']?>'" src="/img/edit.svg" width="25px" height="25px" style="padding:3px;cursor:pointer;"><img onclick="navigator.clipboard.writeText('https://www.amic.ru/news/<?=$model['id']?>')" src="/img/share-boxed.svg" width="25px" height="25px" style="padding:3px;margin-top: 2px;cursor:pointer;"></span>
<?
	}
?>
            <?=$this->render("/layouts/breadcrumbs")?>
            <h1><?=$model->title?></h1>
            <p class="lead"><?=$model->lid?></p>
            <p class="published_at"><?=$model->publishedAt?> <?=date("Y, H:m")?>, <?=$authorName?></p>
            <p class="share_and_comment">
                <a href="#comments" class="comments_btn">Комментировать</a>
                <a onclick="Share.vkontakte('URL','TITLE','IMG_PATH','DESC')">
                    <svg class="icon icon-vk"><use xlink:href="/svg/symbol/sprite-clear.svg#vk"></use></svg>
                </a>
                <a onclick="Share.odnoklassniki('URL','DESC')">
                    <svg class="icon icon-ok"><use xlink:href="/svg/symbol/sprite-clear.svg#ok"></use></svg>
                </a>
                <a onclick="Share.twitter('URL','TITLE')">
                    <svg class="icon icon-tw"><use xlink:href="/svg/symbol/sprite-clear.svg#tw"></use></svg>
                </a>
            </p>
            <p>
                <?php if($model->photo_include == 'Y' && $model->image instanceof \app\models\base\Image):?>
                    <div class="picture-cont-16x9">
                        <picture class="w-100">
                            <source srcset="<?=$model->image->url?>" media="(min-width: 600px)">
                            <img src="<?=$model->image->url?>" alt="<?=$model->photo_title?>" class="w-100">
                        </picture>
                    </div>

                    <div class="image-title"><?=$model->photo_title?></div>
                <?php endif;?>
            </p>
            <section class="text">
                <?=$model->renderBody()?>
                 <?=($model->inscription == 1)?'<span class="advertisement">На правах рекламы</span>':''?>
                 <?=($model->inscription == 2)?'<span class="med_advertisement">Имеются противопоказания, необходима консультация специалиста</span>':''?>
            </section>
            <section class="comments" id="comments">
                <?=$this->render("view/comments/list",["model"=>$model])?>
            </section>
        </div>
    </div>
    <script>
        (function (){
            let main_view_cont = document.getElementById('post');
            let height = main_view_cont.offsetHeight;
            console.log(height)
            let style = document.createElement("style");
            style.innerHTML = "#lenta-container{ max-height:"+height+"px;}";
            main_view_cont.parentNode.append(style);
        })()
    </script>
    <div class="<?=($model->type == 2 || $model->type == 6)?'hidden':'col-md-4'?>" id="lenta-container">
        <?=$this->render("/layouts/lenta/lenta")?>
    </div>
    <div class="col-md-12" id="infinity_newsfeed">
        <?=$this->render("view/infinity_newsfeed",['model'=>$model])?>
    </div>
    <div id="main-carousel" class="splide"></div>
</div>

<?php
    $this->registerJsFile("/js/splide/dist/js/splide.min.js");
    $this->registerCssFile("/js/splide/dist/css/splide.min.css")
?>
<script>
    (function (){
        var gallery = document.getElementById("gallery-0");
        if(gallery===null) return true;
        var wrapper = document.createElement("div");
        wrapper.classList.add('gallery-wrapper')
        gallery.parentNode.insertBefore(wrapper,gallery)
        wrapper.append(gallery)
        var gallery_items = gallery.getElementsByTagName("img");
        var track = document.createElement("div");
        var list = document.createElement('ul');
        gallery.classList.add("splide")
        list.classList.add("splide__list")
        track.classList.add("splide__track")
        gallery.prepend(track)
        track.prepend(list)
        for(var i =0; i<gallery_items.length;i++){
            var img = gallery_items[i];
            var img_cont = document.createElement('li')
            img_cont.classList.add("splide__slide")
            img_cont.append(img)
            list.append(img_cont)
        }
        var main_view = document.createElement("div")
        main_view.classList.add("splide","gallery-view");
        main_view.innerHTML=gallery.innerHTML;
        gallery.style.paddingBottom = "18px";
        gallery.style.marginBottom = 0;
        wrapper.prepend(main_view)
        document.addEventListener( 'DOMContentLoaded', function () {
            var main = new Splide( '.gallery-view', {
                type      : 'fade',
                rewind    : true,
                pagination: false,
                arrows    : true,
                speed: 1000
            } );
            var thumbnails =new Splide( '.gallery', {
                fixedWidth: 100,
                fixedHeight: 60,
                gap       : 10,
                rewind    : true,
                pagination: false,
                isNavigation: true,
                arrows    : false,
            } );

            main.sync( thumbnails );
            main.mount();
            thumbnails.mount();

        } );
    })()
</script>
<script>
    Share = {
        vkontakte: function(purl, ptitle, pimg, text) {
            url  = 'https://vkontakte.ru/share.php?';
            url += 'url='          + encodeURIComponent(purl);
            url += '&title='       + encodeURIComponent(ptitle);
            url += '&description=' + encodeURIComponent(text);
            url += '&image='       + encodeURIComponent(pimg);
            url += '&noparse=true';
            Share.popup(url);
        },
        odnoklassniki: function(purl, text) {
            url  = 'https://www.odnoklassniki.ru/dk?st.cmd=addShare&st.s=1';
            url += '&st.comments=' + encodeURIComponent(text);
            url += '&st._surl='    + encodeURIComponent(purl);
            Share.popup(url);
        },
        facebook: function(purl, ptitle, pimg, text) {
            url  = 'http://www.facebook.com/sharer.php?s=100';
            url += '&p[title]='     + encodeURIComponent(ptitle);
            url += '&p[summary]='   + encodeURIComponent(text);
            url += '&p[url]='       + encodeURIComponent(purl);
            url += '&p[images][0]=' + encodeURIComponent(pimg);
            Share.popup(url);
        },
        twitter: function(purl, ptitle) {
            url  = 'https://twitter.com/share?';
            url += 'text='      + encodeURIComponent(ptitle);
            url += '&url='      + encodeURIComponent(purl);
            url += '&counturl=' + encodeURIComponent(purl);
            Share.popup(url);
        },
        mailru: function(purl, ptitle, pimg, text) {
            url  = 'http://connect.mail.ru/share?';
            url += 'url='          + encodeURIComponent(purl);
            url += '&title='       + encodeURIComponent(ptitle);
            url += '&description=' + encodeURIComponent(text);
            url += '&imageurl='    + encodeURIComponent(pimg);
            Share.popup(url)
        },

        popup: function(url) {
            window.open(url,'','toolbar=0,status=0,width=626,height=436');
        }
    };
</script>
