<?php

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

$sloc=Yii::$app->user->identity->signedin_loc;
$sdistrict=Yii::$app->user->identity->district;

$Referred = Common::getReferredMaster();

$Staffnames= Common::getStaffnames();
$Staffcodes= Common::getStaffcodes();

$Block= Common::getSiteblock();


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

$headJS = <<<JS

function nospaces(ctrl){
   if(ctrl.value.match(/\s/g)){
      ctrl.value=ctrl.value.replace(/\s/g,'');
   }
  }
  
JS;
$this->registerJs($headJS, View::POS_HEAD);

$JS = "
	$('.datepicker').datepicker({autoclose: true, startDate: '0',  format: '".strtolower(Yii::$app->formatter->dateFormat)."'});
	
	$('#appointment-staff_name').on('select2:select', function (evt) {		
		var uid = $(this)[0].value;	
         // console.log(uid); 		
		if(uid != '') {
			var link = $('#uid-info').data('link') + uid;			
			$.ajax({
				type: 'post',
				url: 'staff',
				data: 'uid='+$(this)[0].value,
				success: function(response) {                  		
					if(response != null) {			
						$('#appointment-staff_code').val(response.staff_code);                         						
					} else {						
						$('#appointment-staff_code').val('');                             						
					}
					
				}
			});
		}
	});	
	   
   $('#appointment-staff_name').on('select2:unselect', function (evt) {  
			$('#appointment-staff_code').val('');			
			
   });
	
	";
$this->registerJs($JS);

?>

<div class="departments-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-4">{label}{hint}</div><div class="col-sm-4">{input}{error}</div>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];									
						
					?>	
						
						<?php if($model->isNewRecord) : ?>	
						  <?= $form->field($model, 'district', $template)->textInput(['value' => $sdistrict,'readonly' => true]) ?>	
						  <?= $form->field($model, 'block', $template)->dropDownList($Block,['options' => [$sloc => ['Selected'=>'selected']],'prompt' =>'Select' ]) ?>	
						<?php else: ?>	
						  <?= $form->field($model, 'district', $template)->textInput(['value'=>$model->district,'readonly' => true]) ?> 
						  <?= $form->field($model, 'block', $template)->textInput(['value'=>$model->block,'readonly' => true]) ?>	
						  <?= $form->field($model, 'appointment_id', $template)->textInput(['value'=>$model->appointment_id,'readonly' => true]) ?>
						<?php endif; ?>
				       						 
						 <?= $form->field($model, 'staff_name', $template)->widget(Select2::classname(), [	    
									'data' => $Staffnames,
									'options' => ['placeholder' => 'Select Staff Name'],
									'pluginOptions' => ['allowClear' => true ],
								 ]); ?>	
					     <?= $form->field($model, 'staff_code', $template)->dropDownList($Staffcodes, ['prompt' => 'Select']) ?>
						 <?= $form->field($model, 'referred_source', $template)->dropDownList($Referred, ['prompt' => 'Select']) ?>						 
						 <?= $form->field($model, 'dose', $template)->radioList($model->dosage,['separator'=>'&nbsp&nbsp&nbsp&nbsp'])?>	
                         <?= $form->field($model, 'beneficiary_name', $template)->textInput() ?>	
						 <?= $form->field($model, 'mobile_number', $template)->textInput(['maxlength' => true,'onkeyup' => "nospaces(this)"]) ?>
						 <?= $form->field($model, 'gender', $template)->radioList($model->sex,['separator'=>'&nbsp&nbsp&nbsp&nbsp'])?>					
						 <?= $form->field($model, 'year_of_birth', $template)->textInput(['maxlength' => true,'onkeyup' => "nospaces(this)"]) ?>
						 
                         <?= $form->field($model, 'vaccination_appointment_date', $template)->textInput(['class' => 'form-control datepicker']) ?>
						 	 
						 <?= $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>