<?php
	use yii\helpers\Html;
	use yii\helpers\Url;
	use app\assets\AppAsset;

	$this->title = $model->text;
	$this->params['breadcrumbs'][] = ['text' => 'Опросы', 'url' =>'/inquirer'];
	$url = '/inquirer/'.$model->id;

	$widget = isset( $_REQUEST['widget'] )?$_REQUEST['widget']*1:1;
	AppAsset::register($this);
//	\yii\web\JqueryAsset::register($this);
//	\yii\bootstrap\BootstrapAsset::register($this);

	$do = $model->getPart();

?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="ru" class="h-100" prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# profile: http://ogp.me/ns/profile# fb: http://ogp.me/ns/fb#">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="x-dns-prefetch-control" content="on">
    <link rel="manifest" href="/manifest.json">
     <title><?= Html::encode($model->text)?></title>
    <meta name="title" content="<?= Html::encode($model->text)?>">
    <meta name="description" content="<?= preg_replace('/\s+/', ' ', str_replace( ["\n","\r"],' ',Html::encode( 'Опрос: '.$model->text)))?>">
    <meta name="author" content="ИА Амител" />
    <meta name="copyright" content="Амител">
    <link rel="canonical" href="<?=$url?>">
<?php $this->registerCsrfMetaTags() ?>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div >
			<section class="text">
<?
if( $model->show == 'N' || $do){
?>
				<h1>Результат опроса: <?=$model->text?></h1>
<?
$answers = $model->getAnswers();
$sum = 0;
foreach($answers as $item){
	$sum += $item->count;
}
	if( $widget != 3 ){
		/*
			круговые диаграммы
		*/
?>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<?

$s = '';
foreach($answers as $item){
	 $s .="['".$item['text']."', ".$item['count']."],";

}
	$typech = "{'packages':['corechart']}";
	if( $widget == 2 ){
		$type = "is3D: false,pieHole: 0.4,legend: {position: 'right', textStyle: {color: 'black', fontSize: 12}}";
	}elseif($widget == 3){
		 $type = 'bar: { groupWidth: "100%" },bars: "horizontal"';
		 $typech = "{'packages':['bar']}";
	}else{
		$type = "is3D: true,legend: {position: 'right', textStyle: {color: 'black', fontSize: 12}}";
	}

$this->registerJs(
<<<JS
google.charts.load('current', $typech);
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
	var data = new google.visualization.DataTable();
	data.addColumn('string', 'Ответы');
	data.addColumn('number', 'Голоса');
	data.addRows([$s]);

	var options = {'title':'график',
		chartArea:{left:0,top:10,width:'100%',height:'100%'},
		tooltip: {textStyle: {color: '#000000'}, showColorCode: false},$type
	};
	if( $widget == 3){
		var chart = new google.charts.Bar(document.getElementById('chart_div'));
	}else{
		var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
		google.visualization.events.addListener(chart, 'ready', function () {
			png = chart.getImageURI();
//		chart_div.innerHTML = '<img src="' + png + '">';
//		console.log(png);
			dwnload = '<a href="' + png + '" download>Скачать</a>';
			chart_png.innerHTML = dwnload;
		});
	}
	chart.draw(data, options);

}
JS
    ,
    $this::POS_END,
    'chart'
	);
?>
<div id="chart_div" style="width: 100%; height: 400px;" class="p-0"></div>
<div id="chart_png"></div>
<?
	}else{
		/*
			столбчатая гистограмма на html
		*/
		foreach($answers as $item){
			$percent = ($sum)?round($item->count/$sum*100):0;
?>
<div class="shadow-sm p-2 mt-3 bg-light rounded">
<h4><?=$item->text?></h4>
<div class="progress" style="height: 20px;">
  <div class="progress-bar bg-info" role="progressbar" style="width: <?=$percent?>%;" aria-valuenow="<?=$percent?>" aria-valuemin="0" aria-valuemax="100"><?=$percent?>%</div>
</div>
</div>
<?
		}
	}

}else{
?>
					<h1>Опрос: <?=$model->text?></h1>
<?
	echo yii\base\View::render("poll",['id'=>$model->id, 'model'=>$model]);
}
?>
            </section>
</div>
<style>
input[type=radio]:not(:checked)  {
	content: "";
	background-color: #ff00ff;
	border: 2px solid #36d7d7;
}
</style>
<?php $this->endBody() ?>
<body>
</html>
<?php $this->endPage() ?>
<?
