<?php
$pageTitle='Laporan & Statistik';
require_once __DIR__.'/../../config/functions.php';
requireLogin();if(!isAdmin())redirect(BASE_URL);
$byUnit=$pdo->query("SELECT u.nama_unit,u.warna,COUNT(p.id) as jml FROM unit u LEFT JOIN pegawai p ON p.unit_id=u.id AND p.status=1 GROUP BY u.id,u.nama_unit,u.warna ORDER BY u.urutan ASC, u.id ASC")->fetchAll();
$byJawatan=$pdo->query("SELECT j.nama_jawatan,COUNT(p.id) as jml FROM jawatan j LEFT JOIN pegawai p ON p.jawatan_id=j.id AND p.status=1 GROUP BY j.id,j.nama_jawatan ORDER BY j.urutan")->fetchAll();
$aktByUnit=$pdo->query("SELECT u.nama_unit,u.warna,COUNT(t.id) as jml FROM unit u LEFT JOIN takwim t ON t.unit_id=u.id AND t.status=1 GROUP BY u.id,u.nama_unit,u.warna ORDER BY u.urutan ASC, u.id ASC")->fetchAll();
$breadcrumb=[['label'=>'Utama','url'=>BASE_URL],['label'=>'Laporan','url'=>'#']];
require_once __DIR__.'/../../templates/header.php';
?>
<meta name="base-url" content="<?=BASE_URL?>">
<?php require_once __DIR__.'/../../templates/sidebar.php'; ?>
<div class="main-content">
<?php require_once __DIR__.'/../../templates/topbar.php'; ?>
<div style="padding:2rem">
<div class="row g-4">
  <div class="col-lg-6">
    <div class="content-card">
      <h3 style="font-size:1rem;font-weight:700;color:#fff;margin-bottom:1.5rem"><i class="fas fa-users me-2" style="color:#cc0001"></i>Pegawai Mengikut Unit</h3>
      <canvas id="unitChart" height="250"></canvas>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="content-card">
      <h3 style="font-size:1rem;font-weight:700;color:#fff;margin-bottom:1.5rem"><i class="fas fa-calendar me-2" style="color:#0d6efd"></i>Aktiviti Mengikut Unit</h3>
      <canvas id="aktChart" height="250"></canvas>
    </div>
  </div>
  <div class="col-12">
    <div class="content-card">
      <h3 style="font-size:1rem;font-weight:700;color:#fff;margin-bottom:1.5rem"><i class="fas fa-id-badge me-2" style="color:#198754"></i>Pegawai Mengikut Jawatan</h3>
      <canvas id="jawatanChart" height="100"></canvas>
    </div>
  </div>
</div>
</div></div>
<script>
Chart.defaults.color='#888';Chart.defaults.borderColor='rgba(255,255,255,0.06)';
const u=<?=json_encode($byUnit)?>;
const a=<?=json_encode($aktByUnit)?>;
const j=<?=json_encode($byJawatan)?>;
new Chart(document.getElementById('unitChart'),{type:'doughnut',data:{labels:u.map(x=>x.nama_unit),datasets:[{data:u.map(x=>x.jml),backgroundColor:u.map(x=>x.warna+'aa'),borderColor:u.map(x=>x.warna),borderWidth:2}]},options:{plugins:{legend:{labels:{color:'#ccc'}}}}});
new Chart(document.getElementById('aktChart'),{type:'bar',data:{labels:a.map(x=>x.nama_unit),datasets:[{label:'Aktiviti',data:a.map(x=>x.jml),backgroundColor:a.map(x=>x.warna+'88'),borderColor:a.map(x=>x.warna),borderWidth:2,borderRadius:8}]},options:{plugins:{legend:{display:false}},scales:{y:{beginAtZero:true,ticks:{color:'#888'}}}}});
new Chart(document.getElementById('jawatanChart'),{type:'bar',data:{labels:j.map(x=>x.nama_jawatan),datasets:[{label:'Pegawai',data:j.map(x=>x.jml),backgroundColor:'rgba(204,0,1,0.4)',borderColor:'#cc0001',borderWidth:2,borderRadius:8}]},options:{indexAxis:'y',plugins:{legend:{display:false}},scales:{x:{beginAtZero:true,ticks:{color:'#888'}}}}});
</script>
<?php require_once __DIR__.'/../../templates/footer.php'; ?>
