<?php
 	session_start(); 

	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
	include("connect.php");
	$brmid=$_SESSION['linkarisebranchid'];
	$cmpmid=$_SESSION['linkarisecmpmid'];
	
	date_default_timezone_set("Asia/Singapore");
	//$mdate1= date('Y-m-d h:i:sa');
	$mdate2= date('Y-m-d');
	$mdate= date('Y-m-d');
	$mdate1= date('Y-m-d H:i:sa');
	
$currentDate = date("Y-m-d");

// Set the end date as one year from the current date
 $endDate = date("Y-m-d", strtotime("+365 days", strtotime($currentDate)));

$slotmid=0;
$spotmid=0;
$slotstarttime=0;
$slotendtime=0;
	


// Iterate over the date range and insert data for each date
$currentDate = date("Y-m-d", strtotime($currentDate));
while ($currentDate <= $endDate) {
    // Check if data already exists for the current date
    $query = "SELECT COUNT(*) FROM res_availibility_detail WHERE availabledate = '$currentDate' and cmpmid=$cmpmid";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_row($result);
    $dataExists = $row[0] > 0;

    // Get the date components
    $currentYear = date("Y", strtotime($currentDate));
    $currentMonth = date("m", strtotime($currentDate));
    $currentDay = date("d", strtotime($currentDate));
    
    $currentDateTime = new DateTime($currentDate);
    $currentDateTime->modify('+365 days');
    $endDateLimit = $currentDateTime->format('Y-m-d');

    // Insert data if it doesn't already exist for the current date
    if (!$dataExists && $endDateLimit >= $currentDate) {

		
		$everate12= "SELECT spotmid,slotmid FROM res_spot_detail where cmpmid='$cmpmid'";
	$rerate12 = mysqli_query($conn, $everate12);
	while($rtmaxq12 = mysqli_fetch_assoc($rerate12))
	{
		$spotmid=$rtmaxq12['spotmid'];
		$slotmid=$rtmaxq12['slotmid'];
		
		$everate1= "SELECT * FROM res_slots_master where cmpmid='$cmpmid' and slotmid=$slotmid";
	$rerate1 = mysqli_query($conn, $everate1);
	while($rtmaxq1 = mysqli_fetch_assoc($rerate1))
	{
		$slotmid=$rtmaxq1['slotmid'];
		$slotstarttime=$rtmaxq1['slotstarttime'];
		$slotendtime=$rtmaxq1['slotendtime'];
	
		
        // Insert your data into the table
        $insertQuery = "INSERT INTO res_availibility_detail (spotmid, slotmid, availabledate,starttime,endtime,availablestatus,cmpmid) VALUES ('$spotmid','$slotmid','$currentDate','$slotstarttime','$slotendtime','Available',$cmpmid)";
        mysqli_query($conn, $insertQuery);
    }}

	}

    // Move to the next date
  //  $currentDate = date("Y-m-d", strtotime("+1 day", strtotime($currentDate)));
	   $currentDateTime = new DateTime($currentDate);
    $currentDateTime->modify('+1 day');
    $currentDate = $currentDateTime->format('Y-m-d');
}

//header('Location: availability.php');
?>
