<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model backend\models\Datestsbe */
/* @var $form yii\widgets\ActiveForm */
$status = ['Active' => 'Active', 'Complete' => 'Complete'];
$result = ['Pass' => 'Pass', 'Fail' => 'Fail'];
$types = ['Annual' => 'Annual', 'Pre-Employment' => 'Pre-Employment', 'Unannouced' => 'Unannouced'];
?>

<script>
function saveField(id, field, value) {
	$.ajax({
		type: "POST",
		url: "/NGauge/datests/savefield",
		data: {id: id, field: field, value: value},
		})
	.done(function(result) {
		result = JSON.parse(result);
			$('#message').html(result.message);
			$('#flashmessage').delay(500).fadeIn('normal', function() {
				$(this).delay(2500).fadeOut();
			});
	    $.pjax.reload({container: '#rows', async:false});
	});
}
function addRow() {
	id = "<?= isset($model->id) ? $model->id : Yii::$app->session->get('dateststempid') ?>";
	$.ajax({
		type: "POST",
		url: "/NGauge/datests/addrow",
		data: {id: id},
		})
	.done(function(result) {
		result = JSON.parse(result);
			$('#message').html(result.message);
			$('#flashmessage').delay(500).fadeIn('normal', function() {
				$(this).delay(2500).fadeOut();
			});
	    $.pjax.reload({container: '#rows', async:false});
	});
}
</script>
<div class="datestsbe-form">
	<?php $form = ActiveForm::begin(); ?>
		<div class="card card-default">
			<div class="card-header">Create DA Test</div>
			<div class="card-body">
				<div class="row">
					<div class='col-12 col-lg-2 data-label'>Tester</div>
					<div class='col-12 col-lg-10'><?= $form->field($model, 'tester')->TextInput(['value' => 'Express Medical'])->label(false) ?></div>
				</div>
				<div class="row">
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Employee</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'employee')->dropdownlist($employees, ['prompt' => 'Please Select'])->label(false) ?></div>
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Job No</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'jobno')->textInput()->label(false) ?></div>
				</div>
				<div class="row">
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Date</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'date')->widget(\kartik\date\DatePicker::class, ['pluginOptions' => ['showMeridian' =>true, 'convertFormat' => true, 'format' => 'dd/mm/yyyy']])->label(false) ?></div>
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Status</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'status')->dropdownlist($status, ['prompt' => 'Please Select'])->label(false) ?></div>
				</div>
				<div class="hidden d-none">
					<div class="row">
						<div class="col-sm-3 data-label">Test</div>
						<div class="col-sm-2 data-label">Result</div>
						<div class="col-sm-2 data-label">Level</div>
						<div class="col-sm-4 data-label">Comments</div>
						<div class="col-sm-1"><?= html::Button('<i class="fa fa-plus"></i>', ['onclick' => 'addRow()', 'class' => 'btn btn-sm btn-primary']) ?></div>
					</div>
					<?php \yii\widgets\Pjax::begin(['id' => 'rows']); ?>
					<?php foreach ($model->rows as $key => $steps) { ?>
					<div class="row">
						<div class="col-sm-3"><?= $form->field($steps, 'test')->textInput(['onblur'=> "saveField('$steps->id','test',$(this).val())"])->label(false); ?></div>
						<div class="col-sm-2"><?= $form->field($steps, 'result')->dropdownlist($result, ['prompt' => 'Please Select...', 'onChange'=> "saveField('$steps->id','result',$(this).val())"])->label(false); ?></div>
						<div class="col-sm-2"><?= $form->field($steps, 'level')->textInput(['onblur'=> "saveField('$steps->id','level',$(this).val())"])->label(false); ?></div>
						<div class="col-sm-4"><?= $form->field($steps, 'comments')->textInput(['onblur'=> "saveField('$steps->id','comments',$(this).val())"])->label(false); ?></div>
						<div class="col-sm-1"><?= html::Button('<i class="fa fa-minus"></i>', ['onclick' => "removeRow('$steps->id')", 'class' => 'btn btn-sm btn-danger']) ?></div>
					</div>
					<?php } ?>
					<?php \yii\widgets\Pjax::end(); ?>
				</div>
				<div class="row">
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Type</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'type')->dropdownList($types, ['prompt' => 'Please Select'])->label(false) ?></div>
					<div class='col-12 col-lg-2 col-xl-2 data-label'>Result</div>
					<div class='col-12 col-lg-10 col-xl-4'><?= $form->field($model, 'result')->dropdownList($result, ['prompt' => 'Please Select'])->label(false) ?></div>
				</div>
				<div class="row">
					<div class='col-12 data-label'>Comments</div>
					<div class='col-12'><?= $form->field($model, 'comments')->textArea()->label(false) ?></div>
				</div>
				<div class="form-group">
					<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
				</div>
			</div>
		</div>
	<?php ActiveForm::end(); ?>
</div>
