<?php
$stmt = $pdo->prepare("SELECT 
    SUM(user_hs_aktif) AS user_hs_aktif
    FROM v_jumlah_pelanggan $SqlDataWHIdM");
  $stmt->execute();
  $datauser = $stmt->fetchAll();
?>
<section class="content" style="zoom : 85%;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h4>Pelanggan Aktif</h4>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <table id="example3" class="table table-bordered table-striped">
              <thead>
              <tr class="bg-success">
                <th style="max-width: 20px">No</th>
                <th>Nama</th>
                <th>NIK</th>
                <th>Id Pelanggan</th>
                <?php if($datauser[0]['user_hs_aktif'] > 0) { ?>
                <th>Akun Hotspot</th>
                <?php } ?>
                <th>Alamat</th>
                <th>Koordinat</th>
                <th>Nomor WhatsApp</th>
                <th>Server</th>
                <th>Mikrotik</th>
                <th>Paket</th>
                <th>Status Koneksi</th>
                <th>Mulai Berlangganan</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
            <?php
            // Teknik CHUNGKING
            $no = 1;
            $chunkSize = 200;
            $stmt = $pdo->query("SELECT COUNT(id) FROM v_pelanggan WHERE $SqlSvData status = 'aktif' ORDER BY nama");
            $totalRows = $stmt->fetchColumn();
            for ($offset = 0; $offset < $totalRows; $offset += $chunkSize) {
                $stmt = $pdo->prepare("SELECT id,akun,user_hotspot,nama,alamat,latitude,longitude,nomor_whatsapp,email,nik,nama_server,nama_paket,time_start,status_koneksi,nama_mikrotik,id_setting_mikrotik,time_update,ip_address FROM v_pelanggan WHERE $SqlSvData status = 'aktif' ORDER BY nama LIMIT :limit OFFSET :offset");
                $stmt->bindValue(':limit', $chunkSize, PDO::PARAM_INT);
                $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
                $stmt->execute();

                $data = $stmt->fetchAll();
                foreach ($data as $row) {
                
            ?>
              <tr>
                <td style="max-width: 20px"><?php echo $no; ?></td>
                <td><?php echo $row['nama']; ?></td>
                <td><?php echo $row['nik']; ?></td>
                <td><?php echo $row['akun']; ?></td>
                <?php if($datauser[0]['user_hs_aktif'] > 0) { ?>
                <td><?php echo $row['user_hotspot']; ?></td>
                <?php } ?>
                <td style="max-width: 200px"><?php echo $row['alamat']; ?></td>
                <td style="max-width: 200px">
                  <?php if(substr($row['latitude'],0,1) != '-') { ?>
                  <a href="https://google.com/maps/?q=<?php echo $row['latitude']; ?>" target="_blank"><?php echo $row['latitude']; ?></a>
                  <?php } else { ?>
                  <a href="https://google.com/maps/?q=<?php echo $row['latitude'].','.$row['longitude']; ?>" target="_blank"><?php echo $row['latitude'].', '.$row['longitude']; ?></a>
                  <?php } ?>
                </td>
                <td>
                  <?php echo $row['nomor_whatsapp'];?>
                   <a href="https://wa.me/<?php echo $row['nomor_whatsapp']; ?>" target="_blank" style="border-radius: 20px;" class="btn btn-sm btn-success"><i class="fab fa-whatsapp"></i></a><br>
                   <?php echo $row['email']; ?>
                </td>
                <td><?php echo $row['nama_server']; ?></td>
                <td><?php echo $row['nama_mikrotik']; ?></td>
                <td><?php echo $row['nama_paket']; ?></td>
                <td>
                  <?php echo $row['status_koneksi']; if($row['status_koneksi'] == 'On') { ?>
                  <a href="?<?php echo encrypt_url('user_remote|'.$row['id'].'|'.$row['id_setting_mikrotik']); ?>" class="btn btn-sm btn-success ml-1" style="border-radius: 20px;">Remote</a>
                  <br><?php echo $row['ip_address']; ?>
                  <?php } ?>
                  <br><?php echo $row['time_update']; ?>
                </td>
                <td><?php echo $row['time_start']; ?></td>
                <td style="max-width: 30px">
                  <a href="?<?php echo encrypt_url('user_detail|'.$row['id']); ?>" class="btn btn-sm btn-info btn-sm m-1">Detail</a>
                </td>
              </tr>
              
            <?php 
            $no++; 
          }} ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</section> 