<?php
session_start();
include './connection.php';
$caseid=$_GET["caseid"];
$courtid=$_GET["courtid"];
$date=date("d/m/Y",strtotime($_GET["nextdate"]));
$proceed=$_GET["proceed"];

if($caseid===""|| $courtid==="" ||$date===""||$proceed==="")
{
	echo '<script>alert("Case Can not transfered due to missing value like,next proceeding,next date or New court."); window.location.assign("./transfer.php"); </script>';
	exit();
}






		$checkquery="SELECT * FROM caseproceedings WHERE pcourtid= '".$_SESSION["court_id"]."' AND caseid='".$caseid."' AND proceedingdate='".$date."' ";
		$result=$conn->query($checkquery);
    if (empty($result) && $result->num_rows < 1)
     {
		$q1="INSERT INTO caseproceedings(caseid, proceeding, proceedingdate, pcourtid) VALUES ('".$caseid."','".$proceed."','".$date."','".$courtid."')";
		$q3="UPDATE `caseinfo` SET currentcourtid='".$courtid."',current_proceeding='".$proceed."' WHERE ID=".$caseid."";
		$todaydate=date("d/m/Y");
		$q2="INSERT INTO casetransfers( caseid, tfcourtid, ttocourtid, dateoftransfer) VALUES ('".$caseid."','".$_SESSION["court_id"]."','".$courtid."','".$todaydate."')";
	if($conn->query($q1)&&$conn->query($q2)&&$conn->query($q3) )
		{
			echo '<script>alert("Case has been successfully Transfered."); window.location.assign("./transfer.php"); </script>';
		}
		else
		{
			echo '<script>alert("Case Can not Transfered."); window.location.assign("./transfer.php"); </script>';
		}

     }else
     		{
     			$q1="UPDATE caseproceedings SET  pcourtid='".$courtid."' WHERE pcourtid='".$_SESSION["court_id"]."' AND caseid='".$caseid."' AND proceedingdate='".$date."'";
     			$q3="UPDATE `caseinfo` SET currentcourtid='".$courtid."',current_proceeding='".$proceed."' WHERE ID=".$caseid."";
		$todaydate=date("d/m/Y");
		$q2="INSERT INTO casetransfers( caseid, tfcourtid, ttocourtid, dateoftransfer) VALUES ('".$caseid."','".$_SESSION["court_id"]."','".$courtid."','".$todaydate."')";
	if( $conn->query($q1)&&$conn->query($q2)&&$conn->query($q3) )
		{
			echo '<script>alert("Case has been successfully Transfered."); window.location.assign("./transfer.php"); </script>';
		}
		else
		{
				echo '<script>alert("Case Can Not Transfered."); window.location.assign("./transfer.php"); </script>';
		}
     		
     		}




?>