<?php

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

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

$Surveys = Common::getSurvey();
$Locations = Common::getLocations();
$PIDs = Common::getHivLinkagePIDs();
$ARVs = Common::getARVMasters();
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::getHivLinkagePIDs(Yii::$app->params['SURVEY'], Yii::$app->params['LOCATION']);
}

$JS = "
	$('.datepicker').datepicker({autoclose: true, startDate: '01-01-1900', endDate: '0', format: '".strtolower(Yii::$app->formatter->dateFormat)."'});
	
	var q15;
	
	$('#hivlinkage-hiv_linkage_part_id').on('select2:select', function (evt) {
		var pid = $(this)[0].value;
		q15='';
		if(pid != '') {
			var link = $('#pid-info').data('link') + pid;			
			$.ajax({
				type: 'post',
				url: 'getregdetails',
				data: 'pid='+$(this)[0].value,
				success: function(response) {
					q15=response.corfrm_q15;
					if(response.corfrm_q12 != null) {
						$('#hivlinkage-hiv_linkage_q1').val(response.corfrm_q12);
						if(response.lastHIVTestDate != '-' && response.corfrm_q12 != 9)
							$('#hivlinkage-hiv_linkage_q2').val(response.lastHIVTestDate);
						else
							$('#hivlinkage-hiv_linkage_q2').val('');
					} else {
						$('#hivlinkage-hiv_linkage_q1').val('');
						// $('#hivlinkage-hiv_linkage_q2').val('');
					}
					
				}
			});
		}
	});
		
	
	$('#hivlinkage-hiv_linkage_part_id').on('select2:unselect', function (evt) {
		$('#hivlinkage-hiv_linkage_q1').val('');
		$('#hivlinkage-hiv_linkage_q2').val('');
	});

	$('#hivlinkage-hiv_linkage_q2a').change(function(){
		// $('div.form-group').show();
		$('div.help-block').html('');
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).val() != '' && $(this).val() == 1) {
			$('div.form-group.field-hivlinkage-hiv_linkage_q2b, div.form-group.field-hivlinkage-hiv_linkage_q3, div.form-group.field-hivlinkage-hiv_linkage_q5').addClass('required').show();
		} else {
			$('div.form-group.field-hivlinkage-hiv_linkage_q2b, div.form-group.field-hivlinkage-hiv_linkage_q3, div.form-group.field-hivlinkage-hiv_linkage_q5').removeClass('required').hide();
			
			$('#hivlinkage-hiv_linkage_q2b, #hivlinkage-hiv_linkage_q3, #hivlinkage-hiv_linkage_q5').val('');
		}
		
		if($(this).val() != '' && $(this).val() != 1 && q15==1) {
			$('#modal').modal('show').find('.modal-body').html('Registration form indicates client has an ART book.<br/> Please double check your answer.');
		}
		
		$('#hivlinkage-hiv_linkage_q3, #hivlinkage-hiv_linkage_q5, #hivlinkage-hiv_linkage_q7a, #hivlinkage-hiv_linkage_q7b, #hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').trigger('change');
	});

	$('#hivlinkage-hiv_linkage_q3').change(function(){
		if($('#hivlinkage-hiv_linkage_q2a').val() == 1) {
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() != '') {
				$('div.form-group.field-hivlinkage-hiv_linkage_q4').addClass('required').show();
			} else {
				$('div.form-group.field-hivlinkage-hiv_linkage_q4').removeClass('required').hide();
				
				$('#hivlinkage-hiv_linkage_q4').val('');
			}
		} else {
			$('div.form-group.field-hivlinkage-hiv_linkage_q4').removeClass('required').hide();
			
			$('#hivlinkage-hiv_linkage_q4').val('');
		}
	});

	$('#hivlinkage-hiv_linkage_q5').change(function(){
		if($('#hivlinkage-hiv_linkage_q2a').val() == 1) {
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() != '') {
				$('div.form-group.field-hivlinkage-hiv_linkage_q3, div.form-group.field-hivlinkage-hiv_linkage_q6, div.form-group.field-hivlinkage-hiv_linkage_q7a').addClass('required').show();
				$('div.form-group.field-hivlinkage-hiv_linkage_q7b, div.form-group.field-hivlinkage-hiv_linkage_q7c, div.form-group.field-hivlinkage-hiv_linkage_q7d, div.form-group.field-hivlinkage-hiv_linkage_q8').show();
			} else {
				$('div.form-group.field-hivlinkage-hiv_linkage_q3').removeClass('required');
				$('div.form-group.field-hivlinkage-hiv_linkage_q6, div.form-group.field-hivlinkage-hiv_linkage_q8').removeClass('required').hide();
				$('div.form-group.field-hivlinkage-hiv_linkage_q7a, div.form-group.field-hivlinkage-hiv_linkage_q7b, div.form-group.field-hivlinkage-hiv_linkage_q7c, div.form-group.field-hivlinkage-hiv_linkage_q7d').removeClass('required').hide();
				
				$('#hivlinkage-hiv_linkage_q6, #hivlinkage-hiv_linkage_q8').val('');
                $('#hivlinkage-hiv_linkage_q7a, #hivlinkage-hiv_linkage_q7b, #hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').val('');
			}
		} else {
			$('div.form-group.field-hivlinkage-hiv_linkage_q3').removeClass('required');
			$('div.form-group.field-hivlinkage-hiv_linkage_q6, div.form-group.field-hivlinkage-hiv_linkage_q8').removeClass('required').hide();
			$('div.form-group.field-hivlinkage-hiv_linkage_q7a, div.form-group.field-hivlinkage-hiv_linkage_q7b, div.form-group.field-hivlinkage-hiv_linkage_q7c, div.form-group.field-hivlinkage-hiv_linkage_q7d').removeClass('required').hide();
						
            $('#hivlinkage-hiv_linkage_q6, #hivlinkage-hiv_linkage_q8').val('');
            $('#hivlinkage-hiv_linkage_q7a, #hivlinkage-hiv_linkage_q7b, #hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').val('');
		}
	});
	
	// $('#hivlinkage-hiv_linkage_q7a').change(function(){
	// 	// $('div.form-group').show();
	// 	$('div.help-block').html('');
	// 	$('div.form-group').removeClass('has-success').removeClass('has-error');
	// 	if($(this).val() != '') {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty').addClass('required').show();
	// 	} else {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty').removeClass('required').hide();
	// 	}
	// });
	
	// $('#hivlinkage-hiv_linkage_q7b').change(function(){
	// 	// $('div.form-group').show();
	// 	$('div.help-block').html('');
	// 	$('div.form-group').removeClass('has-success').removeClass('has-error');
	// 	if($(this).val() != '') {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7bqty').addClass('required').show();
	// 	} else {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7bqty').removeClass('required').hide();
	// 	}
	// });
	
	// $('#hivlinkage-hiv_linkage_q7c').change(function(){
	// 	// $('div.form-group').show();
	// 	$('div.help-block').html('');
	// 	$('div.form-group').removeClass('has-success').removeClass('has-error');
	// 	if($(this).val() != '') {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7cqty').addClass('required').show();
	// 	} else {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7cqty').removeClass('required').hide();
	// 	}
	// });
	
	// $('#hivlinkage-hiv_linkage_q7d').change(function(){
	// 	// $('div.form-group').show();
	// 	$('div.help-block').html('');
	// 	$('div.form-group').removeClass('has-success').removeClass('has-error');
	// 	if($(this).val() != '') {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7dqty').addClass('required').show();
	// 	} else {
	// 		$('div.form-group.field-hivlinkage-hiv_linkage_q7dqty').removeClass('required').hide();
	// 	}
	// });
	
	$('#hivlinkage-hiv_linkage_q7 input[type=checkbox]').change(function(){
		$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty, div.form-group.field-hivlinkage-hiv_linkage_q7bqty, div.form-group.field-hivlinkage-hiv_linkage_q7cqty, div.form-group.field-hivlinkage-hiv_linkage_q7dqty').removeClass('required').hide();
		
		$('#hivlinkage-hiv_linkage_q7aqty, #hivlinkage-hiv_linkage_q7bqty, #hivlinkage-hiv_linkage_q7cqty, #hivlinkage-hiv_linkage_q7dqty').val('');
		
		var len = $('#hivlinkage-hiv_linkage_q7 input[type=checkbox]:checked').length;
		if(len == 1) {
			$('#hivlinkage-hiv_linkage_q7b, #hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').val('');
			$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty').addClass('required').show();
		} else if(len == 2) {
			$('#hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').val('');
			$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty, div.form-group.field-hivlinkage-hiv_linkage_q7bqty').addClass('required').show();
		} else if(len == 3) {
			$('#hivlinkage-hiv_linkage_q7d').val('');
			$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty, div.form-group.field-hivlinkage-hiv_linkage_q7bqty, div.form-group.field-hivlinkage-hiv_linkage_q7cqty').addClass('required').show();
		} else if(len >= 4) {
			$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty, div.form-group.field-hivlinkage-hiv_linkage_q7bqty, div.form-group.field-hivlinkage-hiv_linkage_q7cqty, div.form-group.field-hivlinkage-hiv_linkage_q7dqty').addClass('required').show();
		}
		if(len > 4) {
			$(this).prop('checked', false);
		}
		$('#hivlinkage-hiv_linkage_q7 input[type=checkbox]:checked').each(function(index){
			var value = $(this).val();
			var label = $(this).closest('label').text().trim() + ' Qty';
			var qcode = '';
			if (index == 0)
				qcode = 'a';
			else if (index == 1)
				qcode = 'b';
			else if (index == 2)
				qcode = 'c';
			else if (index == 3)
				qcode = 'd';
			$('#hivlinkage-hiv_linkage_q7'+qcode).val(value);
			$('div.form-group.field-hivlinkage-hiv_linkage_q7'+qcode+'qty .form-label').html(label);
		});
	});

	// $('div.form-group.field-hivlinkage-hiv_linkage_q7a, div.form-group.field-hivlinkage-hiv_linkage_q7b, div.form-group.field-hivlinkage-hiv_linkage_q7c, div.form-group.field-hivlinkage-hiv_linkage_q7d').removeClass('required').hide();
	$('div.form-group.field-hivlinkage-hiv_linkage_q7aqty, div.form-group.field-hivlinkage-hiv_linkage_q7bqty, div.form-group.field-hivlinkage-hiv_linkage_q7cqty, div.form-group.field-hivlinkage-hiv_linkage_q7dqty').removeClass('required').hide();
	$('div.form-group.field-hivlinkage-hiv_linkage_q7').removeClass('required').hide();
	// $('#hivlinkage-hiv_linkage_q3, #hivlinkage-hiv_linkage_q5, #hivlinkage-hiv_linkage_q7 input[type=checkbox]').trigger('change');
	// $('#hivlinkage-hiv_linkage_q3, #hivlinkage-hiv_linkage_q5, #hivlinkage-hiv_linkage_q7a, #hivlinkage-hiv_linkage_q7b, #hivlinkage-hiv_linkage_q7c, #hivlinkage-hiv_linkage_q7d').trigger('change');
	$('#hivlinkage-hiv_linkage_q2a').trigger('change');
";

$this->registerJs($JS);

// if($model->isNewRecord)
// 	$model->hiv_linkage_q1 = 1;
?>

<div class="hivlinkage-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']]);
						$form = ActiveForm::begin(['enableAjaxValidation' => true, '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-4 checkbox-list">{input}{error}</div>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
					?>
					<?= Common::generateControl($form, $model, $template, 'hiv_linkage_survey', $Surveys, 'survey', 'getHivLinkagePIDs') ?>
					<?= Common::generateControl($form, $model, $template, 'hiv_linkage_loc', $Locations, 'location', '', [Html::getInputId($model, 'hiv_linkage_survey')]) ?>
					<?= Common::generateControl($form, $model, $template, 'hiv_linkage_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'hiv_linkage_survey'), Html::getInputId($model, 'hiv_linkage_loc')]) ?>
					<?= $form->field($model, 'hiv_linkage_q1', $template)->dropDownList($model->q1, ['options' => [0 => ['disabled' => true],2 => ['disabled' => true],3 => ['disabled' => true]],'prompt' => 'Select']) ?>
					<?= $form->field($model, 'hiv_linkage_q2', $template)->textInput(['class' => 'form-control datepicker'])->hint("<strong>INTERVIEWER INSTRUCTIONS:</strong> If the client has never been tested in the ICC, please obtain the approximate date of HIV diagnosis from the government ART book") ?>
					<?= $form->field($model, 'hiv_linkage_q2a', $template)->dropDownList($model->yes_no, ['prompt' => 'Select']) ?>
					<?= $form->field($model, 'hiv_linkage_q2b', $template)->dropDownList($model->q2b, ['prompt' => 'Select']) ?>
					<?= $form->field($model, 'hiv_linkage_q3', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'hiv_linkage_q4', $template)->textInput(['class' => 'form-control datepicker']) ?>
					<?= $form->field($model, 'hiv_linkage_q5', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'hiv_linkage_q6', $template)->textInput(['class' => 'form-control datepicker']) ?>
					<?= $form->field($model, 'hiv_linkage_q7', $chklist_template)->checkboxList($model->q7) ?>
					
					<?= $form->field($model, 'hiv_linkage_q7a', $template)->dropDownList($ARVs, ['prompt'=>'Select']); ?>
                    <?= $form->field($model, 'hiv_linkage_q7b', $template)->dropDownList($ARVs, ['prompt'=>'Select']); ?>
                    <?= $form->field($model, 'hiv_linkage_q7c', $template)->dropDownList($ARVs, ['prompt'=>'Select']); ?>
                    <?= $form->field($model, 'hiv_linkage_q7d', $template)->dropDownList($ARVs, ['prompt'=>'Select']); ?>
					
					<?php // $form->field($model, 'hiv_linkage_q7a', $template)->widget(Select2::classname(), ['data' => $ARVs, 'options' => ['prompt' => 'Select'], 'pluginOptions' => ['allowClear' => true]]) ?>
					<?= $form->field($model, 'hiv_linkage_q7aqty', $template)->textInput(['maxlength' => true]) ?>
					<?php // $form->field($model, 'hiv_linkage_q7b', $template)->widget(Select2::classname(), ['data' => $ARVs, 'options' => ['prompt' => 'Select'], 'pluginOptions' => ['allowClear' => true]]) ?>
					<?= $form->field($model, 'hiv_linkage_q7bqty', $template)->textInput(['maxlength' => true]) ?>
					<?php // $form->field($model, 'hiv_linkage_q7c', $template)->widget(Select2::classname(), ['data' => $ARVs, 'options' => ['prompt' => 'Select'], 'pluginOptions' => ['allowClear' => true]]) ?>
					<?= $form->field($model, 'hiv_linkage_q7cqty', $template)->textInput(['maxlength' => true]) ?>
					<?php // $form->field($model, 'hiv_linkage_q7d', $template)->widget(Select2::classname(), ['data' => $ARVs, 'options' => ['prompt' => 'Select'], 'pluginOptions' => ['allowClear' => true]]) ?>
					<?= $form->field($model, 'hiv_linkage_q7dqty', $template)->textInput(['maxlength' => true]) ?>
					<?php // echo $form->field($model, 'hiv_linkage_q8', $template)->textInput(['class' => 'form-control datepicker']); ?>
					
					
					<?= $form->field($model, 'hiv_linkage_q8', $htemplate)->hiddenInput()->label() ?>
					<?= $form->field($model, 'oldenddate', $htemplate)->hiddenInput()->label("") ?>
					<?= $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>