<?php
    include('loginsession.php');
    error_reporting(E_ALL);
    $filename = __FILE__;  //---Nama file saat ini
    if (strlen(__FILE__) > 48) {$filename = substr(__FILE__, 48);}
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
//-------------

$mr=  $_POST['mr']; 
$tgl_datang=  $_POST['tgl_datang']; 
$anamnesa=  $_POST['anamnesa']; $sistole=  $_POST['sistole'];$diastole=  $_POST['diastole'];$pulse=  $_POST['pulse'];$weight=  $_POST['weight'];
$tindakan=  $_POST['tindakan']; 
$biaya=  $_POST['biaya']; 
$password = $_POST['password'];
$insert=1;
$today = date("Y-m-d");
if ( $userini_id == "3"){
    $password = 000;
}


$sql = "SELECT date_, mr FROM novia_abc_visit";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
		$s = $row["date_"];
        $dt = new DateTime($s);
        $currdate = $dt->format('Y-m-d');
		
		$tgl_dtg = $tgl_datang;
		$tgl_dtg1 = new DateTime($tgl_dtg);
		$tgl =   $tgl_dtg1->format('Y-m-d');
		$tgl_2 =   $tgl_dtg1->format('Y-m-d H:i:s');
		if ( $currdate == $tgl){
			if ($mr == $row["mr"]){ $insert = 2;}
		}
	}
}

if ( $password != 000 ) { 
	echo "<script>alert('Password Anda Salah');document.location='status-view.php?id=$mr'</script>"; 
}
else if ( $insert == 2 ) { 
	echo "<script>alert('Pasien sudah mendaftar hari ini');document.location='status-view.php?id=$mr'</script>"; 
} else { $save = mysqli_query($conn, "INSERT INTO novia_abc_visit (mr, date_, tindakan, biaya, anamnesa, sistole, diastole, pulse, weight) VALUES ('$mr', '$tgl_2', '$tindakan', '$biaya', '$anamnesa', '$sistole', '$diastole', '$pulse', '$weight')");
	if($save){ echo "<script>document.location='status-view.php?id=$mr'</script>";} 
	else { echo "<script>alert('Proses simpan gagal, coba kembali');document.location='status-view.php?id=$mr'</script>"; }
}
?>
