<?php
session_start();


if($_SESSION['logInSucces'] == NULL){
	?><script type="text/javascript">window.location.href="index.php"</script><?
}
require('../config.php');
require('../conexao.php');
$action = $_GET['action'];

	switch ($action) {
	    case 'tvedit':
		    $titulo = $_POST['titulo'];
		    $idVid   = $_POST['idVid'];
		    $sinopse = $_POST['sinopse'];
		    $link    = $_POST['link1'];
		    $footer  = $_POST['footer'];
		    $bg      = $_POST['bg'];
		    
		    $query = $con->prepare("UPDATE `tv` SET `titulo`=:titulo,`logo`=:footer,`img`=:bg,`descricao`=:sinopse,`link`=:link WHERE id= :id");
				
				$query->bindValue(':id', $idVid);
				$query->bindValue(':titulo', $titulo);
				$query->bindValue(':sinopse', $sinopse);
				$query->bindValue(':link', $link);
				$query->bindValue(':bg', $bg);
				$query->bindValue(':footer', $footer);
				if($query->execute()){
					echo "S1";
				}else{
					echo "S1";
				}
		    break;
		    
		case 'tvadd':
		    $titulo = $_POST['titulo'];
		    $sinopse = $_POST['sinopse'];
		    $link    = $_POST['link1'];
		    $footer  = $_POST['footer'];
		    $bg      = $_POST['bg'];
		    
		    try {
					$query 		= $con->prepare("SELECT * FROM `tv` WHERE titulo= :titulo");
					$query->bindValue(':titulo', $titulo);
					$query->execute();
					if($query->rowCount() == 0){
						$exec = $con->prepare("INSERT INTO `tv`(`titulo`, `logo`, `img`, `descricao`, `link`) VALUES (:titulo, :footer, :bg, :sinopse, :link)");
        				$exec->bindValue(':titulo', $titulo);
        				$exec->bindValue(':sinopse', $sinopse);
        				$exec->bindValue(':link', $link);
        				$exec->bindValue(':bg', $bg);
        				$exec->bindValue(':footer', $footer);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}
		    break;
		    case 'tvexl':

			//Inicio code
			$id 	= $_POST['idMB'];

			$query			= $con->prepare("DELETE FROM `tv` WHERE id= :id");
			$query->bindValue(':id', $id);
			if($query->execute()){
				echo "S1";
			}else{
				echo "E1";
			}

			//Fim Code
			break;
	    case 'addPlan':
	        
		    $titulo = $_POST['titulo'];
		    $valor  = $_POST['valor'];
		    $tempo  = $_POST['tempo'];
		    $tela   = $_POST['tela'];
		    
		    $query = $con->prepare("SELECT * FROM `plans-md` WHERE titulo=:titulo");
		    $query->bindValue(':titulo', $titulo);
		    $query->execute();
		    if($query->rowCount() == 0){
		           $qr = $con->prepare("INSERT INTO `plans-md`(`titulo`, `tempo`, `telas`, `valor`) VALUES (:titulo, :tempo, :tela, :valor)");
		           $qr->bindValue(':titulo', $titulo);
		           $qr->bindValue(':tempo', $tempo);
		           $qr->bindValue(':tela', $tela);
		           $qr->bindValue(':valor', $valor);
		           if($qr->execute()){
		               echo "S1";
		           }else{
		               echo "E1";
		           }
		           
		    }else{
		        echo "E1";
		    }
		    
	        break;
		case 'addUser':
			
			//Code 
				$nome  		= $_POST['nome'];
				$email 		= $_POST['email'];
				$pass		= md5($_POST['pass']);
				
				try {
					$query 		= $con->prepare("SELECT * FROM `login_admin` WHERE email= :email");
					$query->bindValue(':email', $email);
					$query->execute();
					if($query->rowCount() == 0){
						$exec = $con->prepare("INSERT INTO `login_admin`(`email`, `pass`, `user`) VALUES (:email, :pass, :nome)");
						$exec->bindValue(':email', $email);
						$exec->bindValue(':pass', $pass);
						$exec->bindValue(':nome', $nome);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}

			//Fim Code


			break;
			case 'addUsers':
			
			//Code 
				$nome  		= $_POST['nome'];
				$email 		= $_POST['email'];
				$pass		= md5($_POST['pass']);
				$plano 		= $_POST['plan'];
				$data 		= $_POST['data'];
				
				
				
				try {
					$query 		= $con->prepare("SELECT * FROM `user` WHERE email= :email");
					$query->bindValue(':email', $email);
					$query->execute();
					if($query->rowCount() == 0){
					    if($plano != 0){
					    $pl 		= $con->prepare("SELECT * FROM `plans-md` WHERE id= :pl");
					    $pl->bindValue(':pl', $plano);
					    $pl->execute();
					    $plan = $pl->fetch(PDO::FETCH_ASSOC);
					    $plano = $plan['telas'];
					    }else{
					        $plano = 1;
					    }
					    
						$exec = $con->prepare("INSERT INTO `user`(`username`, `email`, `password`, `dateCreate`,`validade`, `datevalid`, `maxtela`) VALUES (:nome, :email, :pass, :datehj, :valid, :datevalid, :max)");
						$exec->bindValue(':email', $email);
						$exec->bindValue(':pass', $pass);
						$exec->bindValue(':nome', $nome);
						$exec->bindValue(':valid', 1);
						$exec->bindValue(':datevalid', $data);
						$exec->bindValue(':datehj', date('Ymd'));
						$exec->bindValue(':max', $plano);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}

			//Fim Code


			break;
		case 'usersexl':

			//Inicio code
			$id 	= $_POST['idMB'];

			$query			= $con->prepare("DELETE FROM `user` WHERE id= :id");
			$query->bindValue(':id', $id);
			if($query->execute()){
				echo "S1";
			}else{
				echo "E1";
			}

			//Fim Code
			break;
		case 'userexl':

			//Inicio code
			$id 	= $_POST['idMB'];

			$query			= $con->prepare("DELETE FROM `login_admin` WHERE id= :id");
			$query->bindValue(':id', $id);
			if($query->execute()){
				echo "S1";
			}else{
				echo "E1";
			}

			//Fim Code
			break;

		case 'editUser':

			//Inicio Codigo
				$nome  		= $_POST['nome'];
				$email 		= $_POST['email'];
				
				
				try {
					$query 		= $con->prepare("SELECT * FROM `login_admin` WHERE email= :email");
					$query->bindValue(':email', $email);
					$query->execute();

					if($query->rowCount() == 1){
						$fetch = $query->fetch(PDO::FETCH_ASSOC);
						if($_POST['pass'] != NULL){
							$pass = md5($_POST['pass']);
						}else{
							$pass = $fetch['pass'];
						}
						$exec = $con->prepare("UPDATE `login_admin` SET `email`=:email,`pass`=:pass,`user`=:nome WHERE email = :email");
						$exec->bindValue(':email', $email);
						$exec->bindValue(':pass', $pass);
						$exec->bindValue(':nome', $nome);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}

			//Fim Codigo

			break;
			
			

			case 'editUsers':

			//Inicio Codigo
				$nome  		= $_POST['nome'];
				$email 		= $_POST['email'];
                $data       = $_POST['data'];
                
				
				
				try {
					$query 		= $con->prepare("SELECT * FROM `user` WHERE email= :email");
					$query->bindValue(':email', $email);
					$query->execute();

					if($query->rowCount() == 1){
						$fetch = $query->fetch(PDO::FETCH_ASSOC);
						if($_POST['pass'] != NULL){
							$pass = md5($_POST['pass']);
						}else{
							$pass = $fetch['password'];
						}
						$exec = $con->prepare("UPDATE `user` SET `email`=:email,`password`=:pass,`username`=:nome, `datevalid`=:datevalid WHERE email = :email");
						$exec->bindValue(':email', $email);
						$exec->bindValue(':pass', $pass);
						$exec->bindValue(':nome', $nome);
						$exec->bindValue(':datevalid', $data);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}

			//Fim Codigo

			break;
			case 'aproved':

			//Inicio Codigo
				$id  		= $_POST['idMB'];

				
				
				try {
					$query 		= $con->prepare("SELECT * FROM `user` WHERE id= :email");
					$query->bindValue(':email', $id);
					$query->execute();

					if($query->rowCount() == 1){
						$exec = $con->prepare("UPDATE `user` SET `validade`=:valid WHERE id=:id");
						$exec->bindValue(':valid', 1);
						$exec->bindValue(':id', $id);
						
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}

					}else{
						echo "E1";
					}
				} catch (Exception $e) {
					print_r($e);
				}

			//Fim Codigo

			break;
			case 'addVid':
				//Inicio Codigo
				 	//Variaveis ;
					$vidTipo			= $_POST['vidTipo'];
					$child				= $_POST['child'];
					$idtmdb				= $_POST['idtmdb'];
					$vidLink			= $_POST['vidLink'];
					$vidLink2 			= $_POST['vidLink2'];
					$servidor			= $_POST['gdrive'];
					$servidor2			= $_POST['gdrive1'];
					$pg 				= $_POST['pg'];
					$trailer 			= $_POST['trailer'];

					$url = "https://api.themoviedb.org/3/".$vidTipo."/".$idtmdb."?language=pt-BR&api_key="."b5ad6a9f75ea4e476b5f08b524ddf83d";
					$ch  =  curl_init($url);
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
					$exec = curl_exec($ch);
					curl_close($ch);

					$json = json_decode($exec);
					
					//Dados Filme Or Serie
					$sinopse = $json->overview;
					if($sinopse == null){
						$sinopse = " - ";
					}
					if($vidTipo == "movie"){
					$lancamento = $json->release_date;
					$lancamento = date('Y',strtotime($lancamento));
					$duration = $json->runtime." Min";
					$title 		= $json->title;

					}else{
						$duration = $json->episode_run_time[0]." Min";
						$lancamento = $json->first_air_date;
						$lancamento = date('Y',strtotime($lancamento));
						$title 	= $json->name;
					}

					if($lancamento == null){
						$lancamento = ' - ';
					}
					
					
					if($duration == null){
						$duration = ' - ';
					}
					$produtora = $json->production_companies[0]->name;
					
					if($produtora == NULL){
						$produtora = ' - ';
					}
					
					$rating 	= $json->vote_average;
					$voteCount  = $json->vote_count;

					$poster = "http://image.tmdb.org/t/p/w300_and_h450_bestv2".$json->poster_path;
					$bg     = "http://image.tmdb.org/t/p/w1280".$json->backdrop_path;

					$genero  	= $json->genres;
					$gen 		= array();
					foreach ($genero as $generos) {
						$gen[] = $generos->name;
						$sr = $con->prepare("SELECT * FROM `category` WHERE categoria= :categoria");
						$sr->bindValue(':categoria', $generos->name);
						$sr->execute();
						if($sr->rowCount() == 0){
							$gn = $con->prepare("INSERT INTO `category`(`categoria`) VALUES (:categoria)");
							$gn->bindValue(':categoria',$generos->name);
							$gn->execute();
						}

					}
					$gens = implode(", ", $gen);
					$query = $con->prepare("SELECT * FROM `filmes` WHERE tmdb=:tmdb");
					$query->bindValue(':tmdb',$idtmdb);
					$query->execute();

					if($vidTipo == 'movie'){
						$tipoFilm = '1';
					}else{
						$tipoFilm = '2';
					}
					if($query->rowCount() == 0){
					
							try {
							$exec = $con->prepare("INSERT INTO `filmes`(`tipo`, `titulo`, `sinopse`, `genero`, `duration`, `diretor`, `pg`, `year`, `rating`, `bg`, `footer`, `trailer`, `serv1`, `serv2`, `link1`, `link2`, `child`, `tmdb`) VALUES (:tipo, :titulo, :sinopse, :genero, :duration, :diretor, :pg, :year, :rating, :bg, :footer, :trailer, :serv1, :serv2, :link1, :link2, :child, :tmdb)");
							$exec->bindValue(':tipo', $tipoFilm);
							$exec->bindValue(':titulo', $title);
							$exec->bindValue(':sinopse', $sinopse);
							$exec->bindValue(':genero', $gens);
							$exec->bindValue(':duration', $duration);
							$exec->bindValue(':diretor', $produtora);
							$exec->bindValue(':pg', $pg);
							$exec->bindValue(':year', $lancamento);
							$exec->bindValue(':rating', $rating);
							$exec->bindValue(':bg', $bg);
							$exec->bindValue(':footer', $poster);
							$exec->bindValue(':trailer', $trailer);
							$exec->bindValue(':serv1', $servidor);
							$exec->bindValue(':serv2', $servidor2);
							$exec->bindValue(':link1', $vidLink);
							$exec->bindValue(':link2', $vidLink2);
							$exec->bindValue(':child', $child);
							$exec->bindValue(':tmdb', $idtmdb);
							if($exec->execute()){
								echo "S1";

							}else{
								echo "E1";
							}
							
						} catch (Exception $e) {
							print_r($e);
						}

					}else{
						echo "E1";
					}


				//Fim Codigo
			break;

			case 'exlVid':

				//Code
					$id  = $_POST['idVid'];

					$query = $con->prepare("SELECT * FROM `filmes` WHERE id= :id");
					$query->bindValue(':id', $id);
					$query->execute();
					if($query->rowCount() == 1){
						$exec = $con->prepare("DELETE FROM `filmes` WHERE id= :id");
						$exec->bindValue(':id', $id);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}
					}else{
						echo "E1";
					}
				//code
			break;
			case 'removeEp':

				//Code
					$id  = $_POST['idEp'];

					$query = $con->prepare("SELECT * FROM `episode` WHERE id= :id");
					$query->bindValue(':id', $id);
					$query->execute();
					if($query->rowCount() == 1){
						$exec = $con->prepare("DELETE FROM `episode` WHERE id= :id");
						$exec->bindValue(':id', $id);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}
					}else{
						echo "E1";
					}
				//code
			break;
			case 'planexl':

				//Code
					$id  = $_POST['idMB'];

					$query = $con->prepare("SELECT * FROM `plans-md` WHERE id= :id");
					$query->bindValue(':id', $id);
					$query->execute();
					if($query->rowCount() == 1){
						$exec = $con->prepare("DELETE FROM `plans-md` WHERE id= :id");
						$exec->bindValue(':id', $id);
						if($exec->execute()){
							echo "S1";
						}else{
							echo "E1";
						}
					}else{
						echo "E1";
					}
				//code
			break;
			case 'vidEdit':
				//Code
				$idVid 		= $_POST['idVid'];
				$titulo		= $_POST['titulo'];
				$sinopse	= $_POST['sinopse'];
				$lanc 		= $_POST['lanc'];
				$bg 		= $_POST['bg'];
				$footer 	= $_POST['footer'];
				$runtime 	= $_POST['runtime'];
				$link1 = "";
				$link2 = "";
				if(isset($_POST['link1'])){  $link1		= $_POST['link1']; }

				if(isset($_POST['link2'])){  $link2 		= $_POST['link2'];}
				
				
				if($link1 == NULL){
					$link1 = '-';
				}if($link2 == NULL){
					$link2 = '-';
				}

				$query = $con->prepare("UPDATE `filmes` SET `titulo`= :titulo ,`sinopse`= :sinopse ,`duration`= :runtime ,`year`= :lanc, `bg`= :bg,`footer`= :footer, `link1`= :link1,`link2`= :link2 WHERE id= :id ");
				
				$query->bindValue(':id', $idVid);
				$query->bindValue(':titulo', $titulo);
				$query->bindValue(':sinopse', $sinopse);
				$query->bindValue(':runtime', $runtime);
				$query->bindValue(':lanc', $lanc);
				$query->bindValue(':link1', $link1);
				$query->bindValue(':bg', $bg);
				$query->bindValue(':footer', $footer);
				$query->bindValue(':link2', $link2);
				if($query->execute()){
					echo "S1";
				}else{
					echo "S1";
				}
				//Code
			break;
			case 'editEp':
				//Code
				$idVid 		= $_POST['idvid'];
				$titulo		= $_POST['titulo'];
				$sinopse	= $_POST['sinopse'];
				$bg 		= $_POST['bg'];
				$link1 = "";
				$link2 = "";
				if(isset($_POST['link1'])){  $link1		= $_POST['link1']; }

				if(isset($_POST['link2'])){  $link2 		= $_POST['link2'];}
				
				
				if($link1 == NULL){
					$link1 = '-';
				}if($link2 == NULL){
					$link2 = '-';
				}

				$query = $con->prepare("UPDATE `episode` SET `titulo`= :titulo,`sinopse`= :sinopse, `bg`= :bg ,`link1`= :link1 ,`link2`= :link2 WHERE id = :id");
				
				$query->bindValue(':id', $idVid);
				$query->bindValue(':titulo', $titulo);
				$query->bindValue(':sinopse', $sinopse);
				$query->bindValue(':bg', $bg);
				$query->bindValue(':link1', $link1);
				$query->bindValue(':link2', $link2);
				if($query->execute()){
					echo "S1";
				}else{
					echo "S1";
				}
				//Code
			break;
			case 'addEp':
				//Code
					$idSerie = $_POST['idSerie'];
					$temp	 = $_POST['temp'];
					$ep 	 = $_POST['ep'];
					$link 	 = $_POST['link'];
					$link2 	 = $_POST['vidLinkLEG'];
					$serv1   = $_POST['gdrive'];
					$serv2 	 = $_POST['gdrive2'];



					$query = $con->prepare("SELECT * FROM `episode` WHERE idf= :idSerie && temp = :temp && ep= :ep");
					$query->bindValue(':idSerie', $idSerie);
					$query->bindValue(':temp', $temp);
					$query->bindValue(':ep', $ep);
					$query->execute();
					

					$movie = $con->prepare("SELECT * FROM `episode` WHERE tmdb= :tmdb");
					$movie->bindValue(':tmdb', $idSerie);
					$movie->execute();
					$fetch = $movie->fetch(PDO::FETCH_ASSOC);
					if($query->rowCount() == 0){

						$url = "https://api.themoviedb.org/3/tv/".$idSerie."/season/".$temp."/episode/".$ep."?language=pt-BR&api_key="."b5ad6a9f75ea4e476b5f08b524ddf83d";
						$ch  =  curl_init($url);
						curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
						$exec = curl_exec($ch);
						curl_close($ch);

						$json = json_decode($exec);


						$titulo = $ep.". ".$json->name;
						if($titulo == null){
							$titulo = $ep.". "."Episodio ".$ep;
						}
						$sinopse = $json->overview;
						if($sinopse == null){
							$sinopse = " - ";
						}
						$bg = "https://image.tmdb.org/t/p/w1280".$json->still_path;
						if($bg == null){
							$bg = $fetch['bg'];
						}
						$footer = $bg;
						$duration = $fetch['duration'];
						if($duration == null){
							$duration = " - ";
						}

						$tempR = $con->prepare("SELECT * FROM `temporada` WHERE idf= :idSerie && temp= :temp");
						$tempR->bindValue(':idSerie', $idSerie);
						$tempR->bindValue(':temp', $temp);
						$tempR->execute();
						if($tempR->rowCount() == 0){
							$tp = $con->prepare("INSERT INTO `temporada`(`idf`, `temp`) VALUES (:idf, :temp)");
							$tp->bindValue(':idf', $idSerie);
							$tp->bindValue(':temp', $temp);
							$tp->execute();

						}

						$episode = $con->prepare("INSERT INTO `episode`(`titulo`, `sinopse`, `bg`, `footer`, `duration`, `ep`, `temp`, `serv1`, `serv2`, `link1`, `link2`, `idf`) VALUES (:titulo, :sinopse, :bg, :footer, :duration, :ep, :temp, :serv1, :serv2, :link1, :link2, :idf)");
						$episode->bindValue(':titulo', $titulo);
						$episode->bindValue(':sinopse', $sinopse);
						$episode->bindValue(':bg', $bg);
						$episode->bindValue(':footer', $footer);
						$episode->bindValue(':duration', $duration);
						$episode->bindValue(':ep', $ep);
						$episode->bindValue(':temp', $temp);
						$episode->bindValue(':serv1', $serv1);
						$episode->bindValue(':serv2', $serv2);
						$episode->bindValue(':link1', $link);
						$episode->bindValue(':link2', $link2);
						$episode->bindValue(':idf', $idSerie);
						if($episode->execute() or die(print_r($episode->errorInfo(), true))){
							echo "S1";
						}else{
							echo "E1";
						}



					}else{
						echo "E1";
					}


				//Code
			break;

		default:
			
			break;
	}
?>