<? 
include_once("valida_sessao.php");
protegePagina(); 
include_once("adm_head.php");
?>

<span class="dir">Editar Usuário</span>
</div>

<div id="conteudo">

<?php
include("config.php");

$date = date("Y-m-d H:i:s");
if (isset($_POST['submit'])) {

$id = $_POST['id'];
$usuario = $_POST['usuario'];
$senhanova = $_POST['senhanova'];
$nome = $_POST['nome'];
$email = $_POST['email'];

$adm_usuarios = $_POST['adm_usuarios'];
$adm_conteudo = $_POST['adm_conteudo'];
$adm_conteudo_pub = $_POST['adm_conteudo_pub'];
$adm_galeria = $_POST['adm_galeria'];
$adm_banner = $_POST['adm_banner'];
$adm_video = $_POST['adm_video'];
$adm_newsletter = $_POST['adm_newsletter'];

$alt_func = $_SESSION['usuarioNome'];
$alt_data = $_POST['alt_data'];

   include("utf8.php");
   
   if($senhanova) {
	   $update = "UPDATE usuarios SET usuario = '$usuario', senha = md5('$senhanova'), nome = '$nome', email = '$email', adm_usuarios = '$adm_usuarios', adm_conteudo = '$adm_conteudo', adm_conteudo_pub = '$adm_conteudo_pub', adm_galeria = '$adm_galeria', adm_banner = '$adm_banner', adm_video = '$adm_video', adm_newsletter = '$adm_newsletter', alt_func = '$alt_func', alt_data = '$alt_data' WHERE id = $id";
   } else {
	   $update = "UPDATE usuarios SET usuario = '$usuario', nome = '$nome', email = '$email', adm_usuarios = '$adm_usuarios', adm_conteudo = '$adm_conteudo', adm_conteudo_pub = '$adm_conteudo_pub', adm_galeria = '$adm_galeria', adm_banner = '$adm_banner', adm_video = '$adm_video', adm_newsletter = '$adm_newsletter', alt_func = '$alt_func', alt_data = '$alt_data' WHERE id = $id";
   }
   mysqli_query($conn, $update);
   mysqli_close($conn);
      
   $local = "usuario_alt.php?id=".$id."&alterado=1";
   echo "<script>location.href='$local';</script>";
?>

<?php
} else {
	
if(isset($_GET['alterado'])){
	echo "<p class=\"sucesso\">Alterado Com Sucesso!</p>";
}

$id = $_GET['id'];

include("utf8.php");

$qry = "SELECT *, date_format(alt_data, '%d/%m/%Y - %H:%i') as data FROM usuarios WHERE id = $id";
$consulta = mysqli_query($conn, $qry);
$result = mysqli_fetch_array($consulta)

?>

<form name="frmusuario" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit="return valida_usuario(this);">
  <table align="center" cellspacing="0" cellpadding="0">
    	  <tr>
            <td>
			Código(ID):
			<?=$result['id']?><br /><br />
            Última alteração feita em <strong><?=$result['data']?></strong><br />
            por <strong><?=$result['alt_func']?> </strong><br /><br />        
            </td>
          </tr>          
          <tr>            
            <td>
            <img src="imagem/importante.gif" />
            Usuário <br />
            <input type="text" name="usuario" size="40" value="<?=$result['usuario']?>" maxlength="30" />
            </td>
          </tr>          
          <tr>            
            <td>
            Senha <br />
            <input type="password" name="senhanova" size="40" maxlength="30" />
            </td>
          </tr>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Nome <br />
            <input type="text" name="nome" size="40" value="<?=$result['nome']?>" maxlength="50" />
            </td>
          </tr>          
          <tr>            
            <td>
            E-mail <br />
            <input type="text" name="email" size="40" value="<?=$result['email']?>" maxlength="30" />
            </td>
          </tr>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Usuários? <br />
            <select name="adm_usuarios">
              <option value="0" <?php if($result['adm_usuarios']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_usuarios']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
		  <?php if(mysqli_query($conn, "SELECT * FROM ctd_blocos")) { ?>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Conteúdo? <br />
            <select name="adm_conteudo">
              <option value="0" <?php if($result['adm_conteudo']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_conteudo']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Publicação de Conteúdo? <br />
            <select name="adm_conteudo_pub">
              <option value="0" <?php if($result['adm_conteudo_pub']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_conteudo_pub']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
		  <?php }
		  if(mysqli_query($conn, "SELECT * FROM glr_galeria")) { ?>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Galerias? <br />
            <select name="adm_galeria">
              <option value="0" <?php if($result['adm_galeria']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_galeria']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
		  <?php }
		  if(mysqli_query($conn, "SELECT * FROM bnr_ban_imagens")) { ?>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Banners? <br />
            <select name="adm_banner">
              <option value="0" <?php if($result['adm_banner']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_banner']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
          <? }
		  if(mysqli_query($conn, "SELECT * FROM vds_videos")) { ?>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Videos? <br />
            <select name="adm_video">
              <option value="0" <?php if($result['adm_video']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_video']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
          <? }
		  if(mysqli_query($conn, "SELECT * FROM nlt_emails")) { ?>
          <tr>
            <td>
            <img src="imagem/importante.gif" />
            Administra Newsletter? <br />
            <select name="adm_newsletter">
              <option value="0" <?php if($result['adm_newsletter']=='0') {echo "selected=selected";}?>>Não</option>
              <option value="1" <?php if($result['adm_newsletter']=='1') {echo "selected=selected";}?>>Sim</option>
            </select>
            </td>
          </tr>
          <? } ?>
          <tr>
            <td align="center">
            <input type="hidden" value="<?=$id?>" name="id" />
            <input type="hidden" value="<?=$date?>" name="alt_data" />
              <input type="button" value="Voltar" name="voltar" onclick="location.href='usuario_adm.php'" />           
              <input type="submit" value="Salvar" name="submit" />
              </td>
          </tr>
        </table>
</form>
<?php
}
mysqli_close($conn);
?>
</div>
<? include_once("adm_foot.php"); ?>