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

 $query1 = mysqli_query($con,"select count(*) FROM bc where status=1 order by id 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 bc 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> Email Broadcast dengan id '.$hapus.' Telah Dihapus
            </div> -->
        
        <script>
        	swal("Good Job","email broadcast berhasil dihapus","success");
        </script>
        
		';
		
	}
	
?>

<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> <?php echo "$jml1";?> Tracking Email</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. Buat broadcast baru <a href="index.php?page=broadnew">Klik Disini</a>. <br>
            2. Berdasarkan penelitian dari MarketingProfs jika Open Rate dibawah dari <div class="badge badge-success"> 25% </div> maka perlu lakukan evaluasi kembali email yang anda kirimkan.<br>
            3. Tombol BC untuk broadcast email kembali.<br>
            4. Tombol Delete untuk menghapus.
            </div>
            </div>
        </div> 
        
            
        <div class="card-body">    
            
            <button class="btn btn-secondary" data-toggle="modal" data-target="#modalForm">
                Grafik Tracking Email
            </button>
			<br>	
            <div class="table-responsive">
				<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                         <thead>
                        <tr>
                            <th>Aksi</th>
                            <th>Judul / Email Sender</th>
                            <th>Nama (No.Grup)</th>
                            <th>Target</th>
                            <th>Tgl.Kirim</th>
                            <th>Jumlah Unsub</th>
                            <th>Jumlah Kirim</th>
                            <th>Gagal Kirim</th>
                            <th>Jumlah Baca</th>
                            <th>Open Rate</th>
                        </tr>
                    </thead>
                    <tbody>
                       <?php
                            $qu=mysqli_query($con,"select * FROM bc where status=1 order by id desc");
                            while($data=mysqli_fetch_row($qu)){
                                
                                $or1 = $data[8] - $data[6];
                                $or2 = $data[7] / $or1;
                                $or = $or2 * 100;
                                $or = number_format($or,2);
                                
                                If($or >25){
                                    $ketor ='<div class="badge badge-success">'.$or.'%</div>';
                                }else{    
                                    $ketor=$or .'%';
                                }
                                
                                If($data[10]==0){
                                    $target ='Prospek Belum Akif';
                                }elseif($data[10]==1){
                                    $target ='Prospek Akif';
                                }elseif($data[10]==2){
                                    $target ='Buyer';    
                                }elseif($data[10]==3){
                                    $target ='Prospek Baca Email 25x keatas'; 
                                }elseif($data[10]==4){
                                    $target ='Prospek Baca Email 50x keatas'; 
                                }elseif($data[10]==5){
                                    $target ='Prospek Baca Email 75x keatas';
                                }elseif($data[10]==6){
                                    $target ='Prospek Baca Email 100x keatas'; 
                                }elseif($data[10]==7){
                                    $target ='Semua Prospek';      
                                }    
                                
                                $id = $data[0];
                                $idgrup = $data[4];
                                $emailsender = $data[11];
                                
                                IF(substr($data[16],6,1)=='M'){;
                                    $emailhost = $data[16];
                                }else{
                                    If ($emailsender==0){
    			
                        			$query = mysqli_query($con,"SELECT * FROM akunn WHERE id=1");
                        	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
                        	        if (isset($check[0])) {
                            	        $emailhost = $check[5]; 
                        	        }   
                            		}else{
                            		    $query = mysqli_query($con,"SELECT * FROM emailsender WHERE id=$emailsender");
                            	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
                            	        if (isset($check[0])) {
                            				$emailhost = $check[2];
                            	        }    
                            		} 
                                }	
                        		
                        		$query = mysqli_query($con,"SELECT * FROM grup WHERE id='$idgrup'");
                    	        $check = mysqli_fetch_array($query,MYSQLI_NUM);
                    	        if (isset($check[0])) {
                        	        $ngrup = $check[1]; 
                    	        }
                                
                                echo "
                                <tr>
                                    <td>
                                    <a onclick='return konfirmasi()' href='?page=broadtrack&hapus=$id'><img src='$weburl/images/user_delete.png' alt='Delete' title='Delete'/></a>
                                    
                                    <a type='button' class='btn btn-icon btn-round btn-default' href='index.php?page=broadnew&aksi=BC&id=$id' >BC</a>
                                    
                                    </td>
                                    <td>
                                        $data[1] <br>
                                        $emailhost
                                    </td>
                                    <td>
                                        $ngrup ($data[4]) 
                                    </td>
                                    <td>
                                        $target 
                                    </td>
                                    <td>
                                        $data[3] 
                                    </td>
                                    <td>
                                        $data[12] 
                                    </td>
                                    <td>
                                        $data[8] 
                                    </td>
                                    <td>
                                        $data[6] 
                                    </td>
                                    <td>
                                        $data[7] 
                                    </td>
                                    <td>
                                        $ketor
                                    </td>
                                </tr>
                                ";
                            }
                       ?>
                    </tbody>
                </table>
                </div>
            </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>
                
                <!-- mulai -->
                <?php
$track2 = mysqli_query($con, "SELECT ID,sum(kirim) as kirim,sum(gagal) as gagal,sum(baca) as baca,sum(unsub) as unsub FROM bc order by ID asc");
?>

<style type="text/css">
            .container {
                width: 90%;
                margin: 5px auto;
            }
    </style>
    
    <div class="container">
            <canvas id="demobar" width="100" height="100"></canvas>
    </div>

      	<script  type="text/javascript">

    	  var ctx = document.getElementById("demobar").getContext("2d");
    	  var data = {
    	            labels: ['Kirim','Gagal','Buka','Unsub'],
            datasets: [
            {
              label: "Grafik Tracking Email",
              data: [<?php while ($p = mysqli_fetch_array($track2)) { 
                  echo '"' . $p['kirim'] . '",';
                  echo '"' . $p['gagal'] . '",';
                  echo '"' . $p['baca'] . '",';
                  echo '"' . $p['unsub'] . '",';
              }?>],
              backgroundColor: [
                '#29B0D0',
                '#2A516E',
                '#F07124',
                '#CBE0E3',
                '#979193'
              ]
            }
            ]
            };

    	  var myBarChart = new Chart(ctx, {
    	            type: 'bar',
    	            data: data,
    	            options: {
    	            barValueSpacing: 20,
    	            scales: {
    	              yAxes: [{
    	                  ticks: {
    	                      min: 0,
    	                  }
    	              }],
    	              xAxes: [{
    	                          gridLines: {
    	                              color: "rgba(0, 0, 0, 0)",
    	                          }
    	                      }]
    	              }
    	          }
    	        });
    	</script>
                <!-- selesai -->
                
            </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-->