<?php
defined("BASEPATH") or exit("No direct access allowed");
?>
<div class="card-header">

			<nav aria-label="breadcrumb">
                      <ol class="breadcrumb bg-warning text-white-all">
                        <li class="breadcrumb-item"><a href="#"><i class="fas fa-tachometer-alt"></i> Home</a></li>
                        <li class="breadcrumb-item"><a href="#"><i class="far fa-file"></i> Prospek</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> Input</li>
                      </ol>
                    </nav>
                    
          </div>
<div class="card-body">   
<div class="alert alert-primary alert-has-icon p-4">
            <div class="alert-icon"><i class="far fa-lightbulb"></i></div>
            <div class="alert-body">
            <div class="alert-title">Petunjuk:</div>
            1. Ini penginputan secara satu-satu data email. Jika ingin import file database email <a href="index.php?page=prosfekimport">KLIK DISINI</a><br>
            2. Pastikan data yang diinput valid.
            </div>
            </div>

<?php

$mail = new VerifyEmail();
$mail->setStreamTimeoutWait(10);
$mail->Debug= FALSE; 
$mail->Debugoutput= 'html'; 
$mail->setEmailFrom($emailakun);
$email = $_POST['email']; 

if (isset($_POST['nama']) && isset($_POST['email'])) {
    
    //$proteksi = 0;
    IF($proteksi ==1){
       if($mail->check($email)){
           $email = $email;
       }elseif(verifyEmail::validate($email)){    
           echo "<script>alert('Email Anda Valid, tetapi tidak ada.'); window.location = 'javascript:history.go(-1)'</script>";
           $email ='';
           exit;
       }else{
           echo "<script>alert('Email Anda tidak valid dan tidak ada.'); window.location = 'javascript:history.go(-1)'</script>";
           $email ='';
           exit;
       } 
    }else{   
       if(domain_exists($email)) {    
           $email = $email;
       }else{
           $email =''; 
       }      
    }    
        
    if (!empty($email)) { 
        
	$nama = mysqli_real_escape_string($con,$_POST['nama']);
	$email = mysqli_real_escape_string($con,$_POST['email']);
	$hp = mysqli_real_escape_string($con,$_POST['hp']);
	$grup = mysqli_real_escape_string($con,$_POST['grup']);
	$stsadd = mysqli_real_escape_string($con,$_POST['stsadd']);

	$ip = $_SERVER['REMOTE_ADDR'];
	
	$query = mysqli_query($con,"SELECT id FROM prospek WHERE email='$email'");
	$check = mysqli_fetch_array($query,MYSQLI_NUM);
	if (!isset($check[0])) {				
		mysqli_query($con,"INSERT INTO prospek (ID,nama,email,tgl_gabung,ip,hp,sts) VALUES (NULL,'$nama','$email',NOW(),'$ip','$hp',0)");
		$user_id = mysqli_insert_id($con);
	} else {
		$user_id = $check[0];
	}
	
	$query = mysqli_query($con,"SELECT id FROM gtm WHERE id_member='$user_id' AND id_grup='$grup'");
	$check = mysqli_fetch_array($query,MYSQLI_NUM);
	if (!isset($check[0])) {
		// Check item pertama
		$query = mysqli_query($con,"SELECT ID FROM champ WHERE grup='$grup' ORDER BY sort asc LIMIT 0,1");	
		$nextitem = mysqli_fetch_array($query,MYSQLI_NUM);
		if (is_numeric($nextitem[0])) {
			$item = $nextitem[0];
		} else {
			$item = 0;
		}
		
		$query = mysqli_query($con,"SELECT nama,unsub FROM grup WHERE id=$grup LIMIT 0,1");
		$check = mysqli_fetch_array($query,MYSQLI_NUM);
		if ($check[1]!=0) {
			mysqli_query($con,"DELETE FROM gtm WHERE id_member='$user_id' AND id_grup='$check[1]'");
		} 
		//echo mysqli_error($con);
		
		mysqli_query($con,"INSERT INTO gtm VALUES (NULL,'$user_id','$grup','$item',NOW(),0,'$stsadd',NOW(),0,0,0,0,'',1,0)");
		
		echo '
		<div class="alert alert-success alert-dismissable" style="margin-top:20px">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon glyphicon glyphicon-remove"></i> Prospek '.stripslashes($nama).' telah ditambahkan
            </div>
            
        <script>
        	        swal("Good Job","berhasil disimpan","success");
                </script>         
		';
		
	} else {
	    
	    echo '
		<div class="alert alert-success alert-dismissable" style="margin-top:20px">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon glyphicon glyphicon-remove"></i> Prospek '.stripslashes($nama).' telah terdaftar di grup ini
            </div>
            
        <script>
        	        swal("Good Job","sudah tersimpan sebelumnya","error");
                </script>     
                
		';
		
	}
	
    }else{
    	echo "<script>alert('Email Anda tidak valid.'); window.location = 'javascript:history.go(-1)'</script>";
    }
}



	echo '
<h1>+ Prospek</h1>
<form action="" method="post">
	<p><label>Nama</label><input type="text" name="nama" class="form-control"/></p>
	<p><label>Email</label><input type="email" name="email" class="form-control"/></p>
	<p><label>Handphone/WA</label><input type="text" name="hp" class="form-control" onkeypress="return hanyaAngka(event)"/></p>
	<p><label>Pilih Grup</label><select name="grup" class="form-control" id="cari">';

    $query = mysqli_query($con,"SELECT grup.id as id, grup.nama as nama FROM grup,champ where grup.id=champ.grup group by grup.id");
    if (mysqli_num_rows($query) > 0) {
    	while ($grup = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
    	    
    	    $query2 = mysqli_query($con,"SELECT count(id_member) FROM gtm WHERE id_grup = '$grup[id]'");
            $status = mysqli_fetch_array($query2,MYSQLI_NUM);
            $jml5 = $status[0];  
                                    	    
            echo '<option value="'.$grup['id'].'">'.$grup['nama'].' (Group '. $grup['id'].') '.$jml5.' Prospek</option>';
    	}
    }
    echo '
    </select>
    <i>Grup tidak tampil berarti belum dibuat pesan auto</i> 
    <p><label>Tentukan status prospek</label>
    <select name="stsadd" id="exampleFormControlSelect1" class="form-control">
                <option value="0">Tidak Aktif</option>
                <option value="1">Aktif</option>
                <option value="2">Buyer</option>
                </select></p>
    <br>
	<input type="submit" class="btn btn-primary" value="Tambah Member"/></p>
</form>';
?>
</div>