<?php
/**
 * @var $topic NewsTopic | NULL
 * @var $this \yii\web\View
 * @var $news_query \yii\db\ActiveQuery
 */

use app\models\base\NewsTopic;
use app\models\base\Story;
use app\models\base\Tags;
use yii\widgets\ListView;
use yii\helpers\Url;

$this->params['breadcrumbs'] = [
    [
        "text"=>"НОВОСТИ",
        "url"=>"/news"
    ]
];

$page = \Yii::$app->request->get('page');
$addpage = $page?'?page='.$page:'';
$addtpage = $page?". Страница ($page)":'';

if($topic instanceof NewsTopic || $topic instanceof Story || $topic instanceof Tags){
    $this->params['breadcrumbs'][] =
        [
            "text"=>$topic->title,
            "url"=>$topic->url
        ];

	$addtitle = ( $topic instanceof Tags )?'Тег: ':'';
//	$addtitle = ( $topic instanceof Story )?'Сюэет: ':''; открыть если будут дубли


	Yii::$app->og->canonical = $topic->getUrl(true);
    $this->title = $topic->title;
    Yii::$app->og->title = $addtitle.$topic->title.$addtpage;
    Yii::$app->og->description = $topic->description?:'Лента новостей - '.Yii::$app->og->description.$addtpage;
    Yii::$app->og->og_title = str_replace( '"',"&quot;",$topic->title);
    Yii::$app->og->og_description = str_replace( '"', "&quot;", Yii::$app->og->description );
}else{
    Yii::$app->og->title = 'Лента новостей - '.Yii::$app->og->title.$addtpage;
    Yii::$app->og->description = 'Лента новостей - '.Yii::$app->og->description.$addtpage;
    Yii::$app->og->og_title = str_replace( '"',"&quot;", Yii::$app->og->title );
    Yii::$app->og->og_description = str_replace( '"',"&quot;", Yii::$app->og->description );
	Yii::$app->og->canonical = Url::base('https')."/news".$addpage;
}
//$pages->setPageSize(0,true);

?>
<div class="archive">
    <div class="row">
        <div class="col-md-3">
            <div class="topics-list">
                <div class="topics-continer">
                    <?php
                        echo Yii::$app->cache->getOrSet("archive_rubrics-{$topic->id}",function () use($topic){
                            return \yii\widgets\ListView::widget([
                                "dataProvider" => new \yii\data\ActiveDataProvider([
                                    "query"=>NewsTopic::find()->andWhere(["show"=>"Y","active"=>1,"topic_id"=>null])->orderBy(["order"=>SORT_ASC]), //"topic_id"=>$topic->id next lavel maybe
                                    "pagination" =>[
                                        "pageSize"=>0
                                    ],
                                ]),
                                'options' => [
                                    'tag' => "ul",
                                    'class' => 'list-wrapper',
                                    'id' => 'list-wrapper',
                                ],
                                "itemOptions"=>[
                                    "tag"=>"li"
                                ],
                                "itemView" => function($model) use($topic) {
									if( $model->url == '----'){
										$ret = \yii\helpers\Html::tag('div','',['class' => ['hr']]);
										$ret .= \yii\helpers\Html::tag('span',$model->title,['class' => ['header']]);
										return $ret;
									}
									return \yii\helpers\Html::a($model->title,["news/{$model->url}"],['class'=>$model->id==$topic->id?"active":""]);
								},
                                "layout" => "{items}"
                            ]);
                        },NewsTopic::$keysCache['archive_rubrics-##']);
                    ?>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <div class="news-list">
                <?=$this->render("/layouts/breadcrumbs")?>
                <?=$this->render("archive/filters")?>
                <?php
                $adkey = ($topic instanceof Story)?'Story-':'';
				$adkey =$adkey.($keycahe ?? '');
				$request = Yii::$app->request;
				$ppage = $request->get('page');
				$ppage = ($ppage)?'-'.$ppage:'';
				$daterange = $request->get('daterange');
				$dr = ($daterange)?'-'.$daterange:'';
                echo Yii::$app->cache->getOrSet("archive_news-{$adkey}{$dr}{$topic->id}{$ppage}",function () use($news_query, $pages){
                    return ListView::widget([
                        "dataProvider" => new \yii\data\ActiveDataProvider([
                            "query"=>$news_query,
							"pagination"=>$pages,
							"totalCount"=>$pages->totalCount,
                        ]),
                        'options' => [
                            'tag' => "ul",
                            'class' => 'list-wrapper',
                            'id' => 'list-wrapper',
                        ],
                        "itemOptions"=>[
                            "tag"=>"li"
                        ],
                        "itemView" => "archive/_archive_item",
                        "layout" => "{items}{pager}"
                    ]);
                },NewsTopic::$keysCache['archive_news-##']);
                ?>

            </div>
        </div>
    </div>
</div>


