<?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\demographics */
/* @var $form yii\widgets\ActiveForm */

$Surveys = Common::getSurvey();
//$PIDs = Common::getAttendancePIDs();
$PIDs = Common::getEnrollPIDs("dg");
$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("dg",Yii::$app->params['SURVEY'], Yii::$app->params['LOCATION']);
}

if(!$model->isNewRecord) {
	$Visits = Common::getPIDsVisit($model->demographics_survey, $model->demographics_loc, $model->demographics_part_id);
}


$JS="
$('#demographics-demographics_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) {
						
						$('#demographics-visit_code').val(response.visit);
						$('#demographics-visit').val(response.visit);
						
					} else {
						$('#demographics-visit_code').val('');
						$('#demographics-visit').val('');
						
					}
					
				}
			});
		}
	});
		
	
	$('#demographics-demographics_part_id').on('select2:unselect', function (evt) {
		$('#demographics-visit_code').val('');
		$('#demographics-visit').val('');
	});
	
$('#demographics-demographics_q3').change(function(){
		if($(this).val() > 10)
			$('#modal').modal('show').find('.modal-body').html('Acceptable range: 0-10. Please double check your answer.');
	});

	$('#demographics-demographics_q6').change(function(){
		if($(this).val() > 10000 && $(this).val() != '')
			$('#modal').modal('show').find('.modal-body').html('Please make sure this number is correct.');
	});
	
	$('#demographics-demographics_q9').change(function(){
		if($(this).val() > 30 && $(this).val() != 996)
			$('#modal').modal('show').find('.modal-body').html('Acceptable range: 0-30, 996. Please double check your answer.');
	});



";
$this->registerJs($JS);

?>

<div class="demographics-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>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
					?>
					<?= Common::generateControl($form, $model, $template, 'demographics_survey', $Surveys, 'survey', 'getEnrollPIDs') ?>
					<?= Common::generateControl($form, $model, $template, 'demographics_loc', $Locations, 'location', '', [Html::getInputId($model, 'demographics_survey')]) ?>
					<?= Common::generateControl($form, $model, $template, 'demographics_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'demographics_survey'), Html::getInputId($model, 'demographics_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, 'demographics_survey'), Html::getInputId($model, 'demographics_loc'), Html::getInputId($model, 'demographics_part_id')], 'placeholder'=>$model->getAttributeLabel('visit_code'), 'url'=>Url::to(['/dashboard/getids']), 'params'=>['common-method1', Html::getInputId($model, 'demographics_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 : I am going to ask you questions for about 45 minutes to 1 hour. I am going to ask questions about different behaviors and medical conditions you may have. Some of the questions I ask may be uncomfortable to answer. You can choose not to answer any questions that you do not feel comfortable answering. Please remember that all of the information you give us is confidential. We ask that you answer the questions as honestly as you can. If any of the questions are unclear, please stop me and I will try and make things clearer. Remember, there are no right or wrong answers. <br> <br> INTERVIEWER READ : I am going to start by asking you some questions about yourself and your living conditions.') ?> 				
 					 </div>
					</div>
					
										
					<?= $form->field($model, 'demographics_q1', $template)->dropDownList($model->q1,['prompt'=>'Select']) ?>
					<?= $form->field($model, 'demographics_q2', $template)->dropDownList($model->q2,['prompt'=>'Select']) ?>
					<?= $form->field($model, 'demographics_q3', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'demographics_q4', $template)->dropDownList($model->q4,['prompt'=>'Select']) ?>
					<?= $form->field($model, 'demographics_q5', $template)->dropDownList($model->q5,['prompt'=>'Select']) ?>	
					<?= $form->field($model, 'demographics_q6', $template)->textInput() ?>
					<div class="form-group">
					 <div class="col-sm-5">
					 <?= Html::label('INTERVIEWER READ : We now have a few questions about where you are living.') ?>
					 </div>
					</div>
					<?= $form->field($model, 'demographics_q7', $template)->dropDownList($model->q7,['prompt'=>'Select']) ?>
					<?= $form->field($model, 'demographics_q8', $template)->dropDownList($model->q8,['prompt'=>'Select']) ?>
				    <?= $form->field($model, 'demographics_q9', $template)->textInput(['maxlength' => true])->hint("<strong>INTERVIEWER INSTRUCTION:</strong> If living in a hostel, participant can estimate the number of persons or Code 996 for Not Applicable<br />") ?>
					<?= $form->field($model, 'demographics_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>