<?php 
include("include/connection.php");
include("winnerResult.php");
$current = strtotime(date("Y-m-d 00:00:00"));
$now = strtotime(date("Y-m-d H:i:s"));
$firstperiodid=date('Ymd').sprintf("%03d",1);
$lastperiodid=date('Ymd',strtotime("-1 days")).sprintf("%03d",480);

//$truncateQuery=mysqli_query($con,"TRUNCATE TABLE `tbl_gameid`");
//if($current==$now)
//{
//$sql1=mysqli_query($con,"INSERT INTO `tbl_gameid` (`gameid`) VALUES ('".$firstperiodid."')");
	//}else{
	
$checkperiod_Query=mysqli_query($con,"select * from `tbl_gameid` order by id desc limit 1");
$periodRow=mysqli_num_rows($checkperiod_Query);
$periodidRow=mysqli_fetch_array($checkperiod_Query);

if($periodRow=='')
{
    $sql1=mysqli_query($con,"INSERT INTO `tbl_gameid` (`gameid`) VALUES ('".$firstperiodid."')");

//php -q 	/usr/local/bin/php /home/popularmall/public_html/uat/wp-cron.php
}else if($lastperiodid==$periodidRow['gameid'])
{
  $truncateQuery=mysqli_query($con,"TRUNCATE TABLE `tbl_gameid`");
  $truncateResultQuery=mysqli_query($con,"TRUNCATE TABLE `tbl_result`");
  $sql1=mysqli_query($con,"INSERT INTO `tbl_gameid` (`gameid`) VALUES ('".$firstperiodid."')");  

}else
{

    // Get the last inserted ID from the database
    $sql = "SELECT MAX(gameid) AS max_id FROM tbl_gameid";
    $result = mysqli_query($con, $sql);
    $row = mysqli_fetch_assoc($result);
    $last_id = $row['max_id'];
    
    // Get the current date
    $current_date = date('Ymd');
    
    // Determine the next ID
    if ($last_id && substr($last_id, 0, 8) == $current_date) {
        // Increment the ID by 1
        $next_numeric_id = intval(substr($last_id, 8)) + 1;
        $next_id = $current_date . sprintf("%03d", $next_numeric_id);
    
    } else {
        // Start a new sequence with "001"
        $next_id = $current_date . "001";
        
    }
    
    // Insert the new ID into the database
    $sql1 = "INSERT INTO `tbl_gameid` (`gameid`) VALUES ('".$next_id."')";
    mysqli_query($con, $sql1);

// $nextid=$periodidRow['gameid']+1;
// $nextid=date('Ymd').sprintf("%03d",1);
// $sql1=mysqli_query($con,"INSERT INTO `tbl_gameid` (`gameid`) VALUES ('".$nextid."')");

// echo $nextid;

}
	//}
	
?>