<?php    
session_start();
include("../includes/conect.php");


if (isset($_POST['btn_send'])){


    $iduser     = $_SESSION['k_temp_id']; 
    $correo     = $_POST['email']; 

    $code_reserva   = $_SESSION['k_temp_user_apto'];
    $user_apto      = $_SESSION['k_temp_user_apto'];
    // exit;

      $query_search = 'SELECT email FROM usuario_area WHERE email =\''.$_POST['email'].'\'';
    if (!$result = mysqli_query($conexion, $query_search)) {
        exit(mysqli_error());
    } 

    if(mysqli_num_rows($result) > 0) {
       echo "este mail ya existe, intente con otro";
       header("location:../email.php?error=errorm");
        exit(); 
    }else{ 

    //actualizacion a BD por apartamento
        $tipo_ocu = $_SESSION['k_temp_ocupacion'];
    $sql ="UPDATE usuario_area Set email='$correo',  code_reserva='$code_reserva',  user_apto='$user_apto', tipo='$tipo_ocu' Where idocupante = '$iduser'";
    $query = mysqli_query($conexion, $sql)  or die ('Operación NO efectuada: '.mysqli_error());
     

        $result = mysqli_query($conexion, 'SELECT * FROM usuario_area WHERE idocupante=\''.$iduser.'\'');
        $row = mysqli_fetch_array($result);
        
        if($row["idocupante"] == $iduser){

            if ($row['estado'] == 0 ) {
                    header('location:../index.php?act=err');
                }else{ 
                                
  
                    $_SESSION['k_name'] =  $_SESSION['k_temp_name'];
                    $_SESSION['k_ape'] = $_SESSION['k_temp_ape'];
                    $_SESSION['k_id'] = $_SESSION['k_temp_id'];
                    $_SESSION['k_access'] = $_SESSION['k_temp_access'];             
                    $_SESSION['k_id_table'] = $row['id_table_ocup'];        
                    $_SESSION['k_correo'] = $row['email'];  
                    $_SESSION['k_code'] = $_SESSION['k_temp_code'];
                    $_SESSION['k_code_reserva'] = $_SESSION['k_temp_code_reserva'];
                    $_SESSION['k_edad'] = $row['edad'];
                    $_SESSION['k_code_ocupacion'] = $_SESSION['k_temp_code_ocupacion'];
                    $_SESSION['k_ocupacion'] = $_SESSION['k_temp_ocupacion'];
                    $_SESSION['k_user_apto'] = $_SESSION['k_temp_user_apto'];

                //acceso
                // echo 1; 
                    echo "logueado correctamente";
                header('location:../dashboard.php');
              
            }
            
        }  
        else{
            //Valores no coinciden
            echo 2; 
            header('location:../index.php?error=erro');
        }       
 
 

    }       

}
else{

    header("location:../index.php"); 
}

  
     mysqli_close($conexion);
 // }                       

?>
    