<?php

use yii\web\View;
use yii\widgets\Pjax;
use app\base\Common;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use wbraganca\dynamicform\DynamicFormWidget;
use yii\helpers\ArrayHelper;
use app\models\Labmaster;
use app\models\Specimenmaster;
use app\models\Labtestmapping;

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

$Surveys = Common::getSurvey();
$PIDs = Common::getLabrequestPIDs();
$Depts = Common::getDepartments();
$LabsMaps = Labtestmapping::find()->innerJoinWith('tests T')->orderBy(["T.lab_test_name" => SORT_ASC])->one();
$Labs = ArrayHelper::map(Labmaster::find()->orderBy(["lab_test_name" => SORT_ASC])->all(), 'lab_test_code', 'lab_test_name');
$Specimens = ArrayHelper::map(Specimenmaster::find()->orderBy(["lab_spcmn_name" => SORT_ASC])->all(), 'lab_spcmn_code', 'lab_spcmn_name');

$headJS = <<<JS
function survey_change(ctrl) {
	// console.log($(ctrl).val());
	jQuery(".dynamicform_wrapper .item").each(function(index) {
		$("#labsamplerequest-" + index + "-lab_smple_sur_id").val($(ctrl).val());
    });
}

function tests(ctrl) {
	var checkedVals = new Array();
	var index = 0;
	$(ctrl).find(':checkbox').each(function () {
		// console.log(index+' '+$(this).val())
		if($(this).is(':checked')) {
			checkedVals[index] = $(this).val();
			index++;
		}
	});
	console.log(checkedVals.join(","));
	callUrl = $(ctrl).data('href');
	$.ajax({
		type: "post",
		url: callUrl,
		data: 'id='+checkedVals.join("','"),
		success: function(result){
			$("#time-display").html(result.body);
		}
	});
}
JS;
$this->registerJs($headJS, View::POS_HEAD);

$js = <<<JS
jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
    jQuery(".dynamicform_wrapper .item").each(function(index) {
    	if($("#labsamplerequest-" + index + "-lab_smple_sur_id").val() == "") {
			$("#labsamplerequest-" + index + "-lab_smple_sur_id").val($("#labsamplerequest-0-lab_smple_sur_id").val());
			jQuery(this).find(':input').each(function() {
			    switch(this.type) {
			        case 'password':
			        case 'text':
			        case 'textarea':
			        case 'file':
			        case 'select-one':
			        case 'select-multiple':
			        case 'date':
			        case 'number':
			        case 'tel':
			        case 'email':
			            jQuery(this).val('');
			            break;
			        case 'checkbox':
			        case 'radio':
			            this.checked = false;
			            break;
			    }
			  });
    	}
    });
});

jQuery(".dynamicform_wrapper").on("beforeDelete", function(e, item) {
	// console.log($(item).find(':input').first().val());
	$("#labsamplerequest-delrec").val($(item).find(':input').first().val() + "," + $("#labsamplerequest-delrec").val());
});

jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
	// console.log(e);
    jQuery(".dynamicform_wrapper .panel-title-address").each(function(index) {
        jQuery(this).html("Address: " + (index + 1))
    });
});
JS;
$this->registerJs($js);
?>

<div class="labsamplerequest-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(['id' => 'dynamic-form1', '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}'
						];
						$dynamictemplate = [
							// 'options' => ['tag' => false],
							'template' => '<div class="col-md-12">{input}{error}{hint}</div>'
						];
						$chklist_template = [
							'labelOptions' => ['class' => 'form-label'],
							'template' => '<div class="col-sm-5">{label}{hint}</div><div class="col-sm-4 checkbox-list">{input}{error}</div>',
						];
					?>
					<table class="table table-bordered container-items" border="0" cellpadding="0" cellspacing="0" align="center">
						<thead>
							<tr>
								<td><?= Html::activeLabel($model,'lab_smple_tst_id'); ?></td>
								<td><?= Html::activeLabel($model,'lab_smple_spcmn_id'); ?></td>
								<td><?= Html::activeLabel($model,'lab_smple_result'); ?></td>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($models as $index => $modelAddress): ?>
								<tr class="item">
									<td class="col-md-4">
										<?= $form->field($modelAddress, "[{$index}]lab_mapng_testmstr_id", $htemplate)->hiddenInput()->label("") ?>
										<?= $modelAddress->tests->lab_test_name ?>
									</td>
									<td class="col-md-4">
										<?= $form->field($modelAddress, "[{$index}]lab_mapng_spcmn_id", $htemplate)->hiddenInput()->label("") ?>
										<?= $modelAddress->tests->lab_test_name ?>
									</td>
									<td>
										<?= $form->field($modelAddress, $modelAddress->isNewRecord ? "[{$index}]create_user" : "[{$index}]update_user", $htemplate)->hiddenInput(['value' => yii::$app->user->identity->id])->label("") ?>
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
					<?php ActiveForm::end(); ?>
				</div>
				<!-- /.col-lg-6 (nested) -->
			</div>
			<!-- /.row (nested) -->
		</div>
		<!-- /.panel-body -->
	</div>
	<!-- /.panel -->
</div>