<?php require_once('../topo.php');?>
<?php require_once('../lateral.php');?>
<br><br><br>
<?php
//GERADOR DE CAMPOS POST

//echo GerarCamposPost("categorias", "linha_categorias");

$idcentrocusto		= $_GET['idcentrocusto'];

if($idcentrocusto == ""){
	$acao		 	= "Salvar";
}else{
	$acao 			= "Alterar";	
}

//Recebe Ação
$acao_bd		    = $_GET['acao'];
$acao_bd		   .= $_POST['acao'];

//Recebendo Dados do Formulário

$idcentrocusto .= $_POST['idcentrocusto'];
$titulo = $_POST['titulo'];
$sobre = addslashes(htmlentities($_POST['sobre']));
$contratante = addslashes(htmlentities($_POST['contratante']));



//Dados da Tabela
$tabela                 ="centro_custo";
$campos_tabela = "titulo, sobre, idfilial, contratante";

$campos_value = "'$titulo', '$sobre', '".$_SESSION['idfilial_usuario']."', '$contratante'";

//INSERIR REGISTROS
if($acao_bd == "Salvar"){
	$salvar = Salvar($tabela,  $campos_tabela, $campos_value);

// RETORNA ULTIMO ID INSERIDO
	$id_insert = mysql_insert_id();
	$redireciona = VerificaSqlSemAlert($salvar, "listar_centrocusto.php", "listar_centrocusto.php");
}

//ATUALIZANDO REGISTRO
if($acao_bd == "Alterar"){

$campos_alterar = "titulo='$titulo', sobre='$sobre', idfilial='".$_SESSION['idfilial_usuario']."', contratante='$contratante'";

    $atualizar = Alterar($tabela, $campos_alterar, "idcentrocusto", $idcentrocusto);
	$redireciona = VerificaSqlSemAlert($atualizar, "manutencao_centrocusto.php?idcentrocusto=".$idcentrocusto."", "manutencao_centrocusto.php?idcentrocusto=".$idcentrocusto."");
}

//EXCLUINDO REGISTRO
if($_GET['excluir'] == "sim"){
    $deletar = Excluir($tabela, "idcentrocusto", $idcentrocusto);	
	$redireciona = VerificaSqlSemAlert($deletar, "listar_centrocusto.php", "manutencao_centrocusto.php?idcentrocusto=".$idcentrocusto."");
} 


if($acao == "Alterar"){

	$selecionar_categorias = Seleciona("centro_custo", "WHERE idcentrocusto='".$idcentrocusto."'", "") or die(mysql_error());
	while($linha_categorias=mysql_fetch_array($selecionar_categorias)){
		
$titulo = $linha_categorias['titulo']; 
$sobre = $linha_categorias['sobre']; 
$contratante = $linha_categorias['contratante']; 

	}
}
?>
<script type="text/javascript" src="js/tinymce/tinymce.min.js"></script>    
<script type="text/javascript">
tinymce.init({
selector: "textarea",
plugins: [
	"advlist autolink lists link image charmap print preview anchor",
	"searchreplace visualblocks code fullscreen"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
<script type="text/javascript">
function EnviarFormulario()
{
    document.forms["cadastro"].submit();
}
</script>
<div id="box-toggle">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="2"><table width="100%" border="0" align="center" cellpadding="5" cellspacing="0">
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="237" height="40" class="TituloPagina" style="padding-left: 5px;">CENTRO DE CUSTO</td>
            <td width="722" align="left" class="TituloPagina" style="padding-left: 5px;"><table width="100" border="0" align="right" cellpadding="2" cellspacing="0">
              <tr>
                <td width="167" align="left"><table width="200" border="0" cellspacing="0" cellpadding="2" class="BoxMenuAzul" onclick="location.href='listar_centrocusto.php';">
                  <tr>
                    <td height="25" align="center" class="TextoMenuCinza"><img src="img/icone_novo.gif" width="29" height="21" /></td>
                    <td align="left"><span class="TextoMenuBranco" style="font-weight:normal; color:#FFF;">Todas os Centro de Custo</span></td>
                    </tr>
                  </table></td>
                <td width="150" align="left"><table width="190" border="0" cellspacing="0" cellpadding="2" class="BoxMenuAzul" onclick="location.href='manutencao_centrocusto.php';">
                  <tr>
                    <td height="25" align="center" class="TextoMenuCinza"><img src="img/icone_novo.gif" width="29" height="21" /></td>
                    <td align="left"><span class="TextoMenuBranco" style="font-weight:normal; color:#FFF;">Nova Centro de Custo</span></td>
                    </tr>
                  </table></td>
                <td width="125" align="left">&nbsp;</td>
                <td width="370" align="right"><table width="180" border="0" align="right" cellpadding="0" cellspacing="0" class="BoxMenuBranco">
                  <form method="post" action="listar_centrocusto.php" enctype="multipart/form-data">
                    <tr>
                      <td width="148" align="left" bgcolor="#FFFFFF"><input type="text" name="busca" id="busca" style="width: 100%; height: 20px;" class="InputBranco" /></td>
                      <td width="28" align="center" bgcolor="#FFFFFF"><input type="image" src="img/icone_buscar.gif" /></td>
                      </tr>
                    </form>
                  </table></td>
                </tr>
              </table></td>
            <td width="5" align="right">&nbsp;</td>
            <td width="80" align="right" style="padding-right: 8px;"><table width="70" border="0" cellspacing="0" cellpadding="2" class="BoxMenuCinza" onclick="history.go(-1);">
              <tr>
                <td height="25" align="center" class="TextoMenuBranco">Voltar</td>
                </tr>
              </table></td>
            </tr>
        </table></td>
        </tr>
        <!-- INICIO FORM -->
<form name="cadastro" id="cadastro" method="post" action="manutencao_centrocusto.php" enctype="multipart/form-data">
<input type="hidden" name="acao" value="<?php echo $acao;?>">
<input type="hidden" name="idcentrocusto" value="<?php echo $idcentrocusto;?>">
      <tr>
        <td width="25%" valign="top" bgcolor="#D9D9D9"><table width="100%" border="0" cellspacing="0" cellpadding="4">
          <tr>
            <td valign="top"><!-- INICIO DIV -->
              <table width="100%" border="0" cellspacing="0" cellpadding="7">
                <tr>
                  <td width="50%" align="left"><strong style="color: #676767;  font-size: 12px;">Titulo</strong></td>
                  <td width="1600" style="color: #676767;  font-size: 12px;">&nbsp;</td>
                  </tr>
                <tr>
                  <td colspan="2" style="color: #676767; font-size: 12px;"><input type="text" name="titulo" id="titulo" class="form-campos"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $titulo;?>" /></td>
                  </tr>
                <tr>
                  <td colspan="2" style="color: #676767; font-size: 12px;"><strong style="color: #676767;  font-size: 12px;">Texto</strong></td>
                  </tr>
                <tr>
                  <td colspan="2" style="color: #676767; font-size: 12px;"><span style="color: #676767;  font-size: 12px;">
                    <textarea name="sobre" class="form-campos" id="sobre" style="width:100%; height: 200px;"><?php echo html_entity_decode($sobre); ?></textarea>
                  </span></td>
                </tr>
                <tr>
                  <td colspan="2" style="color: #676767; font-size: 12px;"><strong style="color: #676767;  font-size: 12px;">Contratante</strong></td>
                </tr>
                <tr>
                  <td colspan="2" style="color: #676767; font-size: 12px;">
                    <textarea name="contratante" class="form-campos" id="contratante" style="width:100%; height: 200px;"><?php echo html_entity_decode($contratante); ?></textarea>
                  </td>
                  </tr>
                </table>
              <!-- FIM DIV --></td>
            </tr>
          </table>
          <table width="100%" border="0" cellspacing="0" cellpadding="7">
            <tr>
              <td align="right"><input name="button" type="submit" class="BoxVerde" id="button" value="Salvar" style="width: 100px; height: 35px;" onclick="EnviarFormulario()"></td>
              <?php if($acao == "Alterar"){ ?>
              <td width="100" align="right"><table width="70" border="0" cellspacing="0" cellpadding="2" class="BoxMenuVermelho" onclick="location.href='manutencao_centrocusto.php?idcentrocusto=<?php echo $idcentrocusto;?>&amp;excluir=sim';" style="width: 100px; height: 35px; color:#FFFFFF;">
                <tr>
                  <td height="25" align="center" class="TextoMenuBranco">Excluir</td>
                  </tr>
                </table></td>
              <?php } ?>
              </tr>
          </table></td>
        </tr>
      </form>
      <!-- FIM FORM -->    
      </table>      
      </td>
  </tr>
  </table>
 </div>
<?php require_once('../rodape.php');?>