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


function innerAdminMenu(){



$str = '
	<li><a class="active" href="'.SITEURL.'/lottery-agent/?u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">Home </a></li>
                    <li><a href="'.SITEURL.'/lottery-agent/create-agent/?u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">Create User </a></li>
                    <li><a href="'.SITEURL.'/lottery-agent/agent-balance/?u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">Balance Transfer </a></li>
                    <li><a href="'.SITEURL.'/lottery-agent/agent-live/?u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">Live Customer </a></li>
';

return $str;

}


function chkUserID(){
	global $DB;
	
	return json_encode($str);
	
}
function saveUser(){
	
}
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');
	header('Access-Control-Allow-Origin: *');

$date = time();

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

$lotHour = $lotHour % 12;
if ($lotHour == 0) {
    $lotHour = 12;
}

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: " . date('h:i', strtotime($time_lot));
//echo "<br>Curr.. Time: " . $time_now;
echo $time_remaining;

}
function saveResult(){
	global $DB;
		date_default_timezone_set('Asia/Kolkata');
	
	$exp = explode('~',$_REQUEST['attr']);
	
	//print_r($exp);die;
	$sernum = ($exp[1]=='00000'?'0000':$exp[1]);
	$sql = 'SELECT lotID FROM '.$DB->pre.'result_tran WHERE sernum="'.$sernum.'" AND  lotnum="'.$exp[2].'" AND lotdate="'.$exp[3].'" AND lotTime="'.$exp[4].'" AND uID="'.$exp[5].'"';
	$D = $DB->dbRow($sql);
	$str = array('str'=>'ERR');
	if($D['lotID'] == ''){
		$sqlI = 'INSERT INTO '.$DB->pre.'result_tran SET cellID="'.$exp[0].'",sernum="'.$exp[1].'",lotnum="'.$exp[2].'",lotdate="'.$exp[3].'",lotTime="'.$exp[4].'",uID="'.$exp[5].'",dateAdded="'.date('Y-m-d H:i:s').'"';
		$DB->dbQuery($sqlI);
		$str = array('str'=>'OK');
	}else{
			$sqlD = 'DELETE FROM '.$DB->pre.'result_tran WHERE sernum="'.$sernum.'" AND  lotnum="'.$exp[2].'" AND lotdate="'.$exp[3].'" AND lotTime="'.$exp[4].'" AND uID="'.$exp[5].'"';
			$DB->dbQuery($sqlD);
			
			$sqlI = 'INSERT INTO '.$DB->pre.'result_tran SET cellID="'.$exp[0].'",sernum="'.$exp[1].'",lotnum="'.$exp[2].'",lotdate="'.$exp[3].'",lotTime="'.$exp[4].'",uID="'.$exp[5].'",dateAdded="'.date('Y-m-d H:i:s').'"';
			$DB->dbQuery($sqlI);
			$str = array('str'=>'OK');
	}
	
	return json_encode($str);
}
function saveAutoResult(){
	global $DB;
	
	$sql = 'SELECT id FROM '.$DB->pre.'users WHERE userType="2" AND underHead="'.$_REQUEST['adminID'].'"';
	$D = $DB->dbRows($sql);
	$agent = '';
	
	foreach($D as $D){
		$agent .= $D['id'].',';
	}
	$agent = substr($agent, 0, -1);


	
	$serArr = array('0000'=>array('00~0-99','10~100-199','20~200-299','30~300-399','40~400-499','50~500-599','60~600-699','70~700-799','80~800-899','90~900-999'),
					'1000'=>array('00~1000-1099','10~1100-1199','20~1200-1299','30~1300-1399','40~1400-1499','50~1500-1599','60~1600-1699','70~1700-1799','80~1800-1899','90~1900-1999'),
					'2000'=>array('00~2000-2099','10~2100-2199','20~2200-2299','30~2300-2399','40~2400-2499','50~2500-2599','60~2600-2699','70~2700-2799','80~2800-2899','90~2900-2999'),
					'3000'=>array('00~3000-3099','10~3100-3199','20~3200-3299','30~3300-3399','40~3400-3499','50~3500-3599','60~3600-3699','70~3700-3799','80~3800-3899','90~3900-3999'),
					'4000'=>array('00~4000-4099','10~4100-4199','20~4200-4299','30~4300-4399','40~4400-4499','50~4500-4599','60~4600-4699','70~4700-4799','80~4800-4899','90~4900-4999'),
					'5000'=>array('00~5000-5099','10~5100-5199','20~5200-5299','30~5300-5399','40~5400-5499','50~5500-5599','60~5600-5699','70~5700-5799','80~5800-5899','90~5900-5999'),
					'6000'=>array('00~6000-6099','10~6100-6199','20~6200-6299','30~6300-6399','40~6400-6499','50~6500-6599','60~6600-6699','70~6700-6799','80~6800-6899','90~6900-6999'),
					'7000'=>array('00~7000-7099','10~7100-7199','20~7200-7299','30~7300-7399','40~7400-7499','50~7500-7599','60~7600-7699','70~7700-7799','80~7800-7899','90~7900-7999'),
					'8000'=>array('00~8000-8099','10~8100-8199','20~8200-8299','30~8300-8399','40~8400-8499','50~8500-8599','60~8600-8699','70~8700-8799','80~8800-8899','90~8900-8999'),
					'9000'=>array('00~9000-9099','10~9100-9199','20~9200-9299','30~9300-9399','40~9400-9499','50~9500-9599','60~9600-9699','70~9700-9799','80~9800-9899','90~9900-9999')
					);

	foreach($serArr as $key=>$serArr){
		foreach($serArr as $S){
			$splt = explode('~',$S);
			$splt1 = explode('-',$splt[1]);
			$cell = array();
				$sqlR = 'SELECT id FROM '.$DB->pre.'users WHERE userType="3" AND underHead in('.$agent.')';
			$R = $DB->dbRows($sqlR);

			foreach($R as $R){
				if($R['id'] !=''){
					$sqlL = 'SELECT cellID FROM '.$DB->pre.'lottary WHERE uID="'.$R['id'].'" AND lotTime="'.$_REQUEST['lotTime'].'" AND lotdate="'.date('Y-m-d').'" AND sernum="'.$key.'" AND lotnum="'.$splt[0].'"';
					$L = $DB->dbRows($sqlL);
					foreach($L as $L){
						$cell[] = $L['cellID'];
					}
				}
				
			}
			
			
			$sql1 = 'SELECT lotID FROM '.$DB->pre.'result_tran WHERE sernum="'.$key.'" AND  lotnum="'.$splt[0].'" AND lotdate="'.date('Y-m-d').'" AND lotTime="'.$_REQUEST['lotTime'].'" AND uID="'.$_REQUEST['adminID'].'"';
			$D1 = $DB->dbRow($sql1);
			
			
			//print_r(array_count_values($cell));die;
			if($D1['lotID'] == ''){
					
				if(count(array_unique($cell)) < 100){
					$newCell = '';
					$flag = true;
					for($i=$splt1[0];$i<=$splt1[1];$i++){
						if($newCell == ''){
							$flag = array_search($i,$cell);
							if(!$flag){
								$newCell = $i;
							}
						}
					}
					$sqlI1 = 'INSERT INTO '.$DB->pre.'result_tran SET cellID="'.$newCell.'",sernum="'.$key.'",lotnum="'.$splt[0].'" , lotdate="'.date('Y-m-d').'",lotTime="'.$_REQUEST['lotTime'].'",uID="'.$_REQUEST['adminID'].'",dateAdded="'.date('Y-m-d h:i:s').'"';
					//echo "<br>";
					$DB->dbQuery($sqlI1);
				}else{
				//	echo "All Cell are fill";
					$sortCell = array_count_values($cell);
			
					$minCell = array_keys($sortCell,min($sortCell));
					
					$sqlI2 = 'INSERT INTO '.$DB->pre.'result_tran SET cellID="'.$minCell[0].'",sernum="'.$key.'",lotnum="'.$splt[0].'" , lotdate="'.date('Y-m-d').'",lotTime="'.$_REQUEST['lotTime'].'",uID="'.$_REQUEST['adminID'].'",dateAdded="'.date('Y-m-d h:i:s').'"';
					//echo "<br>";
					$DB->dbQuery($sqlI2);
					
				}
			}
		}
	}
	
	echo "OK";
	
}
function deleteLottery(){
	global $DB;
	$ID = substr($_REQUEST['ID'], 0, -1);
	$exp = explode(",",$ID);
	$str = array('str'=>'ERR');
	if(count($exp) > 0){
		foreach($exp as $I){
			$sql = 'DELETE FROM '.$DB->pre.'lottary WHERE lotID="'.$I.'"';
			$DB->dbQuery($sql);
			$str = array('str'=>'OK');
		}
	}else{
		$sql = 'DELETE FROM '.$DB->pre.'lottary WHERE lotID="'.$ID.'"';
		$DB->dbQuery	($sql);
		$str = array('str'=>'OK');
	}
	
	
		
	
	return json_encode($str);
}
function winClaimeAdminCommon($userID,$win){
	global $DB;					 
	$dateClause = 'lotDate="'.date('Y-m-d').'"';
					if($_REQUEST['frmdate'] !='' && $_REQUEST['todate'] != ''){
						$dateClause = ' (date(dateAdded) BETWEEN "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';
					}
	  $sql = 'SELECT * FROM '.$DB->pre.'result_win WHERE uID="'.$userID.'" AND '.$dateClause;
	$S = $DB->dbRows($sql);
	//print_r($S);
	$point = 0;
	$cv = 0;
	foreach($S as $S){
	//	echo $S['point'];
		$point += ($S['point']*$win)*$S['cv'];
		//$cv += $S['cv'];
		
	}
	$str = $point;
	return ($str);

}
$retailer = array();
function getAgentRetailerCommon($ID){
	global $DB;
	 $sql = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$ID.'" ';
	
	$D = $DB->dbRows($sql);
	
	foreach($D as $D){
	
		if($D['id'] !=''){
			if($D['userType'] == 3){
				array_push($GLOBALS['retailer'],$D['id']);
				//print_r($GLOBALS['retailer']);
			}
		
			
		}
		
		getAgentRetailerCommon($D['id']);
	}
	return $GLOBALS['retailer'];
}
function getSaleCommon($L){
			$tot = 0;
			foreach($L as $L){
				$tot +=$L['T'];
			}
			return $tot;
		}
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;
	case 'saveResult':
					include("../../connectdb.inc.php");
					echo saveResult();
					break;
	case 'saveAutoResult':
					include("../../connectdb.inc.php");
					echo saveAutoResult();
					break;
	case 'deleteLot':
					include("../../connectdb.inc.php");
					echo deleteLottery();
					break;
	
}

?>