<?php 
session_start();
include_once '../config.php';
$user_id = $_SESSION['Admin']['id'];
$CurrDate = date('dmY');
if($_REQUEST['action'] == 'SaveQueue'){
	$srno = getMaxNo('tbl_patients');
	$id = $_REQUEST['id'];
	$Suffix = addslashes(trim($_REQUEST['Suffix']));
	$Fname = addslashes(trim($_REQUEST['Fname']));
	$Mname = addslashes(trim($_REQUEST['Mname']));
	$Lname = addslashes(trim($_REQUEST['Lname']));
	$Phone = addslashes(trim($_REQUEST['Phone']));
	$Phone2 = addslashes(trim($_REQUEST['Phone2']));
	$Gender = addslashes(trim($_REQUEST['Gender']));
	$City = addslashes(trim($_REQUEST['City']));
	$State = addslashes(trim($_REQUEST['State']));
	$Address = addslashes(trim($_REQUEST['Address']));
	if($_REQUEST['Dob']!=''){
	$Dob = addslashes(trim($_REQUEST['Dob']));
	}
	else{
			$Dob = '0000-00-00';
	}
	$Age = addslashes(trim($_REQUEST['Age']));
	$Consultant = addslashes(trim($_REQUEST['Consultant']));
	$Fees = addslashes(trim($_REQUEST['Fees']));
	$PayMode = addslashes(trim($_REQUEST['PayMode']));
	$RefBy = addslashes(trim($_REQUEST['RefBy']));
	//$MrdNo = $CurrDate."".$srno;
	$MrdNo = $srno;
	$PatientName = $Suffix." ".$Fname." ".$Mname." ".$Lname;
	//$CreatedDate = date('Y-m-d H:i:s');
	$CreatedDate = date('Y-m-d');
	$InDate = date('Y-m-d');
	$InTime = date('H:i:s');
	$OutTime = "";
	$ModifiedBy = 0;
	$Status = 1;
	$ModifiedDate = null;
	$PatientType = 'New';
	$Appointment = 1;

	$sql5 = "SELECT Name FROM tbl_users WHERE id='$Consultant'";
	$row5 = getRecord($sql5);
	$ConsultantName = $row5['Name'];

	//$Mrd_No = addslashes(trim($_REQUEST['Mrd_No']));
	if($id==''){
try{
	$sql = "INSERT INTO tbl_patients (srno,MrdNo,PatientName,Suffix,Fname,Mname,Lname,Gender,Dob,Age,Phone,Phone2,City,State,Address,Consultant,ConsultantName,Fees,PayMode,RefBy,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) VALUES (:srno,:MrdNo,:PatientName,:Suffix,:Fname,:Mname,:Lname,:Gender,:Dob,:Age,:Phone,:Phone2,:City,:State,:Address,:Consultant,:ConsultantName,:Fees,:PayMode,:RefBy,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':srno', $srno);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':PatientName', $PatientName);
$stmt->bindParam(':Suffix', $Suffix);
$stmt->bindParam(':Fname', $Fname);
$stmt->bindParam(':Mname', $Mname);
$stmt->bindParam(':Lname', $Lname);
$stmt->bindParam(':Gender', $Gender);
$stmt->bindParam(':Dob', $Dob);
$stmt->bindParam(':Age', $Age);
$stmt->bindParam(':Phone', $Phone);
$stmt->bindParam(':Phone2', $Phone2);
$stmt->bindParam(':City', $City);
$stmt->bindParam(':State', $State);
$stmt->bindParam(':Address', $Address);
$stmt->bindParam(':Consultant', $Consultant);
$stmt->bindParam(':ConsultantName', $ConsultantName);
$stmt->bindParam(':Fees', $Fees);
$stmt->bindParam(':PayMode', $PayMode);
$stmt->bindParam(':RefBy', $RefBy);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
if ($stmt->execute()) {
	$InsertId = $conn->lastInsertId();
	$srno = getMaxNo('tbl_patients');
	//$MrdNo = $srno;
	$MrdNo = $InsertId;
	if($MrdNo==0 || $MrdNo==''){}
		else{
	$sql3 = "UPDATE tbl_patients SET srno='$srno',MrdNo='$MrdNo' WHERE id='$InsertId'";
	$conn->query($sql3);

	try {
	$sql2 = "INSERT INTO tbl_queue_managements (MrdNo,ConsultantName,PatientName,PatientType,Gender,Age,InDate,InTime,OutTime,Consultant,Amount,PayMode,Diagnosis,RefBy,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate,Appointment) VALUES (:MrdNo,:ConsultantName,:PatientName,:PatientType,:Gender,:Age,:InDate,:InTime,:OutTime,:Consultant,:Amount,:PayMode,:Diagnosis,:RefBy,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate,:Appointment)";
$stmt = $conn->prepare($sql2);
$stmt->bindParam(':MrdNo', $MrdNo);
$stmt->bindParam(':ConsultantName', $ConsultantName);
$stmt->bindParam(':PatientName', $PatientName);
$stmt->bindParam(':PatientType', $PatientType);
$stmt->bindParam(':Gender', $Gender);
$stmt->bindParam(':Age', $Age);
$stmt->bindParam(':InDate', $InDate);
$stmt->bindParam(':InTime', $InTime);
$stmt->bindParam(':OutTime', $OutTime);
$stmt->bindParam(':Consultant', $Consultant);
$stmt->bindParam(':Amount', $Fees);
$stmt->bindParam(':PayMode', $PayMode);
$stmt->bindParam(':Diagnosis', $Diagnosis);
$stmt->bindParam(':RefBy', $RefBy);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
$stmt->bindParam(':Appointment', $Appointment);
if ($stmt->execute()) {
	$PostId = $conn->lastInsertId();

	if($PageVal == 'new'){
	?>
<script>
	window.location.href="../new-patient-lists.php";
</script>
<?php } else{ ?>	
<script>
	window.location.href="../queuemanagement.php";
</script>
	<?php }

	} } 


catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

} } }

catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

	}

}


if($_REQUEST['action'] == 'SaveOldQueue'){
$PatientId = $_REQUEST['PatientId'];
$MRD_NO = $_REQUEST['MRD_NO'];
$PatientName = $_REQUEST['PatientName'];
$Age = addslashes(trim($_REQUEST['PatientAge']));
$Consultant = $_REQUEST['Consultant'];
$Fees = $_REQUEST['Fees'];
$PayMode = $_REQUEST['PayMode'];

$CreatedDate = date('Y-m-d');
$InDate = date('Y-m-d');
$InTime = date('H:i:s');
$ModifiedBy = 0;
	$Status = 1;
	$ModifiedDate = null;
	$PatientType = 'Old';
	$RefBy=null;
	$Appointment = 1;

$sql4 = "SELECT * FROM tbl_users WHERE id='$Consultant'";
$row4 = getRecord($sql4);
$ConsultantName = $row4['Name'];

$sql = "SELECT * FROM tbl_patients WHERE MrdNo='$MRD_NO'";
$row = getRecord($sql);
$Gender = $row['Gender'];
//$Age = $row['Age'];

$sql3 = "SELECT * FROM tbl_queue_managements WHERE MrdNo='$MRD_NO' AND InDate='$InDate'";
$rncnt3 = getRow($sql3);
if($rncnt3 > 0){
echo "<script>alert('Record Already In Queue!');window.location.href='../queuemanagement.php';</script>";
}
else{
	try {
	$sql2 = "INSERT INTO tbl_queue_managements (MrdNo,ConsultantName,PatientName,PatientType,Gender,Age,InDate,InTime,OutTime,Consultant,Amount,PayMode,Diagnosis,RefBy,Status,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate,Appointment) VALUES (:MrdNo,:ConsultantName,:PatientName,:PatientType,:Gender,:Age,:InDate,:InTime,:OutTime,:Consultant,:Amount,:PayMode,:Diagnosis,:RefBy,:Status,:CreatedBy,:CreatedDate,:ModifiedBy,:ModifiedDate,:Appointment)";
$stmt = $conn->prepare($sql2);
$stmt->bindParam(':MrdNo', $MRD_NO);
$stmt->bindParam(':ConsultantName', $ConsultantName);
$stmt->bindParam(':PatientName', $PatientName);
$stmt->bindParam(':PatientType', $PatientType);
$stmt->bindParam(':Gender', $Gender);
$stmt->bindParam(':Age', $Age);
$stmt->bindParam(':InDate', $InDate);
$stmt->bindParam(':InTime', $InTime);
$stmt->bindParam(':OutTime', $OutTime);
$stmt->bindParam(':Consultant', $Consultant);
$stmt->bindParam(':Amount', $Fees);
$stmt->bindParam(':PayMode', $PayMode);
$stmt->bindParam(':Diagnosis', $Diagnosis);
$stmt->bindParam(':RefBy', $RefBy);
$stmt->bindParam(':Status', $Status);
$stmt->bindParam(':CreatedBy', $user_id);
$stmt->bindParam(':CreatedDate', $CreatedDate);
$stmt->bindParam(':ModifiedBy', $ModifiedBy);
$stmt->bindParam(':ModifiedDate', $ModifiedDate);
$stmt->bindParam(':Appointment', $Appointment);
if ($stmt->execute()) {
	$PostId = $conn->lastInsertId();
	?>	
<script>
	window.location.href="../queuemanagement.php";
</script>
	<?php
	} }
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
}
}	

 if($_REQUEST['action'] == 'delete') {
   
      $id = $_REQUEST['id'];
      $query = "DELETE FROM tbl_queue_managements WHERE id = '$id'";
      $conn->query($query);
      echo "Delete Successfully";

  }

  if($_REQUEST['action'] == 'getPatientDetails'){
//$MrdNo = addslashes(trim($_REQUEST['id']));
$val = addslashes(trim($_REQUEST['id']));
$seperate = explode(" / ", $val);
$MrdNo = $seperate[0];
$sql = "SELECT * FROM tbl_patients WHERE MrdNo='$MrdNo'";
$row = getRecord($sql);
echo json_encode($row);
}	

 if($_REQUEST['action'] == 'getNewPatientDetails'){
$id = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_patients WHERE id='$id'";
$row = getRecord($sql);
echo json_encode($row);
}	

  if($_REQUEST['action'] == 'getNewPatientDetails2'){
$id = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_patients WHERE MrdNo='$id'";
$row = getRecord($sql);
echo json_encode($row);
}

if($_REQUEST['action'] == 'getConsultantDetails'){
$id = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_users WHERE id='$id'";
$row = getRecord($sql);
echo json_encode($row);
}

if($_REQUEST['action'] == 'getProcedureDetails'){
$Name = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_procedures WHERE Name='$Name'";
$row = getRecord($sql);
echo json_encode($row);
}

  if($_REQUEST['action'] == 'getIpdPatientDetails'){
$IpdNo = addslashes(trim($_REQUEST['id']));
$sql = "SELECT * FROM tbl_dischargecards WHERE IpdNo='$IpdNo'";
$row = getRecord($sql);
echo json_encode($row);
}

 if($_REQUEST['action'] == 'getQuePatientDetails'){
$MrdNo = addslashes(trim($_REQUEST['id']));
$sql = "SELECT tqm.*,tp.Fname,tp.Mname,tp.Lname,tp.Phone,tp.Address,tp.City,tp.State,tp.Suffix,tp.Dob FROM tbl_queue_managements tqm LEFT JOIN tbl_patients tp ON tp.MrdNo=tqm.MrdNo WHERE tqm.id='$MrdNo'";
$row = getRecord($sql);
echo json_encode($row);
}


if($_REQUEST['action'] == 'getVisitDates'){
$id = $_REQUEST['id'];?>
<option value="" disabled="" selected="">Select Visit</option>
<?php 
	$sql4 = "SELECT DISTINCT(InDate) As VisitDate FROM tbl_queue_managements WHERE MrdNo='$id' ORDER BY InDate DESC";
    $row4 = getList($sql4);
    foreach($row4 as $result){
?>
     <option value="<?php echo $result['VisitDate'];?>"><?php echo date("d/m/Y", strtotime(str_replace('-', '/',$result['VisitDate']))); ?></option>
<?php } }


 if($_REQUEST['action'] == 'getSubRefract'){
$MrdNo = addslashes(trim($_REQUEST['mrdno']));
$sql = "SELECT MAX(id) AS LastId FROM tbl_optometrist WHERE MrdNo='$MrdNo' ORDER by VisitDate DESC LIMIT 1";
$row = getRecord($sql);
$LastId = $row['LastId'];

$sql2 = "SELECT * FROM tbl_optometrist WHERE id='$LastId'";
$row2 = getRecord($sql2);
echo json_encode($row2);
}



if($_REQUEST['action'] == 'EditQueue'){
	$PageVal = $_REQUEST['PageVal'];
	$id = $_REQUEST['id'];
	$Mrd_No = $_REQUEST['Mrd_No'];
	$Suffix = addslashes(trim($_REQUEST['Suffix']));
	$Fname = addslashes(trim($_REQUEST['Fname']));
	$Mname = addslashes(trim($_REQUEST['Mname']));
	$Lname = addslashes(trim($_REQUEST['Lname']));
	$Phone = addslashes(trim($_REQUEST['Phone']));
	$Phone2 = addslashes(trim($_REQUEST['Phone2']));
	$Gender = addslashes(trim($_REQUEST['Gender']));
	$City = addslashes(trim($_REQUEST['City']));
	$State = addslashes(trim($_REQUEST['State']));
	$Address = addslashes(trim($_REQUEST['Address']));
	$Dob = addslashes(trim($_REQUEST['Dob']));
	$Age = addslashes(trim($_REQUEST['Age']));
	$Consultant = addslashes(trim($_REQUEST['Consultant']));
	$Fees = addslashes(trim($_REQUEST['Fees']));
	$Category = addslashes(trim($_REQUEST['CategoryId']));
	$PayMode = addslashes(trim($_REQUEST['PayMode']));
	$RefBy = addslashes(trim($_REQUEST['RefBy']));
	$Diagnosis = addslashes(trim($_REQUEST['Diagnosis']));
	$PatientType = addslashes(trim($_REQUEST['PatientType']));
	//$MrdNo = $CurrDate."".$srno;
	//$MrdNo = $srno;
	$MrdNo = date('Y')."".$srno;
	$PatientName = $Suffix." ".$Fname." ".$Mname." ".$Lname;
	//$CreatedDate = date('Y-m-d H:i:s');
	$CreatedDate = date('Y-m-d');
	$InDate = date('Y-m-d');
	$InTime = date('H:i:s');
	$OutTime = "";
	
	$sql4 = "SELECT Name FROM tbl_users WHERE id='$Consultant'";
    $row4 = getRecord($sql4);
    $ConsultantName = $row4['Name'];

	if($PatientType == 'New'){
	$sql = "UPDATE tbl_patients SET PatientName='$PatientName',Suffix='$Suffix',Fname='$Fname',Mname='$Mname',Lname='$Lname',Gender='$Gender',Dob='$Dob',Age='$Age',Phone='$Phone',Phone2='$Phone2',City='$City',State='$State',Address='$Address',Consultant='$Consultant',ConsultantName='$ConsultantName',Fees='$Fees',PayMode='$PayMode',RefBy='$RefBy',Diagnosis='$Diagnosis',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE MrdNo='$Mrd_No'";
	$conn->query($sql);
	}
	else{
		$sql = "UPDATE tbl_patients SET PatientName='$PatientName',Suffix='$Suffix',Fname='$Fname',Mname='$Mname',Lname='$Lname',Gender='$Gender',Dob='$Dob',Age='$Age',Phone='$Phone',Phone2='$Phone2',City='$City',State='$State',Address='$Address',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE MrdNo='$Mrd_No'";
	$conn->query($sql);
	
	 $sql2 = "UPDATE tbl_queue_managements SET PatientName='$PatientName',Gender='$Gender',Age='$Age',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE MrdNo='$Mrd_No'";
	$conn->query($sql2);
	}
	 $sql2 = "UPDATE tbl_queue_managements SET ConsultantName='$ConsultantName',PatientName='$PatientName',Gender='$Gender',Age='$Age',Consultant='$Consultant',Amount='$Fees',PayMode='$PayMode',Diagnosis='$Diagnosis',RefBy='$RefBy',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE id='$id'";
	$conn->query($sql2);
if($PageVal == 'new'){
	?>
<script>
	window.location.href="../new-patient-lists.php";
</script>
<?php } else{ ?>	
<script>
	window.location.href="../queuemanagement.php";
</script>
	<?php }
	//$conn->close();
}


if($_REQUEST['action'] == 'EditPatient'){
	$PageVal = $_REQUEST['PageVal'];
	$id = $_REQUEST['id'];
	$Mrd_No = $_REQUEST['Mrd_No'];
	$Suffix = addslashes(trim($_REQUEST['Suffix']));
	$Fname = addslashes(trim($_REQUEST['Fname']));
	$Mname = addslashes(trim($_REQUEST['Mname']));
	$Lname = addslashes(trim($_REQUEST['Lname']));
	$Phone = addslashes(trim($_REQUEST['Phone']));
	$Phone2 = addslashes(trim($_REQUEST['Phone2']));
	$Gender = addslashes(trim($_REQUEST['Gender']));
	$City = addslashes(trim($_REQUEST['City']));
	$State = addslashes(trim($_REQUEST['State']));
	$Address = addslashes(trim($_REQUEST['Address']));
	$Dob = addslashes(trim($_REQUEST['Dob']));
	$Age = addslashes(trim($_REQUEST['Age']));
	$CreatedDate = date('Y-m-d');
	$PatientName = $Suffix." ".$Fname." ".$Mname." ".$Lname;

	 $sql = "UPDATE tbl_patients SET PatientName='$PatientName',Suffix='$Suffix',Fname='$Fname',Mname='$Mname',Lname='$Lname',Gender='$Gender',Dob='$Dob',Age='$Age',Phone='$Phone',Phone2='$Phone2',City='$City',State='$State',Address='$Address',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE MrdNo='$Mrd_No'";
	$conn->query($sql);
	
	 $sql2 = "UPDATE tbl_queue_managements SET PatientName='$PatientName',Gender='$Gender',Age='$Age',ModifiedBy='$user_id',ModifiedDate='$CreatedDate' WHERE MrdNo='$Mrd_No'";
	$conn->query($sql2);
if($PageVal == 'new'){
	?>
<script>
	alert('Patient Details Updated Successfully!');
	window.location.href="../new-patient-lists.php?val=all";
</script>
<?php } else{ ?>	
<script>
	alert('Patient Details Updated Successfully!');
	window.location.href="../queuemanagement.php";
</script>
	<?php }
	}

if($_REQUEST['action'] == 'getVal'){

	$val = addslashes(trim($_REQUEST['id']));
	$seperate = explode("/", $val);
	echo $seperate[0];
}	
?>