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

if( isset( $_REQUEST['aksi'] )){
    		$aksi = $_REQUEST['aksi'];
    		switch($aksi){
    		    
    			case 'hapusgrup':
    				//kode
    				
    				//$qu=mysqli_query($con,"select ID,tgl_gabung from prospek order by ID desc");
                    //while($data=mysqli_fetch_row($qu)){    
                                        
                        //$id = $data[0] ;
                        	
                        //$query = mysqli_query($con,"SELECT id_member FROM gtm WHERE id_member ='$id' and id_grup='$idgrup'");
                        //$check = mysqli_fetch_array($query,MYSQLI_NUM);
                        //if (isset($check[0])) {
                            //mysqli_query($con,"DELETE FROM gtm WHERE id_member='$check[0]' and id_grup='$idgrup'");
                            //mysqli_query($con,"DELETE FROM prospek WHERE ID='$check[0]'");
                        //}  
                    //} 
                    mysqli_query($con,"DELETE FROM gtm WHERE id_grup='$idgrup'");
    				//kode
    				echo '
    				<script language="JavaScript">document.location="index.php?page=grupprospek"</script>
            		    
                    <script>
                    	swal("Good Job","berhasil menghapus semua prospek","success");
                    </script>    
            		';
    				break;	
    		}
}
    
if (isset($_GET['hapus']) && is_numeric($_GET['hapus'])) {
		$hapus = $_GET['hapus'];
		mysqli_query($con,"DELETE FROM gtm WHERE id_member=$hapus");
		mysqli_query($con,"DELETE FROM prospek WHERE ID=$hapus");
		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> Data Prospek dengan id '.$hapus.' Telah Dihapus
            </div>
            
        <script>
        	        swal("Good Job","berhasil menghapus data prospek","success");
                </script>     
                
        <script language="JavaScript">document.location="index.php?page=grupprospek"</script>        
                
		';
		
		
	}
	
if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
		include 'editp.php';
		exit;
}

if (isset($_GET['reset']) && is_numeric($_GET['reset'])) {
	$user_id = $_GET['reset'];
	mysqli_query($con,"DELETE FROM gtm WHERE id_member='$user_id' AND id_grup='$idgrup'");
	$query = mysqli_query($con,"SELECT ID FROM champ WHERE grup='$idgrup' 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;
		}
	mysqli_query($con,"INSERT INTO gtm VALUES (NULL,'$user_id','$idgrup','$item',NOW(),0,'$stsadd',NOW(),0,0,0,NOW(),'',1,0)");
	
	echo '<script>
        	        swal("Good Job","berhasil direset pesan","success");
                </script>         
		';
		
}

if (isset($_GET['aktif']) && is_numeric($_GET['aktif'])) {
	$user_id = $_GET['aktif'];
    mysqli_query($con,"UPDATE gtm SET status = 1 WHERE id_member='$user_id' AND id_grup='$idgrup'");
    echo '<script>
        	swal("Good Job","prospek tersebut berhasil diaktifkan","success");
                </script>         
		';
}	
	
if (isset($_GET['ubah']) && is_numeric($_GET['ubah'])) {
		$ubah = $_GET['ubah'];
		
		mysqli_query($con,"update gtm set status=2 where id_member='".$ubah."'");
  
		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> Data Prospek dengan id '.$ubah.' berhasil diubah status menjadi buyer
            </div>
            
        <script>
        	        swal("Good Job","berhasil diubah status menjadi buyer","success");
                </script>     
                
		';
		
	}	
	
//if (isset($_POST['grup'])){ 
if (isset($idgrup)){
	
	$grup = mysqli_real_escape_string($con,$_POST['grup']); 
			
	?>
	
	<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> Grup <?php echo $idgrup ?></a></li>
                        <li class="breadcrumb-item active" aria-current="page"><i class="fas fa-list"></i> Prospek</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. Klik <?php echo "<img src='$weburl/images/user_delete.png' alt='Delete' title='Delete'/> " ?>untuk menghapus. Klik <?php echo "<img src='$weburl/images/user_edit.png' alt='Edit' title='Edit'/> " ?> untuk mengedit. Klik <?php echo "<img src='$weburl/images/email_add.png' alt='Reset' title='Reset'/> " ?> untuk mereset pengiriman email dari awal lagi. Klik <?php echo "<img src='$weburl/images/email_edit.png' alt='Aktifkan' title='Aktifkan'/> " ?> untuk mengubah prospek menjadi aktif.<br>
            2. Sebelum anda ubah jadi buyer, pastikan prospek memang benar pernah melakukan transfer ke rekening anda.<br>
            3. Status Email Auto <a href="#" class="btn btn-primary btn-md"><i class="fas fa-times"></i> Next Email</a> berarti prospek tersebut masih dapat menerima email selanjutnya, saat email auto ditambahkan lagi.
            </div>
            </div>
            
            <form action="" method="post">
            <p><label>Pilih Grup</label><select name="grup" id="cari" class="form-control">
            <?php
            $query = mysqli_query($con,"SELECT id,nama FROM grup");
            if (mysqli_num_rows($query) > 0) {
            	while ($grup = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
            		echo '<option value="'.$grup['id'].'">'.$grup['nama'].' (Grup '.$grup['id'].')</option>';
            	}
            }
            ?>
            </select>
            <input class="btn btn-primary" type="submit" name="pilih" value="Pilih Grup"/></p>
            </form>
            
            <!-- <form method="post" action="grup/exportcsv.php" align="center">
                <input type="hidden" name="idgrup" value="<?php echo $idgrup ?>">
                     Download File : <input type="submit" name="export" value="CSV Export" class="btn btn-success" /> 
                </form> -->
            <div align="right">    
            <button class="btn btn-success" data-toggle="modal" data-target="#modalForm" >
                CSV Export
            </button>    
            </div>
				
	<div class="table-responsive">
				<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Aksi</th>
                            <th>Nama Prospek</th>
                            <th>Email<br>
                            Handphone/WA<br>
                            Tgl.Gabung</th>
                            <th>Tgl.Baca Email<br>Terakhir</th>
                            <th>Sukses Ngirim <br>Email</th>
                            <th>Gagal Ngirim <br> Email </th>
                            <th>Email dibaca</th>
                            <th>Status <br> Pesan Auto<br>(Tanggal,Jam, ID Pesan)</th>
                            <th>Status <br> Prospek</th>
                        </tr>
                    </thead>
                    <tbody>
                       <?php
                            
                            $qu=mysqli_query($con,"select prospek.ID,prospek.nama,prospek.email,prospek.hp,gtm.tgl_gabung,gtm.status,gtm.next_item_id,gtm.next_post,gtm.nextemail from gtm, prospek where gtm.id_member=prospek.ID and gtm.id_grup ='$idgrup' order by prospek.ID desc");
                            while($data=mysqli_fetch_row($qu)){
                                
                                $hp = $data[3];
                        	    $hp =substr($hp, 1,12);
                        	    $hp = '62'.$hp;
                        	    $ctw ='<a href="https://api.whatsapp.com/send?phone='.$hp.'&text=%20Hallo '.$data[1].'" target="_blank"><img src="'.$weburl.'/images/WA.jpg" width="16"></a>';
                        	    If (strlen($hp)>9){
                        	        $ctw = $ctw;
                        	    }else{
                        	        $ctw='';
                        	    }
                                
                                If($data[5]==0){
                                    $ket ='<button type="button" class="btn btn-warning">Belum Aktif</button>';
                                    $taktif = "<a href='?page=grupprospek&aktif=$data[0]'><img src='$weburl/images/email_edit.png' alt='Aktifkan' title='Aktifkan'/></a>";
                                }elseIf($data[5]==1){
                                    $ket ='<button type="button" class="btn btn-success">Aktif/Ubah Jadi buyer?</button>';
                                    $taktif ='';
                                }else{
                                    $ket ='<button type="button" class="btn btn-primary">Buyer</button>';
                                    $taktif ='';
                                }
                                
                                
                                $q1 = mysqli_query($con,"SELECT sum(kirim) FROM gtm  where id_member ='$data[0]' and id_grup='$idgrup'");
                                $s1 = mysqli_fetch_array($q1,MYSQLI_NUM);
                                $jml1 = $s1[0];
                                
                                $q3 = mysqli_query($con,"SELECT sum(gagal) FROM gtm  where id_member ='$data[0]' and id_grup='$idgrup'");
                                $s3 = mysqli_fetch_array($q3,MYSQLI_NUM);
                                $jml3 = $s3[0];
                                
                                $q4 = mysqli_query($con,"SELECT sum(baca) FROM gtm  where id_member ='$data[0]' and id_grup='$idgrup'");
                                $s4 = mysqli_fetch_array($q4,MYSQLI_NUM);
                                $jml4 = $s4[0];
                                
                                $tglbaca = mysqli_query($con,"SELECT tgl_baca,id_member,baca FROM gtm where id_member ='$data[0]' and id_grup='$idgrup'");
                                $tglbaca = mysqli_fetch_array($tglbaca,MYSQLI_NUM);
                                $jbaca = $tglbaca[2];
                                IF($jbaca==0){
                                    $tglbaca = '0000-00-00 00:00:00';
                                }else{    
                                    $tglbaca = $tglbaca[0];
                                }
                                
                                If($data[8]==1){
                                    If($data[7]<=0){
                                        $ketnext ='<a href="#" class="btn btn-icon icon-left btn-danger"><i class="fas fa-times"></i> Stop</a>';
                                    }else{
                                        $ketnext ='<a href="#" class="btn btn-icon icon-left btn-success"><i class="fas fa-check"></i> Play '.$data[7].' ('.$data[6].')</a>';
                                    }
                                }else{
                                    If($data[7]<=0){
                                        $ketnext ='<a href="#" class="btn btn-icon icon-left btn-danger"><i class="fas fa-times"></i> Stop</a>';
                                    }else{    
                                        $ketnext ='<a href="#" class="btn btn-primary btn-md"><i class="fas fa-times"></i> Next Email</a>';
                                    }
                                }    
                                
                                $query = mysqli_query($con,"SELECT customdata,id_member FROM gtm where id_member ='$data[0]' and id_grup='$idgrup'");
                            	if (mysqli_num_rows($query)) {
                            		$prospek = mysqli_fetch_array($query,MYSQLI_ASSOC);
                            		$customdata = unserialize($prospek['customdata']);
                                
                                $query = mysqli_query($con,"SELECT id,nama,custom FROM grup where id='$idgrup'");
                        		while ($gr = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
                        			if ($gr['id'] == $grup) {
                        				$custom = unserialize($gr['custom']);
                        				if (is_array($custom)) {
                        					foreach ($custom as $custom) {
                        					$cket = "$custom :";
                        					$cket1 = $customdata[txtonly($custom)]; 
                        					
                        					
                        					}
                        				}
                        			}
                        		  }	
                        		}
            				    
                                echo "
                                <tr>
                                    
                                    <td>
                                   <a onclick='return konfirmasi()' href='?page=grupprospek&hapus=$data[0]'><img src='$weburl/images/user_delete.png' alt='Delete' title='Delete'/></a> 
                                   <a href='?page=grupprospek&edit=$data[0]'><img src='$weburl/images/user_edit.png' alt='Edit' title='Edit'/></a>
                                   <a href='?page=grupprospek&reset=$data[0]'><img src='$weburl/images/email_add.png' alt='Edit' title='Reset Pesan'/></a>
                                    
                                    </td>
                                    
                                    <td>
                                        $data[1] <br> $cket $cket1
                                    </td>
                                    <td>
                                        $data[2] 
                                    <br>
                                        $ctw $data[3] 
                                    <br>
                                        $data[4] 
                                    </td>
                                    <td>
                                        $tglbaca 
                                    </td>
                                    <td>
                                        $jml1 
                                    </td>
                                    <td>
                                        $jml3 
                                    </td>
                                    <td>
                                        $jml4 
                                    </td>
                                    <td>
                                        $ketnext 
                                    </td>
                                    <td>
                                    <a onclick='return konfirmasi2()' href='?page=grupprospek&ubah=$data[0]'>$ket</a> 
                                     $taktif     
                                    </td>
                                    
                                    
                                </tr>
                                ";
                            }
                       ?>
                    </tbody>
                    </table>
                
                <div style="width:30%; float:left">
                <Font size=4>History Joint Prospek Per Bulan</font>            
            	<?php
            	
            	$query = mysqli_query($con,"SELECT COUNT(id) AS jmlmember, DATE_FORMAT( tgl_gabung, '%Y-%m' ) AS bulan FROM gtm WHERE id_grup='$idgrup' GROUP BY bulan ORDER BY bulan DESC");
                    while ($stat = mysqli_fetch_array($query,MYSQLI_ASSOC)) {	
                    	echo '<li>'.$stat['bulan'].': '.$stat['jmlmember'].' Prospek</li>';
                    }
                ?>
                </div>
                
                <div style="width:30%; float:left">
                <Font size=4>History Joint Prospek</font>            
            	<?php
            	
            	$query = mysqli_query($con,"SELECT  nama,  email ,  id_member 
                    FROM  prospek ,  gtm 
                    WHERE  gtm.id_member =  prospek.ID AND gtm.id_grup='$idgrup'
                    ORDER BY prospek.tgl_gabung DESC
                    LIMIT 0 , 10");
                    while ($prospek = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
                    	echo '<li>'.stripslashes($prospek['nama']).' (<a href="mailto:'.$prospek['email'].'">email</a>)</li>';
                    }
                ?>
                </div>
                
                </div>
                
                <form class="form-horizontal" role="form" action="index.php?page=prospekgrup&aksi=hapusgrup" method="POST" onsubmit="return hapusConfirm();">
				<button class="btn btn-secondary">
				<span class="btn-label">
					<i class="fa fa-minus"></i>
				</span>
				Hapus Prospek Grup
				</button>
				</form>
    <?php
		
} else { 

?>    
 
    <div class="card-body">            
        <div class="container">
    <form action="" method="post">
    <p><label>Pilih Grup</label><select name="grup" id="cari" class="form-control">
    <?php
    $query = mysqli_query($con,"SELECT id,nama FROM grup");
    if (mysqli_num_rows($query) > 0) {
    	while ($grup = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
    		echo '<option value="'.$grup['id'].'">'.$grup['nama'].' (Grup '.$grup['id'].')</option>';
    	}
    }
    ?>
    </select>
    <input class="btn btn-primary" type="submit" name="pilih" value="Pilih Grup"/></p>
    </form>
    </div></div>
    <?php

}
    ?>
    </div>
    
<!-- Modal -->
<div class="modal fade" id="modalForm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Tutup</span>
                </button>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">
                <p class="statusMsg"></p>
                
                 <form method="post" action="grup/exportcsv.php" class="form-horizontal">
                <div class="form-group">
                 <input type="checkbox" name="c1" value="nama">Nama, Email, HP </div>
                 <div class="form-group">
                 <input type="checkbox" name="c2" value="nama">Tgl.Gabung, IP
                 </div>
                <input type="hidden" name="idgrup" value="<?php echo $idgrup ?>">
                     Download File : <input type="submit" name="export" value="CSV Export" class="btn btn-success" /> 

                </form> 
                
            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Keluar</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal end-->    
