<?php

	include('public/functions.php');

 	$qry = "SELECT * FROM tbl_tv where id = '1'";
	$result = mysqli_query($connect, $qry);
	$result_row = mysqli_fetch_assoc($result);

	if(isset($_POST['submit'])) {

      $sql_query = "SELECT * FROM tbl_tv WHERE id = '1'";
  		$img_res = mysqli_query($connect, $sql_query);
  		$img_row=  mysqli_fetch_assoc($img_res);

      if($_FILES['tv_imagem']['name'] != '') {
  				unlink('upload/tv_imagem/'.$_POST['old_image']);
  				$tv_imagem = time().'_'.$_FILES['tv_imagem']['name'];
  				$pic2			 = $_FILES['tv_imagem']['tmp_name'];
     			$tpath2			 = 'upload/tv_imagem/'.$tv_imagem;
  				copy ($pic2, $tpath2);
  			} else {
  				$tv_imagem = $_POST['old_image'];
  			}
			
		

  		$data = array(
              'tv_nome' => $_POST['tv_nome'],
              'tv_imagem' => $tv_imagem,
              'tv_url' => $_POST['tv_url'],
  		);

  		$result_id = Update('tbl_tv', $data, "WHERE id = '1'");

  		if ($result_id > 0) {
  			$_SESSION['msg'] = "";
              header( "Location:tv.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">App Conexão</h5>
                  <ol class="breadcrumb">
                    <li><a href="dashboard.php">TV Url</a>
                    </li>
                    <li><a class="active">Dados da TV</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'>Salvo com sucesso...</span>
                                  </div>

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

                           <!--       <div class="col s12 m12 l6">
                                      <div class="input-field col s12">
                                          <label for="tv_nome">Nome da TV :</label>
                                          <input type="text" name="tv_nome" id="tv_nome" placeholder="" value="<?php echo $result_row['tv_nome'];?>" required />
                                      </div>-->

                                      <div class="input-field col s12">
                                          <label for="tv_url">Url da TV :</label>
                                          <input type="text" name="tv_url" id="tv_url" placeholder="" value="<?php echo $result_row['tv_url'];?>" required />
                                          <!-- add novo -->

			                <!-- add novo fim -->
                                      </div>

				<!--	<div class="col s12 m12 l6"><br><br>
						<h5>Imagens da TV</h5>
                                            <div class="input-field col s12">
                                                <label for="tv_imagem">Logo do APP - Tamanho: 512x512px</label>
                                                    <br><p><img width="100" heigth="100" src="upload/tv_imagem/<?php echo $result_row['tv_imagem']; ?> "></p>
                                                    <p>Deseja alterar a imagem?</p>        
                                                <input type="file" name="tv_imagem" id="tv_imagem" data-max-file-size="1M" data-allowed-file-extensions="jpg png gif" data-default-file="upload/tv_imagem/<?php echo $result_row['tv_imagem']; ?>" data-show-remove="false"/>
                                                <input type="hidden" name="old_image" value="<?php echo $result_row['tv_imagem'];?>">
                                            </div>
					</div>-->
				
				
				<!--	<div class="input-field col s12">
                                                <label for="radio_bg">Imagem de Fundo do App Rádio - Tamanho: 1136x640px</label>
                                                    <br><p><img width="240" heigth="300" src="upload/radio_bg/<?php echo $result_row['radio_bg']; ?> "></p>
                                                    <p>Deseja alterar a imagem?</p>
                                                <input type="file" name="radio_bg" id="radio_bg" data-max-file-size="1M" data-allowed-file-extensions="jpg png gif" data-default-file="upload/radio_bg/<?php echo $result_row['radio_bg']; ?>" data-show-remove="false"/>
                                                <input type="hidden" name="old_image1" value="<?php echo $result_row['radio_bg'];?>">
                                                
                                                <br><br><br>
                                            </div>
				</div>


                                  <div class="col s12 m12 l6">
                                      <label for="privacy_policy">Politica</label>
                                                <br>
                                                <textarea name="privacy_policy" id="privacy_policy" class="form-control" cols="60" rows="10"><?php echo $result_row['privacy_policy'];?></textarea>
                                                    <script>                             
                                                        CKEDITOR.replace( 'privacy_policy' );
                                                    </script>
                                  </div> -->

                                  <div class="input-field col s12">
                                      <button type="submit" name="submit" class="btn cyan waves-effect waves-light right"><i class="mdi-content-save left"></i> Salvar</button>
                                  </div>

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

                  </div>

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

</section>
