<?php

	include('public/functions.php');

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

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

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

            $file_path = "https://painelapp.com.br/maximarecife/upload/notification/".$image;
              
            $content = array(
                             "en" => $_POST['descricao']
                             );

            $fields = array(
                            'app_id' => 'c66c5509-009c-49a6-b914-8dfed1dfb237',
                            'included_segments' => array('All'),                                            
                            'data' => array("foo" => "bar","place_id"=>"1"),
                            'headings'=> array("en" => $_POST['titulo']),
                            'contents' => $content,
                            'big_picture' =>$file_path                    
                            );

            $fields = json_encode($fields);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                                       'Authorization: Basic NTY1ODIyYWItNzIyNS00ZTdkLWJjYWMtOTIyMzEyYTJhYmEx'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            $response = curl_exec($ch);
            curl_close($ch);
            $_SESSION['msg'] = "Notificação enviada com êxito com imagem!";
            
        }
        else
        {

     
            $content = array(
                             "en" => $_POST['descricao']
                              );

            $fields = array(
                            'app_id' => 'c66c5509-009c-49a6-b914-8dfed1dfb237',
                            'included_segments' => array('All'),                                      
                            'data' => array("foo" => "bar","place_id"=>"1"),
                            'headings'=> array("en" => $_POST['titulo']),
                            'contents' => $content
                            );

            $fields = json_encode($fields);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                                       'Authorization: Basic NTY1ODIyYWItNzIyNS00ZTdkLWJjYWMtOTIyMzEyYTJhYmEx'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

            $response = curl_exec($ch);
            
            
            
            curl_close($ch);

            $_SESSION['msg'] = "Notificação enviada com êxito sem imagem!";

        }

	    header( "Location:notification.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">Notificação app</h5>
                  <ol class="breadcrumb">
                    <li><a href="dashboard.php">Dashboard</a>
                    </li>
                    <li><a class="active">Nova notificação</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 da notificação :</label>
                                          <br>
                                          <input type="text" name="titulo" id="titulo" placeholder="Título da notificação" required />
                                      </div>
                                  </div>

                                  <div class="col s12 m12 l12">
                                    <div id="input-select " class="input-field col s12 l6">
                                      <label>Informativo</label>
                                      <br>
                                      <textarea name="descricao" class="form-control" style="min-height: 200px; max-width: 550px;"></textarea>
                                    </div>
                                  </div>

                                  <div class="col s12 m12 l6">
                                    <div class="input-field col s12 l12">
                                      <label >Imagem</label>
                                      <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">Enviar notificação</button>
                                  </div>

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

                  </div>

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

</section>
