<?php

use app\base\Common;
use yii\web\View;
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\Ostmaster */
/* @var $form yii\widgets\ActiveForm */

$Surveys = Common::getSurvey();
$PIDs = Common::getOstMasterPIDs();

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::getOstMasterPIDs(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)."'});
	
	$('#ostmaster-ost_master_part_id').on('select2:select', function (evt) {
	    if($(this)[0].value != '') {
		var ctrlval = $(this)[0].value;
		$.ajax({
			url: '".Url::to(["/ostvisit/list"])."?id='+ctrlval,
            type: 'post',
			success: function (response) {
				// do something with response
				if(response != 'No OST Master') {
					$('#ostmaster-ost_master_q1').val(response.ost_master_q1);
					// $('#ostmaster-ost_master_q1').prop('readonly', true);
					$('#ostmaster-oldenddate').val(response.ost_master_q3);
					$('#ostmaster-ost_master_q4').val(response.ost_master_q4);
					$('#ostmaster-ost_master_q5').val(response.ost_master_q5);
					$('#ostmaster-ost_master_q6').val(response.ost_master_q6);
					$('#ostmaster-ost_master_q7').val(response.ost_master_q7);
				} else {
					$('#ostmaster-ost_master_q1').val('');
					// $('#ostmaster-ost_master_q1').prop('readonly', false);
					$('#ostmaster-ost_master_q4').val('');
					$('#ostmaster-ost_master_q5').val('');
					$('#ostmaster-ost_master_q6').val('');
					$('#ostmaster-ost_master_q7').val('');
				}
			}
		});
	}
  });

	$('#ostmaster-ost_master_q4, #ostmaster-ost_master_q5, #ostmaster-ost_master_q6').change(function(){
		var q4 = $('#ostmaster-ost_master_q4').val();
		var q5 = $('#ostmaster-ost_master_q5').val();
		var q6 = $('#ostmaster-ost_master_q6').val();
		if(!isNaN(q4) && !isNaN(q5) && !isNaN(q6) && q4 != undefined && q5 != undefined && q6 != undefined){
			var tot = (eval(q4) * 2) + (eval(q5) * 0.4) + (eval(q6) * 0.2);
			if(!isNaN(tot))
				$('#ostmaster-ost_master_q7').val(tot.toFixed(2));
		}
	});
	$('#ostmaster-ost_master_q4, #ostmaster-ost_master_q5, #ostmaster-ost_master_q6').trigger('change');
";

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

<div class="ostmaster-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>',
						];
						$htemplate = [
							'options' => ['tag' => false],
							'template' => '{input}'
						];
					?>
					<?= Common::generateControl($form, $model, $template, 'ost_master_survey', $Surveys, 'survey', 'getOstMasterPIDs') ?>
					<?= Common::generateControl($form, $model, $template, 'ost_master_loc', $Locations, 'location', '', [Html::getInputId($model, 'ost_master_survey')]) ?>
					<?= Common::generateControl($form, $model, $template, 'ost_master_part_id', $PIDs, 'pid', '', [Html::getInputId($model, 'ost_master_survey'), Html::getInputId($model, 'ost_master_loc')]) ?>
					
					<?= $form->field($model, 'ost_master_q1', $template)->textInput(['maxlength' => true, 'disabled' => !($model->isNewRecord)]) ?>
					<?= $form->field($model, 'ost_master_q2', $template)->textInput(['class' => 'form-control datepicker', 'disabled' => !($model->isNewRecord)]) ?>
					<?= $form->field($model, 'ost_master_q3', $template)->textInput(['class' => 'form-control datepicker']) ?>
					<?= $form->field($model, 'ost_master_q4', $template)->textInput(['maxlength' => true, 'disabled' => !($model->isNewRecord)]) ?>
					<?= $form->field($model, 'ost_master_q5', $template)->textInput(['maxlength' => true, 'disabled' => !($model->isNewRecord)]) ?>
					<?= $form->field($model, 'ost_master_q6', $template)->textInput(['maxlength' => true, 'disabled' => !($model->isNewRecord)]) ?>
					<?= $form->field($model, 'ost_master_q7', $template)->textInput(['maxlength' => true, 'readonly' => true]) ?>
					<?= $form->field($model, 'oldenddate', $htemplate)->hiddenInput()->label("") ?>
					<?= $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>