<?php 
require_once 'conf/link.php';
//sleep(1);
if((isset($_POST['query'])))// or (isset($_GET['query'])))
{
	$query = (isset($_POST['query'])) ? $_POST['query'] : '';//$_GET['query'];
	$player_name = (isset($_POST['player_name'])) ? $_POST['player_name'] : '';//$_GET['player_name'];
	//$data_player($_GET['player_name'], $conexion);
	//echo "<br>".$_POST['query'];
	//echo "<br>".$_POST['player_name'];
	$data_list =(isset($_POST['data_list'])) ? $_POST['data_list'] : '';
	
	//echo "<br>vars query : ".$query." player_name : ".$player_name;

	switch ($query)
	{
		case 'buscar':
		//echo 'case';
			buscar($player_name, $conexion);
			break;
			
		case 'data_player':
			data_player($player_name, $conexion);
			break;
		
		case 'data_list':
			data_list($data_list, $conexion);
			break;
			
	}
	//if($_POST['query'] == 'buscar') ? buscar($_POST['player_name']);
}
else
{
	echo 'No post';
}

function buscar($player_name, $conexion)
{
	//echo 'function';
    $sql = 'SELECT * FROM players WHERE player_name = "'.$player_name.'"';
	//$result = $conexion->query('SELECT * FROM players WHERE player_name = "'.$player_name.'"');
	//echo $sql."<br>";
	
    if ($result = mysqli_query($conexion, $sql))
	{
		if(mysqli_num_rows($result))
		{
			//echo mysqli_num_rows($result);
			echo 'true';
			//echo '<div class="alert alert-danger"><strong>Oh no!</strong> Nombre de usuario no disponible.</div>';
		}
		else
		{
			//echo mysqli_num_rows($result);
			echo 'false';
		}
    }
	else
	{
        echo 'Error en consulta SQL';
		//echo '<div class="alert alert-success"><strong>Enhorabuena!</strong> Usuario disponible.</div>';
    }
}

function data_player($player_name, $conexion)
{
    $result = $conexion->query('SELECT * FROM players WHERE player_name = "'.$player_name.'"'
    );
	
    if ($row = mysqli_fetch_array($result))
	{
        //echo print_r($row);
		header('Content-Type: application/json');
		echo json_encode($row, JSON_FORCE_OBJECT);
    }
	else
	{
		header('Content-Type: application/json');
		$vacio = '{"0":"","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":"","id":"","player_name":"","lst_assoc":"","res_x":"","res_y":"","x0":"","y0":"","hour_start":"","hour_end":"","datecreate":"","user":""}';
        echo $vacio;
	}
}

function data_list($data_list, $conexion)
{
    $sql_lista = 'SELECT videos_id FROM listas WHERE nombre_lista = "'.$data_list.'"';
	

	//echo "<br>".$sql_lista."<br>";
			$result_lista = mysqli_query($conexion, $sql_lista);
			while($row_lista = mysqli_fetch_array($result_lista))
			{
				$str_aux = $row_lista['videos_id'];
				//$media = $row_lista['videos_id'];
			}
	//$media = $str_aux;

	//echo "<br>str".$str_aux;
	//$playlist_id = explode('|', $str_aux);
	//echo "<br>";
	//print_r($playlist_id);
	/*
	foreach($playlist_id as $index=>$id)
	{
		$sql = 'SELECT * FROM almacen WHERE id = "'.$id.'"';
		//echo "<br>".$sql."<br>";
			$result = mysqli_query($conexion, $sql);
			while($row = mysqli_fetch_assoc($result))
			{
				//$media_files['filename'] = $row['filename'];
				//$media_files['thumbnail'] = $row['thumbnail'];
				
				$media[] = array('id' => $row['id'], 'filename' => $row['filename'], 'thumbnail' => $row['thumbnail']);
				
			}
	}*/
	//echo "<br>-";
	//echo json_encode($media);
	//echo "<br>";
	//print_r($media_thumbnail);
	
//////////////////////////////////////////
	//echo $sql;
	//$result = mysqli_query($conexion, $sql);
    //if ($row = mysqli_fetch_array($result))
	//{
      // echo print_r($row);
		
		
		
		
		header('Content-Type: application/json');
		echo $str_aux;
		
		//echo json_encode($media_files, JSON_FORCE_OBJECT);
		//echo json_encode($media, JSON_FORCE_OBJECT);
		/*
    }
	else
	{
		header('Content-Type: application/json');
		$vacio = '{"0":"","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":"","id":"","player_name":"","lst_assoc":"","res_x":"","res_y":"","x0":"","y0":"","hour_start":"","hour_end":"","datecreate":"","user":""}';
        echo $vacio;
	}*/
}
