<?php
add_action('admin_enqueue_scripts', 'ds_admin_theme_style');
add_action('login_enqueue_scripts', 'ds_admin_theme_style');
function ds_admin_theme_style() {
global $wpdb;
date_default_timezone_set("America/New_York");	
if (!current_user_can('manage_options') && is_user_logged_in()) {
	?>
		<style>
body{
	font-size:30px;
}
	.wpcf7-form{
		font-size:30px;
	}
	.outer-div
	{
		margin: 0 auto;
		width:480px;
		padding: 30px;
		border: 1px solid green;
		height:auto;	 	 
	}
	.title{
		font-size:18px;
		font-weight:bold;
		clear:both;
	}
	.inner-div
	{
		font-size:20px;
		padding:3px 0px 3px 0px;
		display: inline-block;
		margin: 0 auto;
		border: 1px solid grey;
		height:auto;	 	 
		width: 480px;
	}
	.msg_confirm{
		width: 60%;		
	}
	@media screen and (max-width: 600px) 
	{
		.wpcf7-form{
			font-size:30px;
		}
		.msg_confirm{
			width: 90%;		
		}
		
		.qr_code_scan_bmc
		{
			width:90% !important;
			height:auto;
		}
		.logo{
			width:200px;
		}
		.header-wrapper{
			display:none;
		}
		.wpcf7-submit{
			float:right;
			width:80%;
			margin: 0 auto;		
		}
		.inner-div
		{
			padding:3px 0px 3px 0px;
			font-size:25px;
			display: inline-block;
			margin: 0 auto;
			border: 1px solid grey;
			height:auto;	 	 
			width: 380px;
		}
	}
	</style>
		<div><br><center><h1>Welcome to Bethany Registrasi</h1>
		<style>
		#adminmenuback, #wpfooter, #screen-meta-links, .wrap, .update-nag, .updated, .error, .is-dismissible, #dashboard-widgets-wrap { display: none; }
		.pendaftaran{ font-size:20px; }
		table, th, td {border: 1px solid grey; border-spacing: 0px; padding:10px;}
		.admin_register{font-size:30px; line-height: 1.6;}
		</style>
		<div class="pendaftaran">
		<a href="/wp-admin/"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Check Konfirmasi</button></a> | 
		<a href="?listdaftar=true"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">List Pendaftar</button></a><br><br>
		<a href="https://register.bethanymiraclecenter.org/downloadxlsx.php"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Google Sheet</button></a><br><br>
		<?php				
		$nomorconfirm = $_GET['confirmation'];

		$minggutgl = date("Y-m-d",strtotime('sunday'));
		$now = date("Y-m-d");
		
		
		//date("m/d/Y g:i A")
		$nomor_telp = trim($_GET['your-phone']);

		$bmcconfirmasi = $wpdb->get_row( "SELECT * FROM wp_bmc_confirmasi WHERE confirmation_number ='".$nomorconfirm."'");
		$tanggalminggu = $wpdb->get_var( "SELECT max(minggu_tgl) FROM wp_bmc_confirmasi");

		$tglamrik = date("m/d/Y",strtotime($tanggalminggu));		

		if(isset($_GET['listdaftar'])){

//$result = $wpdb->get_results( "SELECT * FROM itung_total WHERE status IS NULL AND ");
$result = $wpdb->get_results( "SELECT * FROM itung_total WHERE minggu_tgl >= '".$now."' AND status IS NULL");

	$totalDewasa8 = 0;
	$totalAnak8 = 0 ;
	$totalDewasa10 = 0;
	$totalAnak10 = 0 ;
	$totalDewasa6 = 0;
	$totalAnak6 = 0 ;	
	foreach ($result as $item){
		//echo "Keren"; print_r($item); exit;
		//$value = unserialize($item->form_value);
		//print_r($value['jam_ibadah'][0]); exit;
		if($item->jam_ibadah=="8"){
			$totalDewasa8=$totalDewasa8 + $item->total_dewasa;
			$totalAnak8=$totalAnak8 + $item->total_anak;	
		}elseif($item->jam_ibadah=="10"){
			$totalDewasa10=$totalDewasa10 + $item->total_dewasa;
			$totalAnak10=$totalAnak10 + $item->total_anak;		
		}else{
			$totalDewasa6=$totalDewasa6 + $item->total_dewasa;
			$totalAnak6=$totalAnak6 + $item->total_anak;					
		}
	}
	$totalSemua8 = $totalDewasa8+$totalAnak8;
	$totalSemua10 = $totalDewasa10+$totalAnak10;
	$totalSemua6 = $totalDewasa6+$totalAnak6;
	?>


	<?php	
	//echo "Hari: " . date("l m/d/Y H:i")."<br>";
	echo "<div classs='outer-div'>";
	echo "<div class='inner-div title'>Pendaftaran Untuk Tanggal: " . $tglamrik." (Max 120 orang per jam ibadah)</div><br>";
	echo "<div class='inner-div' ><b>Total Ibadah Jam 8 pagi terdaftar: ".$totalSemua8;
	echo ($totalSemua8 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";

	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa8."  -  ";
	echo "Total Anak: ".$totalAnak8."</div><br>";

	echo "<div class='inner-div' ><b>Total Ibadah Jam 10 pagi terdaftar: ".$totalSemua10;
	echo ($totalSemua10 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";

	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa10."  -  ";
	echo "Total Anak: ".$totalAnak10."</div><br>";

	echo "<div class='inner-div'><b>Total Ibadah Jam 12 siang atau 7 sore terdaftar: ".$totalSemua6;
	echo ($totalSemua6 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";
	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa6."  -  ";
	echo "Total Anak: ".$totalAnak6."</div>";
	echo "<div><b>Hari ini: ".date("m/d/Y g:i A")."</b></div>";
	echo "</div>";

			
			echo "<h1>List Daftar BMC</h1><table class='daftarlist'><tr>";
			$namacolom = $wpdb->get_col( "DESC " . 'list_pendaftar', 0 );
			foreach ( $namacolom as $column_name ) {
				echo "<th>".$column_name."</th>";
			}
			$listdaftar = $wpdb->get_results( "SELECT * FROM list_pendaftar WHERE `Minggu ke` >= '".$now."' ORDER BY nama ASC");
			foreach($listdaftar as $pendaftar){
				echo "<tr>";				
				foreach ( $namacolom as $columnname ) {
					if($columnname == "Minggu ke"){
						echo "<td>".date("m/d/Y",strtotime($pendaftar->$columnname))."</td>";
					}elseif($columnname == "Edit"){
						echo "<td><a href='?cancelid=".$pendaftar->$columnname."'>Cancel</a></td>";
					}else{
						echo "<td>".$pendaftar->$columnname."</td>";
					}
				}
				
				echo "</tr>";
				//echo "<pre>"; print_r($pendaftar);
			}
			echo "</table>";
			
		}else{
			?>
		<form action="#" method="GET">
			Nomor Confirmasi <input type="text" name="confirmation"  />
			<input type="submit" />
		</form>

			<?php
		}
		if(isset($_GET['cancelid'])){
			$conid = $_GET['cancelid'];
			$wpdb->update('wp_bmc_confirmasi', array(
				'status' => 'batal'),
			array('id'=>$conid));
			echo "<h2>Nomor konfirmasi ini sudah di batalkan</h2>";
		}

		if(!isset($_GET['confirmation'])){
			echo "<br><br>";
		}elseif(empty($bmcconfirmasi) && isset($_GET['confirmation'])){
			echo "<br><div class='admin_register'>Nomor Confirmasi tidak ditemukan.</div><br><br>";
		}else{
			$idbmcconfirmasi = $bmcconfirmasi->id;
			$id_bmc_daftar = $bmcconfirmasi->id_bmc_daftar;
			$jam_ibadah = $bmcconfirmasi->jam_ibadah;

			$bmc_daftar = $wpdb->get_row( "SELECT * FROM wp_bmc_daftar WHERE id =".$id_bmc_daftar);

			$minggutgl = date("Y-m-d",strtotime($bmcconfirmasi->minggu_tgl));
			$name = $bmc_daftar->nama;
			$telp = $bmc_daftar->nomor_telp;
			$TotalOrang = $bmc_daftar->total_dewasa + $bmc_daftar->total_anak;
			$TglPernyataan = $bmc_daftar->tgl_pernyataan;		
			$TglttdAmrik = date("m/d/Y g:i A",strtotime($TglPernyataan));

			echo "<div class='admin_register'>";
			echo ($bmcconfirmasi->status=="batal")?"<b>Nomor Konfirmasi ini sudah di batalkan</b><br>":"<b>Success Terdaftar</b><br>";
			echo "Nama: ".$name."<br>";
			echo "Telp: ".$telp."<br>";
			echo "Total Orang: ".$TotalOrang."<br>";
			echo "Total Dewasa: ".$bmc_daftar->total_dewasa."<br>";
			echo "Total Anak: ".$bmc_daftar->total_anak."<br>";
			echo "Terdaftar tanggal: ".$TglttdAmrik."<br></div>";
			//echo "Nama: ".$name."<br>";
			echo '<a href="?cancelid='.$idbmcconfirmasi.'"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Cancel Pendaftar ini</button></a> ';
			
			echo '<a href="https://register.bethanymiraclecenter.org/wp-admin/"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Kembali</button></a><br><br>';		
			https://register.bethanymiraclecenter.org/wp-login.php?action=logout
			echo "</center><br><br><br><br></div>";
		}
		echo "</div>";
		}
		
	
}
/* 
	$to = "armageddon_retail@yahoo.com";
	$subject = "Registrasi";
	$message = "Test action mantap gak?";
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= "From: bmcdaftar@gmail.com" . "\r\n";
	//$headers .= "CC: armageddon_retail@yahoo.com";

	if(wp_mail($to,$subject,$message,$headers)){
		echo "terkrim";
	}else{
		echo "tidak terkrim";	
	}
	 */
	 
// Add custom Theme Functions here

// define the wpcf7_is_tel callback 
function custom_filter_wpcf7_is_tel( $result, $tel ) { 
  //$result = preg_match( '/^\(?\+?([0-9]{1,4})?\)?[-\. ]?(\d{10})$/', $tel );
  $result = preg_match('/^[0-9]{10}+$/', $tel);
  return $result; 
}

add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );

function qr_code_image($nomor_con){
	require_once (ABSPATH.'qr-code/vendor/autoload.php');

	$barcode = new \Com\Tecnick\Barcode\Barcode();
	$targetPath = "/qr-code/qr-code/";
		
	if (! is_dir($targetPath)) {
			mkdir($targetPath, 0777, true);
	}
		$bobj = $barcode->getBarcodeObj('QRCODE,H', $nomor_con, - 16, - 16, 'black', array(
			- 1,
			- 1,
			- 1,
			- 1
		))->setBackgroundColor('#f0f0f0');
		
		$imageData = $bobj->getPngData();
		$timestamp = time();
		
	file_put_contents(ABSPATH. $targetPath . $timestamp . '.png', $imageData);	
	$image = '<img src="'.get_home_url().$targetPath . $timestamp.'.png" width="250px" height="250px">';
	//$image = '<img src=https://register.bethanymiraclecenter.org'.$targetPath . $timestamp.'.png width="250px" height="250px">';
	return $image;
}

function bmc_save_daftar($wpcf7) {
global $wpdb;
date_default_timezone_set("America/New_York");
 
	$form = WPCF7_Submission::get_instance();
	if ( $form ) {
		$data = $form->get_posted_data();
		//echo "<pre>"; print_r($data); exit;
		$name = trim($data['your-name']);
		$nomor_telp = trim($data['your-phone']);
		$email = trim($data['your-email']);
		$total_dewasa = trim($data['total_dewasa']);
		if(!empty($data['tanggal_ibadah'])){
			$minggutgl = date("Y-m-d",strtotime($data['tanggal_ibadah']));
		}else{
			$minggutgl = date("Y-m-d",strtotime('next sunday'));
		}
		$tglamrik = date("m/d/Y",strtotime($minggutgl));
		$total_anak = empty($data['total_anak'])?0:trim($data['total_anak']);
		$pesan = trim($data['your-message']);
		$total_semua = $total_dewasa+$total_anak;
		$register=FALSE;		

		//if(empty($total_anak)){$total_anak=0;}

		if($data['jam_ibadah']=="Jam 8 pagi"){
			$jam_ibadah=8;
			$jamibadah="Jam 8 pagi";
		}elseif($data['jam_ibadah']=="Jam 10 pagi"){
			$jam_ibadah=10;
			$jamibadah="Jam 10 pagi";
		}elseif($data['jam_ibadah']=="Jam 12 siang"){
			$jam_ibadah=12;
			$jamibadah="Jam 12 siang";
		}elseif($data['jam_ibadah']=="Jam 7 malam"){
			$jam_ibadah=7;
			$jamibadah="Jam 7 malam";
		}

		if($data['pernyataan']==1){ $pernyataan = "iya"; $tglpernyataan = date("Y-m-d H:i:s"); }else{ $pernyataan = "tidak"; $tglpernyataan = ""; }
		
		$to=$email;

		$bmc_daftar = $wpdb->get_row( "SELECT * FROM wp_bmc_daftar WHERE nomor_telp ='".$nomor_telp."'");		
		$nomor_confirmasi = $wpdb->get_var( "SELECT max(id)+1 FROM wp_bmc_confirmasi").rand(1000,9999);
		
		if($bmc_daftar->minggu_tgl != $minggutgl){
				$id_bmc_daftar = $bmc_daftar->id;			
				$register=TRUE;				
		}
	
		if($bmc_daftar->minggu_tgl == $minggutgl){
			$id_bmc_daftar = $bmc_daftar->id;
			$nomor_confirmasi123 = $wpdb->get_row( "SELECT * FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."' AND status IS NULL");
			
			$totaldaf = $wpdb->get_var( "SELECT count(id) FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."' AND status IS NULL");
			
			if($totaldaf==0){
				$register=TRUE;
			}

			if($nomor_confirmasi123->jam_ibadah != $jam_ibadah && $bmc_daftar->pekerja == 1 && $totaldaf <= 2){
				$register=TRUE;
			}
			
		}
		$url_confirmasi = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$nomor_confirmasi;
		$imgqrcode = qr_code_image($url_confirmasi);
		
		$subject = "Registrasi";
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= "From: bmcdaftar@gmail.com" . "\r\n";
		//$headers .= "CC: armageddon_retail@yahoo.com";
		if(empty($bmc_daftar)){
			$wpdb->insert('wp_bmc_daftar', array(
				'nama' => $name,
				'nomor_telp' => $nomor_telp,
				'email' => $email,
				'total_dewasa' => $total_dewasa,
				'total_anak' => $total_anak,
				'jam_ibadah' => $jam_ibadah,
				'pesan' => $pesan,
				'pernyataan' => $pernyataan,
				'tgl_pernyataan' => $tglpernyataan,		
				'minggu_tgl' => $minggutgl,
				'source' => "website",
				'status' => 'registrasi_data_diterima',
				'gmail_telp' => '',
				'created_at' => date("Y-m-d H:i:s")
			));
			$id_bmc_daftar = $wpdb->insert_id;
			if(preg_match("/(Iya|iya)/i", $pernyataan) && $register==FALSE){								
				//$wpdb->query( $wpdb->prepare( "INSERT INTO wp_bmc_confirmasi (id_bmc_daftar, jam_ibadah, total, minggu_tgl, confirmation_number) VALUES ($id_bmc_daftar, $jam_ibadah, $total_semua,'$minggutgl', $nomor_confirmasi) ON DUPLICATE KEY UPDATE id_bmc_daftar = $id_bmc_daftar, jam_ibadah = $jam_ibadah, total = $total_semua, minggu_tgl = '$minggutgl', confirmation_number = $nomor_confirmasi"));
				
				$wpdb->query( $wpdb->prepare( "INSERT INTO wp_bmc_confirmasi (id_bmc_daftar, jam_ibadah,	total_dewasa,	total_anak,	total,	minggu_tgl,	confirmation_number)VALUES(	$id_bmc_daftar,	$jam_ibadah, $total_dewasa, $total_anak, $total_semua, '$minggutgl', $nomor_confirmasi) ON DUPLICATE KEY UPDATE id_bmc_daftar = $id_bmc_daftar,	jam_ibadah = $jam_ibadah,	total_dewasa = $total_dewasa, total_anak = $total_anak, total = $total_semua, minggu_tgl = '$minggutgl', confirmation_number = $nomor_confirmasi"));

				$NomorConfirmasi123 = $wpdb->get_var( "SELECT confirmation_number FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."' AND status IS NULL");
		
				$url_confirmasi123 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi123;
				$imgqrcode123 = qr_code_image($url_confirmasi123);
				
				$message = "<html><head><title>Registrasi BMC</title></head><body>Terima kasih $name atas pendaftaran Anda.<br><br>$imgqrcode123<br><br>Nomor Confirmasi anda: <b>$NomorConfirmasi123</b><br><br>Diharap Datang 30 Menit sebelum Ibadah Dimulai<br><br>Sampai bertemu tanggal $tglamrik $jamibadah total yang hadir: $total_semua <br><br>Tuhan Memberkati.</body></html>";

				if(!empty($to)){
					wp_mail($to,$subject,$message,$headers);
				}				
			}
		//}elseif($bmc_daftar->minggu_tgl != $minggutgl){
		}elseif($register==TRUE || $bmc_daftar->pekerja == 1){			
			//echo $id_bmc_daftar->minggu_tgl." - Keren- ".$minggu_tgl; exit;		
			$name = $bmc_daftar->nama;
			$wpdb->update('wp_bmc_daftar', array(
				'total_dewasa' => $total_dewasa,
				'total_anak' => $total_anak,
				'jam_ibadah'=> $jam_ibadah,
				'pesan' => $pesan,
				'pernyataan' => $pernyataan,
				'tgl_pernyataan' => $tglpernyataan,
				'minggu_tgl'=> $minggutgl,
				'status' => 'data_telah_diupdate'),
			array('nomor_telp'=>$nomor_telp));
				if(preg_match("/(Iya|iya)/i", $pernyataan) && $register==TRUE){
				//if(preg_match("/(Iya|iya)/i", $pernyataan)){
				//$wpdb->query( $wpdb->prepare( "INSERT INTO wp_bmc_confirmasi (id_bmc_daftar, jam_ibadah, total, minggu_tgl, confirmation_number) VALUES ($id_bmc_daftar, $jam_ibadah, $total_semua,'$minggutgl', $nomor_confirmasi) ON DUPLICATE KEY UPDATE id_bmc_daftar = $id_bmc_daftar, jam_ibadah = $jam_ibadah, total = $total_semua, minggu_tgl = '$minggutgl', confirmation_number = $nomor_confirmasi"));
				
				//echo "INSERT INTO wp_bmc_confirmasi (id_bmc_daftar, jam_ibadah,	total_dewasa,	total_anak,	total,	minggu_tgl,	confirmation_number)VALUES(	$id_bmc_daftar,	$jam_ibadah, $total_dewasa, $total_anak, $total_semua, '$minggutgl', $nomor_confirmasi) ON DUPLICATE KEY UPDATE id_bmc_daftar = $id_bmc_daftar,	jam_ibadah = $jam_ibadah,	total_dewasa = $total_dewasa, total_anak = $total_anak, total = $total_semua, minggu_tgl = '$minggutgl', confirmation_number = $nomor_confirmasi"; exit;
				
				$wpdb->query( $wpdb->prepare( "INSERT INTO wp_bmc_confirmasi (id_bmc_daftar, jam_ibadah,	total_dewasa,	total_anak,	total,	minggu_tgl,	confirmation_number)VALUES(	$id_bmc_daftar,	$jam_ibadah, $total_dewasa, $total_anak, $total_semua, '$minggutgl', $nomor_confirmasi) ON DUPLICATE KEY UPDATE id_bmc_daftar = $id_bmc_daftar,	jam_ibadah = $jam_ibadah,	total_dewasa = $total_dewasa, total_anak = $total_anak, total = $total_semua, minggu_tgl = '$minggutgl', confirmation_number = $nomor_confirmasi"));

				$NomorConfirmasi123 = $wpdb->get_var( "SELECT confirmation_number FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND jam_ibadah =".$jam_ibadah." AND minggu_tgl ='".$minggutgl."' AND status IS NULL");
		
				$url_confirmasi123 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi123;
				$imgqrcode123 = qr_code_image($url_confirmasi123);

				$message = "<html><head><title>Registrasi BMC</title></head><body>Terima kasih $name atas pendaftaran Anda.<br><br>$imgqrcode123<br><br>Nomor Confirmasi anda: <b>$NomorConfirmasi123</b><br><br>Diharap Datang 30 Menit sebelum Ibadah Dimulai<br><br>Sampai bertemu tanggal $tglamrik $jamibadah total yang hadir: $total_semua <br><br>Tuhan Memberkati.</body></html>";

				if(!empty($to)){
					wp_mail($to,$subject,$message,$headers);
				}	
			}
		
		}elseif(!empty($bmc_daftar) && $bmc_daftar->minggu_tgl == $minggutgl && 1==2 ){
			//add_filter('wpcf7_skip_mail', 'abort_mail_sending');
			
			//$nomor_confirmasi1123 = $wpdb->get_var( "SELECT confirmation_number FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."'");

			//echo "123<pre> ".$id_bmc_daftar; print_r($nomor_confirmasi1123); exit;
			//$imgqrcode = qr_code_image($nomor_confirmasi1123);
			
			/*  		
			$err_msg = "Anda sudah terdaftar ini adalah nomor confirmasi anda: <b> $nomor_confirmasi1123";
			$msgs = $wpcf7->prop('messages');
			$msgs['mail_sent_ok'] = $err_msg;
			$wpcf7->set_properties(array('messages' => $msgs));
			add_filter('wpcf7_skip_mail', 'abort_mail_sending');
			return $wpcf7;
			*/
			
		}
	
	}

}

add_action('wpcf7_before_send_mail','bmc_save_daftar');
//add_action('wpcf7_before_send_mail','wpcf7_register_user132123');
 
function abort_mail_sending($contact_form){
	return true;
}

//add_filter( 'wpcf7_before_send_mail', 'bmc_save_daftar', 10, 2 );
//add_action( 'wpcf7_before_send_mail', 'bmc_save_daftar' );

function bmc_keren() {
	global $wpdb;
	
	//echo "<pre>";
	//print_r($_SERVER); exit;	
	
	//home3/bethany3/public_html/bmcregister/wp-settings.php
	date_default_timezone_set("America/New_York");	
	//require __DIR__ . 'qr-code/vendor/autoload.php';

?>
<style>
body{
	font-size:30px;
}
	.wpcf7-form{
		font-size:30px;
	}
	.outer-div
	{
		margin: 0 auto;
		width:480px;
		padding: 30px;
		border: 1px solid green;
		height:auto;	 	 
	}
	.title{
		font-size:18px;
		font-weight:bold;
		clear:both;
	}
	.inner-div
	{
		font-size:20px;
		padding:3px 0px 3px 0px;
		display: inline-block;
		margin: 0 auto;
		border: 1px solid grey;
		height:auto;	 	 
		width: 480px;
	}
	
	div.wpcf7-validation-errors, div.wpcf7-acceptance-missing {
		border: 3px solid #b20000;
		color: #b20000;
	}
	.msg_confirm{
		width: 60%;		
	}
	.qrcode td{
		text-align:center;
	}
	.tanggal_ibadah{display:none !important;}
	
	@media screen and (max-width: 600px) 
	{
		.wpcf7-form{
			font-size:30px;
		}
		.msg_confirm{
			width: 90%;		
		}
		
		.qr_code_scan_bmc
		{
			width:90% !important;
			height:auto;
		}
		.logo{
			width:200px;
		}
		.header-wrapper{
			display:none;
		}
		.wpcf7-submit{
			float:right;
			width:80%;
			margin: 0 auto;		
		}
		.inner-div
		{
			padding:3px 0px 3px 0px;
			font-size:25px;
			display: inline-block;
			margin: 0 auto;
			border: 1px solid grey;
			height:auto;	 	 
			width: 380px;
		}
		.tanggal_ibadah{display:none !important;}	
	}
	</style>
	<center><a href="https://www.bethanymiraclecenter.org"><img class="logo" width="300" height="auto" src="https://register.bethanymiraclecenter.org/wp-content/uploads/2020/06/BMC-Logo-HD-1400x551.png"></a>		
	<hr>
<?php
/* Array
stdClass Object
(
    [id] => 9
    [nama] => Wiharta Lim
    [nomor_telp] => 6105131608
    [email] => guemau@gmail.com
    [total_dewasa] => 2
    [total_anak] => 1
    [jam_ibadah] => 8
    [pesan] => 
    [pernyataan] => iya
    [tgl_pernyataan] => 2020-06-19 21:04:44
    [minggu_tgl] => 2020-06-21
    [source] => website
    [status] => registrasi_data_diterima
    [gmail_telp] => 
    [created_at] => 2020-06-18 21:04:41
)
*/
	//echo "<pre>"; print_r($_SERVER); exit;
	if($_SERVER['REDIRECT_URL'] == "/bmcinfo/"){	
		//date("m/d/Y g:i A")
		$nomor_telp = trim($_GET['your-phone']);
	//$minggutgl = date("Y-m-d",strtotime('next sunday'));
	$now = date("Y-m-d");

	$minggutgl = $wpdb->get_var( "SELECT max(minggu_tgl) FROM itung_total");
	$tglamrik = date("m/d/Y",strtotime($minggutgl));
		
	$bmc_daftar = $wpdb->get_row( "SELECT * FROM wp_bmc_daftar WHERE nomor_telp ='".$nomor_telp."'");
	if(empty($bmc_daftar)){
		echo "<div>Anda belum terdaftar, silahkan:<br><br></div>";
		echo '<a href="https://register.bethanymiraclecenter.org"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Daftar Disini</button></a><br><br>';
		exit;
	}

		$id_bmc_daftar = $bmc_daftar->id;
		$name = $bmc_daftar->nama;
		$TglPernyataan = $bmc_daftar->tgl_pernyataan;		
		$TglttdAmrik = date("m/d/Y g:i A",strtotime($TglPernyataan));
		$result = $wpdb->get_results( "SELECT * FROM wp_bmc_confirmasi WHERE id_bmc_daftar = $id_bmc_daftar AND status IS NULL AND minggu_tgl >= '".$now."'");
		echo "<table class='qrcode'><tr>";		
		foreach ($result as $item){			
			$tgl_ibadah = date("m/d/Y",strtotime($item->minggu_tgl));
			if($item->jam_ibadah == 8){
				$TotalOrang8 = $item->total_dewasa + $item->total_anak;
				$NomorConfirmasi8 = $item->confirmation_number;								
				$url_confirmasi8 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi8;
				$imgqr8 = qr_code_image($url_confirmasi8);
		$imgqrcode8 = "<td>".$imgqr8."<br>".$NomorConfirmasi8."</td>";				
			    $jam_ibadah8 = "Tanggal ".$tgl_ibadah." Jam 8 pagi Total orang: " . $TotalOrang8." Nomor Confirmasi: ".$NomorConfirmasi8;				
			}elseif($item->jam_ibadah == 10){
				$TotalOrang10 = $item->total_dewasa + $item->total_anak;
				$NomorConfirmasi10 = $item->confirmation_number;
				$url_confirmasi10 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi10;
				$imgqr10 = qr_code_image($url_confirmasi10);
		$imgqrcode10 = "<td>".$imgqr10."<br>".$NomorConfirmasi10."</td>";
		$jam_ibadah10 = " & Tanggal ".$tgl_ibadah." Jam 10 pagi Total orang: ".$TotalOrang10." Nomor Confirmasi: ".$NomorConfirmasi10;
			}elseif($item->jam_ibadah == 12){
				$TotalOrang6 = $item->total_dewasa + $item->total_anak;
				$NomorConfirmasi6 = $item->confirmation_number;
				$url_confirmasi6 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi6;
				$imgqr6 = qr_code_image($url_confirmasi6);
		$imgqrcode6 = "<td>".$imgqr6."<br>".$NomorConfirmasi6."</td>";				
				$jam_ibadah6 = " & Tanggal ".$tgl_ibadah." Jam 12 siang Total orang: ".$TotalOrang6." Nomor Confirmasi: ".$NomorConfirmasi6;
			}elseif($item->jam_ibadah == 7){
				$TotalOrang7 = $item->total_dewasa + $item->total_anak;
				$NomorConfirmasi7 = $item->confirmation_number;
				$url_confirmasi7 = "https://register.bethanymiraclecenter.org/wp-admin/?confirmation=".$NomorConfirmasi7;
				$imgqr7 = qr_code_image($url_confirmasi7);
		$imgqrcode7 = "<td>".$imgqr7."<br>".$NomorConfirmasi7."</td>";				
				$jam_ibadah7 = " & Tanggal ".$tgl_ibadah." Jam 7 malam Total orang: ".$TotalOrang7." Nomor Confirmasi: ".$NomorConfirmasi7;
			}			
		}
		
		$Textnya = $jam_ibadah8.$jam_ibadah10.$jam_ibadah6.$jam_ibadah7; 
		$imgqrcode = $imgqrcode8.$imgqrcode10.$imgqrcode6.$imgqrcode7."</tr></table>";
		
		//$TotalOrang = $bmc_daftar->total_dewasa + $bmc_daftar->total_anak;

		$tglskr = new DateTime('now');
		$tglttd = new DateTime($TglPernyataan);

		$minutes = $tglskr->diff($tglttd)->i;
				
		//$NomorConfirmasi = $wpdb->get_var( "SELECT confirmation_number FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."' AND status IS NULL");

		if(empty($Textnya)){
			echo "<div>Anda belum terdaftar, silahkan:<br><br></div>";
			echo '<a href="https://register.bethanymiraclecenter.org"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Daftar Disini</button></a><br><br>';
			exit;
		}
		
		//$jam_ibadah = $wpdb->get_var( "SELECT jam_ibadah FROM wp_bmc_confirmasi WHERE id_bmc_daftar =".$id_bmc_daftar." AND minggu_tgl ='".$minggutgl."'");

		//echo "123<pre> ".$id_bmc_daftar; print_r($nomor_confirmasi1123); exit;

		if($minutes < 5){
			$message = "<div class='msg_confirm'>Terima kasih $name anda sudah mendaftar untuk $Textnya <br>$imgqrcode Diharap Datang 30 Menit sebelum Ibadah Dimulai.<br>Terima Kasih dan Tuhan memberkati.";
		}else{
			$message = "<div class='msg_confirm'>$name anda sudah terdaftar pada tanggal $TglttdAmrik untuk $Textnya <br>$imgqrcode Diharap Datang 30 Menit sebelum Ibadah Dimulai.<br>Terima Kasih dan Tuhan memberkati.";
		}
		echo $message."<br>";
		if($_SERVER['HTTP_REFERER']=="https://register.bethanymiraclecenter.org/bmcnew/"){
			echo '<a href="https://register.bethanymiraclecenter.org/bmc/"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Kembali</button></a><br><br>';			
		}else{
			echo '<a href="https://register.bethanymiraclecenter.org"><button style="font-size: 28px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Kembali</button></a><br><br>';
		}
		
		echo '<iframe width="560" height="315" src="https://www.youtube.com/embed/y4QxIVY7gkE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
		
		echo "</center><br><br><br><br>";		
		exit;
	}
		
	if($_SERVER['REQUEST_URI'] == "/bmcnew/"){
		//$minggutgl = $wpdb->get_var( "SELECT max(minggu_tgl) FROM itung_total");
		$minggutgl = date("Y-m-d",strtotime('7/25/2020'));
		$now = date("Y-m-d");
		$jam = "Jam 7 malam";
		$tglamrik = date("m/d/Y",strtotime($minggutgl));		
		$currentDate = date('m/d/Y g:i A');
		$close = FALSE;
//$currentDate = date('m/d/Y', strtotime($currentDate));
$startdate = date("D m/d/Y g:i A",strtotime('last saturday'));
$harinya = date("D m/d/Y g:i A",strtotime('last saturday'));
$enddate = date("D m/d/Y g:i A",strtotime('monday this week'));
//if(date('D') == "Sat" && date('G') >= 12){ $close = TRUE; }
//if(date('D') == "Sun" ||  date('D') == "Mon"){  $close = TRUE; }

if($close==TRUE){
	echo "<br>Tutup";
}
	?>
		<h1>Pendaftaran DOA PENGURAPAN BMC untuk <?=$tglamrik." ".$jam ?><br><small>Khusus hanya bagi Pelayan Tuhan BMC Philadelphia.</small></h1>
		<?php
		$bukajam = date("m/d/Y g:i A",strtotime("06/25/2020 5:00 PM"));
		$saatini = date("m/d/Y g:i A");
		//print_r($_SERVER['REMOTE_ADDR']); exit;
		
		//if($bukajam >= $saatini && $_SERVER['REMOTE_ADDR'] != "96.245.83.66"){ echo "Pendaftaran di buka pada tanggal: ".$bukajam."<br><br><br>"; exit; }

		$result = $wpdb->get_results( "SELECT * FROM wp_bmc_confirmasi WHERE minggu_tgl ='".$minggutgl."' AND status IS NULL ");

		$totalDewasa6 = 0;
		$totalAnak6 = 0 ;	
	
	foreach ($result as $item){
		//echo "Keren"; print_r($item); exit;
		//$value = unserialize($item->form_value);
		//print_r($value['jam_ibadah'][0]); exit;
		if($item->jam_ibadah=="8"){
			$totalDewasa8=$totalDewasa8 + $item->total_dewasa;
			$totalAnak8=$totalAnak8 + $item->total_anak;	
		}elseif($item->jam_ibadah=="10"){
			$totalDewasa10=$totalDewasa10 + $item->total_dewasa;
			$totalAnak10=$totalAnak10 + $item->total_anak;		
		}else{
			$totalDewasa6=$totalDewasa6 + $item->total_dewasa;
			$totalAnak6=$totalAnak6 + $item->total_anak;					
		}
	}
	$totalSemua8 = $totalDewasa8+$totalAnak8;
	$totalSemua10 = $totalDewasa10+$totalAnak10;
	$totalSemua6 = $totalDewasa6+$totalAnak6;

		echo "<div classs='outer-div'>";
		echo "<div class='inner-div title'>Pendaftaran Untuk Tanggal: " . $tglamrik." (Max 120 orang per jam ibadah)</div><br>";

		echo "<div class='inner-div' ><b>Total Ibadah ".$jam." terdaftar: ".$totalSemua6;
		echo ($totalSemua6 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";

		echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa6."  -  ";
		echo "Total Anak: ".$totalAnak6."</div><br>";
		
		echo "<div><b>Hari ini: ".date("m/d/Y g:i A")."</b></div>";
		echo "</div></center>";
		if ($totalSemua6 > 120){
			?>
		<script>
		window.onload = function(){
		  isi = document.getElementById("jam_ibadah123").options[1].disabled = true;
		};	
			//alert(isi);
			//var x = document.getElementById("jam_ibadah123").options[2].disabled = true;
		</script>
			<?php
			//echo "<center><br><br><h1>Mohon maaf Pendaftaran sudah di tutup Karena yang sudah melaupaui total capasitas.</h1></center>";
		}

$close = FALSE;
if(date('D') == "Sat" && date('G') >= 12){ $close = TRUE; }
if(date('D') == "Sun" ||  date('D') == "Mon"){  $close = TRUE; }
if($minggutgl >= $now){ echo "Keren"; $close = TRUE; }

 
if ($close == TRUE){
		echo "<style>.wpcf7-form{display:none;}</style>";
		echo "<center><br><br><h1>Mohon maaf. Pendaftaran saat ini belum dibuka<br>Terima Kasih dan Tuhan Memberkati.</h1></center>";
}
	
	}else{
	$minggutgl = date("Y-m-d",strtotime('next sunday'));
	$result = $wpdb->get_results( "SELECT * FROM itung_total WHERE minggu_tgl ='".$minggutgl."' AND status IS NULL ");

	$totalDewasa8 = 0;
	$totalAnak8 = 0 ;
	$totalDewasa10 = 0;
	$totalAnak10 = 0 ;	
	$totalDewasa6 = 0;
	$totalAnak6 = 0 ;	

/* KerenstdClass Object ( [id] => 60 [Nama] => Okky [Telp] => 2159171868 [Email] => oqy1977@yahoo.com [jam_ibadah] => 6 [Pernyataan] => iya [minggu_tgl] => 2020-07-05 [Sumber] => website [No Konfirmasi] => 539643 [status] => [Edit] => 53 [total_dewasa] => 1 [total_anak] => 0 [pesan] => AVK [tgl_pernyataan] => 2020-06-28 10:13:01 [gmail_telp] => ) */
	
	foreach ($result as $item){
		//echo "Keren"; print_r($item); exit;
		//$value = unserialize($item->form_value);
		
		if($item->jam_ibadah=="8"){
			$totalDewasa8 = $totalDewasa8 + $item->total_dewasa;
			$totalAnak8 = $totalAnak8 + $item->total_anak;	
		}elseif($item->jam_ibadah=="10"){
			$totalDewasa10 = $totalDewasa10 + $item->total_dewasa;
			$totalAnak10 = $totalAnak10 + $item->total_anak;
		}elseif($item->jam_ibadah=="12"){
			$totalDewasa6 = $totalDewasa6 + $item->total_dewasa;
			$totalAnak6 = $totalAnak6 + $item->total_anak;
		}
	}
	$totalSemua8 = $totalDewasa8+$totalAnak8;
	$totalSemua10 = $totalDewasa10+$totalAnak10;
	$totalSemua6 = $totalDewasa6+$totalAnak6;
	?>

	<script>
	function copy_bmc() {
		var copyText = document.getElementById("myInput");
		copyText.select();
		copyText.setSelectionRange(0, 99999);
		document.execCommand("copy");
		alert("Copy Halaman BMC Registrasi: " + copyText.value);
	}
	//window.onload = function(){isi = document.getElementById("jam_ibadah123").options[3].disabled = true;};
	</script>
	
	<?php

	//Doa Pekerja Ubah disini
	//echo "<a href='https://register.bethanymiraclecenter.org/bmcnew/'>Pendaftaran untuk doa Pekerja Sabtu 7/25/2020</a>";
	//echo '<a href="https://register.bethanymiraclecenter.org/bmcnew/">&nbsp;&nbsp;<button style="font-size: 18px; text-decoration: none; background-color: black; color: white; text-align: center; box-sizing: border-box;">Click Disini</button></a><br>';
	
	echo "Pendaftaran dibuka setiap Selasa sampai Sabtu jam 12 Siang<br>(Segera Ditutup saat Kapasitas Penuh) untuk informasi dapat menghubungi:<br>Mimianna 215 500 2805 atau Monica 917 498 2067 - (Sabtu jam 7 pagi - 12 siang)<br>";
	//echo "Selasa: ".date("m/d/Y",strtotime('tuesday this week'));
	//echo " - Minggu:".date("m/d/Y",strtotime('sunday'));
	//echo "Hari: " . date("l m/d/Y H:i")."<br>";
	echo "<div classs='outer-div'>";
	echo "<div class='inner-div title'>Pendaftaran Untuk Minggu Tanggal: " . date("m/d/Y",strtotime('next sunday'))." (Max 120 orang per jam ibadah)</div><br>";
	echo "<div class='inner-div' ><b>Total Ibadah Jam 8 pagi terdaftar: ".$totalSemua8;
	echo ($totalSemua8 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";

	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa8."  -  ";
	echo "Total Anak: ".$totalAnak8."</div><br>";

	echo "<div class='inner-div' ><b>Total Ibadah Jam 10 pagi terdaftar: ".$totalSemua10;
	echo ($totalSemua10 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";

	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa10."  -  ";
	echo "Total Anak: ".$totalAnak10."</div><br>";

	echo "<div class='inner-div'><b>Total Ibadah Jam 12 siang terdaftar: ".$totalSemua6;
	echo ($totalSemua6 >= 120) ? " (Sudah Full)</b></div>" : "</b></div>";
	echo "<div class='inner-div'>Total Dewasa: ".$totalDewasa6."  -  ";
	echo "Total Anak: ".$totalAnak6."</div>";
	echo "<div><b>Hari ini: ".date("m/d/Y g:i A")."</b></div>";
	
	echo "</div></center>";
	if ($totalSemua8 >= 120){
		?>
	<script>
	window.onload = function(){
	  isi = document.getElementById("jam_ibadah123").options[1].disabled = true;
	};	
	</script>
		<?php
	}

	if ($totalSemua10 >= 120){
		?>
	<script>
	window.onload = function(){
	  isi = document.getElementById("jam_ibadah123").options[2].disabled = true;
	};	
	</script>
		<?php
	}

	if ($totalSemua6 >= 120){
		?>
	<script>
	window.onload = function(){
	  isi = document.getElementById("jam_ibadah123").options[3].disabled = true;
	};	
	</script>
		<?php
	}
	

$currentDate = date('m/d/Y');
//$currentDate = date('m/d/Y', strtotime($currentDate));
$startdate = date("m/d/Y",strtotime('last sunday'));
$enddate = date("m/d/Y",strtotime('monday this week'));
//echo $startdate." - ".$enddate;
   
/* if (($currentDate >= $startdate) && ($currentDate <= $enddate) || 1==1){
		echo "<style>.wpcf7-form{display:none;}</style>";
		echo "<center><br><br><h1>Mohon maaf pendaftaran belum di buka. Pendaftaran hanya dibuka setiap Selasa sampai Minggu.<br>Terima Kasih dan Tuhan Memberkati.</h1></center>";
}
 */
$close = FALSE;
if(date('D') == "Sat" && date('G') >= 12){ $close = TRUE; }
if(date('D') == "Sun" ||  date('D') == "Mon"){  $close = TRUE; }
 
if ($close == TRUE){
		echo "<style>.wpcf7-form{display:none;}</style>";
		echo "<center><br><br><h1>Mohon maaf. Pendaftaran saat ini belum dibuka<br>Terima Kasih dan Tuhan Memberkati.</h1></center>";
}


	if ($totalSemua8 >= 120 && $totalSemua10 >= 120){
		echo "<style>.wpcf7-form{display:none;}</style>";
		echo "<center><br><br><h1>Mohon maaf pendaftaran sudah di tutup. Total pendaftar sudah melampaui kapasitas.<br>Terima Kasih dan Tuhan Memberkati.</h1></center>";
	}
}
}
add_filter( 'flatsome_before_page', 'bmc_keren', 10, 2 );

// Prevent Multi Submit on WPCF7 forms
add_action( 'wp_footer', 'mycustom_wp_footer' );

function mycustom_wp_footer() {
?>

<script type="text/javascript">
var disableSubmit = false;
jQuery('input.wpcf7-submit[type="submit"]').click(function() {
    //jQuery(':input[type="submit"]').hide()
	jQuery(':input[type="submit"]').attr('value',"Sending...")
    if (disableSubmit == true) {
        return false;
    }
    disableSubmit = true;
    return true;
})
  
var wpcf7Elm = document.querySelector( '.wpcf7' );
wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
    jQuery(':input[type="submit"]').attr('value',"send")
    disableSubmit = false;
}, false );
</script>
<?php
}
?>