<?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 />"
);

IF(!empty($_GET['id'])){
$tampil=mysqli_fetch_row(mysqli_query($con,"select * from bc where ID='".$_GET['id']."'"));
}

if (isset($_GET['hapus']) && is_numeric($_GET['hapus'])) {
		$hapus = $_GET['hapus'];
		mysqli_query($con,"DELETE FROM bc WHERE ID=$hapus");
		?>
            <script language="JavaScript">document.location='index.php?page=broadnew'</script>
            <script>
        	    swal("Good Job","Anda sukses menghapus broadcast","success");
            </script>
            <?php
		
	}

?>
<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> Broadcast</a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> New</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. Tuk judul dan isi email, dapat menambahkan tag:[nama],[email], [hp] <br>
            2. Pastikan anda mengirim ke grup yang ada prosfek.<br>
            3. Saat posisi login, dapat mengirimkan email per 5 detik. Saat logout pengiriman berdasarkan setingan cron job anda.<br>
            4. Jika ingin menempatkan gambar dalam isi email, upload gambar <a href="https://postimages.org/" target="_blank">Klik Disini</a>. Kemudian copy dan paste link gambarnya. <br>
            5. Buat Countdown Timer  <a href="https://www.sendtric.com/" target="_blank">DISINI</a> 100% layanannya gratis. Kemudian copy dan paste script atau link gambarnya. <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. Jika Broadcast via WA juga pastikan anda menggunakan Woowandroid v2.0
            </div>
            </div>
            

<?php
if (isset($_POST['judul']) && $_POST['judul'] && isset($_POST['pesan']) && $_POST['pesan']) {
	
	$i = 0;
	foreach ($_POST['grup'] as $grup) {
	    
    	$judul = mysqli_real_escape_string($con,$_POST['judul']);
    	$pesan = mysqli_real_escape_string($con,$_POST['pesan']);
    	$grup = mysqli_real_escape_string($con,$_POST['grup'][$i]);
    	$idyoutube = mysqli_real_escape_string($con,$_POST['idyoutube']);
    	$filter = mysqli_real_escape_string($con,$_POST['filter']);
    	$emails = mysqli_real_escape_string($con,$_POST['emailsender']);
    	$tempe = mysqli_real_escape_string($con,$_POST['tempe']);
    	$qr = mysqli_real_escape_string($con,$_POST['qr']);
    	$pesanwa = mysqli_real_escape_string($con,$_POST['pesanwa']);
    	$cec = mysqli_real_escape_string($con,$_POST['cec']);
    	
        IF($cec==1){
            $date = new DateTime('now');
            $date->modify('-6 hours');
            $tanggal = $date->format('Y-m-d H:i:s');
        }else{    
        	if (isset($_POST['tanggal'])) {
        		$date = mysqli_real_escape_string($con,$_POST['tanggal']);
        	} else {
        		$date = date('Y-m-d');
        		//$date = date_create('Y-m-d');
        	}
        	if (isset($_POST['jam'])) {
        		$time = mysqli_real_escape_string($con,$_POST['jam']).':00';
        	} else {
        		$time = date('H:i:s');
        		//$time = date_create('H:i:s');
        	}
        	$tanggal = $date.' '.$time;
        }	
    	
    	IF(!empty($idyoutube)){
            $idyoutube = $idyoutube;
        }else{
            $idyoutube = '-';
        }
    	
        $query = mysqli_query($con,"SELECT count(*) FROM gtm WHERE id_grup = '$grup'");
        $status = mysqli_fetch_array($query,MYSQLI_NUM);
        $jml5 = $status[0];
        
        IF($smtp1a==1){
           $ketBC = 'Email Mailketing '.$from_email;    
        }else{
           $ketBC = 'Email Hosting/SMTP Lain ';    
        }    
        
    	mysqli_query($con,"INSERT INTO bc VALUES (NULL,'$judul','$pesan','$tanggal',$grup,0,0,0,0,'$idyoutube','$filter','$emails',0,'$tempe','$qr','$pesanwa','$ketBC')");
    	
    	mysqli_query($con,"UPDATE gtm SET bc=0 WHERE id_grup=$grup"); //AND status=1
	    
	    $i++;
	}
		
	echo '
	    <script>
        	    swal("Good Job","broadcast anda mulai terkirim","success");
            </script>
	    ';
}


    ?>
    
    <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). Grup, Filter & Jadwal Broadcast</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab2" role="tab" data-toggle="tab">2). Pesan Email (Judul & Isi Email)</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab4" role="tab" data-toggle="tab">3). Pesan WA [beta]</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#tab3" role="tab" data-toggle="tab">4). Setting Lainnya</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>Grup Email:</label>
    	<select name="grup[]" class="form-control" id="cari" multiple="multiple">
    		<?php
    		
    		//$query = mysqli_query($con,"SELECT ID,nama FROM grup");
    		$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'].'"><b>'.$grup['nama'].' (Group '. $grup['id'].') '.$jml5.' Prospek </b></option>';
    		    }
    		}
    		
    		?>
    	</select><i>Grup tidak tampil berarti belum dibuat pesan auto</i></p>
    	
    	<p><label>Filter Prospek:</label>
    	<select name="filter" class="form-control">
    	    <option value='7'>Semua Prospek</option>
    	    <option value='0'>Prospek Belum Aktif</option>
    	    <option value='1'>Prospek Aktif</option>
    	    <option value='2'>Buyer</option>
    	    <option value='3'>Prospek Baca Email 25x keatas</option>
    	    <option value='4'>Prospek Baca Email 50x keatas</option>
    	    <option value='5'>Prospek Baca Email 75x keatas</option>
    	    <option value='6'>Prospek Baca Email 100x keatas</option>
    	    <option value='8'>Prospek Yang Pernah Ikut Event</option>
    	</select></p>  
    	
    	<div class="form-check form-check-inline">
          <input type="checkbox" class="form-check-input" id="materialInline1" name="cec" value="1">
          <label class="form-check-label" for="materialInline1">Kirim Sekarang</label>
        </div>
        <hr>
    	<p><label>Jadwal Broadcast:</label><input type="date" name="tanggal"/>
    	<input type="time" name="jam" style="width:100px" /></p>
    	</div>
    	
    	<div role="tabpanel" class="tab-pane fade" id="tab2"> 
    	<br>
    	<p><label>Judul Email:</label><input type="text" name="judul" class="form-control" value="<?php If(!empty($tampil[1])) { echo $tampil[1]; }?>" required/></p>
    	<p>Isi Email:</p>
    	tag [qrcode] untuk menampilkan qrcode diskont atau event yang sudah anda buat sebelumnya.
    	<p><textarea class="ckeditor" name="pesan"><?php If(!empty($tampil[2])) { echo $tampil[2]; }?></textarea></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="10" cols="50"></textarea></p>
    	</div>
    	
    	<div role="tabpanel" class="tab-pane fade" id="tab3">
    	<br>    
    	<p><label>ID video Youtube</label><input type="text" name="idyoutube" class="form-control" placeholder="D1W3n4YEzJ4 (kosongkan jika tidak melampirkan video youtube)" value="<?php If(!empty($tampil[9])) { echo $tampil[9]; }?>"></p>
    	
    	<p><label>Email Sender. Buat Email Sender <a href="index.php?page=emailsender">disini</a>. Setting Email Default <a href="index.php?page=akun#URL">disini</a></label><select name="emailsender" id="exampleFormControlSelect1" 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. Upload Template email <a href="index.php?page=tempe">disini</a></label><select name="tempe" class="form-control">
    	<option value="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['namatemp'].'</option>';
        	}
        }
        ?>
        </select>
        </p>
        
        <p><label>Kirim QRCode. Buat Qrcode <a href="index.php?page=qrc">disini</a></label><select name="qr" class="form-control">
    	    
        <option value="0">Tidak</option>
        <?php
        $query3 = mysqli_query($con,"SELECT * FROM qrcode where stsqr=1");
        if (mysqli_num_rows($query3) > 0) {
        	while ($grup3 = mysqli_fetch_array($query3,MYSQLI_ASSOC)) {
        	    $jqr = $grup3['jenisqr'];
        	    If($jqr ==0){
        	       $jqr='Diskont'; 
        	    }else{
        	       $jqr='Event'; 
        	    }
        		echo '<option value="'.$grup3['id'].'">('.$jqr.') '.$grup3['judulqr'].'</option>';
        	}
        }
        ?>
        </select>
        </p>
        </div>
    
    	<input type="submit" class="btn btn-primary" value="Mulai Broadcast"/>
    	
    </form>
    </div>
    
    <?php 

$query = mysqli_query($con,"SELECT count(*) FROM bc WHERE status = 0");
$statusbc = mysqli_fetch_array($query,MYSQLI_NUM);

if ($statusbc[0] > 0) {

    ?>
    <h1>Broadcast Sementara Jalan</h1>
    <p> Broadcast sedang berjalan saat ini. Lihat status broadcast dibawah ini.Terima kasih</p>
        <div class="alert alert-success" role="alert">
        
        <div id="responsecontainer3"></div>
        </div>
        
        <div class="table-responsive">
    				<table class="table table-bordered" id="dataTable"  width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th>Judul</th>
                                <th>Pesan Email</th>
                                <th>Pesan WA</th>
                                <th>Tgl.BC</th>
                                <th>Target</th>
                                <th>Grup</th>
                                <th>Jml.Prospek</th>
                            </tr>
                        </thead>
                        <tbody>
                           <?php
                                
                                $qu=mysqli_query($con,"select ID,judul,isi,tgl_bc,status_target,grup_target,pesanwa from bc where status =0 order by id desc");
                                while($data=mysqli_fetch_row($qu)){
                                    
                                      If($data[4]==0){
                                         $ket ='Prosfek Tidak Aktif'; 
                                      }elseIf($data[4]==1){
                                         $ket ='Prosfek Aktif'; 
                                      }elseIf($data[4]==2){
                                         $ket ='Buyer';  
                                      }elseIf($data[4]==3){
                                         $ket ='Prospek Baca Email 25x keatas'; 
                                      }elseIf($data[4]==4){
                                         $ket ='Prospek Baca Email 50x keatas';  
                                      }elseIf($data[4]==5){
                                         $ket ='Prospek Baca Email 75x keatas';  
                                      }elseIf($data[4]==6){
                                         $ket ='Prospek Baca Email 100x keatas';   
                                      }elseIf($data[4]==7){
                                         $ket ='Semua Prospek';     
                                      }
                                      
                                      $q2 = mysqli_query($con,"SELECT count(id_member) as jm1 FROM gtm where id_grup='$data[5]'");
                                       $s2 = mysqli_fetch_array($q2,MYSQLI_NUM);
                                       $jm1 = $s2[0];
                                      
                                    echo "
                                    <tr>
                                        <td>
                                        <a onclick='return konfirmasi()' href='?page=broadnew&hapus=$data[0]'><img src='$weburl/images/user_delete.png' alt='Delete' title='Delete'/></a>
                                            $data[1] 
                                        </td>
                                        <td>
                                            $data[2] 
                                        </td>
                                        <td>
                                            $data[6] 
                                        </td>
                                        <td>
                                            $data[3] 
                                        </td>
                                        <td>
                                            $ket 
                                        </td>
                                        <td>
                                            $data[5]  
                                        </td>
                                        <td>
                                            $jm1  
                                        </td>
                                    </tr>
                                    ";
                                }
                           ?>
                        </tbody>
                    </table>
                    </div>
                    
    <?php } ?>



</div>