<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.'/js/jquery.mask.min.js',CClientScript::POS_HEAD); ?>


<h3><center>Solicitud de Mutuo Auxilio</center></h3>

<div class="alert alert-info"><i class="icon-info-sign"></i> Solo se pueden generar solicitudes de mutuo auxilio para asociados con estatus <strong>"ACTIVO"</strong>.</div>

<?php
    foreach(Yii::app()->user->getFlashes() as $key => $message) {
        echo '<div class="alert alert-' . $key . '">' . $message . "</div>\n";
    }
?>

	<?php
	$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
		'id'=>'solicitudma-form',
		'type' => 'horizontal',
		'focus'=>array($model,'cedula'),
        'enableAjaxValidation' => false,
        'enableClientValidation' => true,
        'clientOptions' => array(
	        'validateOnSubmit' => true,
	        'validateOnChange' => true,
	        'validateOnType' => true,
	        ),
        'htmlOptions' => array('onsubmit' => 'return verificarCheckbox()'),
		));
	?>
	<br>
	<fieldset>  <!--Datos del asociado-->
		<legend style=" font-size: 17px;"><b>Datos del asociado</b></legend>            
		<table class="table table-bordered">
	        <tr>
	            <td style="width: 25%"><b><?php echo $form->labelEx($model,'cedula'); ?></b></td>
	            <td style="width: 25%">

				<div class="input-append">
					<?php echo $form->textField($model,'cedula',array('maxlength'=>'10')); ?>
					<?php $this->widget('bootstrap.widgets.TbButton',
			        	array(
							'buttonType' => 'button',
							'type' => 'primary',
				            'icon'=>'icon-search icon-white',
							'htmlOptions' => array(
											'onclick' => 'findData()',
											'id' => 'btn-datos',
											),
						)
			    	);?>
					<!--<button class="btn btn-primary" id="btn-datos" type="button" onclick="findData()">Buscar</button>-->
				</div>
	             
	                                    <div id="mensajesSistema"  class="datosPersonales"></div>
	                                    <?php echo $form->error($model, "cedula"); ?></td>
	            <td style="width: 20%"><b>Nombres y apellidos</b></td>
	            <td style="width: 30%"><div id="nombres_apellidos" class="datosPersonales"></div></td>
	        </tr>
	        <tr>
	            <td><b>Empresa</b></td>
	            <td><div id="empresa" class="datosPersonales"></div></td>
	            <td><b>Unidad</b></td>
	            <td><div id="unidad" class="datosPersonales"></div></td>
	        </tr>
	        <tr>
	            <td><b>&Uacute;ltimo sueldo</b></td>
	            <td><div id="sueldo" class="datosPersonales"></div></td>
	            <td><b>Porcentaje de aporte</b></td>
	            <td><div id="porcentaje" class="datosPersonales"></div></td>
	        </tr>
	        <tr>
	            <td><b>Fecha de inscripci&oacute;n</b></td>
	            <td><div id="fecha_inscripcion" class="datosPersonales"></div></td>
	            <td><b>Estatus</b></td>
	            <td><div id="estatus" class="datosPersonales"></div></td>
	        </tr>	        
	    </table>
	</fieldset> <!--fin datos del asociado-->

	<div id="nobenef"></div>   <!--alerta de beneficiarios-->

<br>
	<div id="asociado_activo">
		<!--<div id="solicitante">
			<?php /*echo $form->labelEx($model, 'Solicitante'); ?>
			<?php echo $form->dropDownList($model, 'id_beneficiario', CHtml::listData(Beneficiario::model()->findAll(), 'id', 'nombre'), array('prompt' => 'Seleccione ')); */?>
		</div>
		<br>-->
		<div id="datos_fallecido">
			<fieldset>  <!--Datos del beneficiario/fallecido-->
				<legend style=" font-size: 17px;"><b>Beneficiarios</b></legend>            
				
				<?php $this->widget('ext.selgridview.BootSelGridView',
									array(
					                	'type' => 'bordered',
					                    'id'=>'beneficiarios-grid',
					                    'dataProvider'=>$modelBeneficiario->searchBenefid($modelAsociado->idasociado),
					                    'selectableRows'=> 1,

					                    'columns'=>array(
					                            array(
					                            'id'=>'seleccion',
					                            'class' => 'CCheckBoxColumn',
					                            'checkBoxHtmlOptions' => array('onchange'=>'showCampoFecha()',),
					                            ),

					                            
					                            array('header'=>'Nombre',
					                            		'value'=>'($data->nombre)',
					                            		'visible'=>'($data->id_status == 3)?false:true'),

					                            array('header'=>'Apellido',
					                            		'value'=>'($data->apellido)',
					                            		'visible'=>'($data->id_status == 3)?false:true'),

					                            array('header'=>'Cédula',
					                            		'value'=>'($data->cedula)',
					                            		'visible'=>'($data->id_status == 3)?false:true'),

					                            array('header'=>'Fecha de Nacimiento',
					                            		'value'=>'date(\'d-m-Y\' ,strtotime($data->fecha))',
					                            		'visible'=>'($data->id_status == 3)?false:true'),

					                            array('header'=>'Parentesco',
					                            		'value'=>'($data->parent->descripcion)',
					                            		'visible'=>'($data->id_status == 3)?false:true'),

					                            array('header'=>'Estatus',
					                            		'value'=>'($data->idStatus->nombre_estatus)',
					                            		'visible'=>'($data->id_status == 3)?false:true'),
					                    ),
	                    			)
	                    		); 
	            ?>
	            <div id="selGriderror"></div>
	            <div id="f_fallece"><?php echo $form->labelEx($model, 'fecha_Bfallece'); ?>
	            	<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
		                                        'model' => $model,
		                                        'attribute' => 'fecha_Bfallece',
		                                        'value' => $model->fecha_Bfallece,
		                                        'language' => 'es',
		                                        'htmlOptions' => array('readonly' => "readonly", 'class' => 'input-small', 'id'=>'f_deceso'),
		                                        //additional javascript options for the date picker plugin
		                                        'options' => array(
		                                            'autoSize' => true,
		                                            // 'defaultDate'=>$model->fechanacimiento,
		                                            //'dateFormat'=>'yy-m-d',
		                                            'dateFormat' => 'dd-mm-yy',
		                                            /*'buttonImage' => Yii::app()->baseUrl . '/images/calendar.png',
		                                            'buttonImageOnly' => true,
		                                            'buttonText' => 'Escoger fecha',
		                                            'showOn' => 'button',*/
		                                            'selectOtherMonths' => true,
		                                            'showAnim' => 'slide',
		                                            'showButtonPanel' => true,
		                                            'changeMonth' => 'true',
		                                            'changeYear' => 'true',
		                                            'yearRange' => "1900:+nn",
		                                            'maxDate'=> "+0D",
		                                        ),
		                                    ));
		                              ?></div>
		        <div id="error_fecha"></div>
			</fieldset> <!--fin datos del beneficiario/fallecido-->
		</div>
	
		<br><br>
		<div class="form-actions"  style="text-align: center">
		        <?php $this->widget('bootstrap.widgets.TbButton',
		        	array(
						'buttonType' => 'submit',
						'type' => 'success',
			            'icon'=>'icon-ok',
						'label' => 'Realizar solicitud',
						'htmlOptions' => array(
										'id' => 'btn_regSolicitud',
										),
					)
		    	);?>
		
		</div><br>
	</div>
	<div id="msgerror"></div>

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

<br>	

<?php Yii::app()->clientScript->registerScript('datosSolicitud', '

	$(document).ready(function(){

		$("#asociado_activo").hide();
		$("#f_fallece").hide();
		
		$("form").keypress(function(e) {
                                if (e.which == 13) {
                                    return false;
                                }
                            });

	});

	function showCampoFecha(){

		var check = $("table.items tbody tr td input:checkbox");
		var row = $("table.items tbody tr");
		var fecha = $("#f_deceso");
		
		check.each(function(){

			if (row.hasClass("selected")) {
				
				$("#f_fallece").show(0500);
				$("#selGriderror").html("");
				$("#error_fecha").html("");
			
			}else{

				$("#f_fallece").hide(0500);
				fecha.val("");
				$("#error_fecha").html("");
			}
		});
	}
	
	//consultar datos de la cedula introducida del asociado
	function findData(){

                var cedula = $("#'.CHtml::activeId($model,'cedula').'").val();
                var tipo_sol = 2;
                var benef =  '.(($modelBeneficiario->idbeneficiario!='')?$modelBeneficiario->idbeneficiario:0).';

                $("#'.CHtml::activeId($model,'id_beneficiario').'").empty();
                $("#'.CHtml::activeId($model,'id_beneficiario').'").append(\'<option value="">...Seleccione...</option>\');


                if ($.isNumeric(cedula) && cedula>6) {					

                        $.ajax({

                                url:"'.CController::createUrl('FindData').'",
                                cache: false,
                                type: "POST",
                                dataType: "json",
                                data: ({cedula:cedula, tipo:tipo_sol}),
                                beforeSend: function(xkr){

                                		//Datos de las tablas
                                        $(".datosPersonales").html("");
                            			$(".datosBeneficiario").html("");
                            			$(".beneficiarios").html("");


                            			//Alertas
                            			$("#mensajesSistema").html("");
                            			$("#nobenef").html("");
                            			$("#nobenef").hide(0250);
                            			$("#escoger_benef").html("");
                            			$("#escoger_benef").hide(0250);
                            			$("#msgerror").html("");                          			
                                           
                                },

                                success: function(data){
                                    
                                    if(data.error == 0) {

                                    	var registrado = data.registrado;
                                    	var ruta ="'.Yii::app()->createAbsoluteUrl("montepio/solicitud/solicitud_ma").'";
											
											//Datos de tabla
                                            $("#nombres_apellidos").html(data.nombre_apellido);
		                                    $("#empresa").html(data.empresa);
		                                    $("#unidad").html(data.unidad);
		                                    $("#sueldo").html("Bs. "+data.sueldo);
		                                    $("#porcentaje").html(data.porcentaje);
		                                    $("#fecha_inscripcion").html(data.fecha_inscripcion);
		                                    $("#estatus").html(data.estatus);

											if (data.montepio == 1) {
												
												//Alertas
		                                    	$("#nobenef").html("<span class=\"help-inline error\">"+data.nobenef+"</span>");
		                                    	$("#nobenef").show(0250);

											}else{
											
												if (data.no_beneficiario == 1) {
	 		                                    	
	                                     			$("#asociado_activo").hide(); 
	 
	 		                                    }else{

	 		                                    	$("#'.CHtml::activeId($model,'id_beneficiario').'").append(data.bnfs);
	 
	                                     			$("#asociado_activo").show(0500);
	                                     			$.fn.yiiGridView.update(\'beneficiarios-grid\',{type:\'GET\', url:ruta,data:\'cedula=\'+cedula}); 
		                                    		$(\'#beneficiarios-grid\').selGridView(\'addSelection\', [benef]);
		                                    		$("table.items tr td input:text").prop("disabled", true);
			                                    }
			                                    
			                                    //Alertas
			                                    $("#nobenef").html("<span class=\"help-inline error\">"+data.nobenef+"</span>");
			                                    $("#nobenef").show(0250);
			                                }

                                    }
                                    else{
										$("#mensajesSistema").html(data.mensajeError);
										$("#msgerror").html("");						
                                	}
                                }

                        	});
                }
                else{

                	$("#mensajesSistema").html("");
                }
	}

	//comprobacion de seleccion de checkboxes de la tabla de beneficiarios cargados
	function verificarCheckbox(){

		var checkbox = $("#beneficiarios-grid").selGridView("getAllSelection");
		var filas = $(".items tbody tr").length;
		var fecha = $("#f_deceso").val();
					

		if (filas <= 0) {
				
		
			$("#selGriderror").html("<span class=\"help-inline error\"><b>Debe cargar un beneficiario antes de realizar una solicitud!.</b></span>");
			return false;
		
		}else{

			if (checkbox == "") {

				$("#selGriderror").show(0500);
				$("#selGriderror").html("<span class=\"help-inline error\"><b>Debe seleccionar el beneficiario fallecido de la lista de beneficiarios para procesar la solicitud!</b></span>");
				return false;

			}else{

				$("#selGriderror").html("");

				if (fecha == "") {
					
					$("#error_fecha").html("<span class=\"help-inline error\"><b>Debe introducir la fecha de deceso del beneficiario para proceder!</b></span>");
					return false;

				}else{
					
					$("#selGriderror").html("");
					$("#error_fecha").html("");
					return true;

				}
			}
		}		
	}
	
',CClientScript::POS_HEAD); ?>