<?php
defined("BASEPATH") or exit("No direct access allowed");
 $query1 = mysqli_query($con,"select count(*) from prospek order by tgl_gabung desc");
   $statusbc1 = mysqli_fetch_array($query1,MYSQLI_NUM);
   $jml1 = $statusbc1[0];
   
   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 '
		<script language="JavaScript">document.location="index.php?page=listprosfek"</script>
		<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 dihapus","success");
        </script>

		';
		
	}
	
	if(isset($_POST['delete_submit'])){
        $idArr = $_POST['checked_id'];
        foreach($idArr as $id){
    
        mysqli_query($con,"DELETE FROM prospek WHERE ID=".$id);
        mysqli_query($con,"DELETE FROM gtm WHERE id_member=".$id);
        
        } echo ' <script>
	        swal("Good Job","Data yang diceklist berhasil dihapus","success");
        </script>
	';
    }
    	

    if( isset( $_REQUEST['aksi'] )){
    		$aksi = $_REQUEST['aksi'];
    		switch($aksi){
    		    case 'edit':
				    include 'editp.php';
				    break;
    			case 'hapus':
    				//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 status=0");
                        $check = mysqli_fetch_array($query,MYSQLI_NUM);
                        if (isset($check[0])) {
                            mysqli_query($con,"DELETE FROM gtm WHERE id_member='$check[0]'");
                            mysqli_query($con,"DELETE FROM prospek WHERE ID='$check[0]'");
                        }  
                    } 
    				//kode
    				echo '
    				<script language="JavaScript">document.location="index.php?page=listprosfek"</script>
            		<!-- <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> Berhasil menghapus semua prospek yang tidak aktif.
                        </div> -->
                        
                    <script>
                    	swal("Good Job","berhasil menghapus semua prospek yang tidak aktif","success");
                    </script>    
            		';
    				break;
    			case 'hapusall':
    				//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'");
                        //$check = mysqli_fetch_array($query,MYSQLI_NUM);
                        //if (isset($check[0])) {
                            
                            mysqli_query($con,"DELETE FROM gtm"); 
                            //WHERE id_member='$check[0]'
                            mysqli_query($con,"DELETE FROM prospek");
                            //WHERE ID='$check[0]'
                        //}  
                    //} 
    				//kode
    				echo '
    				<script language="JavaScript">document.location="index.php?page=listprosfek"</script>
            		<!-- <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> Berhasil menghapus semua prospek.
                        </div> -->
                        
                    <script>
                    	swal("Good Job","berhasil menghapus semua prospek","success");
                    </script>    
            		';
    				break;	
    		}
    }else{ 
    
?>
<div class="section-header">

<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> <?php echo "$jml1";?> Prospek (All)</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. Data Prospek ini berasal dari Form Opt, Import dan integrasi dengan google form/sheet.<br>
            2. Status Email Auto Play berarti email autonya masih aktif jalan. Ketika status Stop maka prospek tidak dapat menerima email auto lagi. <br>
            3. Status Belum Aktif karena prospek belum membuka email.<br>
            4. Ketika anda mengklik Hapus ALL Prospek, maka data akan terhapus secara permanen, tidak dapat dikembalikan.
            </div>
            </div>
            
                <div class="table-responsive">
                    <form name="form" action="" method="post" onsubmit="return deleteConfirm();"/>
				<table id="example" class="table table-bordered" id="dataTable" width="100%" cellspacing="0"> 
                    <thead>
                        <tr>
                            <th>Ceklist</th>
                            <th>Nama Prospek</th>
                            <th>Email</th>
                            <th>Handphone/WA</th>
                            <th>Tgl.Gabung</th>
                            <th>Jumlah Grup <br> Yang Diikuti</th>
                            <th>Sukses Ngirim <br>Email</th>
                            <th>Gagal Ngirim <br> Email </th>
                            <th>Email dibaca</th>
                            <th>Status <br> Prospek</th>
                        </tr>
                    </thead>
                    <tbody>
                       <?php
                            
                            $qu=mysqli_query($con,"select * from prospek order by id desc");
                            while($data=mysqli_fetch_row($qu)){
                                
                                $hp = $data[5];
                        	    $hp =substr($hp, 1,12);
                        	    $hp = $kodenegara.''.$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[6]==0){
                                    $ket ='Belum Aktif';
                                }else{
                                    $ket ='Aktif';
                                }
                                
                                $q2 = mysqli_query($con,"SELECT count(id) FROM gtm  where id_member ='$data[0]'");
                                $s2 = mysqli_fetch_array($q2,MYSQLI_NUM);
                                $jml2 = $s2[0];
                                
                                $q1 = mysqli_query($con,"SELECT sum(kirim) FROM gtm  where id_member ='$data[0]'");
                                $s1 = mysqli_fetch_array($q1,MYSQLI_NUM);
                                $jml1 = $s1[0];
                                
                                $q3 = mysqli_query($con,"SELECT sum(gagal) FROM gtm  where id_member ='$data[0]'");
                                $s3 = mysqli_fetch_array($q3,MYSQLI_NUM);
                                $jml3 = $s3[0];
                                
                                $q4 = mysqli_query($con,"SELECT sum(baca) FROM gtm  where id_member ='$data[0]'");
                                $s4 = mysqli_fetch_array($q4,MYSQLI_NUM);
                                $jml4 = $s4[0];
                                
                                $status = mysqli_query($con,"SELECT status FROM gtm  where id_member ='$data[0]'");
                                $status = mysqli_fetch_array($status,MYSQLI_NUM);
                                $status = $status[0];
                                
                                If($status==0){
                                    $ket ='<button type="button" class="btn btn-warning">Belum Aktif</button>';
                                }elseIf($status==1){
                                    $ket ='<button type="button" class="btn btn-success">Aktif</button>';
                                }else{
                                    $ket ='<button type="button" class="btn btn-primary">Buyer</button>';
                                }
                                                        
                                echo "
                                <tr>
                                    
                                    <td>
                                    <!-- <a onclick='return konfirmasi()' href='?page=listprosfek&hapus=$data[0]'><img src='$weburl/images/user_delete.png' alt='Delete' title='Delete'/></a> 
                                    <a href='index.php?page=listprosfek&aksi=edit&id=$data[0]'><img src='$weburl/images/user_edit.png' alt='Edit' title='Edit'/></a>-->
                                    
                                    <input type='checkbox' name='checked_id[]' class='checkbox' value='$data[0]'/>
                                    </td>
                                    
                                    <td>
                                        $data[1] 
                                    </td>
                                    <td>
                                        $data[2] 
                                    </td>
                                    <td>
                                        $ctw $data[5] 
                                    </td>
                                    <td>
                                        $data[3] 
                                    </td>
                                    <td>
                                        $jml2 
                                    </td>
                                    <td>
                                        $jml1 
                                    </td>
                                    <td>
                                        $jml3 
                                    </td>
                                    <td>
                                        $jml4 
                                    </td>
                                    <td>
                                        $ket 
                                    </td>
                                    
                                    
                                </tr>
                                ";
                            }
                       ?>
                    </tbody>
                </table>
    
                <input type="submit" class="btn btn-danger" name="delete_submit" value="Hapus Data Ceklist"/>
				</form>
				
				<?php
                $q1 = mysqli_query($con,"SELECT count(ID) FROM gtm where status=0");
                   $s1 = mysqli_fetch_array($q1,MYSQLI_NUM);
                   $jml1 = $s1[0];
                ?>
                <form class="form-horizontal" role="form" action="index.php?page=listprosfek&aksi=hapus" method="POST">
				<button class="btn btn-secondary">
				<span class="btn-label">
					<i class="fa fa-minus"></i>
				</span>
				Clear <?php echo "$jml1"?> Prospek Tidak Aktif
				</button>
				</form>
				
				<form class="form-horizontal" role="form" action="index.php?page=listprosfek&aksi=hapusall" method="POST" onsubmit="return hapusConfirm();">
				<button class="btn btn-secondary">
				<span class="btn-label">
					<i class="fa fa-minus"></i>
				</span>
				Hapus All Prospek
				</button>
				</form>
				
				</div>
                
                
            </div>
            
            
            <!-- /.box-body -->
<!-- /.row -->
<?php
}
?>
