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

<span class="dir">Editar Menu</span>
</div>


<div id="conteudo">

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

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

$id = $_POST['id'];

$nome = $_POST['nome'];
$pagina = $_POST['pagina'];
$ordem = $_POST['ordem'];
$status = $_POST['status'];
	
   include("utf8.php");
   
   $update = "UPDATE ctd_menu SET m_nome = '$nome', m_pagina = '$pagina', m_ordem = '$ordem', m_status = '$status' WHERE m_id = $id";
   mysqli_query($conn, $update);
   mysqli_close($conn);
      
   $local = "ctd_menu_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 * FROM ctd_menu WHERE m_id = $id";
$consulta = mysqli_query($conn, $qry);
$result = mysqli_fetch_array($consulta);

$sql_pag = "SELECT * FROM ctd_paginas ORDER BY p_nome";
$con_pag = mysqli_query($conn, $sql_pag);
?>

<form name="frmmenu" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit="return valida_menu(this);">
  <table align="center" cellspacing="0" cellpadding="0" class="form">
    	  <tr>
            <td colspan="3">
			Código(ID):
			<?=$result['m_id']?>            
            </td>
          </tr>          
          <tr>            
            <td colspan="3">
            <img src="imagem/importante.gif" />
            Nome <br />
            <input type="text" name="nome" value="<?=$result['m_nome']?>" maxlength="30" />
            </td>
          </tr>
          <tr>
            <td>            
            Página <br />
            <select name="pagina" id="pagina">
            <option value=""></option>
            <?PHP
			while($sel_pag = mysqli_fetch_array($con_pag)){
            	echo "<option value=\"". $sel_pag['p_id'] ."\" ";
				if ($sel_pag['p_id'] == $result['m_pagina'] ) { echo "SELECTED"; }
				echo " >". $sel_pag['p_nome'] ."</option>";
			}
            ?>
            </select>
            </td>
            <td width="20%">
            Ordem <br />
            <input name="ordem" type="number" value="<?=$result['m_ordem']?>" />
            </td>
            <td>
            <img src="imagem/importante.gif" />
            Situação <br />
            <select name="status">
            <option value="1" <? if ($result['m_status']==1) { echo "SELECTED"; }	?>>Ativo</option>
            <option value="0" <? if ($result['m_status']==0) { echo "SELECTED"; }	?>>Inativo</option>
            </select>   
            </td>
          </tr>
          <tr>
            <td colspan="3" align="center">
            <input type="hidden" value="<?=$id?>" name="id" />
              <input type="button" value="Voltar" name="voltar" onclick="location.href='ctd_menu_adm.php'" />
              <input type="submit" value="Salvar" name="submit" /></td>
          </tr>
        </table>
</form>
<?php
}
mysqli_close($conn);
?>
</div>
<? include_once("adm_foot.php"); ?>