<?php

	include('public/functions.php');

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

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

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

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

  		$data = array(
              'radio_name' => $_POST['radio_name'],
              'radio_image' => $radio_image,
			  'radio_bg' => $radio_bg,
              'radio_url' => $_POST['radio_url'],
              'privacy_policy' => $_POST['privacy_policy']
  		);

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

  		if ($result_id > 0) {
  			$_SESSION['msg'] = "";
              header( "Location:dashboard.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">Dashboard</a>
                    </li>
                    <li><a class="active">Dados da Rádio</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="radio_url">Nome da Rádio :</label>
                                          <input type="text" name="radio_name" id="radio_name" placeholder="" value="<?php echo $result_row['radio_name'];?>" required />
                                      </div>

                                      <div class="input-field col s12">
                                          <label for="radio_url">Url do Streaming :</label>
                                          <input type="text" name="radio_url" id="radio_url" placeholder="" value="<?php echo $result_row['radio_url'];?>" required />
										  
										  <p>
			                        <div class="col-sm12 container" id="kdkhost">
			                            <!-- implementação de player html5 kdkhost -->
			                        <audio controls style="width:200px; height:25px; margin-left:-15px;">
			                            <source src="<?php echo $result_row['radio_url'];?>" type="audio/mpeg" style="color: rgba(239,222,0,1.00);">
			                        </audio>
			                            <!-- implementação de player html5 kdkhost -->
			                        </div>
			                    </p>
                                          
                                      </div>
									 
							
              

					<div class="col s12 m12 l6"><br><br>
                                            <div class="input-field col s12">
                                                <label for="radio_image"><center>Logo do APP - Tamanho: 512x512px</center></label>
                                                    <br><p><img width="100" heigth="100" src="upload/<?php echo $result_row['radio_image']; ?> "></p>
                                                    <p>Deseja alterar a imagem?</p>        
                                                <input type="file" name="radio_image" id="radio_image" data-max-file-size="1M" data-allowed-file-extensions="jpg png gif" data-default-file="upload/<?php echo $result_row['radio_image']; ?>" data-show-remove="false"/>
                                                <input type="hidden" name="old_image" value="<?php echo $result_row['radio_image'];?>">
                                            </div>
					</div>
				
				
					<div class="input-field col s12">
					                            <br><label for="radio_bg"><center>Imagem de Fundo do App Rádio - Tamanho: 640x918px</center></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>
