<?php

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

if(isset($_GET["code"]))
{
    $email=$_GET["email"];
    $code2=$_GET["code2"];
    
    $pesan = 'Prosfek baru anda dengan email '.$email.' baru saja membuka emailnya via '.$media.' dan secara otomatis menjadi subscriber aktif anda di grup' .$_GET["code"];
    
    $query = mysqli_query($con,"SELECT gtm.id as id, gtm.id_member as idmember, gtm.next_item_id as idBC FROM gtm,prospek WHERE gtm.id_grup = '".$_GET["code"]."' and prospek.email='".$_GET["email"]."' and (gtm.status=0 or prospek.sts=0) ORDER BY gtm.id DESC LIMIT 0,1"); 
    $bc = mysqli_fetch_array($query,MYSQLI_ASSOC);
    if (!empty($bc['id'])) {
        $idmember = $bc['idmember'];
        $idBC = $bc['idBC'];
        mysqli_query($con,"insert into pesan1 values(NULL, '$pesan','8', '$nowtgl')");
        mysqli_query($con,"UPDATE gtm SET status = 1, baca=baca+1, tgl_baca='$nowtgl' WHERE id = '".$bc['id']."' and id_grup = '".$_GET["code"]."' and id_member ='".$idmember."'");
        mysqli_query($con,"UPDATE prospek SET sts = 1 WHERE email = '".$email."'");
        
        mysqli_query($con,"UPDATE champ SET baca = baca + 1 WHERE ID = $code2");
        
        $n = mysqli_query($con,"SELECT ID,delay FROM champ WHERE ID = '$idBC' order by ID asc LIMIT 0,1");
    	if (mysqli_num_rows($n) > 0) {
    		$next = mysqli_fetch_array($n,MYSQLI_ASSOC);			
    		$delay = $next['delay'];
    		$delay2 = $delay - 1;
    		IF($delay==0){
    		    mysqli_query($con,"UPDATE gtm SET next_post=NOW() WHERE id_grup = '".$_GET["code"]."' AND id_member = '$idmember'");
    		}else{    
    		    mysqli_query($con,"UPDATE gtm SET next_post=DATE_ADD(next_post, INTERVAL $delay2 DAY) WHERE id_grup = '".$_GET["code"]."' AND id_member = '$idmember'");
    		}	
    	}
        
    }    
    
    $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);
    
    
  
}

?>