<?php

function user_config_form( $visibility,$show_custom_newsletter,$cif,$razon_social,$poblacion,$direccion,$pais,$correo_contacto,
 $username, $password, $email, $first_name, $last_name, $nickname) {
 	 session_start ();
 	 // $_SESSION['visibility'] = 'visible';
 	// echo $visibility;
 	 
    echo '<style>
    body{
    	height:100%;
    }
     .right{

 	//  margin: auto;
 	  width: 5px;
 	  padding-top:10px;

 }
 .loading-icon-modal{
 	
 }
 .marco{
 	 padding:10px;
	   margin-left:auto;
	   margin-right:auto;
	   margin-top:10px;
	   border-radius: 15px;
	   border:solid grey 1px;
	   width:720px;
 }
 
 .my-button{
	 background-color: rgb(68, 68, 68);
      border-color: #995500;
 }   
label{
  font-weight: 500;
}
 .main-navigation{
 
 }
 
 th{
 	width:30%;
 }
 
 .error{
 	padding-left:5px;
 	color:red;
 	visibility:hidden;
 }

    

    </style>
    ';
 //visibility:hidden;
     echo '<form id="formulario" action="' . $_SERVER['REQUEST_URI'] . '" method="post">';
	$selected_products=array();
	$user=get_current_user();
	$user_id=get_current_user_id();

	$newsletter  = get_the_author_meta( 'newsletter', $user->ID );

	$last_newsletter_date  = get_the_author_meta( 'last_newsletter_date', $user->ID );
	
	$user_info = get_userdata( $user_id);

?>
<input hidden name="id" id="id" disabled value="<?php echo $user_id; ?>"></input>
<div class="marco">
		<i>Seleccione, en el desplegable, la frecuencia con la que desea recibir las alertas de su interés y pulse "Guardar".</i>
		<br><br>
		<table class="form-table">
		<tr>
			<th><label for="favorites"><?php _e('Nombre de usuario:','ainia')?></label></th>
			<td>
					<input name="login" id="login" disabled value="<?php echo $user_info->user_login; ?>"></input>
				
			</td>
		</tr>		
	</table>

	<table class="form-table">
		<tr>
			<th><label for="favorites"><?php _e('Intervalo de envío de las alertas:','ainia')?></label></th>
			<td>
				<select name="newsletter" id="newsletter">
					<option value=""><?php _e("No enviar Newsletter",'ainia')?></option>
					<option value="1" <?php selected($newsletter,'1');?>  ><?php _e("A diario",'ainia')?></option>
					<option value="3" <?php selected($newsletter,'3');?>  ><?php _e("Cada 3 días",'ainia')?></option>
					<option value="7" <?php selected($newsletter,'7');?>  ><?php _e("Una vez por semana",'ainia')?></option>
					<option value="15" <?php selected($newsletter,'15');?>><?php _e("Cada 15 días",'ainia')?></option>
					<option value="30" <?php selected($newsletter,'30');?>><?php _e("Una vez al mes",'ainia')?></option>
				</select>		
				<p class="description"><?php 
					if($last_newsletter_date)
						 echo __("Último correo enviado el",'ainia')." <strong>".$last_newsletter_date."</strong>";
					else echo __("Aún no ha recibido ninguna alerta",'ainia');
					?>
				</p>
			</td>
		</tr>		
	</table>
<div>
	    	<div style="margin-left:auto; margin-right:0;"> 
       		   <button type="button" id="button-save-user" class="search-submit" >Guardar</button><img class="loading-icon-modal-1" src="<?php echo plugin_dir_url( __FILE__)?>images/loading.gif" style="display:none" />
         <br>
         <br></div></div>
         	   			<p id="modal-message-proj">
			
			<p id="modal-message-proj-txt"  style="display:none">Sus datos se han actualizado correctamente.</p>
		</p>
       </div>
       </div>
       <div class="marco">
       <i>Cree el conjunto de conceptos sobre los que desea recibir alertas, introduzca el texto y pulse "Añadir Concepto".
       El sistema buscará coincidencias literales con los textos introducidos,  por ejemplo el texto "recicla" obtendrá 
       resultados relacionados con "</i><b>recicla</b><i>je", ""</i><b>recicla</b><i>do", "</i><b>recicla</b><i>r"...<br> Para combinar varias palabras únalas usando "+", por ejemplo "recicla + PET".</i>
       <br><br>
       <table class="form-table">
		<tr>
			<th><label for="favorites"><?php _e('Conceptos de interés:','ainia')?></label></th>
			<td>
					<input name="concepto" id="concepto" ></input><button type="button" id="button-add-concept" class="search-submit" >Añadir Concepto</button><img class="loading-icon-modal-2" src="<?php echo plugin_dir_url( __FILE__)?>images/loading.gif" style="display:none" />
       
				
			</td>
		</tr>		
	</table>
	


	<?php 

    //echo '<label for="fltrs-bbltc-tematicas">Conceptos de interés:</label><br><br>';
    $user_id=get_current_user_id();
    
   $user_categories = get_user_conceptos_personalizado2($user_id);
	render_custom_terms_table($user_categories);
	
	echo '
     
       </div></div>';?>
       
    
  
  <?php echo ' </form>';?>


     <?php     
 }
 
 
 function complete_config() {
    global $cif,$razon_social,$poblacion,$direccion,$pais,$correo_contacto,$reg_errors, $username, $password, $email,  $first_name, $last_name;
    //echo 'complete_config';
   // if ( 1 > count( $reg_errors->get_error_messages() ) ) {
        $userdata = array(
        'user_login'    =>   $username,
        'user_email'    =>   $email,
        'user_pass'     =>   $password,
        'first_name'    =>   $first_name,
        'last_name'     =>   $last_name,
        );
        $user = wp_insert_user( $userdata );
       
    //}
}

function custom_config_function($show_custom_newsletter) {
	// function registration_validation( $cif,$razon_social,$poblacion,$direccion,$pais,$correo_contacto,
 //$username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
 	$visibility='visible';
	
    if ( isset($_POST['submit'] ) ) {
    	
    	$_POST['newsletter'];
        $_POST['custom_newsletter'];
        $_POST['productos'];
        $_POST['password'];
        $_POST['email0'];

         	//echo'custom_config_function() 1';
         	   global $username, $password, $email, $first_name, $last_name, $nickname;
        // sanitize user form input

		 complete_config();
		   // Just once at the beginning of your code
   
    $visibility='hidden';

   
    }

  	//function user_config_form( $cif,$razon_social,$poblacion,$direccion,$pais,$correo_contacto,
 //$username, $password, $email, $website, $first_name, $last_name, $nickname, $bio )
    user_config_form($visibility,$show_custom_newsletter,
       $cif,$razon_social,$poblacion,$direccion,$pais,$correo_contacto, $username, $password, $email,  $first_name, $last_name, $nickname
        );
}

// Register a new shortcode: [cr_custom_config]
add_shortcode( 'cr_custom_config', 'custom_config_basic' );
 
// The callback function that will replace [book]
function custom_config_basic() {
    ob_start();
    // echo 'X';
    custom_config_function(false);
  
    return ob_get_clean();
}

add_shortcode( 'cr_custom_config_full', 'custom_config' );
 
// The callback function that will replace [book]
function custom_config() {
    ob_start();
     
    custom_config_function(true);
  
    return ob_get_clean();
}



