<?php
$MXTOTREC = 0;
$MXPGINFO["PK"] = "pageID";
$MXPGINFO["TBL"] = "page";



function chkUserID(){
	global $DB;
	$sql = 'SELECT userID FROM '.$DB->pre.'agent WHERE userCode="'.strtolower($_REQUEST['userCode']).'"';
	$data = $DB->dbRow($sql);
	$str = array('str'=>'OK');
	if(count($data) > 0){
		$str = array('str'=>'ERR');
	}
	return json_encode($str);
	
}
function saveUser(){
	//global $DB;
	$DB = new mxDb('localhost','esssifhw_lottery','esssifhw_lottery','nitin@123');
	date_default_timezone_set('Asia/Kolkata');

	$_REQUEST['dateAdded'] = date('Y-m-d H:i:s');	
	print_r($_REQUEST);	
	$DB->table = $DB->pre."users";
	$DB->data  = $_REQUEST;
	$str = array('str'=>'ERR');
	echo $sql = 'INSERT INTO '.$DB->pre.'users SET userCode="'.$_REQUEST['userCode'].'",name="'.$_REQUEST['name'].'",`password`="'.$_REQUEST['password'].'",mobile="'.$_REQUEST['mobile'].'",userType="'.$_REQUEST['userType'].'",underHead="'.$_REQUEST['underHead'].'",winrate="'.$_REQUEST['winrate'].'",commission="'.$_REQUEST['commission'].'",bonus="'.$_REQUEST['bonus'].'",limit="'.$_REQUEST['limit'].'",remark="'.$_REQUEST['remark'].'",dateAdded="'.date('Y-m-d H:i:s').'"';
	
	if($DB->dbQuery($sql)){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function addBalance(){
	global $DB;
	date_default_timezone_set('Asia/Kolkata');
	
	$DB->table = $DB->pre."wallet";	
	$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'tranBal'=>$_REQUEST['tranBal'],'dateAdded'=>date('Y-m-d H:i:s'));
	$DB->dbInsert();
	
	$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE balanceID=(SELECT max(balanceID) FROM '.$DB->pre.'balance WHERE userCode="'.$_REQUEST['userCodeBal'].'")';
	$B = $DB->dbRow($sqlB);
	$bal = (intval($B['balance'])+$_REQUEST['tranBal']);
	
	$DB->table = $DB->pre."balance";
	$DB->data  = array('userCode'=>$_REQUEST['userCodeBal'],'balance'=>$bal);
	$str = array('str'=>'ERR');
	if($DB->dbInsert()){
		$str = array('str'=>'OK');
	}
	return json_encode($str);
}
function getTime(){
	global $DB;
	date_default_timezone_set('Asia/Kolkata');

$date = time();

$lotMin = date("i", $date);
$lotHour = date("H", $date);
$lotSec = date("s", $date);
$ampm = date("A", $date);

if ($lotMin >= 0 && $lotMin < 15) {
    $lotMin = 15;
} else if ($lotMin >= 15 && $lotMin < 30) {
    $lotMin = 30;
} else if ($lotMin >= 30 && $lotMin < 45) {
    $lotMin = 45;
} else if ($lotMin >= 45 && $lotMin < 60) {
    $lotMin = 0;
    $lotHour++;
}

if ($lotHour < 10) {
    $lotHour = "0" . $lotHour;
}
if ($lotMin < 10) {
    $lotMin = "0" . $lotMin;
}
// if ($lotSec < 10) {
//     $lotSec = "0" . $lotSec;
// }

$time_lot = date("Y-m-d", $date) . " $lotHour:$lotMin:$lotSec";
$time_now = date("Y-m-d H:i:s", $date);

// $h = date("h", $date);
$m = $lotMin;
// $s = date("s", $date);

if ($m > 60) {
    $m = $m % 60;
    $h++;
    if ($m < 10) {
        "0" . $m;
    }
}

if ($time_lot == $time_now) {
    echo "Expired";
}

$sec_remaining =  (60 - date("s", time()));
if ($sec_remaining < 10) {
    $sec_remaining = "0" . $sec_remaining;
}

$min_remaining =  ((strtotime($time_lot) - strtotime($time_now)) / 60) - 1;
if ($min_remaining < 10) {
    $min_remaining = "0" . $min_remaining;
}
$time_remaining = $min_remaining . ":" . $sec_remaining;

//echo "Draw Time: " . $time_lot;
//echo "<br>Curr.. Time: " . $time_now;
echo  $time_remaining;

}
switch($_REQUEST['xAction']){
	case 'chkUser':
					include("../../connectdb.inc.php");
					echo chkUserID();
					break;
	case 'save':
					include("../../connectdb.inc.php");
					echo saveUser();
					break;
	case 'addBal':
					include("../../connectdb.inc.php");
					echo addBalance();
					break;
	case 'getTime':
					include("../../connectdb.inc.php");
					echo getTime();
					break;
	
}

?>