	<?php







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);



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++;
}

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

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";
}
$drawTime = $lotHour.':'.$lotMin;

$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.'-'.date('h:i',strtotime($drawTime)); 

}
function winClaime(){
	global $DB;
	$sqlL = 'SELECT * FROM '.$DB->pre.'lottary WHERE barcode="'.$_REQUEST['barcode'].'" AND uID="'.$_REQUEST['userID'].'" AND status="1"';
	$L = $DB->dbRow($sqlL);
	$str = array('RS'=>0);
      $drawTime = drawTime1();
	$msgClaime = '<body style="background-color:pink;"><b>Please waith for result...</b>'; 
 	$AM = array('10:00:00','10:15:00','10:30:00','10:45:00','11:00:00','11:15:00','11:30:00','11:45:00','12:00:00');
	$AP = (in_array($L['lotTime'],$AM)?'AM':'PM');
  	$DAP = (in_array($drawTime.':00',$AM)?'AM':'PM');
  	
  	//new code start here
	if(date('H:i') > '21:45'){
		$DAP = (in_array($drawTime.':00',$AM)?'PM':'PM');
	}
	//new code end here
  	
  	$drawSet = (date('H:i',strtotime($drawTime.$DAP)) == '00:00'?'12:00':date('H:i',strtotime($drawTime.$DAP)));
 // echo $drawTime;
  //echo $L['lotTime'].'--'.in_array($L['lotTime'],$AM);
  //	echo date('H:i',strtotime($L['lotTime'].$AP)).'--'.date('H:i',strtotime($drawTime.$DAP)).'---'.$drawTime;
	if($drawTime.':00' != $L['lotTime'] && date('H:i',strtotime($L['lotTime'].$AP))< $drawSet || (date('Y-m-d')> date('Y-m-d',strtotime($L['lotdate'])))){
	$msgClaime = '<body style="background-color:pink;"><b>Invalid Barcode OR Cancel Ticket</b>'; 
	//echo $DB->numRows;
      if($DB->numRows > 0){
		$sqlW = 'SELECT lotID,dateAdded FROM '.$DB->pre.'result_win WHERE barcode="'.$_REQUEST['barcode'].'" AND uID="'.$_REQUEST['userID'].'"';
		$W = $DB->dbRow($sqlW);
		if($W['lotID'] == ''){
			
			$sqlAD = 'SELECT adminID FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['userID'].'"';
			$AD = $DB->dbRow($sqlAD);
			$admin = $AD['adminID'];
			
			$sqlWin = 'SELECT winrate,userCode FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['userID'].'"';
			$WIN = $DB->dbRow($sqlWin);
			
			$sqlS = 'SELECT R.*,L.cv,L.point FROM mx_result_tran  as R 	
					LEFT JOIN mx_lottary as L on (R.cellID = L.cellID  AND R.lotnum =L.lotnum) 
					WHERE L.barcode="'.$_REQUEST['barcode'].'" AND R.lotTime="'.$L['lotTime'].'" AND R.lotdate="'.$L['lotdate'].'" AND R.uID="'.$admin.'"'; 
			$S = $DB->dbRows($sqlS);
			$claim = 0;
            $showCell = '';
			foreach($S as $S){
				
				$sqlI = 'INSERT INTO '.$DB->pre.'result_win SET cellID="'.$S['cellID'].'",sernum="'.$S['sernum'].'",lotnum="'.$S['lotnum'].'",uID="'.$_REQUEST['userID'].'",lotdate="'.$L['lotdate'].'",lotTime="'.$L['lotTime'].'",cv="'.$S['cv'].'",point="'.$S['point'].'",barcode="'.$_REQUEST['barcode'].'",dateAdded="'.date('Y-m-d h:i:s').'"';
				$DB->dbQuery($sqlI);
				$claim += (($S['point']*$WIN['winrate'])*$S['cv']);
                $showCell .= '<b>'.$S['cellID'].'</b></br>';
				
			}
			$sqlU = 'UPDATE '.$DB->pre.'result_win SET claimAmount="'.$claim.'" WHERE barcode="'.$_REQUEST['barcode'].'" AND uID="'.$_REQUEST['userID'].'"';
			$DB->dbQuery($sqlU);
			$str = array('RS'=>$claim);
			$msgClaime = '<body style="background-color:pink;">'; 
			if($claim > 0){
                $msgClaime .= $showCell;
				$msgClaime .= "<b>Your Barcode: - ".$_REQUEST['barcode']."</b><br>";
				$msgClaime .= "<b>You have win::- ".$claim."</b>";
				
				$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.$WIN['userCode'].'" ORDER BY balanceID DESC limit 0,1';
				$B = $DB->dbRow($sqlB);
				
				$sqlBI = 'INSERT INTO '.$DB->pre.'balance SET userCode="'.$WIN['userCode'].'",balance="'.($B['balance']+$claim).'"';
				$DB->dbQuery($sqlBI);
			}else{
				$msgClaime = '<body style="background-color:pink;">'; 
				$msgClaime .= "<b>No Winning</b>";
			}
			
		}else{
			
				$msgClaime = '<body style="background-color:pink;">'; 
				$msgClaime .= "<b>Your Barcode: - ".$_REQUEST['barcode']."</b><br>";
				$msgClaime .= "<b>Already Claimed ".date('d-M-Y h:i:s',strtotime($W['dateAdded']))."</b>";
			
		}
		


	}
    }
	$msgClaime .= "</body>";
			echo $msgClaime;
	
	//echo $claim;
	//return json_encode($str);
	
}
function drawTime1(){
	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++;
}

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

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

$time_lot =  "$lotHour:$lotMin";
return $time_lot;
}


switch($_REQUEST['xAction']){
	case 'getPageData':
					include("../../connectdb.inc.php");
					echo getPageData();
					break;
	case 'addToCart1111':
					include("../../connectdb.inc.php");
					echo addToCart();
					break;
	case 'save':
					include("../../connectdb.inc.php");
					echo saveTicket();
					break;
	case 'saveAdv':
					include("../../connectdb.inc.php");
					echo saveTicketAdv();
					break;
	case 'getTime':
					include("../../connectdb.inc.php");
					echo getTime();
					break;
	case 'win':
					include("../../connectdb.inc.php");
    				echo '<meta http-equiv="X-UA-Compatible" content="IE=11">';
    				echo '<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
						<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">';
					echo winClaime();
    				
					break;
	case 'result':
					include("../../connectdb.inc.php");
					echo showResult();
					break;
}
?>