<?php

include('config.php');
include('fungsi.php');

if(isset($_GET["code"])){
    $code = $_GET["code"];
    $email=$_GET["email"];
    
    mysqli_query($con,"UPDATE champ SET baca = baca + 1 WHERE ID = $code");
    
    $query2 = mysqli_query($con,"SELECT * FROM prospek WHERE ID = $email ORDER BY ID DESC LIMIT 0,1"); 
    $bc2 = mysqli_fetch_array($query2,MYSQLI_ASSOC);
    if (!empty($bc2['ID'])) {
       $email2 = $bc2['email']; 
    }
    
    
    
    mysqli_query($con,"UPDATE prospek SET sts = 1 WHERE email = '".$email2."'");
    
    $query = mysqli_query($con,"SELECT * FROM champ WHERE ID = $code ORDER BY ID asc LIMIT 0,1"); 
    $bc = mysqli_fetch_array($query,MYSQLI_ASSOC);
    if (!empty($bc['ID'])) {
    	$grup = $bc['grup'];
    	$judul = $bc['judul'];
    	$pesan = 'Prospek anda dengan email '.$email2.' baru saja membaca pesan auto anda via '.$media.' yg berjudul "' .$judul.'"';
    	
    }
    
    mysqli_query($con,"insert into pesan1 values(NULL, '$pesan', '0', '$nowtgl')");
  
    mysqli_query($con,"UPDATE gtm SET baca=baca+1, tgl_baca='$nowtgl' WHERE id_member=$email and id_grup=$grup");
  
    mysqli_query($con,"UPDATE gtm SET status = 1 WHERE id_member=$email and status = 0 and id_grup=$grup");
  
    $query = mysqli_query($con,"SELECT * FROM akunn WHERE id=1");
	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
	        if (isset($check[0])) {
	            $emailapi = $check[12];
    	        $keyapi = $check[13];
    	        $hp = $check[7];
    	        $smsaktif = $check[14];
    	        $telegram_id = $check[16];
    	        $secret_token = $check[17];
	        }
	        
	        $email_api	= urlencode($emailapi);
            $passkey_api	= urlencode($keyapi);
            $no_hp_tujuan	= urlencode($hp);
            $isi_pesan	= $pesan;
            
            //$url	= "https://reguler.medansms.co.id/sms_api.php?action=kirim_sms&email=".$email_api."&passkey=".$passkey_api."&no_tujuan=".$no_hp_tujuan."&pesan=".$isi_pesan;
            
            If($smsaktif==1){
                //$result	= SendAPI_SMS($url);
                $hp = $no_hp_tujuan; 
            	$pesan = $isi_pesan; 
            	$token = $passkey_api;
            	$data = array("hp"=>$hp,
                       "pesan"=>$pesan,
                       "token"=>$token);
                        //$send = sendSMS("https://sms.yantonaim.web.id/bot.php",json_encode($data));
            	//
            }  
            
            $message_text = $pesan;
                sendMessage($telegram_id, $message_text, $secret_token);
  
  
}

?>