<?php

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

if(is_numeric($_GET['id']) && isset($_GET['code'])) {
	$id = $_GET['id'];	
	$bid = $_GET['code'];
	$idbc = $_GET['bc'];
	
	if (isset($_POST['submit'])){
	    
	    $alasan = $_POST['alasan'];
	    
    	$query = mysqli_query($con,"SELECT * FROM gtm,prospek WHERE gtm.id_member=prospek.ID and gtm.id='$id' LIMIT 0,1");
    	$check = mysqli_fetch_array($query,MYSQLI_ASSOC);
    	$code = md5($check['id'].'||'.$check['id_grup']);
    	$grup = $check['id_grup'];
    	if ($code == $_GET['code']) {

    		mysqli_query($con,"DELETE FROM gtm WHERE id='$id'");
    		mysqli_query($con,"UPDATE grup SET jmlunsub=jmlunsub+1 WHERE id='$grup'");
    		
    		IF(!empty($idbc)){
    		    mysqli_query($con,"UPDATE bc SET unsub=unsub+1 WHERE ID='$idbc'");
    		}
    		
    		$email = $check['email'];
    		$pesan = 'Prospek anda dengan email '.$email.' baru saja UNSUB dari grup "' .$grup.'" dengan alasan "' .$alasan.'" ';
    		mysqli_query($con,"insert into pesan1 values(NULL, '$pesan','3', '$nowtgl')");
    		
    		$query2 = mysqli_query($con,"SELECT * FROM alasan WHERE id=$alasan");
    	    $check2 = mysqli_fetch_array($query2,MYSQLI_NUM);
    	    if (isset($check2[0])) {
    	        $alasan2 =$check2[1];
    			$link =$check2[2];
    	    }		
    		
    		mysqli_query($con,"insert into unsub values(NULL, '$email','$alasan2', '$nowtgl','$grup')");
    		
    		$query = mysqli_query($con,"SELECT * FROM akunn WHERE id=1");
    	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
    	        if (isset($check[0])) {
    				$urlhost =$check[4];
        	        $emailhost = $check[5];
        	        $passhost = $check[6];
    	            $namao = $check[10];
    	            $port = $check[11];
    	            $emailapi = $check[12];
        	        $keyapi = $check[13];
        	        $hp = $check[7];
        	        $smsaktif = $check[14];
        	        $telegram_id = $check[16];
        	        $secret_token = $check[17];
    	        }
    	        
    	   $message_text = $pesan;
                    sendMessage($telegram_id, $message_text, $secret_token);     
    		echo '<center>Terima kasih, anda sudah dihapus dari grup ini </center>';	
    		
    		echo "<script language='JavaScript'>document.location='$link'</script>";
    	}
	}	
}
?>

<html>
  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <title>Kami sedih tapi tetap Optimis :) </title>
    <link rel="stylesheet" href="style.css">
    <link href='https://fonts.googleapis.com/css?family=Lato:400,700' rel='stylesheet' type='text/css'>
     <link rel="shortcut icon" href="../images/abt.png" />
  </head>
  <body>
    <form action="" method="post">
      <div class="header">
         <p>Kami sedih tapi tetap Optimis :)</p>
      </div>
      <div class="description">
        <p>Silahkan pilih alasan UNSUB dari layanan kami.</p>
      </div>
      <div class="input">
       
            <select name="alasan" class="button">
            
            <?php
            $query = "SELECT * FROM alasan";
            $hasil = mysqli_query($con,$query);
            while ($data = mysqli_fetch_array($hasil))
            {
            echo "<option value='".$data['id']."'>".$data['alasan']."</option>";
            }
            ?>
            
            </select>

        <input type="submit" class="button" id="submit" name="submit" value="UNSUB">
      </div>
    </form>
  </body>
</html>