<?php

use app\base\Common;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use kartik\widgets\DepDrop;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model app\models\preartbeliefs */
/* @var $form yii\widgets\ActiveForm */

$Surveys = Common::getSurvey();
$PIDs = Common::getEnrollPIDs("phq");
$Visits = Common::getCohortVisits();

if(Yii::$app->params['SURVEY'] != '') {
	$Locations = Common::getSurlocations(Yii::$app->params['SURVEY']);
}
if(Yii::$app->params['SURVEY'] != '' && Yii::$app->params['LOCATION'] != '') {
	$Locations = Common::getSurlocations(Yii::$app->params['SURVEY']);
	$PIDs = Common::getEnrollPIDs("phq",Yii::$app->params['SURVEY'], Yii::$app->params['LOCATION']);
}
if(!$model->isNewRecord) {
	$Visits = Common::getPIDsVisit($model->depression_survey, $model->depression_loc, $model->depression_part_id);
}

$JS="
$('#depression-depression_part_id').on('select2:select', function (evt) {
		var pid = $(this)[0].value;		
		if(pid != '') {
			var link = $('#pid-info').data('link') + pid;			
			$.ajax({
				type: 'post',
				url: 'visit',
				data: 'pid='+$(this)[0].value,
				success: function(response) {	
                    			
					if(response.visit != null) {
						
						$('#depression-visit_code').val(response.visit);
						$('#depression-visit').val(response.visit);
						
					} else {
						$('#depression-visit_code').val('');
						$('#depression-visit').val('');
						
					}
					
				}
			});
		}
	});
		
	
	$('#depression-depression_part_id').on('select2:unselect', function (evt) {
		$('#depression-visit_code').val('');
		$('#depression-visit').val('');
	});

  var q1,q2,q3,q4,q5,q6,q7,q8,q9;
  
	$('#depression-depression_q1 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q1=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q2 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q2=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q3 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q3=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q4 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q4=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q5 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q5=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}		
		
	});	
	
	$('#depression-depression_q6 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q6=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q7 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q7=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}			
		
	});	
	
	$('#depression-depression_q8 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q8=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}		
		
	});	
	
	
	 $('#depression-depression_q9 input[type=radio]').change(function(){	
		$('div.help-block').html('');
		$('div.error-summary').hide();
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			q9=$(this).val();	
			if(q1== '0' && q2== '0' && q3== '0'&& q4== '0'&& q5== '0'&& q6== '0'&& q7== '0'&& q8== '0' && q9== '0') {
			 $('div.form-group.field-depression-depression_q10').removeClass('required').hide();
			 $('#depression-depression_q10').val('');
			
		} else {		  
			 $('div.form-group.field-depression-depression_q10').addClass('required').show();	
		}
		}
	});	
   
  $('#depression-depression_q1 input[type=radio],#depression-depression_q2 input[type=radio],#depression-depression_q3 input[type=radio],#depression-depression_q4 input[type=radio],#depression-depression_q5 input[type=radio],#depression-depression_q6 input[type=radio],#depression-depression_q7 input[type=radio],#depression-depression_q8 input[type=radio],#depression-depression_q9 input[type=radio]').trigger('change');     
	
	";
$this->registerJs($JS);

?>

<div class="depression-form">
	<div class="panel panel-default">
		<div class="panel-heading">
			
		</div>
		<div class="panel-body">
			<div class="row">
				<div class="col-lg-12">
					<?php
						$form = ActiveForm::begin(['options' => ['class' => 'form-horizontal']]);
						$template = [
							'labelOptions' => ['class' => 'form-label'],
							'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-4">{input}{error}</div>',
						];
						$chklist_template = [
							'labelOptions' => ['class' => 'form-label'],
							'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-7 checkbox-list">{input}{error}</div>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
					?>
					<?= Common::generateControl($form, $model, $template, 'depression_survey', $Surveys, 'survey', 'getEnrollPIDs','phq') ?>
					<?= Common::generateControl($form, $model, $template, 'depression_loc', $Locations, 'location', '', [Html::getInputId($model, 'depression_survey')]) ?>
					<?= Common::generateControl($form, $model, $template, 'depression_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'depression_survey'), Html::getInputId($model, 'depression_loc')]) ?>
					
					<?php  if($model->isNewRecord) : ?>					 
                     <?= $form->field($model, 'visit', $template)->dropDownList($Visits,['prompt' => 'Select','disabled' => true]) ?>	
                     <?php  else: ?>	
                      <?= $form->field($model, 'visit_code', $template)->widget(DepDrop::classname(), ['data'=>$Visits, 'options'=>['prompt'=>$model->getAttributeLabel('visit_code')],'disabled' => true, 'pluginOptions'=>['depends'=>[Html::getInputId($model, 'depression_survey'), Html::getInputId($model, 'depression_loc'), Html::getInputId($model, 'depression_part_id')], 'placeholder'=>$model->getAttributeLabel('visit_code'), 'url'=>Url::to(['/dashboard/getids']), 'params'=>['common-method1', Html::getInputId($model, 'depression_part_id')]]]) ?>						 
					 <?php  endif; ?>  					 
					
                     <?= $form->field($model, 'visit_code', $htemplate)->hiddenInput()->label() ?>
					
					<div class="form-group">
					 <div class="col-sm-5">
					 <?= Html::label('INTERVIEWER READ : Over the last two weeks, how often have you been bothered by any of the following problems?') ?>
					 </div>
					</div>
					
					<?= $form->field($model, 'depression_q1', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>
					<?= $form->field($model, 'depression_q2', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>			
					<?= $form->field($model, 'depression_q3', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>
					<?= $form->field($model, 'depression_q4', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>	
					<?= $form->field($model, 'depression_q5', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>
					<?= $form->field($model, 'depression_q6', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>	
					<?= $form->field($model, 'depression_q7', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>
                    <?= $form->field($model, 'depression_q8', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>						
					<?= $form->field($model, 'depression_q9', $chklist_template)->radioList($model->often)->hint("<strong>INTERVIEWER INSTRUCTION : </strong> Use <strong>Card PHQ1.</strong><br />")?>
				    <?= $form->field($model, 'depression_q10', $template)->dropDownList($model->q10 , ['prompt' => 'Select']) ?>								
					<?= $form->field($model, 'status', $htemplate)->hiddenInput(['value' => 1])->label() ?>
					<?= $form->field($model, $model->isNewRecord ? 'create_user' : 'update_user', $htemplate)->hiddenInput(['value' => yii::$app->user->identity->id])->label("") ?>
					<div class="form-group">
						<div class="col-sm-offset-5 col-sm-7">
							<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
							<?= Html::a('Cancel', ['/'.Yii::$app->controller->id], ['class' => 'btn btn-default']) ?>
						</div>
					</div>
					<?php ActiveForm::end(); ?>
				</div>
				<!-- /.col-lg-6 (nested) -->
			</div>
			<!-- /.row (nested) -->
		</div>
		<!-- /.panel-body -->
	</div>
	<!-- /.panel -->
</div>