<?php

/** @var yii\web\View $this */
/** @var \yii\data\ActiveDataProvider $newsDataProvider */

use app\models\Design;
use app\modules\ldjson\jsonLD;
use app\models\front\News;

jsonLD::addAmicORG();
jsonLD::$ld = jsonLD::$wp;

Yii::$app->og->title = 		 	'Новости Барнаула и Алтайского края — на сайте amic.ru ИА Амител';
Yii::$app->og->og_title = 		Yii::$app->og->title;
Yii::$app->og->canonical =   	'/';
Yii::$app->og->og_description = 'Новости в Барнауле и Алтайском крае: оперативно репортажи с фото и видео. Интересные темы, эксперты, ответы на вопросы, тесты, игры.';
Yii::$app->og->og_url = 		'https://www.amic.ru';
Yii::$app->og->og_type = 		'website';
Yii::$app->og->og_image =		'https://www.amic.ru/img/amic-logo.svg';

$auto_news = Yii::$app->cache->get('main_auto_news');

?>
<div class="site-index">
    <div class="row">
        <div class="col-md-8 " id="main-view-container">
<?
	$keyadd = News::keyFilter();
    if ($this->beginCache('main-page-main-view-glob'.$keyadd, ['duration' => News::$keysCache['main-page-main-view-glob']])) {
		if( $auto_news || $keyadd){
			echo $this->render("index/main_view_auto",[]);
		}else{
			echo $this->render("index/main_view",[]);
		}
		$this->endCache();
	}
?>
        </div>
        <script>
            (function (){
                let main_view_cont = document.getElementById('main-view-container');
                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="col-md-4" id="lenta-wrapper">
            <div id="lenta-container">
                <?=$this->render("/layouts/lenta/lenta")?>
            </div>
        </div>
    </div>
    <?//$this->render("index/colorful_widget",['design'=>new Design(['id'=>1]),'newsDataProvider'=>new \yii\data\ActiveDataProvider(["query"=>News::find()])])?>

    <?
    if ($this->beginCache('MainPageBlock_' . ((Yii::$app->deviceDetect->isMobile())?"mobile":"desktop"), ['duration' => News::$keysCache['MainPageBlock']])) { //News::$keysCache['MainPageBlock']
		if( $auto_news ){
			$q = News::find()->andWhere(["(flags & 1)"=>0])->andWhere(['<>', 'id', News::getNH()->select('id')->limit(1)])->andWhere(['or',['NH'=>'Y'],['top'=>'Y']])->limit(3)->offset(3);
		}else{
			$q = \app\models\Kd::find()->andWhere(['>','order',2])->orderBy(["order"=>SORT_ASC])->limit(3);
		}
        $newsDataProvider = new \yii\data\ActiveDataProvider(
            [
                "query"=>$q,
                "pagination" => false
            ]
        );
        $sort = $newsDataProvider->getModels();
        $ActiveNews = new \yii\data\ActiveDataProvider();
        $models = [];
        foreach( $sort as $item ){
            $models[] = \app\models\News::findOne($item->id);
        }
        $ActiveNews->setModels($models);

?>
    <?=$this->render("index/three_widgets",['newsDataProvider'=>$ActiveNews])?>

    <?php
	$i = 0;
	$design  = Design::find()->andWhere(['active'=>'Y'])->orderBy(['order'=>SORT_ASC])->all();

    foreach ($design as $item) {
        $i++;
        if($i==3) echo $this->render('@app/views/_etc/banners/firstDesktopTransparant');
        if($i==6 && Yii::$app->deviceDetect->isMobile()) echo $this->render('@app/views/_etc/banners/bannerMobile3');
        if($i==6 && !Yii::$app->deviceDetect->isMobile()) echo $this->render('@app/views/_etc/banners/bannerDesktop3');
        /**
         * @var $item Design
         */
        switch ($item->design)
        {
            case Design::TOPIC_WIDGET:
                $pageSize = 10;
                break;
            case Design::IMAGINE_NEWS_WIDGET:
                $pageSize = 3;
                break;
            default:
                $pageSize = 4;
        }
        switch ($item->type)
        {
            case "topic":
                $newsDataProvider = new \yii\data\ActiveDataProvider(
                    [
                        "query"=>News::find()->joinWith("topics t")->andWhere(["t.id"=>[$item->parent_id], "(flags & 1)"=>0]),
                        "pagination" =>[
                            "pageSize"=>$pageSize
                        ],
                    ]
                );
                $topic = \app\models\base\NewsTopic::findOne(['id'=>$item->parent_id]);
                break;
            case "tag":
                $newsDataProvider = new \yii\data\ActiveDataProvider(
                    [
                        "query"=>News::find()->joinWith("tags t")->andWhere(["t.id"=>[$item->parent_id], "(flags & 1)"=>0]),
                        "pagination" =>[
                            "pageSize"=>$pageSize
                        ],
                    ]
                );
                $topic = \app\models\base\Tags::findOne(['id'=>$item->parent_id]);
                break;
            default:
                $newsDataProvider = new \yii\data\ActiveDataProvider(
                    [
                        "query"=>News::find()->joinWith("stories t")->andWhere(["t.id"=>[$item->parent_id], "(flags & 1)"=>0]),
                        "pagination" =>[
                            "pageSize"=>$pageSize
                        ],
                    ]
                );
                $topic = \app\models\base\Story::findOne(['id'=>$item->parent_id]);
                break;
        }

        $view = \yii\helpers\ArrayHelper::getValue(Design::$id_to_view_map,$item->design,Design::$id_to_view_map[Design::IMAGINE_NEWS_WIDGET]);

        echo $this->render($view,['newsDataProvider'=>$newsDataProvider,"topic"=>$topic,"design" => $item]);
    }
//    if(!Yii::$app->deviceDetect->isMobile()) echo $this->render('@app/views/_etc/banners/desktopfirstPage');
	$this->endCache();
	}
    ?>

</div>
