<?php
session_start();
$mr="";
$mr=$_SESSION["MRNo"];
$nm=$_SESSION["Patname"];
include('koneksi.php');

$tgl1=$tgl=$dok=$jamin=$poli=$telp=$tglper=$jam="";
//$telp=$_SESSION["telp"];
//echo $telp;
//$mr=$_POST['MRNo'];
//$tgl=$_POST["myLocalDate"];
$tgl=$_GET['id'];
$jam=$_GET['jam'];
$_SESSION["tglperiksa"]=$tgl;
//$dok=$_POST["dokter"];
$dok=$_SESSION["dokter"];
//$jamin=$_POST["debt"];
$jamin=$_SESSION["debt"];
//$poli=$_POST["poli"];
$poli=$_SESSION["poli"];
//$telp=$_POST["telp"];
$telp=$_SESSION["telp"];
//$jam=$_SESSION["jam"];
//echo "<script>window.alert('$dok')</script>";
$sql="SELECT daftar.MRNo, daftar.TglPeriksa, doctor.Name FROM daftar LEFT OUTER JOIN doctor on doctor.DoctCode = daftar.Dokter WHERE daftar.MRNo='$mr' and dbo.condate(TglPeriksa)=dbo.condate('$tgl') and penjamin='$jamin' and Dokter='$dok'";
$result = sqlsrv_query($koneksi, $sql);
$level=sqlsrv_num_rows($result);
$r=sqlsrv_fetch_array($result);

//$sql1="SELECT CURDATE() as ttgl";
//$sql1="SELECT convert(varchar, GETDATE(), 101) as ttgl";
//$result = sqlsrv_query($koneksi, $sql1);
//$level1=sqlsrv_num_rows($result);
//$r1=sqlsrv_fetch_array($result);
//$ttgl=$r1['ttgl'];
//$ttgl=date_format($r1['ttgl'],"Y/m/d");
//$tgl1=date_format($tgl,"Y/m/d");
//if ($ttgl==$tgl1) {
//	echo "<script>window.alert('Maaf Untuk Pendaftaran Hari Ini Silahkan Datang Langsung Ke RSI NU Demak')
$nmDok=$r['Name'];
//echo "<script>window.alert('$nmDok')";
//echo "<script>window.history.back()</script>";
//    </script>";
//	//return;
//	echo "<script>window.history.back()</script>";
	
//}
//else {
//if ($level === false )
//if (sqlsrv_query($koneksi, $sql) === false)
	if ($nmDok == '' )
		{
$sql="insert into daftar(MRNo, TglPeriksa, Dokter, Penjamin, Polycode, FPeriksa, telp,jampraktek) values('$mr', '$tgl', '$dok', '$jamin', '$poli', 'N', '$telp','$jam')";
//if (sqlsrv_query( $koneksi, $sql ) === TRUE) {
//   echo("<script>alert('Terima kasih $nm sudah mendaftar ke rumah sakit islam NU Demak. Nomor antrian dokter akan segera dikirim melalui SMS')</script>");
//	echo("<script>location='index.php'</script>");
//} else {
//    //echo "Error: " . $sql . "<br>" . $koneksi->error;
//	die( print_r( sqlsrv_errors(), true));
//}	
$nmDok=$r['Name'];
if (sqlsrv_query( $koneksi, $sql ) === false) {
	 //echo "Error: " . $sql . "<br>" . $koneksi->error;
	die( print_r( sqlsrv_errors(), true));
} else {
   //echo("<script>alert('Terima kasih $nm sudah mendaftar ke RSU PKU Muhammadiyah Gubug. Nomor Antrian Dokter Akan Segera Dikirim Melalui SMS')</script>");
	//echo("<script>location='index.php'</script>");
	sleep(10);
	//echo("<script>location='konfirmberhasil.php?id=$tgl'</script>");
	echo("<script>location='konfirmberhasil.php'</script>");
	//echo("<script>location='konfirmdouble.php'</script>");
}	
}
//}
	else
	{
//	session_start();
//isset($_SESSION['Name']);
//$_SESSION['Name'] = $r['Name'];
//$nm=$_SESSION["Name"];
$nmDok=$r['Name'];
	//echo "<script>window.alert('Maaf Anda sudah terdaftar pada hari tersebut dengan dokter $nmDok')
	
    //</script>";
	//echo("<script>location='loading.php'</script>");
	echo("<script>location='konfirmdouble.php'</script>");
	//echo "<script>window.history.back()</script>";
	//echo("<script>goBack()</script>");
	//echo("location.reload()</script>"); 
	//echo("<script>location='admisi.php'</script>");
}

?>