<?php
session_start();
if (empty($connection)) {
  header('location:../../');
  exit;
} else {
  include_once 'mod/sw-panel.php';

  // Helper aman
  if (!function_exists('h')) {
    function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
  }

  $allowed_months = ['01','02','03','04','05','06','07','08','09','10','11','12'];
  $thisYear = (int)date('Y');
  $minYear  = $thisYear - 5;
  $maxYear  = $thisYear + 4;
  $mod = "aktivitas";

  echo '<div class="content-wrapper">';

  switch (@$_GET['op']) {

  // ================== LIST DATA ==================
  default:

    $rawBulan = isset($_GET['bulan']) ? $_GET['bulan'] : date('m');
    $bulan    = in_array($rawBulan, $allowed_months, true) ? $rawBulan : date('m');

    $rawTahun = isset($_GET['tahun']) ? $_GET['tahun'] : date('Y');
    $tahunInt = (int)$rawTahun;
    $tahun    = ($tahunInt >= $minYear && $tahunInt <= $maxYear) ? (string)$tahunInt : date('Y');

    $cari = isset($_GET['cari']) ? trim($_GET['cari']) : '';
    $cari = $connection->real_escape_string($cari);
    if ($cari !== '') {
      $cari = str_replace(['%', '_'], ['\%','\_'], $cari);
    }

    echo '
    <section class="content-header">
      <h1>Data <small> Aktivitas</small></h1>
      <ol class="breadcrumb">
        <li><a href="./"><i class="fa fa-dashboard"></i> Beranda</a></li>
        <li class="active">Data Aktivitas</li>
      </ol>
    </section>

    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box box-solid">
            <div class="box-header with-border">
              <h3 class="box-title"><b>Data Aktivitas</b></h3>
              <div class="box-tools pull-right">
                <div class="btn-group">
                  <button type="button" class="btn btn-success">Ekspor</button>
                  <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
                    <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu" role="menu">
                    <li><a href="mod/aktivitas/print.php?action=pdf&from='.$bulan.'&to='.$tahun.'" target="_blank">PDF</a></li>
                    <li><a href="mod/aktivitas/print.php?action=excel&from='.$bulan.'&to='.$tahun.'" target="_blank">EXCEL</a></li>
                    <li><a href="mod/aktivitas/print.php?action=excel&from='.$bulan.'&to='.$tahun.'&print=1" target="_blank">PRINT</a></li>
                  </ul>
                </div>
              </div>
            </div>
            <div class="box-body">

              <!-- Filter -->
              <form method="GET" class="form-inline mb-3">
                <input type="hidden" name="op" value="">
                <div class="form-group">
                  <select name="bulan" class="form-control">';
                    $bulan_array = [
                      "01"=>"Januari","02"=>"Februari","03"=>"Maret",
                      "04"=>"April","05"=>"Mei","06"=>"Juni",
                      "07"=>"Juli","08"=>"Agustus","09"=>"September",
                      "10"=>"Oktober","11"=>"November","12"=>"Desember"
                    ];
                    foreach($bulan_array as $key=>$nama){
                      $sel = ($bulan==$key)?'selected':'';
                      echo "<option value='$key' $sel>$nama</option>";
                    }
                echo '</select>
                </div>
                <div class="form-group">
                  <select name="tahun" class="form-control">';
                    for($i=$minYear; $i<=$maxYear; $i++){
                      $sel = ($tahun==(string)$i)?'selected':'';
                      echo "<option value='$i' $sel>$i</option>";
                    }
                echo '</select>
                </div>
                <div class="form-group">
                  <input type="text" name="cari" value="'.h(isset($_GET['cari'])?$_GET['cari']:'').'" class="form-control" placeholder="Cari nama">
                </div>
                <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> Tampilkan</button>
              </form>

              <!-- Tabel Aktivitas -->
              <div class="table-responsive">
                <table id="swdatatable" class="table table-bordered table-striped table-hover">
                  <thead>
                    <tr>
                      <th>No</th>
                      <th>Tanggal</th>
                      <th>Nama</th>
                      <th>Aktivitas</th>
                      <th>Kendala</th>
                      <th>Solusi</th>
                      <th>Status</th>
                      <th>Foto</th>
                    </tr>
                  </thead>
                  <tbody>';

                  $where = "WHERE MONTH(tanggal)='".(int)$bulan."' AND YEAR(tanggal)='".(int)$tahun."'";
                  if ($cari !== '') {
                    $where .= " AND (nama LIKE '%$cari%' ESCAPE '\\\\')";
                  }

                  $query  = "SELECT * FROM aktivitas $where ORDER BY tanggal DESC";
                  $result = $connection->query($query);

                  if ($result && $result->num_rows > 0) {
                    $no=0;
                    $selesai=0; $pending=0; $konsultasi=0;

                    while ($row = $result->fetch_assoc()) {
                      $no++;

                      // hitung rekap
                      if ($row['status']==="Selesai") $selesai++;
                      elseif ($row['status']==="Konsultasi") $konsultasi++;
                      else $pending++;

                      echo '
                      <tr>
                        <td class="text-center">'.$no.'</td>
                        <td>'.h(tgl_indo($row['tanggal'])).'</td>
                        <td>'.h($row['nama']).'</td>
                        <td>
                          1. '.h($row['aktivitas_1']).'<br>
                          2. '.h($row['aktivitas_2']).'<br>
                          3. '.h($row['aktivitas_3']).'<br>
                          4. '.h($row['aktivitas_4']).'<br>
                          5. '.h($row['aktivitas_5']).'
                        </td>
                        <td>'.nl2br(h($row['kendala'])).'</td>
                        <td>'.nl2br(h($row['solusi'])).'</td>
                        <td>';
                          if ($row['status'] === "Selesai") {
                            echo '<span class="badge bg-green">Selesai</span>';
                          } elseif ($row['status'] === "Konsultasi") {
                            echo '<span class="badge bg-yellow">Konsultasi</span>';
                          } else {
                            echo '<span class="badge bg-red">Pending</span>';
                          }
                      echo '</td>
                        <td class="text-center">';
                          if (!empty($row['foto'])) {
                            echo '<a href="uploads/'.h($row['foto']).'" target="_blank">
                                    <img src="uploads/'.h($row['foto']).'" width="50" class="img-thumbnail">
                                  </a>';
                          } else {
                            echo '-';
                          }
                      echo '</td>
                      </tr>';
                    }

                    $total = $selesai + $pending + $konsultasi;
                    echo '
                      <tr>
                        <td colspan="8">
                          <b>Rekapitulasi:</b> 
                          Selesai = <span class="badge bg-green">'.$selesai.'</span>,
                          Konsultasi = <span class="badge bg-yellow">'.$konsultasi.'</span>,
                          Pending = <span class="badge bg-red">'.$pending.'</span>,
                          Total = <span class="badge bg-blue">'.$total.'</span>
                        </td>
                      </tr>';
                  } else {
                    echo '<tr><td colspan="8" class="text-center">Data tidak ditemukan</td></tr>';
                  }

                echo '
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>';
    break;

  } // end switch
  echo '</div>';
} // end session
?>
