<?php
header("Access-Control-Allow-Origin: *");
include_once 'includes/variables.php';

DEFINE('DB_HOST', $host);
DEFINE('DB_USER', $user);
DEFINE('DB_PASSWORD', $pass);
DEFINE('DB_NAME', $database);

$mysqli = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to MySQL');
@mysql_select_db(DB_NAME) or die('Could not select the database');

//$response = array("error" => FALSE);
//mysql_query("SET NAMES 'utf8'");

/**
 * Get user by Mobile and password
 */
//CONCAT('#',LPAD(CONV(ROUND(RAND()*16777215),10,16),6,0)) AS color,
try
{
    $json = file_get_contents('php://input');
    //converting json object to php associative array
    $loginData = json_decode($json, true);
if (isset($loginData['categoryId'])) {
    $pageno = $loginData['pageNo'];
    $limit = 5;
    $endlimit = $pageno * $limit;
    $startlimit = $endlimit - ($limit-1);

    $querycnt = "SELECT COUNT(slno) as RCOUNT
	FROM video
	WHERE video_category_id = '".$loginData['categoryId']."' ";

    $resouter = mysql_query($querycnt);
    $data = mysql_fetch_assoc($resouter);

    $query = "SELECT slno,  video_title, video_name, video_thumbnail, likes_count, r_timestamp
	FROM video
	WHERE video_category_id = '".$loginData['categoryId']."' 
    AND slno >= '".$startlimit."' AND slno <='".$endlimit."'
    ORDER BY slno ASC";

    $resouter = mysql_query($query);

    //$response = array();
    $total_records = mysql_num_rows($resouter);
    $i = 0;
    if ($total_records >= 1) {
            $response["status"] = "succcess";
            if($endlimit < $data['RCOUNT'] )
                $response["is_last"] = false;
            else
            $response["is_last"] = true;

        while ($results = mysql_fetch_array($resouter, MYSQL_ASSOC)) {
           
            $response['video'][$i]['Id'] = $results['slno'];
            $response['video'][$i]['videoTitle'] = $results['video_title'];
            $response['video'][$i]['videoName'] = "http://timepass.net.in/videos/" . $results['video_name'];
            $response['video'][$i]['videoThumbnail'] = "http://timepass.net.in/videos_thumbnail/" . $results['video_thumbnail'];
            $response['video'][$i]['videoLikes'] = $results['likes_count'];
            $response['video'][$i]['videoDateTime'] = $results['r_timestamp'];
            $i++;
        }
        echo json_encode($response);
    } else {
        $response[] = array();
        echo json_encode($response, JSON_FORCE_OBJECT);
    }
}
} catch (Exception $e) {
    $response["status"] = "failed";
    $response["message"] = $e->getMessage();
    echo json_encode($response);
}
// echo $val= str_replace('\\/', '/', json_encode($set));
