<?php
    include('../config.php');
    include('../fungsi.php');
    
    $akun=mysqli_fetch_row(mysqli_query($con,"select * from akunn where id=1"));
    $telegram_id = $akun[16];
    $secret_token = $akun[17];
    
    $qr = $_POST['kode'];
    $data = explode("-" , $qr);
    $qr = $data['0'];
    $email = $data['1'];
    
    $sql=mysqli_query($con, "SELECT * FROM qrcode WHERE kodeqr='$qr' and jenisqr=1 and stsqr=1 and tglexqr > NOW()");
    $d=mysqli_fetch_array($sql);
    if(mysqli_num_rows($sql) < 1){
        
        ?>
         <script language="JavaScript">document.location='event.php?p=error'</script>    
         <?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'];
                mysqli_query($con,"UPDATE gtm SET status = 3 WHERE id_member='$idmember'");
            }
            $pesannn = $nama. ' dengan email '.$email.' baru saja divalidasi kehadirannya pada Event '.$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);
                }
            }    
        }else{
            ?>
         <script language="JavaScript">document.location='event.php?p=ver'</script>    
         <?php
        }    
        
        ?>
         <script language="JavaScript">document.location='event.php?p=sukses'</script>    
         <?php
    }    
?> 
