<?php

use yii\web\View;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use app\models\Surveymaster;
use app\models\Locationmaster;

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

$Surveys = ArrayHelper::map(Surveymaster::find()->orderBy(["sur_id" => SORT_ASC])->all(), 'sur_code', 'sur_title');
$Locations = ArrayHelper::map(Locationmaster::find()->orderBy(["loc_name" => SORT_ASC])->all(), 'loc_code', 'loc_name');

$headJS = <<<JS
	function dropdown_validation(ctrl)
	{
		if(ctrl.selector == undefined)
			ctrlvalue = ctrl.value;
		else
			ctrlvalue = $(ctrl.selector).val();
		
		$("div.form-group").show();
		$("div.help-block").html("");
		$("div.form-group").removeClass("has-success").removeClass("has-error");

		if(ctrlvalue == 1) {
			$("div.form-group.field-coordinator-ngoname").addClass("required");
		} else {
			$("div.form-group.field-coordinator-ngoname").removeClass("required");
			$("div.form-group.field-coordinator-ngoname").hide();
		}
	}
    
	function ostid_change(ctrl)
	{
		if(ctrl.selector == undefined)
			ctrlvalue = ctrl.value;
		else
			ctrlvalue = $(ctrl.selector).val();
		
		$("div.form-group").show();
		$("div.help-block").html("");
		$("div.form-group").removeClass("has-success").removeClass("has-error");

		if(ctrlvalue != "") {
			$("div.form-group.field-coordinator-ost_date, div.form-group.field-coordinator-ost_2mg, div.form-group.field-coordinator-ost_04mg, div.form-group.field-coordinator-ost_02mg").addClass("required");
		} else {
			$("div.form-group.field-coordinator-ost_date, div.form-group.field-coordinator-ost_2mg, div.form-group.field-coordinator-ost_04mg, div.form-group.field-coordinator-ost_02mg").removeClass("required");
			$("div.form-group.field-coordinator-ost_date, div.form-group.field-coordinator-ost_2mg, div.form-group.field-coordinator-ost_04mg, div.form-group.field-coordinator-ost_02mg").hide();
		}
	}
JS;
$this->registerJs($headJS, View::POS_HEAD);

$JS = "
	$('.datepicker').datepicker({autoclose: true, startDate: '01-01-1900', endDate: '0', format: '".strtolower(Yii::$app->formatter->dateFormat)."'});

	$('div.form-group.field-coordinator-ngoname, div.form-group.field-coordinator-ost_date, div.form-group.field-coordinator-ost_2mg, div.form-group.field-coordinator-ost_04mg, div.form-group.field-coordinator-ost_02mg').hide();
";

if($this->context->action->id == "update") {
	$JS .= "
		$('#coordinator-corfrm_q10, #coordinator-ostid').trigger('change');
	";
}

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

<div class="coordinator-form">
	<div class="panel panel-default">
		<div class="panel-heading">
			Form Elements
		</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}'
						];
					?>
					<?= $form->errorSummary($model); ?>
					<?= $form->field($model, 'corfrm_sid', $template)->dropDownList($Surveys, ['prompt' => $model->getAttributeLabel("corfrm_sid")]) ?>
					<?= $form->field($model, 'corfrm_pid', $template)->textInput(['maxlength' => true])->hint("<strong>This will Automatically popup from Bio Metric Application</strong>") ?>
					<?= $form->field($model, 'corfrm_loc', $template)->dropDownList($Locations, ['prompt' => $model->getAttributeLabel("corfrm_loc")]) ?>
					<?= $form->field($model, 'corfrm_q1', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'corfrm_q2', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'corfrm_q3', $template)->textInput()->hint("<strong>INTERVIEWER INSTRUCTION:</strong> Code 999997 if Don't Know; Code 999998 if Refused; If homeless record the PINCODE of where client stays most often<br />") ?>
					<?= $form->field($model, 'corfrm_q4', $template)->textInput() ?>
					<?= $form->field($model, 'corfrm_q5', $template)->dropDownList($model->q4, ['prompt'=>'Select']); ?>
					<?= $form->field($model, 'corfrm_q6', $template)->dropDownList($model->q5, ['prompt'=>'Select']); ?>
					<?= $form->field($model, 'corfrm_q7', $template)->dropDownList($model->q6, ['prompt'=>'Select']); ?>
					<?= $form->field($model, 'corfrm_q8', $template)->dropDownList($model->q7, ['prompt'=>'Select']); ?>
					<?= $form->field($model, 'corfrm_q9', $template)->textInput() ?>
					<?= $form->field($model, 'corfrm_q10', $template)->dropDownList($model->q10, ['prompt'=>'Select', 'onChange' => 'dropdown_validation(this);']); ?>
					<?= $form->field($model, 'ngoname', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'corfrm_q11', $template)->dropDownList($model->q11, ['prompt'=>'Select'])->hint("<strong>If client is updated +ve, no future edits and only from chennai DMC, else site coordinator can update to +ve if -ve/unknown</strong>") ?>
					<?= $form->field($model, 'spouseid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'ref_no', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'tiid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'ictcid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'rntcpid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'artid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'mbid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'stdid', $template)->textInput(['maxlength' => true]) ?>
					<?= $form->field($model, 'ostid', $template)->textInput(['maxlength' => true, 'onChange' => 'ostid_change(this);']) ?>
					<?= $form->field($model, 'ost_date', $template)->textInput(['class' => 'form-control datepicker']) ?>
					<?= $form->field($model, 'ost_2mg', $template)->textInput() ?>
					<?= $form->field($model, 'ost_04mg', $template)->textInput() ?>
					<?= $form->field($model, 'ost_02mg', $template)->textInput() ?>
					<?= $form->field($model, 'corfrm_q12', $template)->dropDownList($model->q12, ['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>