<?php

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

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

$Surveys = Common::getSurvey();
$Locations = Common::getLocations();
$PIDs = Common::getNurseServicePIDs("stitest");
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("stitest", 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)."'});
	
	$('#sti-sti_test_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: 'getregdetails',
				data: 'pid='+$(this)[0].value,
				success: function(response) {
					
					if(response.corfrm_q5 == 1) {
						$('#sti-sti_test_q2-4').prop('checked', false).prop('disabled', true);
					} else {
						$('#sti-sti_test_q2-4').prop('disabled', false);	
					}					
					
				}
			});
		}
	});
	
	$('#sti-sti_test_part_id').on('select2:unselect', function (evt) {
		$('#sti-sti_test_q2-4').prop('disabled', false);
	});

	$('#sti-sti_test_q2-9').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-sti-sti_test_q3').addClass('required').show();
		} else {
			$('div.form-group.field-sti-sti_test_q3').removeClass('required').hide();
			$('#sti-sti_test_q2 input[type=checkbox]').not('#sti-sti_test_q2-9').prop('checked', false);
			$('#sti-sti_test_q3 input[type=checkbox]').not('#sti-sti_test_q3-10').prop('checked', false);
		}
	});

	$('#sti-sti_test_q3-9').change(function(){
		if($(this).prop('checked')) {
			$('#sti-sti_test_q3 input[type=checkbox]').not('#sti-sti_test_q3-9').prop('checked', false);
		}
	});

	$('#sti-sti_test_q2 input[type=checkbox]').change(function(e) {
		var _this = $(this);
		if($('#sti-sti_test_q2-9').prop('checked') && $(this).attr('id') != 'sti-sti_test_q2-9') {
			// console.log(_this.attr('id'));
			_this.prop('checked', false);
			e.preventDefault();
		}
	});

	$('#sti-sti_test_q3 input[type=checkbox]').change(function(e) {
		var _this = $(this);
		if($('#sti-sti_test_q3-9').prop('checked') && $(this).attr('id') != 'sti-sti_test_q3-9') {
			// console.log(_this.attr('id'));
			_this.prop('checked', false);
			e.preventDefault();
		}
	});

	$('#sti-sti_test_q2-9').trigger('change');
";

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

<div class="sti-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-4 checkbox-list">{input}{error}</div>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
					?>
					<?= Common::generateControl($form, $model, $template, 'sti_test_survey', $Surveys, 'survey', 'getNurseServicePIDs', 'stitest') ?>
					<?= Common::generateControl($form, $model, $template, 'sti_test_loc', $Locations, 'location', '', [Html::getInputId($model, 'sti_test_survey')]) ?>
					<?= Common::generateControl($form, $model, $template, 'sti_test_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'sti_test_survey'), Html::getInputId($model, 'sti_test_loc')]) ?>
					<?= $form->field($model, 'sti_test_q1', $template)->dropDownList($model->yes_no, ['prompt'=>'Select']); ?>
					<?= $form->field($model, 'sti_test_q2', $chklist_template)->checkboxList($model->q2); ?>
					<?= $form->field($model, 'sti_test_q3', $chklist_template)->checkboxList($model->q3); ?>
					<?= $form->field($model, 'sti_test_q4', $template)->dropDownList($model->yes_no, ['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>