<?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> <a href="index.php?page=pesanauto">Pesan</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i>Auto</li>
                      </ol>
                    </nav>
                    
<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. Pilih kembali grup <a href="index.php?page=pesanauto">DISINI</a>.<br>
            2. Tidak tampil tombol <img src="<?php echo $weburl ?>/images/email_delete.png"/>, berarti pesan autonya masih dalam proses antrian pengiriman.<br>
            3. Untuk Delai pengiriman misalnya email ke 1 Hari ini maka prospek saat selesai mengisi Form Opt langsung mendapatkan email ke 1. Email ke 2 bisa Lompat 1 hari, maka nanti besok di waktu yang sama saat subscriber, prospek akan mendapatkan email. Delai terhitung setelah email sebelumnya, bukan dari email ke 1.<br>
            4. Isian SMS/WA boleh dikosongkan. Jika diisi dapat menambahkan tag [nama],[email],[hp].<br> Pastikan anda telah melakukan <a href="index.php?page=akun" target="_blank">setting SMS</a> dan <a href="index.php?page=notif&set=woowaandroid" target="_blank">Setting Woowandroid. </a><br>
            5. Saat ini pesan Auto terkirim menggunakan email default (menu setting) dan SMPT Mailketing. <br>
            6. 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
            <br>
            7. Delay Not Found berarti anda menginput delai saat edit diluar dari angka 0,1,2,3,4,5,6,7,14,21,30,60,90,120,150,180,365 <br>
            8. Pastikan pesan auto urutan ke 1 untuk prospek tidak aktif. Email ke 2 dst untuk yang aktif atau buyer. Email ke 2 dst akan terkirim saat prospek berstatus aktif/buyer.
            </div>
            </div>
<?php

$cekg=mysqli_fetch_array(mysqli_query($con,"select * from grup where id='$idgrup'"));

if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
	$id = $_GET['edit'];
	if (isset($_POST['judul']) && isset($_POST['isi'])) {
		$judul = mysqli_real_escape_string($con,$_POST['judul']);
		$delay = mysqli_real_escape_string($con,$_POST['delay']);
		$sort = mysqli_real_escape_string($con,$_POST['sort']);
		$isi = mysqli_real_escape_string($con,$_POST['isi']);
		$filter = mysqli_real_escape_string($con,$_POST['filter']);
		$sms = mysqli_real_escape_string($con,$_POST['sms']);
		$tempe = mysqli_real_escape_string($con,$_POST['tempe']);
		$pesanwa = mysqli_real_escape_string($con,$_POST['pesanwa']);
		$idyoutube = mysqli_real_escape_string($con,$_POST['idyoutube']);
		
		mysqli_query($con,"UPDATE champ SET judul='$judul', delay='$delay', sort='$sort', isi='$isi', status_target='$filter', sms='$sms', tempe='$tempe', pesanwa='$pesanwa', idyoutube='$idyoutube' WHERE ID=$id");
		
		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> Pesan Auto berhasil diedit
            </div>
		
		<div class="container">
                 <form class="form-horizontal" role="form" action="index.php?page=pesanauto&aksi=listpesan" method="POST">
                   <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-log-out"></span> Kembali</button>
                 </form>
               </div>
               
        <script>
        	        swal("Good Job","berhasil disimpan","success");
                </script>     
                
		';
		
	}
	$query = mysqli_query($con,"SELECT * FROM champ WHERE ID=$id LIMIT 0,1");
	$champ = mysqli_fetch_array($query,MYSQLI_ASSOC);
	$grup = $champ['grup'];
	
	echo '
	<ul class="nav nav-tabs" role="tablist">
      <li class="nav-item active">
        <a class="nav-link active" href="#tab1" role="tab" data-toggle="tab">(1) Edit Delay & Filter</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab2" role="tab" data-toggle="tab">(2) Edit Judul & Pesan Email</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab3" role="tab" data-toggle="tab">(3) Edit Pesan SMS</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab4" role="tab" data-toggle="tab">(4) Edit Pesan WA</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab5" role="tab" data-toggle="tab">(5) Edit Template Email</a>
      </li>
    </ul>
	
	<div class="tab-content">
	
	<div role="tabpanel" class="tab-pane fade in active" id="tab1"> 
        <br>
	<form action="" method="post">
		
		<p><label>Delay (Hari)</label><input type="number" style="width:100px" name="delay" value="'.$champ['delay'].'" class="form-control" required/>Angka hari: 0,1,2,3,4,5,6,7,14,21,30,60,90,120,150,180,365</p>
		<p><label>Urutan</label><input type="number" style="width:100px" name="sort" value="'.$champ['sort'].'" onkeypress="return hanyaAngka(event)" class="form-control" required/></p>
		<p><label>Filter Prospek</label><input type="text" style="width:50px" name="filter" value="'.$champ['status_target'].'" class="form-control" required/>0=Belum Aktif, 1= Aktif, 2=Buyer</p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab2">
		<p><label>Judul Email</label><input type="text" name="judul" value="'.$champ['judul'].'" class="form-control" required/></p>
		<p><label>Pesan Email (Isi)</label><br>
		Bisa Tambahkan tag: [nama],[email],[hp]
		<div style="clear:both;"></div>
		<p><textarea class="ckeditor" name="isi">'.$champ['isi'].'</textarea></p>
		<p><label>Lampirkan ID Video Youtube (Optional)</label><input type="text" name="idyoutube" value="'.$champ['idyoutube'].'" class="form-control"/></p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab3">
		<br>
		Bisa Tambahkan tag: [nama],[email],[hp]
		<p><textarea  onKeyPress=cek_karakter(this.form); onKeyDown=cek_karakter(this.form); class="form-control" name="sms" rows="2" cols="50">'.$champ['sms'].'</textarea><input size=1 value=160 name=total>Karakter</p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab4">
		<br>
		Bisa Tambahkan tag: [nama],[email],[hp]
		<p><textarea class="form-control" name="pesanwa" rows="2" cols="50">'.$champ['pesanwa'].'</textarea></p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab5">
		<p><label>Template Email</label><input type="text" style="width:50px" name="tempe" value="'.$champ['tempe'].'" class="form-control"/>0=Template Standart, lainnya sesuaikan dengan Nomor Template</p>
		<p><input type="hidden" name="action" value="edit"/>		
		<input type="submit" class="btn btn-primary" value="Simpan Edit Pesan Auto"/></p>
		</div>
	</form>
	
	</div>
	';
} else {
	if (isset($_POST['action']) && $_POST['action'] == 'sort') {
		$i = 0;
		foreach ($_POST['id'] as $itemid) {
			$sort = $_POST['sort'][$i];
			mysqli_query($con,"UPDATE `champ` SET `sort`=$sort WHERE `ID`=$itemid");
			$i++;
		}
		
		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> Update Berhasil
            </div>
            
            <script>
            	swal("Good Job","berhasil menyimpan update","success");
            </script>

            ';
            
	} elseif (isset($_GET['hapus']) && is_numeric($_GET['hapus'])) {
		$hapus = $_GET['hapus'];
		mysqli_query($con,"DELETE FROM `champ` WHERE `ID`=$hapus");
		echo '<div class="notif">Pesan Auto dengan ID '.$hapus.' Telah Dihapus</div>
		
		<script>
        	swal("Good Job","berhasil dihapus","success");
        </script>
        <!-- <script language="JavaScript">history.go(-1); </script> -->

		';
	} elseif (isset($_POST['action']) && $_POST['action'] == 'add' && !empty($_POST['judul'])) {
		$judul = mysqli_real_escape_string($con,$_POST['judul']);
		$delay = mysqli_real_escape_string($con,$_POST['delay']);
		$sort = mysqli_real_escape_string($con,$_POST['sort']);		
		$isi = mysqli_real_escape_string($con,$_POST['isi']);
		$filter = mysqli_real_escape_string($con,$_POST['filter']);
		$sms = mysqli_real_escape_string($con,$_POST['sms']);
		$tempe = mysqli_real_escape_string($con,$_POST['tempe']);
		$pesanwa = mysqli_real_escape_string($con,$_POST['pesanwa']);
		$idyoutube = mysqli_real_escape_string($con,$_POST['idyoutube']);
		
		if (empty($delay)) {$delay = 0;}
		if (empty($sort)) {$sort = 1;}
		if (empty($isi)) {echo '<div class="error">Isi champaign masih kosong</div>';} else {
		
		mysqli_query($con,"INSERT INTO `champ` (`ID`, `grup`, `judul`, `isi`, `delay`, `sort`, `baca`, `gagal`, `kirim`, `status_target`, `sms`, `stschamp`, `tempe`, `pesanwa`, `idyoutube`) VALUES (NULL, '".$idgrup."', '".$judul."', '".$isi."', '".$delay."', '".$sort."', '0', '0', '0', '".$filter."', '".$sms."', '1', '".$tempe."', '".$pesanwa."', '".$idyoutube."')");
		$next_item_id = mysqli_insert_id($con);
        		
		mysqli_query($con,"UPDATE gtm SET nextemail=1, next_item_id=$next_item_id, next_post=NOW() WHERE id_grup = $idgrup and nextemail=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> Auto Pesan Telah Ditambahkan
            </div>
            
            <script>
        	        swal("Good Job","berhasil disimpan","success");
                </script>     
            ';
        
		}
	}
	
	
	
	echo '
	<h3>List Pesan Auto (Grup '.$idgrup.') '.$cekg['nama'].'</h3>
	<form action="" method="post">
	<div class="table-responsive">
	<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
	<tr>
	<td width="15%">Urutan</td>
	<td width="45%">(ID) Subject</td>
	<td width="25%">Target</td>
	<td width="15%">Delay</td>
	
	<td width="15%">No.Template</td>
	<td width="10%">Action</td></tr>';
	$query = mysqli_query($con,"SELECT ID,judul,delay,sort,status_target,tempe,kirim,gagal,baca FROM champ WHERE grup='$idgrup' ORDER BY sort");
	$sort = 0;
	if (mysqli_num_rows($query) > 0) {
		while ($champ = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
		    
		    If($champ['status_target']==0){
		        $ktarget='Prospek Belum Aktif';
		    }elseIf($champ['status_target']==1){
		        $ktarget='Prospek Aktif'; 
		    }elseIf($champ['status_target']==2){
		        $ktarget='Buyer';       
		    }
		    
		    IF($champ['delay']==0){
		       $kdelai = 'Kirim Sekarang';
		    }elseIF($champ['delay']==1){
		       $kdelai = '1 Hari Kemudian';
		    }elseIF($champ['delay']==2){
		       $kdelai = '2 Hari Kemudian';     
		    }elseIF($champ['delay']==3){
		       $kdelai = '3 Hari Kemudian'; 
		    }elseIF($champ['delay']==4){
		       $kdelai = '4 Hari Kemudian';     
		    }elseIF($champ['delay']==5){
		       $kdelai = '5 Hari Kemudian';    
		    }elseIF($champ['delay']==6){
		       $kdelai = '6 Hari Kemudian';     
		    }elseIF($champ['delay']==7){
		       $kdelai = '7 Hari Kemudian';  
		    }elseIF($champ['delay']==14){
		       $kdelai = '2 Minggu Kemudian';
		    }elseIF($champ['delay']==21){
		       $kdelai = '3 Minggu Kemudian';   
		    }elseIF($champ['delay']==30){
		       $kdelai = '1 Bulan Kemudian';     
		    }elseIF($champ['delay']==60){
		       $kdelai = '2 Bulan Kemudian';      
		    }elseIF($champ['delay']==90){
		       $kdelai = '3 Bulan Kemudian';     
		    }elseIF($champ['delay']==120){
		       $kdelai = '4 Bulan Kemudian';     
		    }elseIF($champ['delay']==150){
		       $kdelai = '5 Bulan Kemudian';   
		    }elseIF($champ['delay']==180){
		       $kdelai = '6 Bulan Kemudian';  
		    }elseIF($champ['delay']==365){
		       $kdelai = '1 Tahun Kemudian';     
		    }else{
		       $kdelai = 'Not Found'; 
		    }
		    
			echo '<tr>
			<td><input type="text" name="sort[]" style="width:50px" value="'.$champ['sort'].'" onkeypress="return hanyaAngka(event)" required/>
			<td>('.$champ['ID'].') '.$champ['judul'].'
			<br><div class="badge badge-success">Dikirim: '.$champ['kirim'].'</div><div class="badge badge-warning">Gagal: '.$champ['gagal'].'</div><div class="badge badge-success">Baca: '.$champ['baca'].'</div> </td>
			<td>'.$ktarget.'</td><td>'.$kdelai.'</td>
			
			<td>('.$champ['tempe'].')
			<input type="hidden" name="id[]" value="'.$champ['ID'].'"/></td>
			<td><a href="?page=editauto&edit='.$champ['ID'].'"><img src="'.$weburl.'/images/email_edit.png" alt="Edit" title="Edit"/></a> &nbsp;'; 
			
			//$cid = $champ['ID'];
			//$sql2 = mysqli_query($con,"select * from gtm where next_item_id ='$cid'");
            //$row2=mysqli_fetch_array($sql2,MYSQLI_ASSOC);
            //$cek2 = mysqli_num_rows($sql2);
            //if($cek2 > 0){
                //echo '';
            //}else{   
                echo '
                <a onclick="return konfirmasi()" href="?page=editauto&hapus='.$champ['ID'].'"><img src="'.$weburl.'/images/email_delete.png" alt="Delete" title="Delete"/></a> &nbsp; ';
            //}  
            
			$sort = $champ['sort'];			
		}
	} else {
		echo '<tr><td colspan="4">Anda belum membuat champaign. Silahkan <a href="#addemail">buat email champaign</a></li>';
	}
	echo '</table></div>	
	<p><input type="hidden" name="action" value="sort"/>
	<input type="submit" class="btn btn-primary" value="Update Urutan"/></p>
	</form>
	<div style="margin:50px;"></div>';
	
	echo '
	<a name="addemail"></a>
	
	<ul class="nav nav-tabs" role="tablist">
      <li class="nav-item">
        <a class="nav-link active" href="#tab1" role="tab" data-toggle="tab">(1) Delay & Filter</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab2" role="tab" data-toggle="tab">(2) Judul & Pesan Email</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab3" role="tab" data-toggle="tab">(3) Pesan SMS</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab4" role="tab" data-toggle="tab">(4) Pesan WA</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab5" role="tab" data-toggle="tab">(5) Template Email</a>
      </li>
    </ul>
	
	<div class="tab-content">
	<div role="tabpanel" class="tab-pane fade in active" id="tab1"> 
        <br>
	    <form action="" method="post" name="form">
		
		<p><label>Delay</label>
		<!-- <input type="text" style="width:50px" name="delay" class="form-control"/> hari -->
		
		<select name="delay" class="form-control">
        <option value="0">Kirim Sekarang </option>
        <option value="1">1 Hari Kemudian</option>
        <option value="2">2 Hari Kemudian</option>
        <option value="3">3 Hari Kemudian</option>
        <option value="4">4 Hari Kemudian</option>
        <option value="5">5 Hari Kemudian</option>
        <option value="6">6 Hari Kemudian</option>
        <option value="7">7 Hari Kemudian</option>
        <option value="14">2 Minggu Kemudian</option>
        <option value="21">3 Minggu Kemudian</option>
        <option value="30">1 Bulan Kemudian</option>
        <option value="60">2 Bulan Kemudian</option>
        <option value="90">3 Bulan Kemudian</option>
        <option value="120">4 Bulan Kemudian</option>
        <option value="150">5 Bulan Kemudian</option>
        <option value="180">6 Bulan Kemudian</option>
        <option value="365">1 Tahun Kemudian</option>
        </select>
        
		<label>Urutan (Tidak boleh sama)</label><input type="number" style="width:100px" name="sort" class="form-control" value="'.($sort+1).'" onkeypress="return hanyaAngka(event)" required/></p>
		
		<p><label>Filter Prospek:</label>
    	<select name="filter" class="form-control">
    	    <option value="0">Prospek Belum Aktif</option>
    	    <option value="1">Prospek Aktif</option>
    	    <option value="2">Buyer</option>
    	</select></p> 
    	<i>Email ke 1 (pertama), wajib prospek belum aktif</i>
    	</div>
    	
    	
    	<div role="tabpanel" class="tab-pane fade" id="tab2"> 
    	<br>
    	<p><label>Judul Email</label><input type="text" name="judul" class="form-control" required/></p>
	
		<p><label>Pesan Email (Isi)</label></p>
		<div style="clear:both;"></div>
		';
		$query = mysqli_query($con,"SELECT custom FROM `grup` WHERE id=$idgrup");
		$q = mysqli_fetch_array($query,MYSQLI_NUM);
		$custom = unserialize($q[0]);
		echo 'Bisa Tambahkan tag: [nama],[email],[hp] <br>';
		if (is_array($custom)) {
		foreach ($custom as $custom) {
			echo '['.txtonly($custom).'] : '.$custom.'<br/>';
		}
		}
		echo '</p>
		<p><textarea class="ckeditor" name="isi"></textarea></p>
		<p><label>Lampirkan ID Video Youtube (Optional)</label><input type="text" name="idyoutube" class="form-control" placeholder="D1W3n4YEzJ4 (kosongkan jika tidak melampirkan video youtube)"/></p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab3">
    	<br>
		<p><label>Pesan SMS</label><br>
		Bisa Tambahkan tag: [nama],[email],[hp]<textarea  onKeyPress=cek_karakter(this.form); onKeyDown=cek_karakter(this.form); class="form-control" name="sms" rows="2" cols="50"></textarea><input size=1 value=160 name=total>Karakter</p>
		</div>
		
		<div role="tabpanel" class="tab-pane fade" id="tab4">
    	<br>
		<p><label>Pesan WhatsApp</label><br>
		Bisa Tambahkan tag: [nama],[email],[hp] <br>';
		$query = mysqli_query($con,"SELECT custom FROM `grup` WHERE id=$idgrup");
		$q = mysqli_fetch_array($query,MYSQLI_NUM);
		$custom = unserialize($q[0]);
		if (is_array($custom)) {
		foreach ($custom as $custom) {
			echo '['.txtonly($custom).'] : '.$custom.'<br/>';
		}
		}
		echo '
		<p><textarea  class="form-control" name="pesanwa" rows="2" cols="50"></textarea><br></p>
		</div>
		
		';
		?>
		
		<div role="tabpanel" class="tab-pane fade" id="tab5">
    	<br>
		<p><label>Template Email. Upload Template email <a href="index.php?page=tempe">disini</a></label><select name="tempe" class="form-control">
    	<option value="0">(0) 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['id'].'">('.$grup2['id'].') '.$grup2['namatemp'].'</option>';
        	}
        }
        echo '
        </select>
        </p>
		
		<p><input type="hidden" name="action" value="add"/>
		<input type="submit" class="btn btn-primary" value="Simpan Pesan Auto Baru"/></p>
		</div>
	</form>
	</div>
	';
}
?>
</div>