<?php

	include('public/functions.php');

 	$qry = "SELECT * FROM tbl_anuncio";
	$result = mysqli_query($connect, $qry);

  setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
  date_default_timezone_set('America/Sao_Paulo');

  if(!empty($_GET['anunciodelete'])){
    if(!empty($_GET['idanuncio'])){
      $nt = mysqli_query($connect,"SELECT * FROM tbl_anuncio WHERE id = '".$_GET['idanuncio']."' ");
      $ntr = mysqli_fetch_assoc($nt);

      if(!empty($ntr['imagem'])){
        unlink('upload/anuncio/'.$ntr['imagem']);
      }

      Delete("tbl_anuncio","WHERE id = '".$_GET['idanuncio']."' ");
      $_SESSION['msg'] = "Baner excluido com êxito!";
      header('Location:anuncio.php');
      exit;
    }
  }

?>

<!-- START CONTENT -->
<section id="content">

      <!--breadcrumbs start-->
      <div id="breadcrumbs-wrapper" class=" grey lighten-3">
          <div class="container">
            <div class="row">
                <div class="col s12 m12 l12">
                  <h5 class="breadcrumbs-title">Baner app</h5>
                  <ol class="breadcrumb">
                    <li><a href="dashboard.php">Dashboard</a>
                    </li>
                    <li><a class="active">Lista de Baner</a>
                    </li>
                  </ol>
                </div>
            </div>
          </div>
      </div>
      <!--breadcrumbs end-->

      <!--start container-->
      <div class="container">
          <div class="section">
            <a href="addanuncio.php" class="btn cyan waves-effect waves-light right"> Add Splash</a>
              <div class="row">
                  <div class="col s12 m12 l12">
                      <div class="card-panel">
                        <div class="row">
                          <?php if(isset($_SESSION['msg'])) { ?>
                          <div class='card-panel teal lighten-2'>
                              <span class='white-text text-darken-2'><?php echo $_SESSION['msg'] ?></span>
                          </div>

                          <?php unset($_SESSION['msg']); }?>

                          <div class="col s12 m12 l12">
                              <table>
                                <thead>
                                  <tr>
                                    <th>ID</th>
                                    <th width="5%">Imagem</th>
                                    <th>Data da postagem</th>
                                    <th width="10%">Ação</th>
                                  </tr>
                                </thead>
                                <tbody>
                                    <?php while($data = mysqli_fetch_object($result)){ ?>
                                    <tr>
                                      <td><?php echo $data->id ?></td>
                                      <td><img width="100%" src="upload/anuncio/<?php echo $data->imagem ?>" ></td>
                                      <td><a href="editanuncio.php?idanuncio=<?php echo $data->id ?>"><?php echo $data->titulo ?></a></td>
                                      <td><?php echo substr(strip_tags($data->link),0,50) ?>...</td>
                                      <td><?php echo strftime('%A, %d de %B de %Y', strtotime($data->data)) ?></td>
                                      <td><a href="anuncio.php?anunciodelete=true&idanuncio=<?php echo $data->id ?>" class="btn cyan waves-effect waves-light">Excluir</a></td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                              </table>
                          </div>

                          </div>
                      </div>

                  </div>

              </div>
          </div>
      </div>

</section>
