<?php
include '../../connect.php';
session_start();

// Validasi parameter GET
if (!isset($_GET['bilikID']) || empty($_GET['bilikID'])) {
    echo "<script>alert('Ralat: ID bilik tidak sah.'); window.location.href = '../DashboardBilikMesyuarat.php';</script>";
    exit;
}

$bilikID = $_GET['bilikID'];

$query = "SELECT bilikID FROM tempahan_bilik WHERE bilikID='".$bilikID."'";
$strQuery = mysqli_query($conn,$query);

	if($strQuery)
	{
		$query1= "UPDATE `tempahan_bilik` SET status='BATAL' WHERE bilikID='".$bilikID."'";
		$strQuery1 = mysqli_query($conn,$query1);
		
		if($strQuery1)
		{
			$sql = "SELECT * FROM tempahan_bilik WHERE bilikID='$bilikID'";
			$result = mysqli_query($conn,$sql);
			$row = mysqli_fetch_array($result);

			$tarikhMesyuarat =$row['tarikh'];
			$masaDari = $row['masaDari'];
			$jenisBilik = $row['jenisBilik'];

			$sql1="SELECT bilikID FROM tempahan_bilik WHERE tarikh='$tarikhMesyuarat' AND masaDari='$masaDari' AND jenisBilik='$jenisBilik' AND status ='SENARAI MENUNGGU' ";
			$result1 = mysqli_query($conn,$sql1);
			
			if($row1=mysqli_fetch_array($result1))
			{
				$query2= "UPDATE `tempahan_bilik` SET status='SEDANG DIPROSES' WHERE bilikID='".$row1['bilikID']."'";
				$strQuery2 = mysqli_query($conn,$query2);
				
				if($strQuery2)
				{
					echo "<script>";
					echo "window.location.href = '../DashboardBilikMesyuarat.php';</script>";
					exit;
				}
				else
				{
					echo 'MySQL Error update: ' . mysqli_error($conn);
					exit;
				}
			}
			else
			{
				echo "<script>";
				echo "window.location.href = '../DashboardBilikMesyuarat.php';</script>";
				exit;
			}
	
		}
		else
		{
			echo 'MySQL Error select *: ' . mysqli_error($conn);
			exit;
		}
	}
	else
	{
		echo 'MySQL Error batal: ' . mysqli_error($conn);
		exit;
	}
	
?>