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

$q1 = mysqli_query($con,"SELECT count(id) FROM prospek");
   $s1 = mysqli_fetch_array($q1,MYSQLI_NUM);
   $jml1 = $s1[0];
   
$q2 = mysqli_query($con,"SELECT count(id) FROM bc");
   $s2 = mysqli_fetch_array($q2,MYSQLI_NUM);
   $jml2 = $s2[0];
   
$q3 = mysqli_query($con,"SELECT sum(jmlunsub) FROM grup");
   $s3 = mysqli_fetch_array($q3,MYSQLI_NUM);
   $jml3 = $s3[0];

$q4 = mysqli_query($con,"SELECT sum(kirim),sum(gagal),sum(baca) FROM bc");
   $s4 = mysqli_fetch_array($q4,MYSQLI_NUM);
    $or1 = $s4[0] - $s4[1];
    $or2 = $s4[2] / $or1;
    $or = $or2 * 100;
    $or = number_format($or,2);   
    
if(isset($_POST['delete_submit'])){
        
        mysqli_query($con,"DELETE FROM pesan1");
        
        echo ' <script>
	        swal("Good Job","berhasil dihapus","success");
        </script>
	';
    }
?>

<div class="container-fluid">
    
<?php
if($update <> $decode3['up1']) {
?>
<div class="alert alert-primary alert-dismissible fade show">
 Anda menggunakan #AutoBiznisTop update lama. Segera lakukan Instal Update <a href='index.php?page=up'><b>KLIK DISINI</b></a>
 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
 <span aria-hidden="true">&times;</span>
 </button> </div>
<?php } ?>     

          <!-- Breadcrumbs-->
          
           <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> Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="#"><i class="far fa-file"></i> Home</a></li>
                      </ol>
                    </nav>

          <!-- Icon Cards-->
          <div class="row">
            <div class="col-xl-3 col-sm-6 mb-3">
              <div class="card text-white bg-primary o-hidden h-100">
                <div class="card-body">
                  <div class="card-body-icon">
                    <i class="fas fa-fw fa-comments"></i>
                  </div>
                  <div class="mr-5"><?php echo "$jml2"; ?> Broadcast</div>
                </div>
                <a class="card-footer text-white clearfix small z-1" href="index.php?page=broadtrack">
                  <span class="float-left">View Details</span>
                  <span class="float-right">
                    <i class="fas fa-angle-right"></i>
                  </span>
                </a>
              </div>
            </div>
            <div class="col-xl-3 col-sm-6 mb-3">
              <div class="card text-white bg-warning o-hidden h-100">
                <div class="card-body">
                  <div class="card-body-icon">
                    <i class="fas fa-fw fa-list"></i>
                  </div>
                  <div class="mr-5"><?php echo "$jml3"; ?> Unsub</div>
                </div>
                <a class="card-footer text-white clearfix small z-1" href="#">
                  <span class="float-left">View Details</span>
                  <span class="float-right">
                    <i class="fas fa-angle-right"></i>
                  </span>
                </a>
              </div>
            </div>
            <div class="col-xl-3 col-sm-6 mb-3">
              <div class="card text-white bg-success o-hidden h-100">
                <div class="card-body">
                  <div class="card-body-icon">
                    <i class="fas fa-fw fa-shopping-cart"></i>
                  </div>
                  <div class="mr-5"><?php echo "$or"; ?>% Open Rate</div>
                </div>
                <a class="card-footer text-white clearfix small z-1" href="#">
                  <span class="float-left">View Details</span>
                  <span class="float-right">
                    <i class="fas fa-angle-right"></i>
                  </span>
                </a>
              </div>
            </div>
            <div class="col-xl-3 col-sm-6 mb-3">
              <div class="card text-white bg-danger o-hidden h-100">
                <div class="card-body">
                  <div class="card-body-icon">
                    <i class="fas fa-fw fa-life-ring"></i>
                  </div>
                  <div class="mr-5"><?php echo "$jml1"; ?> Prospek</div>
                </div>
                <a class="card-footer text-white clearfix small z-1" href="index.php?page=listprosfek">
                  <span class="float-left">View Details</span>
                  <span class="float-right">
                    <i class="fas fa-angle-right"></i>
                  </span>
                </a>
              </div>
            </div>
          </div>

          <!-- Area Chart Example
          <div class="card mb-3">
            <div class="card-header">
              <i class="fas fa-chart-area"></i>
              Grafik</div>
            <div class="card-body">
               <canvas id="myAreaChart" width="100%" height="30"></canvas> 
               <canvas id="myChart"></canvas>
                
            </div>
            
            <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
          </div>
          <form class="form-horizontal" method="post" enctype="multipart/form-data">
          <input type="text" name="username" class="form-control" id="username" value="<?php echo $weburl ?>"> <span id="pesan"></span>
          </form>-->
          
          <!--<div class="alert alert-success alert-dismissable" style="margin-top:20px">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h6><i class="icon glyphicon glyphicon-remove"></i> 
                <?php
                $sumber ="aHR0cHM6Ly9iaXpuaXMudG9wL3VybC9jZWt1cC5odG1s";
				$sumber =  bacaURL(base64_decode($sumber));
                $split = explode('<h6>', $sumber);
                $splitLagi = explode('</h6>', $split[1]);
                echo $splitLagi[0];
                ?>
                </div> -->
        
        
        <div class="table-responsive">    
        <div class="card-body" style="width: 800px;margin: 0px auto;">
		<canvas id="myChart"></canvas></div>
		</div>
                
        <script>
		var ctx = document.getElementById("myChart").getContext('2d');
		var myChart = new Chart(ctx, {
			type: 'pie',
			data: {
				labels: ["Prospek Aktif", "Prospek Tidak Aktif", "Prospek Buyer"],
				datasets: [{
					label: '',
					data: [
					<?php 
					$jmlaktif = mysqli_query($con,"select * from prospek where sts=1");
					echo mysqli_num_rows($jmlaktif);
					?>, 
					<?php 
					$jmltdkaktif = mysqli_query($con,"select * from prospek where sts=0");
					echo mysqli_num_rows($jmltdkaktif);
					?>,
					<?php 
					$jmlbuyer = mysqli_query($con,"select * from gtm where status=2");
					echo mysqli_num_rows($jmlbuyer);
					?>
					],
					backgroundColor: [
					'rgba(255, 99, 132, 0.2)',
					'rgba(54, 162, 235, 0.2)'
					],
					borderColor: [
					'rgba(255,99,132,1)',
					'rgba(54, 162, 235, 1)'
					],
					borderWidth: 1
				}]
			},
			options: {
				scales: {
					yAxes: [{
						ticks: {
							beginAtZero:true
						}
					}]
				}
			}
		});
	</script>        
                
        <div class="row">
          <!-- DataTables Example -->
          <div class="col-lg-8">
          <div class="card mb-3">
            <div class="card-header">
              <i class="fas fa-table"></i>
              Pesan</div>
            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                  <thead>
                    <tr>
                      <th>Tanggal</th>
                      <th>Pesan</th>
                      <th>Time</th>
                    </tr>
                  </thead>
                  <tfoot>
                    <tr>
                      <th>Tanggal</th>
                      <th>Pesan</th>
                      <th>Time</th>
                    </tr>
                  </tfoot>
                  <tbody>
                       
                    <?php
                            
                            $qu=mysqli_query($con,"select * from pesan1 where kodepesan <>10 order by id DESC LIMIT 0,50");
                            while($data=mysqli_fetch_row($qu)){
                                
                                $tgl = time_since(strtotime($data[3]));
                                
                                echo "
                                <tr>
                                    <td>
                                        $data[3] 
                                    </td>
                                    <td>
                                        $data[1] 
                                    </td>
                                    <td>
                                        $tgl 
                                    </td>
                                </tr>
                                ";
                            }
                       ?>
                       
                  </tbody>
                </table>
              </div>
            </div>
            <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
            <form name="form" action="" method="post"/>
                <input type="submit" class="btn btn-danger" name="delete_submit" value="Clear Pesan"/>
                </form>
          </div>
          </div>
          
          <div class="col-lg-4">
              <div class="card mb-3">
                <div class="card-header">
                  <i class="list-unstyled list-unstyled-border"></i>
                  5 Top Prospek Baru</div>
                <div class="card-body">
                    
                    <?php
                            $qu=mysqli_query($con,"select * from prospek order by ID desc LIMIT 0,5");
                            while($data=mysqli_fetch_row($qu)){
                                
                                $tgl = time_since(strtotime($data[3]));
                                $sts = $data[6];
                                If($sts==0){
                                    $sts ='Prospek belum aktif.';
                                }else{    
                                    $sts ='Prospek sudah aktif.';
                                }
                                
                                echo "
                                <li class='media'>
                              <img class='mr-3 rounded-circle' width='50' src='images/avatar-1.png' alt='avatar'>
                              <div class='media-body'>
                                <div class='float-right text-primary'>$tgl</div>
                                <div class='media-title'>$data[1]</div>
                                <span class='text-small text-muted'>$sts</span>
                              </div>
                            </li>
                                ";
                                
                            }
                       ?>
                    
                    

                </div>
                <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
              </div>
            
            
            
              <div class="card mb-3">
                <div class="card-header">
                  <i class="list-unstyled list-unstyled-border"></i>
                  5 Top Prospek Buka Email</div>
                <div class="card-body">
                    
                    <?php
                            $qu=mysqli_query($con,"select prospek.nama,prospek.tgl_gabung,gtm.baca,gtm.id_grup from gtm,prospek where gtm.id_member=prospek.ID order by gtm.baca desc LIMIT 0,5");
                            while($data=mysqli_fetch_row($qu)){
                                
                                echo "
                                <li class='media'>
                              <img class='mr-3 rounded-circle' width='50' src='images/avatar-1.png' alt='avatar'>
                              <div class='media-body'>
                                <div class='float-right text-primary'>Grup: $data[3]</div>
                                <div class='media-title'>$data[0]</div>
                                <span class='text-small text-muted'>Jumlah buka email: $data[2] </span>
                              </div>
                            </li>
                                ";
                                
                            }
                       ?>
                    
                    

                </div>
                <div class="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
                
              </div>
          </div>
            
            
          </div>
          
        </div>  

</div>

        
        