<?php
use app\models\Authors;

$this->title="Новости";

//тест (заменить на новостьи дня и по календарику)
/*

$newsDataProvider = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\News::getMainView()->orderBy(["dt_pub"=>SORT_DESC])->limit(50),
        "pagination" =>[
            "pageSize"=>50
        ],
    ]
);
*/

	$b_date = date("Y-m-d 00:00:00",time()-60*60*24*5);
	$e_date = date("3000-m-d 23:59:59");
	$tnow = date("Y-m-d H:i:s");

//\app\models\base\News::find()->andwhere(['between', 'dt_pub',  $b_date , $e_date])->orwhere(['active'=>'N'])->orderBy(["dt_pub"=>SORT_DESC])->limit(200)
//echo "SELECT * FROM `news` WHERE (`dt_pub` between '$b_date' and '$e_date' and `active`='N') or (`dt_pub` > '$tnow' and `active`='Y')"; exit;
$newsDataProvider = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\base\News::findBySql("SELECT * FROM `news` FORCE INDEX(`calendar`) WHERE (`dt_pub` between '$b_date' and '$e_date' and `active`='N') or (`dt_pub` > '$tnow' and `active`='Y') order by `dt_pub` DESC"),
            "pagination" =>[
				"pageSize"=>200
        ],
    ]
);

$cache = Yii::$app->cache;


/**** ****/
?>


<div class="table-responsive-sm custom-range-badge">
<table class="table table-hover table-Light">
  <thead class="bg-gray table-sm">
    <tr>
      <th scope="col" class="col-md-1 col-sm-2 col-xs-5">время</th>
      <th scope="col" class="col-md-1 col-sm-2 col-xs-5">фото</th>
      <th scope="col">заголовок</th>
      <th scope="col">статусы</th>
      <th scope="col">управление</th>
    </tr>
  </thead>
  <tbody>
<?
	foreach( $newsDataProvider->getModels() as $item ){
//		print_r($item->getTopics());
		$strTopics = '';
		foreach( $item->getTopics()->primaryModel->topics as $topic ){
			$strTopics = $strTopics." [".$topic->title."]";
		}

		$strbage = '';
		if( $item->verifed == 'Y' ) $strbage .= '<span class="badge badge-success font-weight-normal">проверено</span><br>';
		if( $item->top == 'Y' ) $strbage .= '<span class="badge badge-primary font-weight-normal">картина дня</span><br>';
		if( $item->NH == 'Y' ) $strbage .= '<span class="badge badge-info font-weight-normal">НЧ</span><br>';
		if( $item->comments  == 'N' ) $strbage .= '<span class="badge badge-secondary font-weight-normal">no comments</span><br>';
		if( ($item->flags & 1) == 1 ) $strbage .= '<span class="badge badge-warning font-weight-normal">без главной</span><br>';
		if( $res = $cache->get("editNews_".$item->id) ){
			$strbage .= '<span class="badge badge-danger font-weight-normal">'.unserialize($res)['author'].'</span><br>';
		}
		$show = ($item->active == 'N' || strtotime($item->dt_pub) > time())?'preview/'.$item->uid:$item->id;

		$author = Authors::getAuthor($item->author);
		$icon = $item->getImage()->getIcon();
?>
    <tr<?=($item->active == 'N')?' class="table-secondary"':''?>>
      <th scope="row" class="text-nowrap"><span class="<?=( strtotime($item->dt_pub) < time() )?'text-fuchsia':''?>"><?=date("H:i d-m-Y",strtotime($item->dt_pub))?></span></th>
      <td><img src="<?=$icon?>" onerror="this.src='/images/defaultsm.jpg'"  width="94" height="auto"></td>
      <td><?=$item->title?><br><small><?=$strTopics?><br><?=str_replace(',',' ',$item->editors)?></small><small class="bg-olive px-1"><?=$author->name?></small></td>
      <td><?=$strbage?></td>
      <td><div class="btn-group btn-group-toggle" data-toggle="a" role="group"><a href="update?id=<?=$item->id?>" title="Править" role="button" class="btn btn-secondary btn-sm"><i class="far fa-edit"></i></a><a href="newsshow?id=<?=$item->id?>" title="спрятать" role="button" class="btn btn-sm btn-secondary"><i class="fa <?=($item->active == 'N')?'fa-eye-slash':'fa fa-eye'?>"></i></a><a href="newskd?id=<?=$item->id?>" role="button" class="btn btn-sm <?=($item->top == 'N')?'btn-secondary':'btn-primary'?>">КД</a><a href="newsnh?id=<?=$item->id?>" role="button" class="btn <?=($item->NH == 'N')?'btn-secondary':'btn-primary'?> btn-sm">НЧ</a><a href="/news/<?=$show?>" title="открыть на сайте" role="button" class="btn btn-secondary btn-sm" target="_blank"><i class="far fa-window-restore"></i></a><?if(Yii::$app->user->can('deleteNews')){?><a href="delete?id=<?=$item->id?>" title="открыть на сайте" role="button" class="btn btn-secondary btn-sm" target="_blank" onclick="return confirm('Вы уверены?')"><i class="fa fa-trash"></i></a><?}?></div></div></td>
    </tr>
<?
//	<pre>
//	print_r($item);
//	</pre>
?>
<?
	}
?>
  </tbody>
</table>
</div>

<?php
//print_r($newsDataProvider->getModels());
