<?php
session_start();

require_once "./connection.php";

	$name=$_POST["f1"]." بنام  ".$_POST["f2"];
	$caseno=$_POST["caseno"];
	$courtid=$_SESSION["court_id"];
 	$instdate=date("d/m/Y",strtotime( $_POST["instdate"]));
 	$casetype=$_POST["casetype"];
 	$casenature=$_POST["caset"];

 if(empty($name)||empty($caseno) ||empty($instdate))
		
		{
			echo '<script>alert("There are some attributes are empty."); window.location.assign("./addnew.php"); </script>';

		} else 
			{


	$q1="Select * from caseinfo Where  currentcourtid='".$courtid."' AND caseno='".$caseno."' AND instdate='".$instdate."'";
	$result=$conn->query($q1);
	if (!empty($result) && $result->num_rows > 0)
	 
	 		{
 	echo '<script>alert("One case is already present."); window.location.assign("./addnew.php"); </script>';
 exit();
			}else{
				$query="INSERT INTO caseinfo(caseno,instdate,casetitle,casetype,currentcourtid,status,casenature)VALUES('".$caseno."', '".$instdate."','".$name."','".$casetype."','".$courtid."' ,'1' ,'".$casenature."' )";
					if(mysqli_query($conn,$query))
						{
				 				
							
				$cq="Select * from caseinfo Where  currentcourtid='".$courtid."' AND caseno='".$caseno."' AND instdate='".$instdate."' ";
				$result11=$conn->query($cq);
			if (!empty($result11) && $result11->num_rows > 0)
			 {
 				 $row=$result11->fetch_assoc();
 				 $id=$row["ID"]; 
 				
							
				}else{exit();}



			$q="Select * from caseproceedings Where  pcourtid='".$_SESSION["court_id"]."' AND caseid='".$id."'";
			$result1=$conn->query($q);
			if (!empty($result1) && $result1->num_rows > 0)
			 {
 				echo '<script>alert("This case is already in Proceedings."); window.location.assign("./addnew.php"); </script>';
 				exit();
			}else{
				$ndate=date("d/m/Y");
				$ddd=date("d/m/Y",strtotime( $_POST["fdate"]));
				 $quer="INSERT INTO `tbl_criminal`( `case_id`, `ps`, `fir_no`, `fir_date`) VALUES ('".$id."','".$_POST["fps"]."','".$_POST["firno"]."','".$ddd."')";
             
				$query1="INSERT INTO `caseproceedings`( `caseid`, `proceeding`, `proceedingdate`, `pcourtid`, `status`) VALUES ('".$id."',' ".$_POST["proceed"]."','".$ndate." ','".$_SESSION["court_id"]." ','0')";
			 
					if(mysqli_query($conn,$query1) & mysqli_query($conn,$quer))
						{
							 echo '<script>alert("File  inserted to Proceedings successfully."); window.location.assign("./addnew.php"); </script>';
						}else {
								 echo '<script>alert("Can not Insert File to Proceedings..."); window.location.assign("./addnew.php"); </script>';
								}
			}

						}else {
								 echo '<script>alert("Can not Insert data..."); window.location.assign("./addnew.php"); </script>';
								}
							}
			
			}

?>