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

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

$JS = "
	$('#attandance-pid').on('change', function (evt) {
		$('#services').html('');
		if($(this)[0].value != '') {
			$.ajax({
				type: 'post',
				url: 'ajax',
				data: 'id='+$(this)[0].value,
				success: function(result) {
					$('#services').html(result);
					if($('#services').find('.text-danger').length != 0) {
						$('#attandance-servicechk').val(0);
						$('div.form-group.field-attandance-remarks').addClass('required');
					}
					else {
						$('div.form-group.field-attandance-remarks').removeClass('required');
						$('#attandance-servicechk').val(1);
					}
				}
			});
		}
	});
	
	$('#attandance-pid').on('select2:select', function (evt) {
		var pid = $(this)[0].value;		
		if(pid != '') {
			var link = $('#pid-info').data('link') + pid;			
			$.ajax({
				type: 'post',
				url: 'evalinccheck',
				data: 'pid='+$(this)[0].value,
				success: function(response) {	
                    			
					if(response.pid != null) {						
                       $('#modal').modal('show').find('.modal-body').html('Please double check that Evaluation of Incentive Form is completed by Cohort Supervisor.');
					} 
					
				}
			});
		}
	});
	
	

	$('#attandance-pid').on('select2:unselect', function (evt) {
		$('#services').html('');
		$('div.form-group.field-attandance-remarks').removeClass('required');
		$('#attandance-servicechk').val(1);
	});
";
$this->registerJs($JS);

?>

<div class="attandance-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, 'sid', $Surveys, 'survey', 'getAttendanceOutPIDs') ?>
					<?= Common::generateControl($form, $model, $template, 'location', $Locations, 'location', '', [Html::getInputId($model, 'sid')]) ?>
					<?= Common::generateControl($form, $model, $template, 'pid', $PIDs, 'pid', '', [Html::getInputId($model, 'sid'), Html::getInputId($model, 'location')]) ?>
					<div class="col-md-push-2 col-md-8" id="services"></div>
					<?= $form->field($model, 'out_interviewer', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'remarks', $template)->textarea(['maxlength' => true,'rows' => '4']) ?>
					<?= $form->field($model, 'servicechk', $htemplate)->hiddenInput(['value' => 1])->label("") ?>
					<?= $form->field($model, 'status', $htemplate)->hiddenInput(['value' => 1])->label("") ?>
					<?= $form->field($model, '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>