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

if(isset($_GET['p'])){ $id_blog = $_GET['p']; }

// Cadastrar Postagem
if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'postagem') {

  $banner = $_POST['banner'];
  $titulo = $_POST['titulo'];
  $inf1 = $_POST['inf1'];
  $inf2 = $_POST['inf2'];
  $inf3 = $_POST['inf3'];
  $data = $datahora_bd;
  $conteudo = $_POST['conteudo'];

  $sql_cadastrar = $bd->getConexao()->prepare('INSERT INTO blog
    (data, banner, titulo, conteudo, inf1, inf2, inf3, status) VALUES
    ("'.$data.'", "'.$banner.'", "'.$titulo.'", "'.$conteudo.'", "'.$inf1.'", "'.$inf2.'", "'.$inf3.'", "0")');
  if( $sql_cadastrar->execute() );
}


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

  $banner = $_POST['banner'];
  $titulo = $_POST['titulo'];
  $texto_curto = $_POST['texto_curto'];
  $inf1 = $_POST['inf1'];
  $inf2 = $_POST['inf2'];
  $inf3 = $_POST['inf3'];
  $data = $_POST['data'];
  $conteudo = $_POST['conteudo'];

  $sql_alt_post = $bd->getConexao()->prepare('UPDATE blog SET
    banner = :banner,
    titulo = :titulo,
    inf1 = :inf1,
    inf2 = :inf2,
    inf3 = :inf3,
    data = :data,
    conteudo = :conteudo
    WHERE id = :id');
  $sql_alt_post->bindValue(':banner', $banner, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':titulo', $titulo, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':inf1', $inf1, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':inf2', $inf2, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':inf3', $inf3, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':data', $data, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':conteudo', $conteudo, PDO::PARAM_STR);
  $sql_alt_post->bindValue(':id', $id_blog, PDO::PARAM_STR);
  if ($sql_alt_post->execute()) ;
}


// BLOG - DETALHES POSTAGEM
$sql_blog = $bd->getConexao()->prepare('
SELECT blog.*,
  DATE_FORMAT(blog.data,"%d/%m/%Y") AS data_post,
  blog_banners.img
FROM blog
  LEFT JOIN blog_banners ON blog_banners.id = blog.banner
WHERE blog.id = :id');
$sql_blog->bindValue(':id', $id_blog, PDO::PARAM_STR);
if( $sql_blog->execute() ) { $resul_blog = $sql_blog->fetch(PDO::FETCH_ASSOC);}

// BANNERS
$sql_banners = $bd->getConexao()->prepare('SELECT * FROM blog_banners ORDER BY img ASC');
if( $sql_banners->execute() ) { $resul_banners = $sql_banners->fetchall(PDO::FETCH_ASSOC);}

// MARKERS
$sql_markers = $bd->getConexao()->prepare('SELECT * FROM blog_markers ORDER BY titulo ASC');
if( $sql_markers->execute() ) { $resul_markers = $sql_markers->fetchall(PDO::FETCH_ASSOC);}

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <?php include('fncr-geral/html-head.php');?>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/ui/trumbowyg.min.css">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/trumbowyg@2.28.0/dist/trumbowyg.min.js"></script>
    <script>
      $(document).ready(function(){
        $('#conteudo').trumbowyg();
      });

    </script>
</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_blog)){echo 'editar';}else{echo 'cadastrar';}?>" value="postagem" />

    <div class="col-sm-12">
      <section class="panel">
        <header class="panel-heading">
          <div class="col-sm-4">
            <strong>Postagem</strong>
          </div>
        </header>
        <div class="panel-body">
          <div class="col-sm-5">
            <div class="form-group">
              <label class="col-lg-3 control-label">Banner</label>
              <div class="col-lg-8">
                <?php if(isset($id_blog)){ ?>
                <div class="img"><img src="https://pokerweb.com.br/blog/post_<?php echo $resul_blog['img']?>"></div>
                <?php } ?>
                <select class="form-control" name="banner">
                  <option></option>
                  <?php foreach($resul_banners as $bn){
                    if($resul_blog['banner']==$bn['id']){$selected = "selected";}else{$selected = "";}
                    echo '<option value="'.$bn['id'].'" '.$selected.'>'.$bn['img'].'</option>';
                  }?>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">T&iacute;tulo</label>
              <div class="col-lg-8">
                <input type="text" name="titulo" data-required="true" class="form-control" value="<?php echo $resul_blog['titulo']?>">
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Marker 1</label>
              <div class="col-lg-8">
                <select class="form-control" name="inf1">
                  <option></option>
                  <?php foreach($resul_markers as $mk){
                    if($resul_blog['inf1']==$mk['id']){$selected = "selected";}else{$selected = "";}
                    echo '<option value="'.$mk['id'].'" '.$selected.'>'.$mk['titulo'].'</option>';
                  }?>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Marker 2</label>
              <div class="col-lg-8">
                <select class="form-control" name="inf2">
                  <option></option>
                  <?php foreach($resul_markers as $mk){
                    if($resul_blog['inf2']==$mk['id']){$selected = "selected";}else{$selected = "";}
                    echo '<option value="'.$mk['id'].'" '.$selected.'>'.$mk['titulo'].'</option>';
                  }?>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Marker 3</label>
              <div class="col-lg-8">
                <select class="form-control" name="inf3">
                  <option></option>
                  <?php foreach($resul_markers as $mk){
                    if($resul_blog['inf3']==$mk['id']){$selected = "selected";}else{$selected = "";}
                    echo '<option value="'.$mk['id'].'" '.$selected.'>'.$mk['titulo'].'</option>';
                  }?>
                </select>
              </div>
            </div>
            <?php if(isset($id_blog)){ ?>
            <div class="form-group">
              <label class="col-lg-3 control-label">Data</label>
              <div class="col-lg-8">
                <input type="text" name="data" data-required="true" class="form-control" value="<?php echo $resul_blog['data']?>">
              </div>
            </div>
            <?php } ?>
            <div class="form-group">
              <div class="col-lg-9 col-lg-offset-3">
                <button type="submit" class="btn btn-primary">Salvar</button>
              </div>
            </div>
          </div>
          <div class="col-sm-7">
            <label class="col-lg-2 control-label">Texto Curto</label>
            <div class="col-app-10" style="margin-bottom: 20px">
              <textarea name="texto_curto" class="form-control" rows="4"><?php echo $resul_blog['texto_curto']?></textarea>
            </div>
          </div>
          <div class="col-sm-12">
              <textarea id="conteudo" name="conteudo" class="form-control"></textarea>
          </div>
        </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;?>