<?php

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

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

$Surveys = Common::getSurvey();
$PIDs = Common::getReimbursePIDs();
$JS = "
	$('.datepicker').datepicker({autoclose: true, startDate: '01-01-1900', endDate: '0', format: '".strtolower(Yii::$app->formatter->dateFormat)."'});
	
	$('button[type=submit]').prop('disabled', true);
	
	$('#reimbursementmaster-reimburse_master_part_id').on('select2:unselect', function (evt) {
		$('#pid_list').html('');
		$('#pid_list').addClass('hidden');
		$('button[type=submit]').prop('disabled', true);
	});
	
	
	
	$('#reimbursementmaster-reimburse_master_part_id').on('select2:select', function (evt) {
		$('button[type=submit]').prop('disabled', true);
		var pid = $(this)[0].value;		
		if(pid != '') {
			var link = $('#pid-info').data('link') + pid;			
			$.ajax({
				type: 'post',
				url: 'visit',
				data: 'pid='+$(this)[0].value,
				success: function(response) {	
                    			
					if(response.visit != null) {						
						
						$('#reimbursementmaster-reimburse_master_visitcode').val(response.visit);
						
					} else {
						
						$('#reimbursementmaster-reimburse_master_visitcode').val('');
						
					}
					
				}
			});
		}
	});
	
	$('#reimbursementmaster-reimburse_master_part_id').on('select2:select', function (evt) {
		$('button[type=submit]').prop('disabled', true);
		var pid = $(this)[0].value;		
		if(pid != '') {
			var link = $('#pid-info').data('link') + pid;			
			$.ajax({
				type: 'post',
				url: 'visitsch',
				data: 'pid='+$(this)[0].value,
				success: function(response) {               				
                    			
								
				if(response == '1' ) {
					$('#reimbursementmaster-reimflag').val('1');
				}
				else {
					$('#reimbursementmaster-reimflag').val('0');
				}
				}
			});
		}
	});
	
	$('#reimbursementmaster-reimburse_master_part_id').on('select2:select', function (evt) {
		var pid = $(this)[0].value;
		if(pid != '') {
			var link = '".Url::toRoute(['ajax', 'id' => ''])."' + pid;
			$('#pid_list').removeClass('hidden');
			$('#pid_list').html('');
			$('#pid_list').load(link);
			$.ajax({
				type: 'post',
				url: 'ajax',
				data: 'id='+pid,
				success: function(result) {
					// console.log(result)
					if(result != 1)
						$('button[type=submit]').prop('disabled', true);
					else
						$('button[type=submit]').prop('disabled', false);
				}
			});
		}
	});
	
	
";
$this->registerJs($JS);
?>

<div class="reimbursementmaster-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}'
						];
					?>
					<?php // $form->errorSummary($model); ?>
					<?= $form->field($model, 'reimburse_master_survey', $template)->dropDownList($Surveys) ?>
					<?= $form->field($model, 'reimburse_master_part_id', $template)->widget(Select2::classname(), ['data' => $PIDs, 'options' => ['prompt' => $model->getAttributeLabel("reimburse_master_part_id")]])->hint("<strong>Please Bar Code Scan the ICC Client ID</strong>"); ?>
					 <?= $form->field($model, 'reimburse_master_visitcode', $htemplate)->hiddenInput()->label() ?>
					 <?= $form->field($model, 'reimflag', $htemplate)->hiddenInput()->label() ?>
					<div id="pid_list" class="row-fluid popup"></div>
					<div class="form-group">
						<div class="col-sm-offset-5 col-sm-7">
							<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Generate') : 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>