<?
require("acesso.php");

$acao = $_POST[acao];

if ($acao == "salvar") {
	$id = $_SESSION["id_usuario"];
	$nome = $_POST['nome'];
	$email = $_POST['email'];
	$senha = $_POST['nova_senha'];
	if ($senha != '') {
		$senha_md5 = md5($senha);
	}
	if ($senha != '') {
		$query_update = "UPDATE usuarios SET nome='$nome', email='$email', senha='$senha_md5' WHERE id='$id'";
	} else {
		$query_update = "UPDATE usuarios SET nome='$nome', email='$email' WHERE id='$id'";
	}
	$update = mysql_query($query_update);
	if (!$update) {
		die ("Opsss, nao achei o banco.");
	}
	else {
		$_SESSION["usuario"] = $nome;
		header("Location: usuario.php?info=ok&causa=".urlencode('Usu&aacute;rio editado com sucesso!'));
		exit;
	}
} else {
include("topo.php");
#Seleciona o Usuário conforme o ID do mesmo, obtido através da Session.
$id = $_SESSION["id_usuario"];
$query = "SELECT * FROM usuarios WHERE id='$id'";
$resultado = mysql_query($query);
$monta_select = mysql_fetch_array($resultado);
?>
<form name="ficha" method="post" action="usuario.php">
<input name="acao" type="hidden" value="salvar" />
<table width="780" border="0" cellspacing="0" cellpadding="0" align="center" style="margin-top:25px; background:url(images/fundo_painel.jpg) bottom right no-repeat;">
	<tr>
    	<td>
            <?
			if($_GET['info'] == "falhou") {
				$erro = $_GET['causa'];
			?>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:15px;">
              <tr>
                <td align="center"><img src="images/icones/aviso.jpg" align="absmiddle"/><span class="erro"><? echo $erro; ?></span></td>
              </tr>
            </table>
            <? } 
			if($_GET['info'] == "ok") {
				$ok = $_GET['causa'];
			?>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:15px;">
              <tr>
                <td align="center"><img src="images/icones/aviso.jpg" align="absmiddle"/><span class="ok"><? echo $ok; ?></span></td>
              </tr>
            </table>
            <? } ?>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:3px #F2F2F2 double; padding:5px 10px 0px 10px; background-color:#FFFFFF;">
              <tr>
                <td><img src="images/icones/home.jpg" align="absmiddle" /><span class="titulo">Editar Usu&aacute;rio</span></td>
				<td width="100" align="center" class="texto"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /><br />
				Salvar Usu&aacute;rio</td>
				<td width="100" align="center" class="texto"><a href="index2.php"><img src="images/icones/home.jpg" border="0" /><br />
				Voltar</a></td>
              </tr>
            </table>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="texto" style="margin-top:20px;">
              <tr>
                <td height="27" colspan="2" align="left" style="padding:10px;">
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border:3px #F2F2F2 double;">
                      <tr>
                        <td width="43"><img src="images/icones/info.jpg" width="33" height="33" /></td>
                        <td>Edite as suas informa&ccedil;&otilde;es, como e-mail, nome e senha.<br />
                          IMPORTANTE: Altere sua senha periodicamente para evitar transtornos.</td>
                      </tr>
                    </table>                </td>
              </tr>
                 <?
					do {
						$nome = $monta_select["nome"];
						$email = $monta_select["email"];
					
					#Imprime o formulário com os dados do piloto
				?>
              <tr>
                <td width="130" height="27" align="right">Nome: </td>
                <td><input name="nome" type="text" id="nome" value="<? echo "$nome"; ?>" class="inputbox" /></td>
              </tr>
              <tr>
                <td width="130" height="27" align="right">Email: </td>
                <td><input name="email" type="text" id="email" value="<? echo "$email"; ?>" class="inputbox" /></td>
              </tr>
              <tr>
                <td width="130" height="27" align="right">Senha: </td>
                <td><input name="nova_senha" type="password" id="nova_senha" class="inputbox" /> 
                <em>Digite a nova senha neste campo caso deseje alterar a mesma.</em></td>
              </tr>
				<?
					}
					while ($monta_select = mysql_fetch_array($resultado)); 
                ?>
              <tr>
                <td align="left">&nbsp;</td>
                <td align="left" style="padding-top:15px;"><input name="Salvar" type="image" src="images/icones/salvar.jpg" /></td>
              </tr>
          </table>
      </td>
	</tr>
</table>
</form>
<?php
include("rodape.php");
}
?>