<?php

	include('public/functions.php');

  if(empty($_GET['idbaner'])){ 
    header( "Location:banertv.php"); 
    exit; 
  }

  $qry = "SELECT * FROM tbl_banertv where id = '".$_GET['idbaner']."' ";
  $result = mysqli_query($connect, $qry);
  $result_row = mysqli_fetch_assoc($result);
  
	if(isset($_POST['submit'])) {

      if($_FILES['imagem']['name'] != '') {

        unlink('upload/baner_tv/'.$result_row['imagem']);

  			$image = time().'_'.$_FILES['imagem']['name'];
  			$pic2			 = $_FILES['imagem']['tmp_name'];
   			$tpath2			 = 'upload/baner_tv/'.$image;
  			copy ($pic2, $tpath2);

        $data = array(
          'titulo' => $_POST['titulo'],
          'imagem' => $image,
          'link' => $_POST['link']
        );

  		}else{
        $data = array(
          'titulo' => $_POST['titulo'],
          'link' => $_POST['link']
        );
      }

  		Update('tbl_banertv', $data, " WHERE id = '".$_GET['idbaner']."' ");
  		
			$_SESSION['msg'] = "Baner alterado 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">Editar Baner</a>
                    </li>
                  </ol>
                </div>
            </div>
          </div>
      </div>
      <!--breadcrumbs end-->

      <!--start container-->
      <div class="container">
          <div class="section">
              <div class="row">

                  <div class="col s12 m12 l12">

                      <div class="card-panel">
                          <form method="post" enctype="multipart/form-data">
                              <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">
                                      <div class="input-field col s12 l6">
                                          <label>Título do Baner :</label>
                                          <br>
                                          <input type="text" name="titulo" id="titulo" value="<?php echo $result_row['titulo'] ?>" placeholder="Título do baner" required />
                                      </div>
                                  </div>

                                  <div class="col s12 m12 l12">
                                    <div id="input-select " class="input-field col s12 l6">
                                      <label>Link</label>
                                      <br>
										<input type="text" name="link" id="link" value="<?php echo $result_row['link'] ?>" placeholder="Link do baner" />
                                    </div>
                                  </div>

                                  <div class="col s12 m12 l6">
                                    <div class="input-field col s12 l12">
                                      <label >Imagem</label>
                                      <br>
                                      <img width="20%" src="upload/baner_tv/<?php echo $result_row['imagem'] ?>">
                                      <br>
                                      <input type="file" name="imagem" id="imagem" value="">
                                    </div>
                                  </div>

                                  <div class="input-field col s12"><br>
                                      <button type="submit" name="submit" class="btn cyan waves-effect waves-light">Alterar Baner</button>
                                  </div>

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

                  </div>

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

</section>
