
<?php   Yii::app()->getClientScript()->registerScriptFile(Yii::app()->baseUrl.'/js/bootstrap2-toggle.js');
        Yii::app()->getClientScript()->registerCssFile(Yii::app()->baseUrl.'/css/bootstrap2-toggle.css');
?><?php
/* @var $this EstructuraArchivoBancoController */
/* @var $model EstructuraArchivoBanco */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget(
	'bootstrap.widgets.TbActiveForm', array(
	'id'=>'estructura-archivo-banco-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>false,
        'type'=>'horizontal',
)); ?>
<?php $this->widget('bootstrap.widgets.TbDetailView', array(
	'data'=>$model,
	'attributes'=>array(
        [
			'name'=>'Banco ',
			'value'=>$modelBanco->banco
        ],
        [
			'name'=>'Tipo de cuenta',
			'value'=>$modelBanco->tipo_cuenta
        ],
        [
			'name'=>'Numero de cuenta',
			'value'=>$modelBanco->num_cuenta
        ],
	),
)); ?>
	<div class="row">
		<div class="span8">
			<h4 style="text-align: center">Estructura del archivo de pago</h4>
			<hr>
				<?= $form->dropDownListRow($model, 'id_banco_tipo_servicio', CHtml::listData(BancoTipoServicio::model()->with('idTipoServicio')->findAll(['select'=>"t.id, nombre||' ('||codigo_servicio||')' AS codigo_servicio",'condition'=>'idbanco=:idbanco','params'=>[':idbanco'=>$banco->idbanco]]), 'id','codigo_servicio'),['style'=>"width:100%",'prompt'=>'-- TIPO DE SERVICIO --'] );?>

				<?php echo $form->textFieldRow($model,'cantidad_registro',array('class'=>'span5','style'=>'width:100%')); ?>
				<?php echo $form->textAreaRow($model,'encabezado',array('onChange'=>'verificacionVariable()','class'=>'span5','style'=>'width:100%')); ?>
				<?php echo $form->textAreaRow($model,'cuerpo',array('onChange'=>'verificacionVariable()','class'=>'span5','style'=>'width:100%')); ?>
			
				
		</div>
		<div class="span4">
			<h4 style="text-align: center">Varaiables</h4>
			<hr>
			<div style="overflow-y: scroll; height:18em; " id="variablesBanco">
				<?php $cantidad = 0; 
					foreach (BancoVariable::model()->findAll('blnborrado=false AND idbanco=:idbanco',[':idbanco'=>$model->id_banco_tipo_servicio]) as $key => $value) {
							$id =str_replace(['[','{','}',']'], '', $value->idConfVariableBanco->nomenclatura_variable);
						?>
					<p id="<?= $id ?>" 
						class="variable span3" 
						title="<?php echo $value->idConfVariableBanco->descripcion_variable?>" 
						onClick="agregarVariable('<?= trim($value->idConfVariableBanco->nomenclatura_variable)?>')"  >
					  	 &nbsp; &nbsp; &nbsp;<?= trim($value->nombre_variable)?> &nbsp; &nbsp; &nbsp;
					  </p> <br><br>

				<?php $cantidad++; if($cantidad==5){echo "<br><br><br>";$cantidad= 0; }}?>
			</div>
		</div>
	</div>
	
		<div class="span12">
		</div>
	</div>
	<div class="form-actions">
		<?php $this->widget('bootstrap.widgets.TbButton', array(
			'buttonType'=>'submit',
			'type'=>'primary',
			'label'=>$model->isNewRecord ? 'Guardar' : 'Modificar',
		)); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<style type="text/css">
	.variable{
		background: #0F326F;
		color: white;
		text-align: center;
		font-size: 14px;
		height: 2em;
		padding-top: 1em;
		cursor: pointer;
		padding-bottom: 1em;
	}
</style>

<script type="text/javascript">
var encabezado = true;
$(function(){
	verificacionVariable();
}); 
$("#<?= Chtml::activeId($model,'encabezado')?>").focus(function(){encabezado= true});
$("#<?= Chtml::activeId($model,'cuerpo')?>").focus(function(){encabezado= false});
function agregarVariable(variable){
	var elemento ='';
	if(encabezado){
		elemento ='EstructuraArchivoBanco_encabezado';
	}else{
		elemento ='EstructuraArchivoBanco_cuerpo';
	}
	if(elemento !=''){
		let texto = $("#"+elemento).val();
		let posicion_cursor = document.getElementById(elemento).selectionStart;
		let numero_caracteres = texto.length;
		let cadena='';
		let formato=variable;
		//saber si la posicion del cursor esta en el inicio de la cadena
		if(posicion_cursor ==0){
			cadena = formato + texto;
		}
		else if(posicion_cursor ==numero_caracteres){
			cadena = texto + formato;
		}
		else{
			inicio_cadena =texto.substring(0, posicion_cursor);
			fin_cadena = texto.substring(posicion_cursor+1, numero_caracteres);
			cadena = inicio_cadena + formato + fin_cadena;
		}
		$("#"+elemento).empty().val(cadena);
		verificacionVariable();
	}
	};
function verificacionVariable(){
	let texto ='';
	texto += $("#<?= Chtml::activeId($model,'encabezado')?>").val();
	texto += $("#<?= Chtml::activeId($model,'cuerpo')?>").val();
	$(".variable.span3").each(function(){
        buscado = $(this).attr('id');
        respuesta = eval(texto.indexOf(buscado));
        if(respuesta>= 0){
        	if($(this).is(":visible")){
        			$(this).hide("slow");
        	}
        }
        else{
        	$(this).show("slow");
        }

     });
}
</script>
<?php Yii::app()->clientScript->registerScript('tablaAmortizacion', '
        $("#'.Chtml::activeId($model,'id_banco_tipo_servicio').'").change(function(){
    		let idBancoTipoServicio =$(this).val();
            $.ajax({
                    url:"'.CController::createUrl('BusquedaVariableBancoTipoServicio').'",
                    cache: false,
                    type: "POST",
                    data: ({idBancoTipoServicio:idBancoTipoServicio}),
                    beforeSend: function(xkr){
                       $("#variablesBanco").html();
                       $("#'.CHtml::activeId($model,'encabezado').'").val("");
                       $("#'.CHtml::activeId($model,'cuerpo').'").val("");
                    },
                    success:  function(data) {
                        $("#variablesBanco").html(data);
                    }
            });

    	});
    ');?>