<?php

	include('public/functions.php');

 	$qry = "SELECT * FROM tbl_banertv";
	$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['banerdelete'])){
    if(!empty($_GET['idbaner'])){
      $nt = mysqli_query($connect,"SELECT * FROM tbl_banertv WHERE id = '".$_GET['idbaner']."' ");
      $ntr = mysqli_fetch_assoc($nt);

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

      Delete("tbl_banertv","WHERE id = '".$_GET['idbaner']."' ");
      $_SESSION['msg'] = "Baner excluido com êxito!";
      header('Location:banertv.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="addbanertv.php" class="btn cyan waves-effect waves-light right"> Add Baner</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>Título</th>
                                    <th>Link</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/baner_tv/<?php echo $data->imagem ?>" ></td>
                                      <td><?php echo $data->titulo ?></td>
                                      <td><?php echo substr(strip_tags($data->link),0,50) ?>...</td>
                                      <td><?php echo strftime('%d de %B de %Y', strtotime($data->data)) ?></td>
										
										<td>
                                      <a href="editbanertv.php?idbaner=<?php echo $data->id ?>" class="btn-floating cyan tooltipped" data-position="left" data-tooltip="Editar"><i class="material-icons">mode_edit</i></a>
                                      <a href="banertv.php?banerdelete=true&idbaner=<?php echo $data->id ?>" class="btn-floating cyan tooltipped" data-position="top" data-tooltip="Deletar"><i class="material-icons">delete_sweep</i></a>
                                    </td>

                                    <?php } ?>
                                </tbody>
                              </table>
                          </div>

                          </div>
                      </div>

                  </div>

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

</section>
