<?php
if ( ! function_exists( 'wp_terms_checklist' ) ) {
    include ABSPATH . 'wp-admin/includes/template.php';
}


//add_action( 'wp_header', array( $this, 'render_ajax_url' ) );

		function render_ajax_url() {

			echo "<script>var ajaxurl = '" . admin_url( 'admin-ajax.php' ) . "'; var home_url = '" . home_url() . "';</script>";

		}



  function render_combo_classifications( $selected_terms ) {

			$products = get_terms( array( 'taxonomy' => 'producto' ) );

			echo '<select id="fltrs-bbltc-productos" name="productos[]" multiple class="chosen-select" data-placeholder="Seleccione sus categorías de producto...">';

			foreach ( $products as $product ) {
				$selected = ( in_array( $product->term_id, $selected_terms ) ? 'selected="selected"' : '' );
				echo "<option value='$product->term_id' $selected>$product->name</option>";
			}
			echo '</select>';

}

function render_custom_terms_table($terms){
	
		$html_tabla= $html_tabla.'<table id="tabla_accesos" style="border:1px solid #ccd0d4;width:80%;margin: 0 auto;"><tbody>';
		$html_tabla=$html_tabla.'<tr style="border:1px solid #ccd0d4;">
					<td style="width:5%;padding:5px;color:#32373c;">';
		$html_tabla=$html_tabla.'<input id="check-header"  type="checkbox"></td>';
		$html_tabla=$html_tabla.'
					<td style="width:60%;padding:5px;color:#32373c;font-size:14px;">Concepto</td>';		
		/*$html_tabla=$html_tabla.'
					<td style="width:70%;padding:5px;color:#32373c;font-size:14px;">Etiquetado</td>';	*/	
				
		
		
		$html_tabla=$html_tabla.'</tr>';
		$color='#F9F9F9';
		$count=0;		
		 foreach ($terms as $term) {
		 	
		 		if($color=='white'){
		 			$color='#F9F9F9';
		 		}else{
		 			$color='white';
		 		}
		 		$html_tabla=$html_tabla.'<tr>';
		 		$html_tabla=$html_tabla.'<td style="padding-left:8px;background-color:'.$color.'">';
		 		$html_tabla=$html_tabla.'<input id="chk-'.$term->term_id.'" data-id="'.$term->term_id.'" type="checkbox"></td>';
		 		$html_tabla=$html_tabla.'<td class="registro-tabla"  style="font-size:14px;width:2%;padding:13px 5px;background-color:'.$color.';" data-pid="'. $term->term_id.'">'.$term->name.'<br>';
		 		$html_tabla=$html_tabla.'<div id="acciones" class="acciones" style="visibility:hidden">';
		 		//$html_tabla=$html_tabla.'<div class="edit-link" data-cid="'.$term->term_id.'" style="font-size:13px;width:10%;float:left;"><a>Editar</a></div><div  style="padding:0 5px;float:left;"> |</div>';
		 		$html_tabla=$html_tabla.' <div class="delete-link" data-cid="'.$term->term_id.'" style="font-size:13px;width:40%;float:left;"><a>Eliminar</a></div></div></td>';
				//$html_tabla=$html_tabla.'<td class="registro-tabla"  style="font-size:14px;width:2%;padding:13px 5px;background-color:'.$color.';" data-pid="'.$term->term_id.'">'.get_etiquetado($term->term_id).'<br>';
		 			 	
				$count=$count+1;
		 
		 }
		 $html_tabla=$html_tabla.'</tbody></table>';	
		 
		 echo $html_tabla;
	
}

		add_action('wp_ajax_updateuser',	function () {
			
					$newsletter= intval( $_POST['newsletter'] );
			
					$id  = $_POST['id'] ;
					$producto_int=array();
					foreach ($productos as $producto){
						$producto_int[]=intval($producto);
						
					}
					
					
					$validation_id=email_exists($mail);
					
					if(!$validation_id || $validation_id==$id){
						$user_data = wp_update_user( array( 'ID' => $id, 'first_name' => $nombre,'last_name'=>$apellido,'user_email'=>$mail ) );
						
					}else{
						wp_send_json_success('email_error');
						//return 'email_error';
					}
					
					update_user_meta( $id, 'newsletter', $newsletter );	
					//update_user_meta( $id, 'custom_newsletter', $custom_newsletter );	
					//wp_set_object_terms( $id, $producto_int, 'producto' );
						wp_send_json_success('user_saved');
					//return 'user_saved';
				});
				
			add_action('wp_ajax_savecustomterm',	function () {
			
					$newsletter= intval( $_POST['newsletter'] );
			
					$id  = $_POST['id'] ;
					$producto_int=array();
					foreach ($productos as $producto){
						$producto_int[]=intval($producto);
						
					}
					
					
					$validation_id=email_exists($mail);
					
					if(!$validation_id || $validation_id==$id){
						$user_data = wp_update_user( array( 'ID' => $id, 'first_name' => $nombre,'last_name'=>$apellido,'user_email'=>$mail ) );
						
					}else{
						wp_send_json_success('email_error');
						//return 'email_error';
					}
					
					update_user_meta( $id, 'newsletter', $newsletter );	
					//update_user_meta( $id, 'custom_newsletter', $custom_newsletter );	
					//wp_set_object_terms( $id, $producto_int, 'producto' );
						wp_send_json_success('user_saved');
					//return 'user_saved';
				});
				
				add_action('wp_ajax_deleteConcept',	function () {
			
				$id  = $_POST['id'] ;							
				
				//wp_delete_term($id);
				ainia_remove_user_terms(get_current_user_id(), $id);
				//wp_clean_object_term_cache();
				//wp_clean_cache();
					//update_user_meta( $id, 'custom_newsletter', $custom_newsletter );	
					//wp_set_object_terms( $id, $producto_int, 'producto' );
						wp_send_json_success('concept_deleted');
					//return 'user_saved';
				});
				
				add_action('wp_ajax_insertConcept',	function () {
			
					$concepto= $_POST['concepto'] ;								
					
					$term_id=wp_insert_term($concepto, 'concepto_personalizado');
					wp_clean_cache();
				
					
					//ainia_set_user_terms(get_current_user_id(), $term_id);
					//update_user_meta( $id, 'custom_newsletter', $custom_newsletter );	
					//wp_set_object_terms( $id, $producto_int, 'producto' );
						wp_send_json_success('term_saved');
					//return 'user_saved';
				});