<?php
	$this->title="Статистика";
	$this->params['breadcrumbs'][] = ['label' => 'Статистика', 'url' => ['index']];
	$this->params['breadcrumbs'][] = $this->title;
	$y = date("Y")*1;
	if (Yii::$app->request->isGet && $m = Yii::$app->request->get('m')){
		if( $m == -1 ){
			$b_date = date(($y-1)."-12-01 00:00:00");
			$e_date = date(($y-1)."-12-31 23:59:59");
			$m = 12;
		}else{
			$d = cal_days_in_month(CAL_GREGORIAN, $m, $y);
			$m = $m<10?"0$m":$m;
			$d = $d<10?"0$d":$d;
			$b_date = date("Y-".$m."-01 00:00:00");
			$e_date = date("Y-".$m."-".$d."  23:59:59");
		}
	}else{
		$b_date = date("Y-m-01 00:00:00");
		$e_date = date("Y-m-d  H:i:s");
		$m = date("m");
	}

$newsDataProvidera = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\base\News::find()->select(["SUBSTRING_INDEX(`editors`,',',1) as e"])->andwhere(['between', 'dt_pub',  $b_date , $e_date])->andwhere(['active'=>'Y'])->groupBy(["e"]),
            "pagination" =>false
    ]
);
$cnt = $newsDataProvidera->getCount();

$newsDataProviderau = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\base\News::find()->select('count(author)')->andwhere(['between', 'dt_pub',  $b_date , $e_date])->andwhere(['active'=>'Y'])->andwhere(['<>', 'author', '5'])),
            "pagination" =>false
    ]
);
$cnt2 = $newsDataProviderau->getCount();

$newsDataProvideras = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\base\News::find()->select('count(*)')->andwhere(['between', 'dt_pub',  $b_date , $e_date])->andwhere(['<>', 'author', '5'])->andwhere(['active'=>'Y'])->andWhere(['show_author'=>'Y'])),
            "pagination" =>false
    ]
);
$cnt3 = $newsDataProvideras->getCount();

$authors = Yii::$app->db->createCommand("select e, count(e) as c from (SELECT SUBSTRING_INDEX(`editors`,',',1) as e FROM `news` WHERE `dt_pub` BETWEEN '".$b_date."' AND '".$e_date."' and active = 'Y') t where 1 group by e")->queryAll();


$newsDataProviderau = new \yii\data\ActiveDataProvider(
    [
        "query"=>\app\models\base\News::find()->andwhere(['between', 'dt_pub',  $b_date , $e_date])->andwhere(['active'=>'Y']),
            "pagination" =>false
    ]
);
$cntall = $newsDataProviderau->getCount();

$sql = "select e, count(e) as c, name from (SELECT SUBSTRING_INDEX(`editors`,',',1) as e, `authors`.name as name FROM `news` LEFT JOIN `authors` ON( news.author = authors.id and authors.id <> 5 ) WHERE `dt_pub` BETWEEN '".$b_date."' AND '".$e_date."' and `news`.active = 'Y' ) t where 1 group by e, `name`";
$authorsfix = Yii::$app->db->createCommand( $sql )->queryAll();

$a = array();
foreach( $authors as $author ){
	$a[str_replace( ['ё','Ё'], 'е', $author['e'])] = $author['c'];
}
//print_r($a);
foreach( $authorsfix as $author ){
	if( $author['name'] ){
		$name = str_replace( ['ё','Ё'], 'е', $author['name'] );
		$authorn = str_replace( ['ё','Ё'], 'е', $author['e'] );
		if( $name != $authorn ){
			if( !isset( $a[$name] ) ) $a[$name] = 0;
			$a[$name] += $author['c'];
			$a[$authorn] -= $author['c'];
		}
	}
}
$sum = 0;
$max = 0;
foreach( $a as $key => $item ){
	$sum += $item;
	if( $item == 0 ){
		unset($a[$key]);
	}
	$max = ( $item > $max )?$item:$max;
}
//$authors = $newsDataProvidera->query->createCommand()->queryAll();
$months = array( 1 => 'Январь' , 'Февраль' , 'Март' , 'Апрель' , 'Май' , 'Июнь' , 'Июль' , 'Август' , 'Сентябрь' , 'Октябрь' , 'Ноябрь' , 'Декабрь' );
?>
<section class="content">
<div class="container-fluid">
<div class="callout callout-info">
<h5><i class="fas fa-info"></i> Note:</h5>
<h6>Даты расчёта <strong class="page-month"><?=$months[$m*1]?></strong> с <?="$b_date по $e_date"?></h6>
</div>

<ul class="pagination pagination-month justify-content-center">
<li class="page-item">
<a class="page-link" href="?m=-1">
<p class="page-month">Декабрь</p>
<p class="page-year"><?=$y-1?></p>
</a>
</li>
<?
foreach( $months as $key => $m ){
?>
<li class="page-item<?=($key==date("m"))?' active':''?>">
<a class="page-link" href="?m=<?=$key?>">
<p class="page-month"><?=$m?></p>
<p class="page-year"><?=$y?></p>
</a>
</li>
<?}?>
</ul>

<div class="row">

<div class="info-box col-3 m-3">
  <span class="info-box-icon bg-info"><i class="far fa-copy"></i></span>
  <div class="info-box-content">
    <span class="info-box-text">Всего материалов</span>
    <span class="info-box-number"><?=$cntall?></span>
  </div>
</div>

<div class="info-box col-3 m-3">
  <span class="info-box-icon bg-success"><i class="far fa-flag"></i></span>
  <div class="info-box-content">
    <span class="info-box-text">Авторов на ленте</span>
    <span class="info-box-number"><?=$cnt?></span>
    <span class="info-box-number">
      <?=$cnt2?> Указаны авторы
    </span>
  </div>
</div>

<div class="info-box col-3 m-3">
  <span class="info-box-icon bg-danger"><i class="far fa-star"></i></span>
  <div class="info-box-content">
    <span class="info-box-text">Подписаных материалов</span>
    <span class="info-box-number"><?=$cnt3?></span>
  </div>
</div>

</div>
</div>

<div class="row">
	<div class="col-md-6">
		<div class="card direct-chat direct-chat-warning">
			<div class="card-header">
				<h3 class="card-title">Авторы по материалам</h3>
				<div class="card-tools">
					<span title="<?=$cntall?> New Messages" class="badge badge-warning"><?=$cntall?></span>
					<button type="button" class="btn btn-tool" data-card-widget="collapse">
						<i class="fas fa-minus"></i>
					</button>
					<button type="button" class="btn btn-tool" data-card-widget="remove">
						<i class="fas fa-times"></i>
					</button>
				</div>
			</div>
			<div class="card-body">
				<table class="table m-0">
					<thead>
					<tr>
						<th>Автор</th>
						<th>Материалов</th>
					</tr>
					</thead>
					<tbody>
<?
foreach( $authors as $author ){
?>
<tr>
	<td><?=$author['e']?></td><td><?=$author['c']?></td>
</tr>
<?}?>
					<tbody>
				</table>
				<div class="card-footer">
				По данным редакторов
				</div>
			</div>
		</div>
	</div>

	<div class="col-md-6">
		<div class="card direct-chat direct-chat-warning">
			<div class="card-header">
				<h3 class="card-title">Авторы по материалам диаграмма</h3>
				<div class="card-tools">
					<button type="button" class="btn btn-tool" data-card-widget="collapse">
						<i class="fas fa-minus"></i>
					</button>
					<button type="button" class="btn btn-tool" data-card-widget="remove">
						<i class="fas fa-times"></i>
					</button>
				</div>
			</div>
			<div class="card-body mx-2">
<?
foreach( $authors as $author ){
?>

<div class="progress-group">
	<?=$author['e']?>
	<span class="float-right"><b><?=$author['c']?></b>/<?=$cntall?></span>
	<div class="progress progress-sm">
		<div class="progress-bar bg-danger" style="width: <?=round($author['c']*100/$max)?>%"></div>
	</div>
</div>

<?}?>

				<div class="card-footer">
				По данным редакторов
				</div>
			</div>
		</div>
	</div>
</div>

<div class="row">
	<div class="col-md-6">
		<div class="card direct-chat direct-chat-warning">
			<div class="card-header">
				<h3 class="card-title">Авторы по материалам (пересчёт)</h3>
				<div class="card-tools">
					<span title="<?=$sum?> New Messages" class="badge badge-warning"><?=$sum?></span>
					<button type="button" class="btn btn-tool" data-card-widget="collapse">
						<i class="fas fa-minus"></i>
					</button>
					<button type="button" class="btn btn-tool" data-card-widget="remove">
						<i class="fas fa-times"></i>
					</button>
				</div>
			</div>
			<div class="card-body">
				<table class="table m-0">
					<thead>
					<tr>
						<th>Автор</th>
						<th>Материалов</th>
					</tr>
					</thead>
					<tbody>
<?
foreach( $a as $author => $c){
?>
<tr>
	<td><?=$author?></td><td><?=$c?></td>
</tr>
<?}?>
					<tbody>
				</table>
				<div class="card-footer">
					Уточнённый данные по указанным авторам
				</div>
			</div>
		</div>
	</div>

	<div class="col-md-6">
		<div class="card direct-chat direct-chat-warning">
			<div class="card-header">
				<h3 class="card-title">Авторы по материалам диаграмма</h3>
				<div class="card-tools">
					<button type="button" class="btn btn-tool" data-card-widget="collapse">
						<i class="fas fa-minus"></i>
					</button>
					<button type="button" class="btn btn-tool" data-card-widget="remove">
						<i class="fas fa-times"></i>
					</button>
				</div>
			</div>
			<div class="card-body mx-2">
<?
foreach( $a as $author => $c ){
?>

<div class="progress-group">
	<?=$author?>
	<span class="float-right"><b><?=$c?></b>/<?=$sum?></span>
	<div class="progress progress-sm">
		<div class="progress-bar bg-danger" style="width: <?=round($c*100/$max)?>%"></div>
	</div>
</div>

<?}?>
				<div class="card-footer">
					Уточнённый данные по указанным авторам
				</div>
			</div>
		</div>
	</div>
</div>

</section>
