<?php

use app\base\Common;
use yii\web\View;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use rmrevin\yii\fontawesome\FA;
use wbraganca\dynamicform\DynamicFormWidget;

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

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

$headJS = <<<JS
	function medicinemandatory(show)
	{
		if(show) {
			if($('#hivvisit-hiv_visit_q6a').val() != '')
				$('div.form-group.field-hivvisit-hiv_visit_q6').addClass('required');
			else
				$('div.form-group.field-hivvisit-hiv_visit_q6').removeClass('required');
			
			if($('#hivvisit-hiv_visit_q7a').val() != '')
				$('div.form-group.field-hivvisit-hiv_visit_q7').addClass('required');
			else
				$('div.form-group.field-hivvisit-hiv_visit_q7').removeClass('required');
			
			if($('#hivvisit-hiv_visit_q8a').val() != '')
				$('div.form-group.field-hivvisit-hiv_visit_q8').addClass('required');
			else
				$('div.form-group.field-hivvisit-hiv_visit_q8').removeClass('required');

			if($('#hivvisit-hiv_visit_q9a').val() != '')
				$('div.form-group.field-hivvisit-hiv_visit_q9').addClass('required');
			else
				$('div.form-group.field-hivvisit-hiv_visit_q9').removeClass('required');
		} else {
			$('div.form-group.field-hivvisit-hiv_visit_q6, div.form-group.field-hivvisit-hiv_visit_q7, div.form-group.field-hivvisit-hiv_visit_q8, div.form-group.field-hivvisit-hiv_visit_q9').removeClass('required').hide();
		}
	}

	function medicinemandatoryarray(show, index = 0, q5 = 0)
	{
		var len = 0;
		$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').find('select').prop('disabled', true);
		$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q5, label.hiv_visit_q5').closest('td').hide();
		if(show) {
			if($('#hivvisit-' + index + '-hiv_visit_q6a').val() != '' || q5 == 1) {
				len++;
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a').find('select').prop('disabled', false);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6').find('input[type=text]').prop('disabled', false);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6, label.hiv_visit_q6').closest('td').show();
			} else {
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a').find('select').prop('disabled', true);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6').find('input[type=text]').prop('disabled', true);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6, label.hiv_visit_q6').closest('td').hide();
			}
			
			if($('#hivvisit-' + index + '-hiv_visit_q7a').val() != '' || q5 == 1) {
				len++;
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a').find('select').prop('disabled', false);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7').find('input[type=text]').prop('disabled', false);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7, label.hiv_visit_q7').closest('td').show();
			} else {
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a').find('select').prop('disabled', true);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7').find('input[type=text]').prop('disabled', true);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q7, label.hiv_visit_q7').closest('td').hide();
			}
			
			if($('#hivvisit-' + index + '-hiv_visit_q8a').val() != '' || q5 == 1) {
				len++;
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a').find('select').prop('disabled', false);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8').find('input[type=text]').prop('disabled', false);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8, label.hiv_visit_q8').closest('td').show();
			} else {
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a').find('select').prop('disabled', true);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8').find('input[type=text]').prop('disabled', true);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q8, label.hiv_visit_q8').closest('td').hide();
			}

			if($('#hivvisit-' + index + '-hiv_visit_q9a').val() != '' || q5 == 1) {
				len++;
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').find('select').prop('disabled', false);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9').find('input[type=text]').prop('disabled', false);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9, label.hiv_visit_q9').closest('td').show();
			} else {
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').find('select').prop('disabled', true);
				$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9').find('input[type=text]').prop('disabled', true);
				// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q9, label.hiv_visit_q9').closest('td').hide();
			}

			// jQuery('.dynamicform_wrapper thead td').each(function(index) {
			// 	$(this).removeAttr('class');
			// 	if(len == 1) {
			// 		$(this).addClass('col-md-2');
			// 	} else {
			// 		$(this).addClass('col-md-1');
			// 	}
			// });
		} else {
			// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').hide();
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9').find('input[type=text]').prop('disabled', true);
		}
	}

	function q4_change(ctrl) {
		index = $('#'+ctrl.id).closest(".item").find(":input[type=hidden]:first").val();
		$('div.help-block').html('');
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if(ctrl.value == 1) {
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q5').find('select').prop('disabled', false);
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q10').find('select').prop('disabled', false);
			medicinemandatoryarray(true, index);
		} else {
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q5').find('select').prop('disabled', true);
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q10').find('select').prop('disabled', true);
			medicinemandatoryarray(false, index);
		}
		$("#hivvisit-" + index + "-hiv_visit_q5").val(ctrl.value);
		$("#hivvisit-" + index + "-hiv_visit_q5").trigger("change");
	}

	function q5_change(ctrl) {
		index = $('#'+ctrl.id).closest(".item").find(":input[type=hidden]:first").val();
		$('div.help-block').html('');
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if(ctrl.value == 1) {
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').show();
			// $('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9').hide();
			medicinemandatoryarray(true, index, ctrl.value);
		} else {
			$('div.form-group.field-hivvisit-' + index + '-hiv_visit_q6a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q7a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q8a, div.form-group.field-hivvisit-' + index + '-hiv_visit_q9a').hide();
			// $('#hivvisit-hiv_visit_part_id').trigger('select2:select');
			// medicinemandatoryarray(true, index, ctrl.value);
		}
	}
JS;
$this->registerJs($headJS, View::POS_HEAD);

if(!$model->isNewRecord):
	$JS = "
		$('.datepicker').datepicker({autoclose: true, startDate: '01-01-1900', endDate: '0', format: '".strtolower(Yii::$app->formatter->dateFormat)."'});
		
		$('#hivvisit-hiv_visit_part_id').on('select2:select', function (evt) {
			if($(this)[0].value != '') {
				$.ajax({
					type: 'post',
					url: 'ajax',
					data: 'id='+$(this)[0].value,
					success: function(result) {
						var txt7a = result.hiv_linkage_q7a;
						var txt7b = result.hiv_linkage_q7b;
						var txt7c = result.hiv_linkage_q7c;
						var txt7d = result.hiv_linkage_q7d;

						var txt7atxt = result.hiv_linkage_q7atxt;
						var txt7btxt = result.hiv_linkage_q7btxt;
						var txt7ctxt = result.hiv_linkage_q7ctxt;
						var txt7dtxt = result.hiv_linkage_q7dtxt;

						if($('#hivvisit-hiv_visit_q6a').val() != '')
							$('#hivvisit-hiv_visit_q6a').val(txt7a);
						if($('#hivvisit-hiv_visit_q7a').val() != '')
							$('#hivvisit-hiv_visit_q7a').val(txt7b);
						if($('#hivvisit-hiv_visit_q8a').val() != '')
							$('#hivvisit-hiv_visit_q8a').val(txt7c);
						if($('#hivvisit-hiv_visit_q9a').val() != '')
							$('#hivvisit-hiv_visit_q9a').val(txt7d);

						if(txt7a != '' && txt7a != null) {
							var qtxt = '".$model->getAttributeLabel("hiv_visit_q6")."';
							var ctrl = $('.form-group.field-hivvisit-hiv_visit_q6 label');
							var txt = qtxt.replace('[HIVLINK7a]', txt7atxt);
							ctrl.html(txt);
						}
						if(txt7b != '' && txt7b != null) {
							var qtxt = '".$model->getAttributeLabel("hiv_visit_q7")."';
							var ctrl = $('.form-group.field-hivvisit-hiv_visit_q7 label');
							var txt = qtxt.replace('[HIVLINK7b]', txt7btxt);
							ctrl.html(txt);
						}
						if(txt7c != '' && txt7c != null) {
							var qtxt = '".$model->getAttributeLabel("hiv_visit_q8")."';
							var ctrl = $('.form-group.field-hivvisit-hiv_visit_q8 label');
							var txt = qtxt.replace('[HIVLINK7c]', txt7ctxt);
							ctrl.html(txt);
						}
						if(txt7d != '' && txt7d != null) {
							var qtxt = '".$model->getAttributeLabel("hiv_visit_q9")."';
							var ctrl = $('.form-group.field-hivvisit-hiv_visit_q9 label');
							var txt = qtxt.replace('[HIVLINK7d]', txt7dtxt);
							ctrl.html(txt);
						}
					}
				});
			}
		});
		
		$('#hivvisit-hiv_visit_part_id').on('select2:unselect', function (evt) {
		$('#pid_list').html('');		
	});

		$('#hivvisit-hiv_visit_q2').change(function(){
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() == 1) {
				$('div.form-group.field-hivvisit-hiv_visit_q3, div.form-group.field-hivvisit-hiv_visit_q4, div.form-group.field-hivvisit-hiv_visit_q5, div.form-group.field-hivvisit-hiv_visit_q6, div.form-group.field-hivvisit-hiv_visit_q7, div.form-group.field-hivvisit-hiv_visit_q8, div.form-group.field-hivvisit-hiv_visit_q9, div.form-group.field-hivvisit-hiv_visit_q10, div.form-group.field-hivvisit-hiv_visit_q11').addClass('required').show();
				medicinemandatory(true);
			} else {
				$('div.form-group.field-hivvisit-hiv_visit_q3, div.form-group.field-hivvisit-hiv_visit_q4, div.form-group.field-hivvisit-hiv_visit_q5, div.form-group.field-hivvisit-hiv_visit_q6, div.form-group.field-hivvisit-hiv_visit_q7, div.form-group.field-hivvisit-hiv_visit_q8, div.form-group.field-hivvisit-hiv_visit_q9, div.form-group.field-hivvisit-hiv_visit_q10, div.form-group.field-hivvisit-hiv_visit_q11').removeClass('required').hide();
				medicinemandatory(false);
			}
		});

		$('#hivvisit-hiv_visit_q4').change(function(){
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() == 1) {
				$('div.form-group.field-hivvisit-hiv_visit_q5, div.form-group.field-hivvisit-hiv_visit_q6, div.form-group.field-hivvisit-hiv_visit_q7, div.form-group.field-hivvisit-hiv_visit_q8, div.form-group.field-hivvisit-hiv_visit_q9').addClass('required').show();
				medicinemandatory(true);
			} else {
				$('div.form-group.field-hivvisit-hiv_visit_q5, div.form-group.field-hivvisit-hiv_visit_q6, div.form-group.field-hivvisit-hiv_visit_q7, div.form-group.field-hivvisit-hiv_visit_q8, div.form-group.field-hivvisit-hiv_visit_q9').removeClass('required').hide();
				medicinemandatory(false);
			}
		});
		$('#hivvisit-hiv_visit_q2, #hivvisit-hiv_visit_q4').trigger('change');
	";
else:
	$JS = "
		$('.datepicker').datepicker({autoclose: true, startDate: '01-01-1900', endDate: '0', format: '".strtolower(Yii::$app->formatter->dateFormat)."'});
		
		$('#hivvisit-hiv_visit_part_id').on('select2:select', function (evt) {
			var pid = $(this)[0].value;
			if(pid != '') {
				var link = $('#pid-list').data('link') + pid;
				$('#pid-list').attr('link', link);
				$('#pid_list').load(link);
				var link = $('#pid-info').data('link') + pid;
				$('#pid-info').removeClass('hidden');
				$('#pid-info').attr('link', link);
				$.ajax({
					type: 'post',
					url: 'ajax',
					data: 'id='+pid,
					success: function(result) {
						var txt7a = result.hiv_linkage_q7a;
						var txt7b = result.hiv_linkage_q7b;
						var txt7c = result.hiv_linkage_q7c;
						var txt7d = result.hiv_linkage_q7d;

						var txt7atxt = result.hiv_linkage_q7atxt;
						var txt7btxt = result.hiv_linkage_q7btxt;
						var txt7ctxt = result.hiv_linkage_q7ctxt;
						var txt7dtxt = result.hiv_linkage_q7dtxt;

						jQuery('.dynamicform_wrapper .item').each(function(index) {
							$('#hivvisit-' + index + '-hiv_visit_part_id').val(pid);
							$('#hivvisit-' + index + '-hiv_visit_survey').val($('#hivvisit-hiv_visit_survey').val());
							$('#hivvisit-' + index + '-hiv_visit_loc').val($('#hivvisit-hiv_visit_loc').val());
							// $('#hivvisit-' + index + '-hiv_visit_q6a').val(txt7a);
							// $('#hivvisit-' + index + '-hiv_visit_q7a').val(txt7b);
							// $('#hivvisit-' + index + '-hiv_visit_q8a').val(txt7c);
							// $('#hivvisit-' + index + '-hiv_visit_q9a').val(txt7d);

							if(txt7a != '' && txt7a != null) {
								var qtxt = '".$model->getAttributeLabel("hiv_visit_q6")."';
								var ctrl = $('label.hiv_visit_q6');
								var txt = qtxt.replace('[HIVLINK7a]', txt7atxt);
								ctrl.html(txt);
							}
							if(txt7b != '' && txt7b != null) {
								var qtxt = '".$model->getAttributeLabel("hiv_visit_q7")."';
								var ctrl = $('label.hiv_visit_q7');
								var txt = qtxt.replace('[HIVLINK7b]', txt7btxt);
								ctrl.html(txt);
							}
							if(txt7c != '' && txt7c != null) {
								var qtxt = '".$model->getAttributeLabel("hiv_visit_q8")."';
								var ctrl = $('label.hiv_visit_q8');
								var txt = qtxt.replace('[HIVLINK7c]', txt7ctxt);
								ctrl.html(txt);
							}
							if(txt7d != '' && txt7d != null) {
								var qtxt = '".$model->getAttributeLabel("hiv_visit_q9")."';
								var ctrl = $('label.hiv_visit_q9');
								var txt = qtxt.replace('[HIVLINK7d]', txt7dtxt);
								ctrl.html(txt);
							}
							medicinemandatoryarray(true, index);
					    });
					    $('.q4').trigger('change');
					}
				});
			}
		});
		
		$('#hivvisit-hiv_visit_part_id').on('select2:unselect', function (evt) {
		$('#pid_list').html('');		
	});

		$('#hivvisit-hiv_visit_survey').change(function(){
			var sid = $(this).val();
			jQuery('.dynamicform_wrapper .item').each(function(index) {
				$('#hivvisit-' + index + '-hiv_visit_survey').val(sid);
		    });
		});
		
		$('#hivvisit-hiv_visit_q1').change(function(){
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() != '') {
				var q1val = $(this).val();
					jQuery('.dynamicform_wrapper .item').each(function(index) {
						$('#hivvisit-' + index + '-hiv_visit_q1').val(q1val);					
				    });				
			} 			
		});

		$('#hivvisit-hiv_visit_q2').change(function(){
			// $('div.form-group').show();
			$('div.help-block').html('');
			$('div.form-group').removeClass('has-success').removeClass('has-error');
			if($(this).val() != '') {
				var q2val = $(this).val();
				if($(this).val() == 1) {
					$('.dynamicform_wrapper').show();
					jQuery('.dynamicform_wrapper .item').each(function(index) {
						$('#hivvisit-' + index + '-hiv_visit_q2').val(q2val);
						medicinemandatoryarray(true, index);
				    });
				} else {
					$('.dynamicform_wrapper').hide();
					jQuery('.dynamicform_wrapper .item').each(function(index) {
						$('#hivvisit-' + index + '-hiv_visit_q2').val(q2val);
						medicinemandatoryarray(false, index);
				    });
				}
			} else {
				$('.dynamicform_wrapper').hide();
			}
		});

		$('#hivvisit-hiv_visit_q2, .q4').trigger('change');
	";
endif;
	$this->registerJs($JS);

$js = '
jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
    jQuery(".dynamicform_wrapper .item").each(function(index) {
        var txt7a = $("#hivvisit-0-hiv_visit_q6a").val();
		var txt7b = $("#hivvisit-0-hiv_visit_q7a").val();
		var txt7c = $("#hivvisit-0-hiv_visit_q8a").val();
		var txt7d = $("#hivvisit-0-hiv_visit_q9a").val();
		var status = $("#hivvisit-0-status").val();
		var user = $("#hivvisit-0-create_user").val();
		
		$("#hivvisit-" + index + "-rows").val(index);
		$("#hivvisit-" + index + "-hiv_visit_survey").val($("#hivvisit-hiv_visit_survey").val());
		$("#hivvisit-" + index + "-hiv_visit_part_id").val($("#hivvisit-hiv_visit_part_id").val());
		$("#hivvisit-" + index + "-hiv_visit_loc").val($("#hivvisit-hiv_visit_loc").val());
		$("#hivvisit-" + index + "-hiv_visit_q1").val($("#hivvisit-hiv_visit_q1").val());
		$("#hivvisit-" + index + "-hiv_visit_q2").val($("#hivvisit-hiv_visit_q2").val());
		
		if($("#hivvisit-" + index + "-hiv_visit_q6a").val() == "")
			$("#hivvisit-" + index + "-hiv_visit_q6a").val(txt7a);
		if($("#hivvisit-" + index + "-hiv_visit_q7a").val() == "")
			$("#hivvisit-" + index + "-hiv_visit_q7a").val(txt7b);
		if($("#hivvisit-" + index + "-hiv_visit_q8a").val() == "")
			$("#hivvisit-" + index + "-hiv_visit_q8a").val(txt7c);
		if($("#hivvisit-" + index + "-hiv_visit_q9a").val() == "")
			$("#hivvisit-" + index + "-hiv_visit_q9a").val(txt7d);
		$("#hivvisit-" + index + "-status").val(status);
		$("#hivvisit-" + index + "-create_user").val(user);
		
		$("div.form-group.field-hivvisit-" + index + "-hiv_visit_q6, div.form-group.field-hivvisit-" + index + "-hiv_visit_q7, div.form-group.field-hivvisit-" + index + "-hiv_visit_q8, div.form-group.field-hivvisit-" + index + "-hiv_visit_q9").find("input[type=text]").prop("disabled", true);

		// if(txt7a == "" || txt7a == null) {
		// 	$("div.form-group.field-hivvisit-" + index + "-hiv_visit_q6").closest("td").hide();
		// }
		// if(txt7b == "" || txt7b == null) {
		// 	$("div.form-group.field-hivvisit-" + index + "-hiv_visit_q7").closest("td").hide();
		// }
		// if(txt7c == "" || txt7c == null) {
		// 	$("div.form-group.field-hivvisit-" + index + "-hiv_visit_q8").closest("td").hide();
		// }
		// if(txt7d == "" || txt7d == null) {
		// 	$("div.form-group.field-hivvisit-" + index + "-hiv_visit_q9").closest("td").hide();
		// }
		// $("#hivvisit-" + index + "-hiv_visit_q1, #hivvisit-" + index + "-hiv_visit_q2").trigger("change");
    });
    $(".datepicker").datepicker("destroy");
	addPicker("'.strtolower(Yii::$app->formatter->dateFormat).'");
	$(".q4").trigger("change");
});

jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
    jQuery(".dynamicform_wrapper .panel-title-address").each(function(index) {
        jQuery(this).html("Row: " + (index + 1))
    });
});
';
$this->registerJs($js);
?>

<div class="hivvisit-form">
	<div class="panel panel-default">
		<div class="panel-heading">
			
		</div>
		<div class="panel-body">
			<div class="row">
				<div class="col-lg-12">
					<?php
						$ajaxdata['hiv_visit_part_id'] = '';
						$form = ActiveForm::begin(['enableAjaxValidation' => true, 'id' => 'dynamic-form', '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>',
						];
						$template_icon = [
							'labelOptions' => ['class' => 'form-label'],
							'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-4">{input}{error}</div><a id="pid-info" class="btn btn-warning hidden"  href="#" link="'.Url::to(['getlist', 'pid' => ""]).'" header="List" data-link="'.Url::to(['getlist', 'pid' => ""]).'" data-toggle="modal" data-target="#modal-popup">'.FA::icon('info-circle').'</a><a id="pid-list" class="btn btn-warning hidden" href="#" link="'.Url::toRoute(['/hivvisit', 'ajaxload' => true, 'pagesize' => 6, 'HivvisitSearch' => $ajaxdata]).'" header="List" data-link="'.Url::toRoute(['/hivvisit', 'ajaxload' => true, 'pagesize' => 6, 'HivvisitSearch' => $ajaxdata]).'" data-toggle="modal" data-target="#modal-popup">'.FA::icon('info-circle').'</a>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
						$dynamictemplate = [
							// 'options' => ['tag' => false],
							// 'template' => '<div class="col-md-12">{input}{error}{hint}</div>'
							'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-4">{input}{error}</div>',
						];
						$dynamictemplate1 = [
							// 'options' => ['tag' => false],
							'template' => '<div class="col-md-12">{input}{error}{hint}</div>'
						];
					?>
					<?php // echo $form->errorSummary($model); ?>
					<?= Common::generateControl($form, $model, $template, 'hiv_visit_survey', $Surveys, 'survey', 'getNurseServicePIDs', 'hivvisit') ?>
					<?= Common::generateControl($form, $model, $template, 'hiv_visit_loc', $Locations, 'location', '', [Html::getInputId($model, 'hiv_visit_survey')]) ?>
					<?= Common::generateControl($form, $model, $template_icon, 'hiv_visit_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'hiv_visit_survey'), Html::getInputId($model, 'hiv_visit_loc')]) ?>
					<?= $form->field($model, 'hiv_visit_q1', $template)->dropDownList($model->q1, ['prompt' => 'Select']) ?>
					<?= $form->field($model, 'hiv_visit_q2', $template)->dropDownList($model->q2, ['prompt' => 'Select']) ?>
					<?php if(!$model->isNewRecord): ?>
						<?= $form->field($model, 'hiv_visit_q3', $template)->textInput(['class' => 'form-control datepicker']) ?>
						<?= $form->field($model, 'hiv_visit_q4', $template)->dropDownList($model->yes_no, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q5', $template)->dropDownList($model->yes_no, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q6a', $template)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q6', $template)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
						<?= $form->field($model, 'hiv_visit_q7a', $template)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q7', $template)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
						<?= $form->field($model, 'hiv_visit_q8a', $template)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q8', $template)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
						<?= $form->field($model, 'hiv_visit_q9a', $template)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
						<?= $form->field($model, 'hiv_visit_q9', $template)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
						<?= $form->field($model, 'hiv_visit_q10', $template)->dropDownList($model->q10, ['prompt' => 'Select'])->hint("<strong>INTERVIEWER INSTRUCTIONS:</strong> Visit site must be on the list") ?>
						<?= $form->field($model, 'hiv_visit_q11', $template)->dropDownList($model->q11, ['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("") ?>
					<?php else: ?>
						<div id="pid_list" class="row-fluid popup"></div>
						<?php
						    DynamicFormWidget::begin([
						        'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
						        'widgetBody' => '.container-items', // required: css class selector
						        'widgetItem' => '.item', // required: css class
						        'limit' => 999, // the maximum times, an element can be cloned (default 999)
						        'min' => 1, // 0 or 1 (default 1)
						        'insertButton' => '.add-item', // css class
						        'deleteButton' => '.remove-item', // css class
						        'model' => $models[0],
						        'formId' => 'dynamic-form',
						        'formFields' => [
						            'hiv_visit_q1',
						            'hiv_visit_q2',
						            'hiv_visit_q3',
						            'hiv_visit_q4',
						            'hiv_visit_q5',
						            'hiv_visit_q6',
						            'hiv_visit_q7',
						            'hiv_visit_q8',
						            'hiv_visit_q9',
						            'hiv_visit_q10',
						            'hiv_visit_q11',
						        ],
						    ]);
					    ?>
						<table class="table table-bordered container-items" border="0" cellpadding="0" cellspacing="0" align="center">
							<thead>
								<tr>
									<td><?= Html::activeLabel($model,'hiv_visit_q3', ['class'=> 'hiv_visit_q3']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q4', ['class'=> 'hiv_visit_q4']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q5', ['class'=> 'hiv_visit_q5']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q6', ['class'=> 'hiv_visit_q6']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q7', ['class'=> 'hiv_visit_q7']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q8', ['class'=> 'hiv_visit_q8']); ?></td>
									<td><?= Html::activeLabel($model,'hiv_visit_q9', ['class'=> 'hiv_visit_q9']); ?></td>
									<td>
										<?= Html::activeLabel($model,'hiv_visit_q10', ['class'=> 'hiv_visit_q10']); ?>
										<div class="hint-block"><strong>INTERVIEWER INSTRUCTIONS:</strong> Visit site must be on the list</div>
									</td>
									<td><?= Html::activeLabel($model,'hiv_visit_q11', ['class'=> 'hiv_visit_q11']); ?></td>
									<td align="center">
										<button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
									</td>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($models as $index => $modelAddress): ?>
									<?php
										if($modelAddress->isNewRecord)
											$modelAddress->hiv_visit_q5 = 1;
									?>
									<tr class="item">
										<td>
											<input type="hidden" id="hivvisit-0-rows" class="form-control" name="Hivvisit[0][rows]" value="0">
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_survey", $htemplate)->hiddenInput()->label() ?>
			                                <?= $form->field($modelAddress, "[{$index}]hiv_visit_part_id", $htemplate)->hiddenInput()->label() ?>
			                                <?= $form->field($modelAddress, "[{$index}]hiv_visit_loc", $htemplate)->hiddenInput()->label() ?>
			                                <?= $form->field($modelAddress, "[{$index}]hiv_visit_q1", $htemplate)->hiddenInput()->label() ?>
			                                <?= $form->field($modelAddress, "[{$index}]hiv_visit_q2", $htemplate)->hiddenInput()->label() ?>
			                                <?= $form->field($modelAddress, "[{$index}]hiv_visit_q3", $dynamictemplate1)->textInput(['class' => 'form-control datepicker']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q4", $dynamictemplate1)->dropDownList($model->yes_no, ['class' => 'form-control q4', 'prompt' => 'Select', 'onchange' => "q4_change(this);"]) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q5", $dynamictemplate1)->dropDownList($model->yes_no, ['prompt' => 'Select', 'onchange' => "q5_change(this);"]) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q6a", $dynamictemplate1)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q6", $dynamictemplate1)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q7a", $dynamictemplate1)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q7", $dynamictemplate1)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q8a", $dynamictemplate1)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q8", $dynamictemplate1)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q9a", $dynamictemplate1)->dropDownList($ARVs, ['prompt' => 'Select']) ?>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q9", $dynamictemplate1)->textInput(['maxlength' => true, 'data-min' => 0, 'data-max' => 60, 'data-msg' => 'Please double-check that this answer is correct']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q10", $dynamictemplate1)->dropDownList($model->q10, ['prompt' => 'Select']) ?>
										</td>
										<td>
											<?= $form->field($modelAddress, "[{$index}]hiv_visit_q11", $dynamictemplate1)->dropDownList($model->q11, ['prompt' => 'Select']) ?>
											<?= $form->field($modelAddress, "[{$index}]status", $htemplate)->hiddenInput(['value' => 1])->label() ?>
											<?= $form->field($modelAddress, $modelAddress->isNewRecord ? "[{$index}]create_user" : "[{$index}]update_user", $htemplate)->hiddenInput(['value' => yii::$app->user->identity->id])->label("") ?>
										</td>
										<td align="center">
											<button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					    <?php DynamicFormWidget::end(); ?>
					<?php endif;?>

					<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>