<?php include('fncr-geral/configuracao_geral.php');
$pagina = "email";

if(isset($_GET['e'])){ $id_conteudo = $_GET['e']; }


// Cadastrar E-mail
if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'email') {

  $ass = $_POST['assunto'];
  $cont = $_POST['conteudo'];
  $desc = $_POST['descricao'];

  $sql_cadastrar = $bd->getConexao()->prepare('INSERT INTO emails
    (data, assunto, conteudo, descricao) VALUES
    (NOW(), "'.$ass.'", "'.$cont.'", "'.$desc.'")');
  if( $sql_cadastrar->execute() );

  $sql_email_criado = $bd->getConexao()->prepare('
  SELECT * FROM emails ORDER BY id DESC LIMIT 1');
  if( $sql_email_criado->execute() ) { $resul_email_criado = $sql_email_criado->fetch(PDO::FETCH_ASSOC);}

  $id_conteudo = $resul_email_criado['id'];

}


// Cadastrar Postagem
if(isset($_POST['editar']) && $_POST['editar'] == 'email') {

  $ass = $_POST['assunto'];
  $cont = $_POST['conteudo'];
  $desc = $_POST['descricao'];

  $sql_alt_post = $bd->getConexao()->prepare('UPDATE emails SET
    assunto = :assunto,
    conteudo = :conteudo,
    descricao = :descricao
    WHERE id = :id');
  $sql_alt_post->bindValue(':assunto', $ass, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':conteudo', $cont, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':descricao', $desc, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':id', $id_conteudo, PDO::PARAM_STR);
  if ($sql_alt_post->execute()) ;

}


$assunto = ''; $conteudo = ''; $descricao = '';
if(isset($id_conteudo)){

  // EMAIL
  $sql_email = $bd->getConexao()->prepare(' SELECT * FROM emails WHERE id = :id');
  $sql_email->bindValue(':id', $id_conteudo, PDO::PARAM_STR);
  if( $sql_email->execute() ) { $resul_email = $sql_email->fetch(PDO::FETCH_ASSOC);}

  $assunto = $resul_email['assunto'];
  $conteudo = $resul_email['conteudo'];
  $descricao = $resul_email['descricao'];

}


?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <?php include('fncr-geral/html-head.php');?>


</head>
<body>

<!-- BARRA SUPERIOR -->
<?php include('barra-sup.php');?>
<!-- MENU PRINCIPAL -->
<?php include('menu.php');?>

<section id="content">

<div class="clearfix padder bg-fncr-clearfix">
<h3 class="m-b">Blog</h3>

</div>

<section id="content" class="content-sidebar bg-white" style="padding-bottom: 100px">
<section class="main">
<br>

  <form class="form-horizontal" method="post" action="" data-validate="parsley" enctype="multipart/form-data">
  <input type="hidden" name="<?php if(isset($id_conteudo)){echo 'editar';}else{echo 'cadastrar';}?>" value="email" />

    <div class="col-sm-12">
      <section class="panel">
        <header class="panel-heading">
          <div class="col-sm-12">
            <button type="submit" class="btn btn-primary pull-right">Salvar</button>
            <strong>Postagem</strong>
          </div>
        </header>
        <div class="panel-body">
          <div class="form-group">
            <label class="col-lg-2 control-label">Assunto</label>
            <div class="col-lg-10">
              <input type="text" name="assunto" data-required="true" class="form-control" value="<?php echo $assunto?>">
            </div>
          </div>
          <div class="form-group">
            <label class="col-lg-2 control-label">Descri&ccedil;&atilde;o</label>
            <div class="col-lg-10">
              <input type="text" name="descricao" data-required="true" class="form-control" value="<?php echo $descricao?>">
            </div>
          </div>
            <textarea id="editor" name="conteudo" style="width: 100%" rows="30"><?php echo $conteudo?></textarea>
          <pre><?php echo $conteudo?></pre>
        </div>
      </section>
    </div>

  </form>

</section>
</section>

<!-- FOOTER -->
<?php include('footer.php');?>

<!-- Modal Excluir -->
<div id="modal_excluir" class="modal fade bs-example-modal-sm">
  <form class="m-b-none" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="excluir" value="postagem" />
    <input type="hidden" class="id" name="id" value=""/>
    <div class="modal-dialog modal-sm" style="width: 350px;">
      <div class="modal-content">
        <div class="modal-header bg-info">
          <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
          <h4 class="modal-title" id="myModalLabel">Excluir Postagem</h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-lg-12 text-center">
              Tem certeza que deseja excluir a postagem?
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-sm btn-info" data-loading-text="Excluindo..">Excluir</button>
        </div>
      </div>
    </div>
  </form>
</div>

<!-- app -->
<script src="js/app.js"></script>
<script src="js/app.plugin.js"></script>
<script src="js/app.data.js"></script>
<!-- Sparkline Chart -->
<script src="js/charts/sparkline/jquery.sparkline.min.js"></script>
<!-- Easy Pie Chart -->
<script src="js/charts/easypiechart/jquery.easy-pie-chart.js"></script>
  <!-- select2 -->
<script src="js/select2/select2.min.js"></script>
  <!-- fuelux -->
<script src="js/fuelux/fuelux.js"></script>
<!-- datepicker -->
<script src="js/datepicker/bootstrap-datepicker.js"></script>
<!-- slider -->
<script src="js/slider/bootstrap-slider.js"></script>
<!-- file input -->
<script src="js/file-input/bootstrap.file-input.js"></script>
<!-- combodate -->
<script src="js/combodate/moment.min.js"></script>
<script src="js/combodate/combodate.js"></script>
<!-- parsley -->
<script src="js/parsley/parsley.min.js"></script>

</body>
</html>
<?php $bd = null; $bd_fn = null;?>