<?php

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

/* @var $this yii\web\View */
/* @var $model frontend\models\Option */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="option-form">
    <?php $form = ActiveForm::begin(); ?>
    <input type="hidden" name="Option[model]" value="<?=$modeldb?>"/>
    <input type="hidden" name="Option[model_id]" value="<?=$model_id?>"/>
	<?php if($multiple) {?><p class="text-right"><span class="glyphicon glyphicon-plus add_row"></span></p><?php }?>
	<?php if(empty($models)) {
	?>
	<div class="form-group" id="main_row">
		<div class="form-wrapper">
		<?php foreach($fields as $index => $field) {
		    ?>
		    <div class="fields_block">
		    <label for="Option[0][value][<?=$field['name']?>]" class="pull-left"><?php echo Yii::t('app', $field['name']); ?></label>
		    <?php 
		       if($field['translate']) { ?><p class="text-right"><span class="glyphicon glyphicon-globe add_lang pull-right"></span></p><?php }
		    ?>
		    <div class="clearfix"></div>
	    	<input type="hidden" name="Option[0][<?=$field['name']?>][template]" value="<?=$field['template']?>"/>
	    	<input type="hidden" name="Option[0][<?=$field['name']?>][translate]" value="<?=$field['translate']?>"/>
	    	<input type="<?=$field['template']?>" name="Option[0][value][<?=$field['name']?>]" class="form-control required main_input" required/>
		    <?php
		    if($field['translate']) {
		        ?>
		        <div class="lang_inputs" style="display: none">
		        <?php
		        foreach($model->getLangs() as $lang) {
		        ?>
		        	<div class="form-group">
		        		<div class="col-xs-1"><?=$lang['lang_code']?></div>
		        		<div class="col-xs-11"><input type="<?=$field['template']?>" name="Option[0][lang][<?=$field['name']?>][<?=$lang['language_id']?>]" class="form-control" /></div>
		        		<div class="clearfix"></div>
		        	</div>
		        <?php
		        }
		        ?>
		        </div>
		        <?php
		    }
		    ?>
		    </div>
		    <?php
		}?>
		<p class="text-right"><span class="glyphicon glyphicon-minus remove_lang pull-right"></span></p>
		<hr />
		</div>
	</div>
	<?php
	} else {
	$first = 1;
	foreach($models as $index => $row) {
	    foreach($row as $key => $value) {
	        if(!$modellang[$index][$key][0]->hasErrors('value')) {
	            continue;
	        }
	        if($first) {
	        ?>
	        <div class="form-group" id="main_row">
			    <?php
			    }
		        ?>
		        <div class="form-wrapper">
		        	<div class="fields_block">
		        	<label for="Option[0][<?=$key?>]"><?php echo Yii::t('app', $key); ?></label>
		            <input type="hidden" name="Option[0][template]" value="<?=$value->template?>"/>
		            <input type="<?=$value->template?>" name="Option[0][value][<?=$key?>]" class="form-control required main_input" required value="<?=$modellang[$index][$key][0]->value?>"/>
		            <div class="help-block"><?=$modellang[$index][$key][0]->getFirstError('value')?></div>
		        	<?php
		        	if($field['translate']) { 
		        	    ?>
		        	    <div class="lang_inputs">
		        	    <?php
		            	foreach($value->getLangs() as $lang) {
		            	   ?>
		            	   <div class="form-group">
		            	   	<div class="col-xs-1"><?=$lang['lang_code']?></div>
		            	   	<div class="col-xs-11"><input type="<?=$value->template?>" name="Option[0][lang][<?=$key?>][<?=$lang['language_id']?>]" class="form-control" value="<?=$modellang[$index][$key][$lang['language_id']]->value?>"/></div>
		            	   </div>
		            	   <?php
		            	}
		            	?>
		            	</div>
		            	<?php
		        	} ?>
		        	</div>
		        	<?php
			        if($first) {
			        ?>
			        <p class="text-right"><span class="glyphicon glyphicon-minus remove_lang pull-right"></span></p>
			        <hr />
	        	</div>
	        </div>
	        <?php
	        $first = 0;
	        }
	        ?>
	        <?php
	    }
	}
	}?>
	
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>
    <script>
		var form = '<?= $form->getId()?>';
    </script>
</div>
