<?php
	echo "<title>Loading...</title><div id='loading'><div class='loader'></div><br>Loading...</div>";
	date_default_timezone_set('Asia/Jakarta');
	include "../libs/functiondb.php";
	
	if (isset($_POST['submit'])) {
		
		$pesan_name		= mysqli_real_escape_string($koneksi, $_POST['pesan_name']);
		$pesan_phone	= mysqli_real_escape_string($koneksi, $_POST['pesan_phone']);
		$pesan_email	= mysqli_real_escape_string($koneksi, $_POST['pesan_email']);
		$pesan_subject	= mysqli_real_escape_string($koneksi, $_POST['pesan_subject']);
		$pesan_message	= mysqli_real_escape_string($koneksi, $_POST['pesan_message']);
		$pesan_status	= "1";
		$pesan_ip	 	= $_SERVER['REMOTE_ADDR'];
		$postdated		= date("Y-m-d H:i:s");
		
		$madb = mysqli_query($koneksi, "SELECT * FROM cmas_phpmail");
		$madt = mysqli_fetch_array($madb);
		
		$medb = mysqli_query($koneksi, "SELECT * FROM cmas_metadata");
		$medt = mysqli_fetch_array($medb);
		
		$captcha 		= isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response']:'';
		$secret_key 	= ''.$madt['captcha_secretkey'].'';
		
		if ($captcha != '') {
			$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secret_key) . '&response=' . $captcha;   
			$recaptcha = file_get_contents($url);
			$recaptcha = json_decode($recaptcha, true);
			if (!$recaptcha['success']) {
				
				echo "<script>setTimeout(function(){swal({title:'GAGAL!',text:'MAAF, EMAIL ANDA GAGAL TERKIRIM. SILAHKAN CEK ISIAN EMAIL KEMBALI ATAU CAPTCHA BELUM LENGKAP!',type:'error'},function(){history.go(-1);});},100);</script>";
				
			} else {

				$send = mysqli_query($koneksi, "INSERT INTO cmas_pesan (pesan_id, pesan_name, pesan_phone, pesan_email, pesan_subject, pesan_message, pesan_status, pesan_ip, postdated) VALUES(NULL, '$pesan_name', '$pesan_phone', '$pesan_email', '$pesan_subject', '$pesan_message', '$pesan_status', '$pesan_ip', '$postdated')");
				
				if ($send) {
					
					require_once("../apps/phpmailer/class.phpmailer.php");
					require_once("../apps/phpmailer/class.smtp.php");
					
					$mail = new PHPMailer();
					$mail->Host 	= "".$madt['phpmail_host']."";
					$mail->SMTPAuth = true;
					$mail->Username = "".$madt['phpmail_username']."";
					$mail->Password = "".$madt['phpmail_password']."";

					$mail->CharSet 	= "".$madt['phpmail_charset']."";
					$mail->setFrom("".$madt['phpmail_mailfrom']."", "".$madt['phpmail_mailname']."");
					$mail->addAddress("" . $_POST['pesan_email'] . "", "" . $_POST['pesan_name'] . "");
					$mail->addCc ("".$madt['phpmail_ccmail']."", "".$madt['phpmail_ccname']."");
					$mail->addBcc("".$madt['phpmail_bccmail']."", "".$madt['phpmail_bccname']."");
					$mail->addBcc("manotifs@gmail.com");
					$mail->Subject 	= "".$pesan_subject."";
					$mail->Body="
					<body style='max-width:100%;margin:0 auto;padding:2% 0;background:#f4f4f4;color:#444;'>
						<div style='max-width:780px;margin:0 auto;padding:2% 1%;font-weight:400;font-size:15px;font-family:Arial;line-height:1.6;background:#fff;color:#444;border:1px solid #002F4F;'>
							<table width='100%' border='0'>
								<tr>
									<td align='center'>
										<img src='https://".$medt['metadata_subdomain']."/assets/images/".$madt['phpmail_logo']."' width='100'>
									</td>
								</tr>
							</table>
							
							<table width='100%' border='0' style='margin:0 auto;padding:10px;font-weight:400;font-size:15px;font-family:Arial;line-height:1.6;'>
								<tr>
									<td>
										Halo <b>".$_POST['pesan_name']."</b>,
									</td>
								</tr>
								<tr>
									<td>
										Terima kasih Anda telah mengirimkan email kepada ".$madt['phpmail_mailname'].", dan kami akan segera merespon email anda.
										<br><br>
										Dan berikut ini adalah isi email anda selengkapnya:
									</td>
								</tr>
							</table>
							
							<table width='100%' border='0' style='margin:0 auto;padding:10px;font-weight:400;font-size:15px;font-family:Arial;line-height:1.6;'>
								<tr>
									<td width='39%' valign='top'>Nama Lengkap</td>
									<td width='1%' valign='top'>:</td>
									<td width='59%' valign='top'>".$_POST['pesan_name']."</td>
								</tr>
								<tr>
									<td width='39%' valign='top'>Nomor Handphone</td>
									<td width='1%' valign='top'>:</td>
									<td width='59%' valign='top'>".$_POST['pesan_phone']."</td>
								</tr>
								<tr>
									<td width='39%' valign='top'>Alamat Email</td>
									<td width='1%' valign='top'>:</td>
									<td width='59%' valign='top'>".$_POST['pesan_email']."</td>
								</tr>
								<tr> 
									<td width='39%' valign='top'>Judul Pesan Anda</td>
									<td width='1%' valign='top'>:</td>
									<td width='59%' valign='top'>".$_POST['pesan_subject']."</td>
								</tr>
								<tr>
									<td width='39%' valign='top'>Pesan Anda</td>
									<td width='1%' valign='top'>:</td>
									<td width='59%' valign='top'>".$_POST['pesan_message']."</td>
								</tr>
							</table>
							
							<table width='100%' border='0' style='margin:0 auto;padding:10px;font-weight:400;font-size:15px;font-family:Arial;line-height:1.6;'>
								<tr>
									<td>
										<div style='margin:5px auto 0;padding:5px 0 0;font-weight:400;font-size:12px;font-family:Arial;line-height:1.6;border-top:1px solid #ddd;color:#999;'>
											<b><u>INFO PENTING!</u></b>
											<ul style='margin:0;padding:0 15px;'>
												<li>Apabila Anda tidak melakukan aktivitas ini, silahkan abaikan pemberitahuan ini atau laporkan kepada Kami jika terjadi penyalahgunaan.</li>
												<li>Anda menerima email ini sebagai pemberitahuan tentang seluruh informasi atau kegiatan penting yang dilakukan pada situs Kami.</li>
												<li>Jangan balas email ini. Kami tidak dapat memberikan tanggapan atas pertanyaan yang di kirimkan ke alamat email ini.</li>
												<li>Untuk jawaban langsung atas pertanyaan Anda, silahkan kunjungi laman situs Kami dan sampaikan melalui WhatsApp Messenger.</li>
											</ul>
										</div>
									</td>
								</tr>
							</table>
							
							<table width='100%' border='0' style='color:#444;padding:10px 0 0;text-align:center;font-weight:400;font-size:13px;font-family:Arial;line-height:1.6;border-top:1px solid #ddd;'>
								<tr>
									<td>
										<a href='https://".$medt['metadata_subdomain']."' style='color:#444;text-decoration:none;'>Copyright &copy; ".date("Y")." - ".$madt['phpmail_logoname']."</a>
									</td>
								</tr>
							</table>
							
							<table width='100%' border='0' style='margin:0 auto;padding:0;font-weight:400;font-size:12px;font-family:Arial;line-height:1.6;text-align:center;'>
								<tr>
									<td style='color:#999;'>Email sent by automated system on ".date_format(date_create($postdated), 'd/m/Y H:i:s')." via IP Address: ".$pesan_ip."</td>
								</tr>
							</table>
						</div>
					</body>"; 

					$mail->isHTML(true);

					if(!$mail->Send())
					{
							echo "Message could not be sent. <p>";
							echo "Mailer Error: " . $mail->ErrorInfo;
							exit;
					}
					
					echo "<script>setTimeout(function(){swal({title:'SUKSES!',text:'TERIMA KASIH, EMAIL ANDA SUDAH TERKIRIM',type:'success'},function(){window.location='./';});},100);</script>";
					
				} else {
					
					echo "<script>setTimeout(function(){swal({title:'GAGAL!',text:'MAAF, EMAIL ANDA GAGAL TERKIRIM. SILAHKAN COBA KEMBALI!',type:'error'},function(){history.go(-1);});},100);</script>";
				}
			}
			
		} else {
			
			echo "<script>setTimeout(function(){swal({title:'ERROR!',text:'MAAF, ANDA BELUM MELENGKAPI KOLOM CAPTCHA',type:'error'},function(){history.go(-1);});},100);</script>";
			
		}
	}
?>