<?php
/* @var $this SubController */
/* @var $model Sub */

$this->breadcrumbs=array(
	'Subs'=>array('index'),
	$model->name,
);

$this->pageTitle=$model->name;
?>


<div class="col-sm-12">

	<H4 class="alert well">

		<div class="row">
			<div class="col-md-10">
				<!-- Ukuran : <BR><?php echo $model->measure; ?><BR><BR> -->
				<h4><B><?php echo $model->name; ?></B></h4>
				<small>
					<span title="Total Indikator Inovasi yang sudah diisi" class="label label-primary">( <?php echo Question::model()->countPointMY($model->id_sub,$u,$sessionid); ?> / <?php echo Question::model()->countPoint($model->id_sub); ?> )</span> <span title="Nilai Indikator Inovasi Anda Sekarang" class="label label-success"> <?php echo Question::model()->sumPointMY($model->id_sub,$u,$sessionid); ?> / <?php echo $model->max_point; ?> </span></BR>
				</small>
			</div>
			<div class="col-md-2 hidden-xs">
				<?php
				echo CHtml::link('<i class="icon icon-left mdi mdi-home"></i> Kembali',
					array('site/dashboard'),
					array('class' => 'btn btn-info btn-flat pull-right btn-lg', 'title'=>'Kembali'));
					?>
					<?php if(YII::app()->user->record->level==1): ?>
						<?php
						echo CHtml::link('<i class="icon icon-left mdi mdi-lock"></i> Unlock',
							array('site/dashboard'),
							array('class' => 'btn btn-warning btn-flat pull-right btn-lg', 'title'=>'Unlock'));
							?>
						<?php endif; ?>
					</div>
					<div class="col-md-2 visible-xs">
						<?php
						echo CHtml::link('<i class="icon icon-left mdi mdi-home"></i>',
							array('site/dashboard'),
							array('class' => 'btn btn-info btn-flat pull-right btn-lg', 'title'=>'Kembali'));
							?>

							<?php if(YII::app()->user->record->level==1): ?>
								<?php
								echo CHtml::link('<i class="icon icon-left mdi mdi-lock"></i> Unlock',
									array('site/dashboard'),
									array('class' => 'btn btn-warning btn-flat pull-right btn-lg', 'title'=>'Unlock'));
									?>
								<?php endif; ?>
							</div>
						</div>

					</H4>

					<div class="row">

						<div class="col-12 col-lg-12">
							<div class="tab-container">

								<div class="tab-content">

									<div class="tab-pane" id="22" role="tabpanel">
										<h2>
											<i class="icon icon-left mdi mdi-graphic-eq"></i> Laporan Hasil Evaluasi Inovasi
										</h2>

										<div class="row">
											<div class="col-md-6">
												<div id="laporan-1" style="min-width: 300px; height: 400px; margin: 0 auto"></div>
											</div>
											<div class="col-md-6">
												<div id="laporan-2" style="min-width: 300px; height: 400px; margin: 0 auto"></div>
											</div>
										</div>
										<script src="https://code.highcharts.com/highcharts.js"></script>
										<script src="https://code.highcharts.com/modules/exporting.js"></script>
										<script src="https://code.highcharts.com/modules/export-data.js"></script>

										<script>
											Highcharts.chart('laporan-1', {
												chart: {
													type: 'pie'
												},
												title: {
													text: 'Sudah di Verifikasi'
												},
												xAxis: {
													type: 'category',
													labels: {
														rotation: -45,
														style: {
															fontSize: '13px',
															fontFamily: 'Verdana, sans-serif'
														}
													}
												},
												yAxis: {
													min: 0,
													title: {
														text: 'Total Point'
													}
												},
												legend: {
													enabled: false
												},
												credit: {
													enabled: false
												},
												tooltip: {
													pointFormat: 'Total Point: <b>{point.y}</b>'
												},
												series: [{
													name: 'Tahun',
													data: [
													<?php
													foreach (Answer::getAnalysis($sessionid,1) as $data) {
														?>
														<?php echo "['Bobot: ".$data['weight']." - ".substr($data['question'], 0, strpos($data['question'], ':')).": ".$data['name']."',".$data['point']."],"; ?>
													<?php } ?>

													],
													dataLabels: {
														enabled: false,
														rotation: -90,
														color: '#FFFFFF',
														align: 'right',
														format: '{point.y}',
														y: 10,
														style: {
															fontSize: '11px',

														}
													}
												}],
											});

											Highcharts.chart('laporan-2', {
												chart: {
													type: 'pie'
												},
												title: {
													text: 'Belum di verifikasi'
												},
												xAxis: {
													type: 'category',
													labels: {
														rotation: -45,
														style: {
															fontSize: '13px',
															fontFamily: 'Verdana, sans-serif'
														}
													}
												},
												yAxis: {
													min: 0,
													title: {
														text: 'Total Point'
													}
												},
												legend: {
													enabled: false
												},
												credit: {
													enabled: false
												},
												tooltip: {
													pointFormat: 'Total Point: <b>{point.y}</b>'
												},
												series: [{
													name: 'Tahun',
													data: [
													<?php
													foreach (Answer::getAnalysis($sessionid,2) as $data) {
														?>
														<?php echo "['Bobot: ".$data['weight']." - ".substr($data['question'], 0, strpos($data['question'], ':')).": ".$data['name']."',".$data['point']."],"; ?>
													<?php } ?>

													],
													dataLabels: {
														enabled: false,
														rotation: -90,
														color: '#FFFFFF',
														align: 'right',
														format: '{point.y}',
														y: 10,
														style: {
															fontSize: '11px',

														}
													}
												}],
											});
										</script>

									</div>

									<?php
									$x = 0;
									foreach (Question::getQuestions($model->id_sub) as $question) { ?>
										<?php
										$x++;
										$jawaban=Answer::model()->findByAttributes(
											array(
												'question_id'=>$question['id_question'],
												'created_id'=>$u,
												'session_id'=>$sessionid,
											)
										); ?>

										<!-- START TAB -->
										<div class="tab-pane" id="<?php echo $x; ?>" role="tabpanel">


											<p>
												<h2>
													<i class="icon icon-left mdi mdi-caret-right-circle"></i>
													<?php
													$str = $question['question'];
													$headline = substr($str, 0, strpos($str, ':'));
													echo $headline;
													?></h2>
													<h4>
														<?php echo "Bagaimana " . str_replace(array($headline.": "), "", $str); ?>
														<?php //echo $question['attachment']==1 ? "<span class='text-info'><b>( Lampirkan dokumen Pendukung )</b></span>" : ""; ?>
													</h4>
												</p>
												<?php if($jawaban->answer_id!=NULL): ?>
													<table class="table table-condensed">

														<?php if($question['question_extend']!=""){ ?>
															<TR>
																<TD class="question"></TD>
																<TD class="answer">
																	<?php echo Answer::model()->AnswerStatus($jawaban->answer_status); ?>
																</TD>
															</TR>
														<?php } ?>

														<?php if($jawaban->answer_status==1){ ?>
															<?php if($question['question_extend']!=""){ ?>
																<TR>
																	<TD class="question"></TD>
																	<TD class="answer">
																		<?php echo $question['question_extend']; ?>
																	</TD>
																</TR>
															<?php } ?>
															<TR>
																<TD class="question">Jawaban</TD>
																<TD class="answer">
																	<?php if($question['type']==1){ ?>

																		<?php echo $jawaban->answer_id==NULL ? "-" : $jawaban->QuestionAnswer->name; ?>

																	<?php }elseif($question['type']==2){ ?>

																		<?php $jawaban->answer_multiple==NULL ? "-" : Answer::model()->multipleKu($jawaban->answer_multiple); ?>

																	<?php }elseif($question['type']==3){ ?>

																		<?php echo $jawaban->male + $jawaban->female; ?>

																	<?php }else{ ?>

																		<?php echo $jawaban->answer_id==NULL ? "-" : $jawaban->QuestionAnswer->name; ?>

																	<?php } ?>
																</TD>
															</TR>
														<?php }else{ ?>
															<TR>
																<TD class="question"></TD>
																<TD class="answer">
																	<?php echo $jawaban->answer_status == 0 ? "-" : "Jika Tidak, Alasannya"; ?>
																</TD>
															</TR>
															<TR>
																<TD class="question"></TD>
																<TD class="answer">
																	<?php echo $jawaban->reason == NULL ? "-" : $jawaban->reason; ?>
																</TD>
															</TR>
														<?php } ?>
														<?php if($jawaban->answer!=""){ ?>
															<TR>
																<TD class="question">Catatan</TD>
																<TD class="answer">
																	<?php echo $jawaban->answer == NULL ? "-" : $jawaban->answer; ?>
																</TD>
															</TR>
														<?php } ?>
											<!--
											<?php if($question['attachment']==1){ ?>
												<TR>
													<TD class="question">Lampiran</TD>
													<TD class="answer">
														<?php echo Answer::model()->Attachment($jawaban->attachment); ?>
													</TD>
												</TR> -->
											<?php } ?>
											<?php if($question['total_children']==1){ ?>
												<TR>
													<TD class="question">Sebelum</TD>
													<TD class="answer">
														<?php echo $jawaban->male; ?>
													</TD>
												</TR>
												<TR>
													<TD class="question">Sesudah</TD>
													<TD class="answer">
														<?php echo $jawaban->female; ?>
													</TD>
												</TR>
											<?php } ?>
											<!--
											<TR>
												<TD class="question">Nilai</TD>
												<TD class="answer">
													<?php //echo Question::model()->sumQustionPointMY($question['id_question,$jawaban->created_id); ?>
													<?php echo $jawaban->point; ?>
												</TD>
											</TR> -->
											<?php if($jawaban->verified_id==NULL): ?>
												<TR>
													<TD class="question">Status Jawaban</TD>
													<TD class="answer">
														<?php echo Answer::model()->AnswerVerified($jawaban->verified_answer); ?>
													</TD>
												</TR>
											<?php endif; ?>

										</table>
									<?php endif; ?>

									<?php
									if($jawaban->attachment==1 && AnswerAttachment::model()->countAttachment($jawaban->question_id)>=1):
										$dataAnswerAttachment=new CActiveDataProvider('AnswerAttachment',array('criteria'=>array('condition'=>'question_id='.$question['id_question'].' AND session_id='.$jawaban->session_id)));
									$this->widget('zii.widgets.grid.CGridView', array(
										'id'=>'answer-attachment-grid',
										'summaryText'=>'',
										'dataProvider'=>$dataAnswerAttachment,
										'itemsCssClass' => 'table-responsive table table-striped table-hover table-vcenter',
										'columns'=>array(

											array(
												'header'=>'Lampiran',
												'type'=>'raw',
												'value'=>'CHtml::link($data->lampiran, array("main/answer/download/id/$data->id_attachment"))',
											),

										),
									));
								endif;
								?>

								<span class="pull-left">
									<span class="label label-success">Nilai <b><?php echo $jawaban->point; ?></b></span>
									<span class="label label-danger">Max <b><?php echo QuestionAnswer::model()->getPointMax($question['id_question'],$question['type']); ?></b></b></span>
								</span>

								<?php
								if(YII::app()->user->record->level==1){

									echo CHtml::link('<i class="icon icon-left mdi mdi-caret-right-circle"></i> Verifikasi',
										array('master/question/verification', 'id'=>$jawaban->id_answer,'u'=>$u,'token'=>$token,'sessionid'=>$sessionid),
										array('class' => 'btn btn-warning btn-flat pull-right btn-lg','title'=>'Koreksi Jawaban Kuesioner',
											'ajax'=>array(
												'type'=>'POST',
												'url'=>"js:$(this).attr('href')",
												'success'=>'function(data) { $("#viewModalVerified .modal-body p").html(data); $("#viewModalVerified").modal(); }'
											),
										));

									// echo CHtml::link('<i class="mdi mdi-check"></i> Benar',
									// 	array('main/answer/true', 'id'=>$jawaban->id_answer,'subid'=>$question['sub_id'],'userid'=>$u,'token'=>$token,'sessionid'=>$sessionid),
									// 	array('class' => 'btn btn-success btn-info pull-right btn-flat','title'=>'Ubah Jawaban'));

									// echo CHtml::link('<i class="mdi mdi-close"></i> Tidak Sesuai',
									// 	array('main/answer/false', 'id'=>$jawaban->id_answer,'subid'=>$question['sub_id'],'userid'=>$u,'token'=>$token,'sessionid'=>$sessionid),
									// 	array('class' => 'btn btn-danger btn-info pull-right btn-flat','title'=>'Ubah Jawaban'));

								}else{
									if($jawaban->answer_status==0){
										echo CHtml::link('<i class="icon icon-left mdi mdi-caret-right-circle"></i> Jawab',
											array('master/question/answer', 'id'=>$jawaban->id_answer,'u'=>$u,'token'=>$token,'sessionid'=>$sessionid),
											array('class' => 'btn btn-info btn-flat pull-right btn-lg','title'=>'Jawab Kuesioner',
												// 'ajax'=>array(
												// 	'type'=>'POST',
												// 	'url'=>"js:$(this).attr('href')",
												// 	'success'=>'function(data) { $("#viewModal .modal-body p").html(data); $("#viewModal").modal(); }'
												// ),
										));
									}else{
										if($jawaban->verified_id==0){
											echo CHtml::link('<i class="icon icon-left mdi mdi-caret-right-circle"></i> Jawab',
												array('master/question/answer', 'id'=>$jawaban->id_answer,'u'=>$u,'token'=>$token,'sessionid'=>$sessionid),
												array('class' => 'btn btn-info btn-flat pull-right btn-lg','title'=>'Jawab Kuesioner',
												// 'ajax'=>array(
												// 	'type'=>'POST',
												// 	'url'=>"js:$(this).attr('href')",
												// 	'success'=>'function(data) { $("#viewModal .modal-body p").html(data); $("#viewModal").modal(); }'
												// ),
											));
										}
									}
								}
								echo "<BR>";
								echo "<BR>";
								echo "<BR>";
								?>



							</div>
							<!-- END TAB -->

						<?php } ?>

					</div>


					<ul class="nav nav-tabs" id="myTab">
						<?php if(YII::app()->user->record->level==1): ?>
							<li class="nav-item">
								<a class="nav-link" href="#22" data-toggle="tab" role="tab" aria-selected="">
									<h4>
										<b>Laporan</b>
									</h4>
								</a>
							</li>
						<?php endif; ?>
						<?php
						$x = 0;
						foreach (Question::getQuestions($model->id_sub) as $question) { ?>
							<?php
							$x++;
							$jawaban=Answer::model()->findByAttributes(
								array(
									'question_id'=>$question['id_question'],
									'created_id'=>$u,
									'session_id'=>$sessionid,
								)
							); ?>
							<li class="nav-item <?php echo $jawaban->answer_status==1 || $jawaban->answer_status==2 ? "bg-success" : "bg-warning" ?> text-white"><a class="nav-link <?php if($x==1){echo "active";} ?>" href="#<?php echo $x; ?>" data-toggle="tab" role="tab" aria-selected="<?php if($x==1){echo "true";}else{echo "false";} ?>">
								<h4>
									<b><?php echo $x; ?> <?php if($jawaban->verified_answer==1): ?><i class="icon mdi mdi-check-circle text-white"></i><?php endif; ?></b>
								</h4>
							</a></li>
						<?php } ?>
					</ul>

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


	</div>

	<STYLE>
		th{width:150px;}
		.question{width: 10px;color:#000;}
		.answer{font-weight: 700;font-size: 14px;color:#31708f;}
	</style>

	<!-- Modal -->
	<div class="modal fade colored-header colored-header-primary" id="viewModal" tabindex="-1" role="dialog" aria-labelledby="viewModal" aria-hidden="true">
		<div class="modal-dialog full-width">
			<div class="modal-content">
				<div class="modal-header modal-header-colored">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title"><strong>Inovasi</strong> Daerah</h4>
				</div>
				<div class="modal-body">
					<p>Details</p>
				</div>
			</div>
		</div>
	</div>


	<div class="modal fade colored-header colored-header-warning" id="viewModalVerified" tabindex="-1" role="dialog" aria-labelledby="viewModalVerified" aria-hidden="true">
		<div class="modal-dialog full-width">
			<div class="modal-content">
				<div class="modal-header modal-header-colored">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title"><strong>Verifikasi</strong> Jawaban</h4>
				</div>
				<div class="modal-body">
					<p>Loading...</p>
				</div>
			</div>
		</div>
	</div>
