<?php
    include('../config.php');
    include('../fungsi.php');
    
    $data=mysqli_fetch_row(mysqli_query($con,"select * from akunn where id=1"));
    $telegram_id = $data[16];
    $secret_token = $data[17];
    
?>   
<!DOCTYPE html>
<html>
<head>
	<title>Scan QRCode Diskont</title>
	<link rel="stylesheet" type="text/css" href="diskon.css">
	<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
	
	<!-- bagian header template -->
	<header>
		<h1 class="judul">Scan QRCode Diskont</h1>
		<p class="deskripsi">Arahkan QRCode ke Kamera Laptop/HP</p>
	</header>
	<!-- akhir bagian header template -->
	
	<div class="wrap">
		<!-- bagian menu		 -->
		<nav class="menu">
			<ul>
				<li>
					<a href="#">Home</a>
				</li>
				
			</ul>
		</nav>
		<!-- akhir bagian menu -->

		<!-- bagian sidebar website -->
		<aside class="sidebar">
			<div class="widget">
				<h2>Logo</h2>
				<p>
				<img width="250" class="img-rounded" src="<?php echo '../'.$data[18]; ?>">
				</p>
			</div>
			<div class="widget">
				<h2>Kontak CS Kami</h2>
				<p><?php echo $data[10]; ?> <br>
				<?php echo $data[7]; ?> <br>
				</p>
			</div>
		</aside>
		<!-- akhir bagian sidebar website -->

		<!-- bagian konten Blog -->
		<div class="blog">
			<div class="conteudo">
			    
			    <?php
			    
			        $qr = $_POST['kode'];
                    $data = explode("-" , $qr);
                    $qr = $data['0'];
                    $email = $data['1'];
			        
                    $sql=mysqli_query($con, "SELECT * FROM qrcode WHERE kodeqr='$qr' and jenisqr= 0 and stsqr=1 and tglexqr > NOW()");
                    $d=mysqli_fetch_array($sql);

                    if(mysqli_num_rows($sql) < 1){
                        ?>
                        <div class="alert alert-danger">
                            <center>
                            <strong>Maaf, Diskont Sudah Tidak ada lagi atau QRCode Anda tidak terdaftar. </strong><br>
                            <i>Pastikan selalu mengecek email anda karena suatu saat kami akan mengirimkan QRCode Diskont lagi.</i>
                            </center>
                        </div>
                        <?php
                    }else{
                        
                        $id = $d['id'];
                        $judulqr = $d['judulqr'];
                        $pesanwa = $d['pesan1'];
                        
                        $cek=mysqli_query($con, "SELECT * FROM qrcode_detail WHERE idqr='$id' and email='$email'");
                        $dcek=mysqli_fetch_array($cek);
                        if(mysqli_num_rows($cek) < 1){
                        
                            mysqli_query($con,"INSERT INTO qrcode_detail VALUE (NULL,'".$id."','".$email."',NOW())");
                            
                            $member=mysqli_query($con, "SELECT * FROM prospek WHERE email='$email'");
                            $mcek=mysqli_fetch_array($member);
                            if(mysqli_num_rows($member) > 0){
                                $idmember=$mcek['ID'];
                                $nama=$mcek['nama'];
                                $email=$mcek['email'];
                                $hp=$mcek['hp'];
                            }
                            
                            $pesannn = $nama. ' dengan email '.$email.' baru saja divalidasi qrcode diskontnya untuk '.$judulqr;
                            $message_text = $pesannn;
                            sendMessage($telegram_id, $message_text, $secret_token);
                            
                            $notifwa=mysqli_fetch_row(mysqli_query($con,"select * from notifwa where id=1"));
                            IF($notifwa[8]==1){
                                If (strlen($hp)>9){
                                    $appid = $notifwa[5];
                                    $csid = $notifwa[6];
                                    $pesanwa = str_replace('[judul]',$judulqr,$pesanwa);
                                    $pesanwa = str_replace('[nama]',$nama,$pesanwa);
                            		$pesanwa = str_replace('[email]',$email,$pesanwa);
                            		$pesanwa = strip_tags($pesanwa);
                                    $hp =substr($hp, 1,12);
                                    $hp = '62'.$hp;
                                    sendWoowandroid($appid, $csid, $pesanwa, $hp);
                                }
                            } 
                            
                            
                        }   
                        
                    ?>
                    <table class="table table-bordered table-striped">
                        <tr>
                            <th>Judul</th>
                            <th>Keterangan</th>
                        </tr>
                        <tr>
                            <td><?php echo $d['judulqr']; ?></td>
                            <td><?php echo $d['ketqr']; ?></td>
                        </tr>
                    </table>
                    <?php } ?>
			        
			        <center><a class="btn btn-danger" href="./diskon.php">Scan QRCode Kembali</a></center>
			    
			</div>
			
		</div>
		<!-- akhir bagian konten -->
	</div>

</body>
</html>