<?php
defined("BASEPATH") or exit("No direct access allowed");

$sm=array (
':)' => "<img src='$weburl/images/smiles/happy.png' width='20' height='20' border=0 />",
':(' => "<img src='$weburl/images/smiles/sad.png' width='20' height='20' border=0 />",
':D' => "<img src='$weburl/images/smiles/devil.png' width='20' height='20' border=0 />",
';)' => "<img src='$weburl/images/smiles/kiss.png' width='20' height='20' border=0 />",
':?' => "<img src='$weburl/images/smiles/nervous.png' width='20' height='20' border=0 />",
':8'  => "<img src='$weburl/images/smiles/confused.png' width='20' height='20' border=0 />",
':T' => "<img src='$weburl/images/smiles/tunjuk.png' width='20' height='20' border=0 />",
':i' => "<img src='$weburl/images/smiles/surprised.png' width='20' height='20' border=0 />"
)

?>
<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> Email</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> Testing</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. Masukan email tujuan dan isi email.<br>
            2. Pengiriman email ini ditujukan per email.<br>
            3. Anda dapat mengupload template email <a href="index.php?page=tempe">Disini</a> <br>
            4. Sorot icon tuk menampilkan kode smile icon
            <?php 
            	foreach ($sm as $key => $val) { ?>
            		<a href="javascript:setIkon('<?=$key?>')"
            			title="<?=$key?>"> <?=$val?> 
            		</a>
            <?php	} ?> kemudian ketik pada isi email       
            </div>
            </div>
         
<?php
	if (isset($_POST['email']) && $_POST['email'] && isset($_POST['isi']) && $_POST['isi']) {
		$judul = $_POST['judul'];
		$isi = $_POST['isi'];
		$idyoutube = $_POST['idyoutube'];
		$emailsender = $_POST['emailsender'];
		$tempe = $_POST['tempe'];
		$hp = $_POST['hp'];
		$hps = $hp;
		$nama = 'Coba';
		//kirimemail
		
		If ($emailsender==0){
			
			$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]; 
    	        $namal = $check[10];
    	        $port = $check[11];
    	        $usersmtp = $check[22];
	        }   
	          
		}else{
		    $query = mysqli_query($con,"SELECT * FROM emailsender WHERE id=$emailsender");
	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
	        if (isset($check[0])) {
				$urlhost =$check[1];
    	        $emailhost = $check[2];
    	        $passhost = $check[3];
    	        $namal = $check[5];
    	        $port = $check[4];
    	        $namato = $check[5];
    	        $emailto = $check[6];
    	        $usersmtp = $check[7];
	        }    
		}          
	   
	    $qcunsub = mysqli_query($con,"SELECT * FROM akunn WHERE id=1");
	        $cunsub = mysqli_fetch_array($qcunsub,MYSQLI_NUM);
	        IF(empty($emailto)){
	            $emailto =$cunsub[21];
	            $namato = $cunsub[20];
	        }else{     
	            $emailto =$emailto;
	            $namato = $namato;
	        }     
	        $catkaki = $cunsub[15];
	        $twa = $cunsub[9];
	        $telegram_id = $cunsub[16];
    	    $secret_token = $cunsub[17];
    	    $keyapi = $cunsub[13];
    	    $smsaktif = $cunsub[14];
	        if (!empty($cunsub[19])) {
				$unsub =$cunsub[19];
				$linkunsub ='<font size="1"><a href="'.$weburl.'/unsub/unsub.php?id=0&code=0">UNSUB</a></font>';
	        }else{
    	        $unsub ='<font size="1">Email ini terkirim kepada '.$email.' karena pernah mengisi form option kami dan sudah menjadi subscriber kami. Ini bukan email spam. Jika email ini masuk ke kotak SPAM, harap klik tombol/menu BUKAN SPAM agar informasi dari kami selalu masuk ke kotak inbox. Maaf jika email ini mengganggu, untuk berhenti
                silahkan <i><a href="#">KLIK DISINI</a></i>, maka anda tidak menerima lagi email bisnis dari kami tapi anda dapat kembali lagi dengan mengisi form option yang kami sediakan.</font>';
	        }
        
        If ($download =='Y'){
            $gbr ='';
        }else{
            $gbr ='<img src="'.$weburl.'/images/autobiznistop.png">';
        }
        
        $querywa = mysqli_query($con,"SELECT * FROM wacs order by rand()");
	        $checkwa = mysqli_fetch_array($querywa,MYSQLI_NUM);
	        if (isset($checkwa[0])) {
				$namall =$checkwa[1];
    	        $thp = $checkwa[2];
	        }else{
	            $namall = $namal;
	            $thp = $check[7];
	        }    
        
        $thp =substr($thp, 1,12);
        $thp = $kodenegara.''.$thp;
        //$tombolWA ='<a href="https://api.whatsapp.com/send?phone='.$thp.'&text=halo%20pak/ibu%20'.$namall.'%20saya%20mau%20konsultasi" target="_blank"><img src="'.$weburl.'/images/tanyaWA.png"></a>';  
        
        $tombolWA ='<a href="'.$weburl.'/wa.php" target="_blank"><img src="'.$weburl.'/images/tanyaWA.png"></a>'; 
        
        $catkaki ='<font size="1">'.$catkaki.'</font>';
            
		If ($twa==0){
              $isi = $isi .'<br>'. $gbr .'<br>'. $catkaki;    
        }else{
              $isi = $isi .'<br>'. $tombolWA .'<br>'. $gbr .'<br>'. $catkaki; 
        }
        
        If($idyoutube !=''){
        $video = '<a href="https://youtu.be/'.$idyoutube.'">
	            <img src="https://www.hansvangent.com/download_youtube_screenshot.php?youtube_id='.$idyoutube.'"/>
                </a>';
        }else{
           $video=''; 
        }   
        
        $isi = $isi .'<br>'.$video.'<br><br>';
        
        If ($tempe==1){
            $body = file_get_contents('template/default/index.html');
        }else{
            $body = file_get_contents($tempe);
        }    
        
        $body = str_replace('[isi]',$isi,$body);
        $body = str_replace('[judul]',$judul,$body);
		$body = str_replace('[unsub]',$unsub,$body);
		$body = str_replace('[linkunsub]',$linkunsub,$body);
        $body = str_replace('[nama]',$nama,$body);
        $body = str_replace('[email]',$email,$body);
        $body = str_replace(array_keys($sm), array_values($sm), $body);
        //$judul = str_replace(array_keys($sm), array_values($sm), $judul);
        
        If(empty($usersmtp)){
            $usersmtp = $emailhost;
        }else{    
            $usersmtp = $usersmtp;
        }
        
        IF($smtp1a==1){
            $recipient = $email;
            $subject = $judul;
            $content = $body;
            SendEmailMalketing($from_name,$from_email,$recipient,$subject,$content,$api_token);
            $pesannn ='Anda mengirimkan Test email dengan judul '.$judul.' ke email '.$email.' menggunakan mailketing. Cek trackingnya di mailketing';
        }else{
		    echo "<font color ='white'>";
    	    include "classes/class.phpmailer.php";
            $mail = new PHPMailer; 
            $mail->IsSMTP();
            $mail->SMTPSecure = 'ssl'; //tls 
            $mail->Host = $urlhost; 
            $mail->SMTPDebug = 2;
            $mail->Port = $port;  
            $mail->SMTPAuth = true;
            $mail->Username = $usersmtp; 
            $mail->Password = $passhost;  
            //$mail->Domain ='domain.id';
            $mail->AddReplyTo($emailto, $namato);
            $mail->SetFrom($emailhost,$namal); 
            $mail->Subject = $judul; 
            $mail->AddAddress($email,$nama);  
            //$mail->MsgHTML($isi .'<br>'.$video.'<br><br>'. $unsub );
            $mail->MsgHTML($body);
            //$mail->AltBody = $isi .'<br>'.$video.'<br><br>'. $unsub;
            if($mail->Send()){ 
                $pesannn ='Test Ngirim email dengan judul '.$judul.' ke email '.$email.' sukses.'; 
                mysqli_query($con,"insert into pesan1 values(NULL, '$pesannn','4', NOW())");
                $message_text = $pesannn;
                sendMessage($telegram_id, $message_text, $secret_token);
                echo "Message has been sent";
            }else{
                $pesannn ='Test Ngirim email dengan judul '.$judul.' ke email '.$email.' gagal.'; 
                mysqli_query($con,"insert into pesan1 values(NULL, '$pesannn','4', NOW())");
                $message_text = $pesannn;
                sendMessage($telegram_id, $message_text, $secret_token);
                echo "Failed to sending message </font>";
            }
        }
		//kirimemail
		
		    //wa
    		$notifwa=mysqli_fetch_row(mysqli_query($con,"select * from notifwa where id=1"));
            IF($notifwa[4]==1){
                If (strlen($hp)>9){
                    $hp =substr($hp, 1,12);
                    $hp = '+62'.$hp;
                    //$pesan2 = nl2br($notifwa[2]);
                    $pesan2 = strip_tags($notifwa[2]);
                    $img = $weburl.'/'.$notifwa[3];
                    $key = $notifwa[1];
                    sendWA($hp,$pesan2,$img, $key);
                }
            }
            IF($notifwa[8]==1){
                If (strlen($hp)>9){
                    $appid = $notifwa[5];
                    $csid = $notifwa[6];
                    //$pesan3 = nl2br($notifwa[7]);
                    $pesan3 = $notifwa[7];
                    $pesan3 = strip_tags($pesan3);
                    //$pesan3 = trim($pesan3,"\n");
                    $hp =substr($hp, 1,12);
                    $hp = '62'.$hp;
                    sendWoowandroid($appid, $csid, $pesan3, $hp);
                    sendWoowandroiddua($csid, $pesan3, $hp);
                }
            }    
    		//wa
    		
    		//sms
    		If($smsaktif==1){
            	If (strlen($hps)>9){
            	    $pesan2 = 'Test Ngirim SMS. By.Sistem AutoBiznisTop';
            	    $data = array("hp"=>$hps,
                   "pesan"=>$pesan2,
                   "token"=>$keyapi);
                    $send = sendSMS("https://sms.yantonaim.web.id/bot.php",json_encode($data));
            	}
            }    
    		
		echo '
		    <script>
        	    swal("Good Job","'.$pesannn.'","success");
            </script>
            <script language="JavaScript">document.location="index.php?page=emailtest"</script>
            
		';

	}
	
?>
<script type="text/javascript">
	function setIkon(ikon) {
		document.post.isi.value = document.post.isi.value + ikon;
	}
</script>
<h4>Test Ngirim Pesan via Email & WA</h4>
<form action="" method="post">
<p><label>Email Tujuan</label><input type="email" name="email" class="form-control" required/></p>
<p><label>HP WA</label><input type="text" name="hp" onkeypress="return hanyaAngka(event)" class="form-control" placeholder="Contoh: 082150651122"/></p>
<p><label>Judul Email</label><input type="text" name="judul" class="form-control" required/></p>

<p><textarea class="ckeditor" name="isi" required></textarea></p>
<p><label>ID Youtube</label><input type="text" name="idyoutube" class="form-control" placeholder="D1W3n4YEzJ4"></p>
<p><label>Email Sender</label><select name="emailsender" class="form-control">
	    
    <option value="0">Default</option>
    <?php
    $query = mysqli_query($con,"SELECT id,e2,e5 FROM emailsender");
    if (mysqli_num_rows($query) > 0) {
    	while ($grup = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
    		echo '<option value="'.$grup['id'].'">'.$grup['e5'].' - '.$grup['e2'].'</option>';
    	}
    }
    ?>
    </select>
    </p>
    
 <p><label>Template Email</label><select name="tempe" class="form-control">
	    
    <option value="1">Default</option>
    <?php
    $query2 = mysqli_query($con,"SELECT * FROM template");
    if (mysqli_num_rows($query2) > 0) {
    	while ($grup2 = mysqli_fetch_array($query2,MYSQLI_ASSOC)) {
    		echo '<option value="'.$grup2['linktemp'].'">'.$grup2['namatemp'].'</option>';
    	}
    }
    ?>
    </select>
    </p>
    
<p><input class="btn btn-primary" type="submit" class="tombol" value="Kirim Sekarang"/></p>
</form>
</div>