<?php

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

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

$Surveys = Common::getSurvey();
$Locations = Common::getLocations();
$PIDs = Common::getNursePIDs();
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::getNursePIDs(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)."'});
	
	$('div.form-group.field-nursevisit-nursefrm_q3_no_needle').removeClass('required').hide();
	$('div.form-group.field-nursevisit-nursefrm_q4_no_condom').removeClass('required').hide();
			
	
	$('#nursevisit-nursefrm_pid').on('select2:select', function (evt) {
		var pid = $(this)[0].value;
		var link = $('#pid-info').data('link') + pid;
		$('#pid-info').removeClass('hidden');
		$('#pid-info').attr('link', link);
		$.ajax({
			type: 'post',
			url: 'ajax',
			data: 'id='+$(this)[0].value,
			success: function(result) {
				var registration = result.registration;
				var labsamples = result.labsamples;
				// console.log(registration);
				// console.log(labsamples);
				if(registration.corfrm_q12 != 1 && registration.corfrm_q12 != 9) {
					$('#nursevisit-nursefrm_q1-1, #nursevisit-nursefrm_q1-3, #nursevisit-nursefrm_q1-4').prop('checked', false).prop('disabled', true);
				} else {
					$('#nursevisit-nursefrm_q1-1, #nursevisit-nursefrm_q1-3, #nursevisit-nursefrm_q1-4').prop('disabled', false);
				}
			}
		});
	});
		
	
	$('#nursevisit-nursefrm_q1 input[type=checkbox]').change(function(){
		var selq1 = [];
		if($(this).prop('checked')) {
			if($(this).val() == '1')
				showmsg('Please Fill CD4, HIV Visit and Viral Load Form');
		}
		$('#nursevisit-nursefrm_q1 input[type=checkbox]').each(function(){
			if($(this).prop('checked')) {
				selq1.push($(this).val());
			} else {
				var index = selq1.indexOf($(this).val());
				if (index > -1) {
					selq1.splice(index, 1);
			    }
			}
		});
		var unique = selq1.filter((v, i, a) => a.indexOf(v) === i);
		$('#nursevisit-hdnq1').val(unique);
	});

	$('#nursevisit-nursefrm_q2 input[type=checkbox]').change(function(){
		var selq1 = [];
		if($(this).prop('checked')) {
			if($(this).val() == '1')
				showmsg('Please Ensure proper doseages in OST Visit Form');
			else if($(this).val() == '4' || $(this).val() == '5')
				showmsg('Please Fill ' + $(this).closest('label').text() + ' Form');
		}
		$('#nursevisit-nursefrm_q2 input[type=checkbox]').each(function(){
			if($(this).prop('checked')) {
				selq1.push($(this).val());
			} else {
				var index = selq1.indexOf($(this).val());
				if (index > -1) {
					selq1.splice(index, 1);
			    }
			}
		});
		var unique = selq1.filter((v, i, a) => a.indexOf(v) === i);
		$('#nursevisit-hdnq2').val(unique);
	});
	
	$('#nursevisit-nursefrm_q2-2').change(function(){
		// $('div.form-group').show();
		$('div.help-block').html('');
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			$('div.form-group.field-nursevisit-nursefrm_q3_no_needle').addClass('required').show();
		} else {
			$('div.form-group.field-nursevisit-nursefrm_q3_no_needle').removeClass('required').hide();
            $('#nursevisit-nursefrm_q3_no_needle').val('');			
		}
	});
	
	$('#nursevisit-nursefrm_q2-3').change(function(){
		// $('div.form-group').show();
		$('div.help-block').html('');
		$('div.form-group').removeClass('has-success').removeClass('has-error');
		if($(this).prop('checked')) {
			$('div.form-group.field-nursevisit-nursefrm_q4_no_condom').addClass('required').show();
		} else {
			$('div.form-group.field-nursevisit-nursefrm_q4_no_condom').removeClass('required').hide();		
            $('#nursevisit-nursefrm_q4_no_condom').val('');			
		}
	});
	
	$('#nursevisit-nursefrm_q2 input[type=checkbox]').trigger('change');
";
if(!$model->isNewRecord) {
	$JS .= "$('#nursevisit-nursefrm_q1 input[type=checkbox], #nursevisit-nursefrm_q2 input[type=checkbox]').trigger('change');";
}

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

<div class="nursevisit-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(['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>',
						];
						if($model->isNewRecord) {
							$disp = "hidden";
							$pid = "";
						} else {
							$disp = "";
							$pid = $model->nursefrm_pid;
						}
						$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 '.$disp.'" href="#" link="'.Url::to(['/dashboard/hivpos', 'pid' => $pid]).'" header="List" data-link="'.Url::to(['/dashboard/hivpos', 'pid' => $pid]).'" data-toggle="modal" data-target="#modal-popup">'.FA::icon('info-circle').'</a>',
						];
						$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, 'nursefrm_sid', $Surveys, 'survey', 'getNursePIDs') ?>
					<?= Common::generateControl($form, $model, $template, 'nursefrm_loc', $Locations, 'location', '', [Html::getInputId($model, 'nursefrm_sid')]) ?>
					<?= Common::generateControl($form, $model, $template, 'nursefrm_pid', $PIDs, 'pid', '', [Html::getInputId($model, 'nursefrm_sid'), Html::getInputId($model, 'nursefrm_loc')]) ?>
					<?= $form->field($model, 'nursefrm_q1', $chklist_template)->checkboxList($model->q1) ?>
					
					<?php if($model->getAppidusite() ==1) : ?>					
					 <?= $form->field($model, 'nursefrm_q2', $chklist_template)->checkboxList($model->q2) ?>
					<?php else: ?>	
					 <?= $form->field($model, 'nursefrm_q2', $chklist_template)->checkboxList($model->q2msm) ?>
					<?php endif; ?>
					
										
					<?= $form->field($model, 'nursefrm_q3_no_needle', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'nursefrm_q4_no_condom', $template)->textInput(['maxlength' => true]) ?>
					
					<?= $form->field($model, 'hdnq1', $htemplate)->hiddenInput() ?>
                   	<?= $form->field($model, 'hdnq2', $htemplate)->hiddenInput() ?>
					<?= $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>