<?php
do_action( 'bp_before_profile_edit_content' );

global $wpdb, $bp, $field;

if ( bp_has_profile( 'profile_group_id=' . bp_get_current_profile_group_id() ) ) : while ( bp_profile_groups() ) : bp_the_profile_group();
?>
<form action="<?php bp_the_profile_group_edit_form_action() ?>" method="POST" id="profile-edit-form" class="standard-form <?php bp_the_profile_group_slug() ?>" style="display: block; padding-top: 25px;">
    
    <?php
    do_action( 'bp_before_profile_field_content' );
    
    while ( bp_profile_fields() ) : bp_the_profile_field(); 
    
        /*
        Fields Id
        var_dump($_COOKIE['bp-message'],$_COOKIE['bp-message-type']);
        var_dump(bp_get_the_profile_field_id());
        1   ** Nombre
        2   ** Apellidos
        3   ** Poblacion
        4   ** Patologia
        15  ** Privacidad
        19  ** auto_estado
        25  ** Pais
        27  ** Inicio
        */
        
        if (bp_get_the_profile_field_id() == "1") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Nombre', 'buddypress') . ' *'; ?>
                </label>
                  
                <input type="text" name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>" value="<?php echo xprofile_get_field_data( '1', get_current_user_id()) ?>" autocomplete="off">
                
            </div>
            <?php
            continue;
        }
        
        if (bp_get_the_profile_field_id() == "2") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Apellido', 'buddypress'); ?>
                </label>
                <input type="text" name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>" value="<?php echo xprofile_get_field_data( '2', get_current_user_id()) ?>" autocomplete="off">
            </div>
            <?php
            continue;
        }
        
        if (bp_get_the_profile_field_id() == "3") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Población', 'buddypress') . ' *'; ?>
                </label>
                <input type="text" name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>" value="<?php echo xprofile_get_field_data( '3', get_current_user_id()) ?>" autocomplete="off">
            </div>
            <?php
            continue;
        }
            
        if (bp_get_the_profile_field_id() == "25") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Pais', 'buddypress') . ' *'; ?>
                </label>
                
                <select id="<?php bp_the_profile_field_input_name() ?>" name="<?php bp_the_profile_field_input_name() ?>">
                    <?php
                    $results = $wpdb->get_results("SELECT * FROM iron_paises");
                    
                    foreach ($results as $pais) {
                        if (xprofile_get_field_data( 'Pais', get_current_user_id()) == $pais->valor ) {
                            $sel="selected='selected'"; $s=1;
                        } else {
                            $sel = "";
                        }
                        echo '<option value="'. $pais->valor .'" '.$sel.'>'. $pais->pais .'</option>';
                    }
                    ?>
                </select>
                
            </div>
            <?php
            do_action( 'bp_custom_profile_edit_fields' );
            continue;
        }
        
        if (bp_get_the_profile_field_id() == "27") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Inicio', 'buddypress'); ?>
                </label>
                <select name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>">
                    <option value="Todos" <?php echo (xprofile_get_field_data( '27', get_current_user_id()) == 'Todos' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Todos', 'buddypress'); ?>
                    </option>
                    <option value="Mis amigos y yo" <?php echo (xprofile_get_field_data( '27', get_current_user_id()) == 'Mis amigos y yo' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Mis amigos y yo', 'buddypress'); ?>
                    </option>
                    <option value="Grupos" <?php echo (xprofile_get_field_data( '27', get_current_user_id()) == 'Grupos' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Grupos', 'buddypress'); ?>
                    </option>
                    <option value="Sólo yo" <?php echo (xprofile_get_field_data( '27', get_current_user_id()) == 'Sólo yo' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Sólo yo', 'buddypress'); ?>
                    </option>
                </select>
            </div>
            <?php
            continue;
        }
        
        if (bp_get_the_profile_field_id() == '4') {
            $s = 0;
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Patologia', 'buddypress'); ?>
                </label>
                
                <select id="<?php bp_the_profile_field_input_name() ?>" name="<?php bp_the_profile_field_input_name() ?>">
                    <?php
                    global $wpdb;
                    
                    $table_name = 'iron_patologias';
                    $familia    = 'familia_' . $_COOKIE['LANG'];
                    $patologia  = 'patologia_' . $_COOKIE['LANG'];
                    $query      = "SELECT DISTINCT " . $familia . " FROM " . $table_name . " ORDER BY " . $familia;
                    $results    = $wpdb->get_results($wpdb->prepare($query));
                    
                    foreach ($results as $fam) {
                        ?>
                        <optgroup label="<?php echo $fam->$familia?>">
                            <?php
                            $queryPatologias = "SELECT " . $patologia . " FROM " . $table_name . " WHERE " . $familia . " ='". $fam->$familia ."' ORDER BY " . $patologia;
                            $results         = $wpdb->get_results($wpdb->prepare($queryPatologias));
                            
                            foreach ($results as $pat) {
                                
                                if (xprofile_get_field_data( 'Patologia', get_current_user_id()) == $pat->$patologia ) {
                                    $sel = "selected";
                                    $s   = 1;
                                } else {
                                    $sel = "";
                                }
                                ?>
                                <option value="<?php echo $pat->$patologia; ?>" <?php echo $sel; ?>>
                                    <?php echo utf8_decode($pat->$patologia); ?>
                                </option>
                                <?php 
                            }
                            ?>
                        </optgroup>
                    <?php
                    }
                    ?>
                    <optgroup label="<?php _e('Otra patolog&iacute;a','buddypress')?>">
                        <option <?php if (xprofile_get_field_data( 'Patologia', get_current_user_id()) == 'Otra patología') echo 'selected'; ?> value="Otra patolog&iacute;a">
                            <?php _e('Otra patolog&iacute;a','buddypress'); ?>
                        </option>
                    </optgroup>
                    
                    <optgroup label="<?php _e('Sin Especificar','buddypress');?>">
                        <option <?php if (xprofile_get_field_data( 'Patologia', get_current_user_id()) == 'Sin Especificar') echo 'selected'; ?> value="Sin Especificar">
                            <?php _e('Sin Especificar','buddypress'); ?>
                        </option>
                    </optgroup>
                </select>
                
                <?php
                do_action( 'bp_custom_profile_edit_fields' );
                ?>
                
                <p class="description">
                    <?php bp_the_profile_field_description() ?>
                </p>
                
            </div>
            
            <?php
            continue;
        }
        
        if (bp_get_the_profile_field_id() == '15') {
            ?>
            
            <div<?php bp_field_css_class( 'editfield' ) ?> style="clear: both;">
                
                <label for="Privacidad">
                    <?php _e ('Privacidad', 'buddypress'); ?>
                </label>
                
                <select id="field_15" name="field_15">
                        
                        <option value="Privado" <?php if (xprofile_get_field_data( 'Privacidad', get_current_user_id()) == 'Privado' ) { echo " selected"; } ?>>
                            <?php _e('Privado', 'buddypress'); ?>
                        </option>
                        
                        <option value="Con mis amigos" <?php if (xprofile_get_field_data( 'Privacidad', get_current_user_id()) == 'Con mis amigos' ) { echo " selected"; } ?>>
                            <?php _e( 'Con mis amigos' , 'buddypress' );?>
                        </option>
                        <option value="Con toda la comunidad" <?php if (xprofile_get_field_data( 'Privacidad', get_current_user_id()) == 'Con toda la comunidad' ) { echo " selected"; } ?>>
                            <?php _e( 'Con toda la comunidad' , 'buddypress' )?>
                        </option>
                        
                </select>
                
            </div>
            <?php
            continue;
        }
        
        if (bp_get_the_profile_field_id() == "44") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?>>
                
                <label for="<?php bp_the_profile_field_input_name() ?>">
                    <?php echo __('Quién puede ver "Patologia"?', 'buddypress'); ?>
                </label>
                
                <select name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>">
                    <option value="Todos" <?php echo (xprofile_get_field_data( '44', get_current_user_id()) == 'Todos' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Todos', 'buddypress'); ?>
                    </option>
                    <option value="Mis Amigos" <?php echo (xprofile_get_field_data( '44', get_current_user_id()) == 'Mis Amigos' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Mis Amigos', 'buddypress'); ?>
                    </option>
                    <option value="Sólo yo" <?php echo (xprofile_get_field_data( '44', get_current_user_id()) == 'Sólo yo' ? 'selected="selected""' : '' ); ?>>
                        <?php _e('Sólo yo', 'buddypress'); ?>
                    </option>
                </select>
                
            </div>
            <?php
            continue;
        }
        /*
        if (bp_get_the_profile_field_id() == "19") {
            ?>
            <div<?php bp_field_css_class( 'editfield' ) ?> style="width: 100%; display: block; float: left;">

                <label for="<?php bp_the_profile_field_input_name() ?>" style="width: 270px;">
                    <?php echo __('Recordar mis cambios de Estado de Ánimo', 'buddypress'); ?>
                </label>
                <input type="checkbox" name="<?php bp_the_profile_field_input_name() ?>" id="<?php bp_the_profile_field_input_name() ?>" value="Recordar mis cambios de Estado de Ánimo" <?php echo (count(xprofile_get_field_data( '19', get_current_user_id())) == 1 ? 'checked="checked"' : '' ); ?> style="margin: 10px;" />
                    
            </div>
            <?php
            continue;
        }
        */
        /**
         * No mostramos el campo EstadoAnimo
         */
        if (bp_get_the_profile_field_name() == 'EstadoAnimo') { continue; }
        if (bp_get_the_profile_field_name() == 'Categoria') { continue; }
        
    endwhile;
    ?>
    
    <div class="submit">
        <input type="submit" name="profile-group-edit-submit" id="profile-group-edit-submit" value="<?php _e( 'Save changes', 'buddypress' ) ?> " />
    </div>
    <?php
    if ($_COOKIE['bp-message-type'] == 'success') {
        echo '<span style=" color: green; margin-left: 33px; ">' . __($_COOKIE['bp-message'] ,'buddypress') . '</span>';
    }
    ?>
    <input type="hidden" name="field_ids" id="field_ids" value="1,2,3,4,15,19,25,27,44" />
<?php
/*
1   ** Nombre
2   ** Apellidos
3   ** Poblacion
4   ** Patologia
15  ** Privacidad
19  ** auto_estado
25  ** Pais
27  ** Inicio
*/

wp_nonce_field( 'bp_xprofile_edit' );
?>

</form>

<?php
endwhile; endif;

do_action( 'bp_after_profile_edit_content' );
?>