<?php
session_start();
date_default_timezone_set('Asia/Jakarta');
//$nm=$_SESSION["Patname"];
include('koneksi.php');
//$patVoc="";

$dok=$pol=$tipe="";
$tipe = $_POST['tipe'];
$_SESSION["tipe"] = $tipe;
//echo "<script>window.alert('$tipe')    </script>";
//$dok=$_POST['dok'];
$dok=$_SESSION["dok"];
//$pol=$_POST['pol'];
$pol=$_SESSION["pol"];
//$tipe=$_SESSION["tipe"];
$tgl=date('m/d/Y');
$tgljam=date("Y-m-d H:i:s");
//echo "<script>window.alert('$tipe')    </script>";
$sql="select top 1 '' as panggil, isnull(nomor, '') as nomor, 
				'' as datang, SUBSTRING(nomor, 2, 1) as ini, SUBSTRING(nomor, 3, 1) as separator, 
				cast(cast(right(nomor, 3) as int) as varchar(50)) as nom
				, left(nomor, 1) as tip
from ANTRIAN_DOKTER
where doctcode = '".$dok."' 
and cast(cast(month(tgl) as varchar(2)) + '/' + 
	cast(day(tgl) as varchar(2)) + '/'+ 
	cast(year(tgl) as varchar(4)) as datetime) = '".$tgl."'
and isnull(flagPanggil, '') = ''
and left(nomor, 1) = left('".$tipe."', 1)
order by nomor asc"
;
$result = sqlsrv_query($koneksi, $sql);
//$result1 = sqlsrv_query($koneksi, $sql1);
$level=sqlsrv_num_rows($result);
$r=sqlsrv_fetch_array($result);
//$r1=sqlsrv_fetch_array($result1);
//echo $level;
//echo "<script>window.alert('$tgl')    </script>";
//echo "<script>window.alert('$dok')    </script>";
//$sql2="select name from doctor where doctcode = '".$dok."'";
//$result2 = sqlsrv_query($koneksi, $sql2);
//$r2=sqlsrv_fetch_array($result2);
	$nom=$r['nomor'];
	$nomss=$r['nom'];
	$_SESSION["nomer"] = $nom;
	//$_SESSION["dok"] = $r2['name'];
//$_SESSION["polino"] = $pol;
//	echo "<script>window.alert('$nom')    </script>";
//	if ($r1['jml'] = 0){
//		echo "<script>window.alert('Tidak..')
 //   </script>";
//	echo "<script>window.history.back()</script>";
//	}
//	else {
//		echo "<script>window.alert('ok..')</script>";
//		echo "<script>window.history.back()</script>";
//	}
if ($nom ==''){
//	session_start();
//isset($_SESSION['Name']);
//$_SESSION['Name'] = $r['Name'];
//$nm=$_SESSION["Name"];
//$nmDok=$r['Nama'];
	echo "<script>window.alert('Tidak Terdapat Nomor Antrian Pasien $tipe atau Pasien $tipe Belum Di Daftar Oleh Pendaftaran..')
    </script>";
	//echo "<script>window.history.back()</script>";
	echo("<script>location='utama.php'</script>");
}
	else{
		//echo "<script>window.alert('ok..')</script>";
		//echo "<script>window.history.back()</script>";
$patVoc="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\PEMBUKA-TING_TONG_AWALNew-.wav";
//$patVoc="wav";
$patNom="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\PEMBUKA-NOMOR_ANTRIAN-.wav";
$initipe="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\" .$r['tip']. ".wav";
$inis="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\" .$r['ini']. ".wav";
$noms="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\" .$r['nom']. ".wav";
$sil="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\PEMBUKA-SILAHKAN_MENUJU-.wav";
$polb="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\PREFIX-POLI-.wav";
$pola="\\\\rsiarafah\\ProgramExe\\ANTRIANNEW\\Suara\\" .$pol. ".wav";
//str_replace('/', '\\', $patNom)
//echo "<script>window.alert('$patVoc')</script>";
//echo "<script>window.alert('$patNom')</script>";
//echo "<script>window.alert('$inis')</script>";
//echo "<script>window.alert('$noms')</script>";
//echo "<script>window.alert('$sil')</script>";
//echo "<script>window.alert('$polb')</script>";
//echo "<script>window.alert('$pola')</script>";
//		echo "<script>window.history.back()</script>";
//$tsql="insert into ANTRIAN_PANGGIL(bukating,bukanomor,huruf,angka,bukatuju,daftar,loket,tglpanggil,angkasemua) 
//		values($patVoc, '$patNom', '$inis', '$noms', '$sil', '$polb', '$pola', '$tgl',$noms )";
//$tsql="insert into ANTRIAN_PANGGIL_FARMASI(bukating, bukanomor,huruf,angka,bukatuju,daftar,loket,tglpanggil,angkasemua, huruf2) 
if ($pol ==6 OR $pol ==5) {
	$tsql="insert into ANTRIAN_PANGGIL_POLI_6(bukating, bukanomor,huruf,angka,bukatuju,daftar,loket,tglpanggil,angkasemua, huruf2) 
values('$patVoc', '$patNom', '$inis', '$noms', '$sil', '$polb', '$pola', '$tgljam',$nomss, '$initipe')";
$tsql1="insert into ANTRIAN_MONCEK_POLI_6(nomor) 
values('$nom')";
}
else {
$tsql="insert into ANTRIAN_PANGGIL_POLI(bukating, bukanomor,huruf,angka,bukatuju,daftar,loket,tglpanggil,angkasemua, huruf2) 
values('$patVoc', '$patNom', '$inis', '$noms', '$sil', '$polb', '$pola', '$tgljam',$nomss, '$initipe')";
$tsql1="insert into ANTRIAN_MONCEK_POLI(nomor) 
values('$nom')";	
}


		//values(?, ?, ?, ?, ?, ?, ?, ?, ? )";
		/* Set parameter values. */  
//$params = array($patVoc, $patNom, $inis, $noms, $sil, $polb, $pola, $tgl,$noms);
//values($patVoc, $patNom, $inis, $noms, $sil, $polb, $pola, $tgl,$noms )";
$stmt = sqlsrv_query( $koneksi, $tsql);  
/* Prepare and execute the query. */  
//$stmt = sqlsrv_query($koneksi, $tsql, $params);  
if ($stmt=== false) {  
   //echo "<script>window.history.back()</script>";
   echo "Row insertion failed.\n";  
    die(print_r(sqlsrv_errors(), true));  
} else {  
    //echo "Row insertion failed.\n";  
    //die(print_r(sqlsrv_errors(), true));  
	//echo "<script>window.history.back()</script>";
	$stmt = sqlsrv_query( $koneksi, $tsql1);
	
	$tsql="update ANTRIAN_DOKTER set flagPanggil='Y' where nomor = '".$nom."' and cast(cast(month(tgl) as varchar(2)) + '/' + 
	cast(day(tgl) as varchar(2)) + '/'+ 
	cast(year(tgl) as varchar(4)) as datetime)='".$tgl."' ";
	$stmt = sqlsrv_query( $koneksi, $tsql);
	if ($stmt=== false) {  
		echo "Row insertion failed.\n";  
		die(print_r(sqlsrv_errors(), true));  
	} else {
		echo("<script>location='utama.php'</script>");
	}  
} 
/* Free statement and connection resources. */  
sqlsrv_free_stmt($stmt);  
sqlsrv_close($koneksi);  
}

?>