<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require "../config.php";

$tahun = isset($_GET['tahun'])
    ? $_GET['tahun']
    : date('Y');

/* ===============================
   AMBIL DAFTAR SK & PERUSAHAAN
================================= */

$qPerusahaan = mysqli_query($conn,"
SELECT
    nomor_sk,
    nama_perusahaan
FROM master_sk
ORDER BY nama_perusahaan ASC
");
/* ===============================
   DATA YANG SUDAH DITERIMA
================================= */

$qData = mysqli_query($conn,"
SELECT
    nomor_sk,
    laporan_triwulan_ke
FROM rekomtek
WHERE tahun_laporan='$tahun'
AND status='Diterima'
");

$dataTriwulan = [];

while($r = mysqli_fetch_assoc($qData)){

    $dataTriwulan[$r['nomor_sk']]
                  [$r['laporan_triwulan_ke']] = true;

}

/* ===============================
   TOTAL SK
================================= */

$totalSK = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) AS total
FROM master_sk
"))['total'];
/* ===============================
   STATISTIK TW
================================= */

$tw1 = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(DISTINCT nomor_sk) total
FROM rekomtek
WHERE tahun_laporan='$tahun'
AND laporan_triwulan_ke='Triwulan I'
AND status='Diterima'
"))['total'];

$tw2 = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(DISTINCT nomor_sk) total
FROM rekomtek
WHERE tahun_laporan='$tahun'
AND laporan_triwulan_ke='Triwulan II'
AND status='Diterima'
"))['total'];

$tw3 = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(DISTINCT nomor_sk) total
FROM rekomtek
WHERE tahun_laporan='$tahun'
AND laporan_triwulan_ke='Triwulan III'
AND status='Diterima'
"))['total'];

$tw4 = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(DISTINCT nomor_sk) total
FROM rekomtek
WHERE tahun_laporan='$tahun'
AND laporan_triwulan_ke='Triwulan IV'
AND status='Diterima'
"))['total'];

$menunggu = $conn->query("
SELECT COUNT(*) as jml
FROM rekomtek
WHERE status='Menunggu'
")->fetch_assoc()['jml'];

$diterima = $conn->query("
SELECT COUNT(*) as jml
FROM rekomtek
WHERE status='Diterima'
")->fetch_assoc()['jml'];

$ditolak = $conn->query("
SELECT COUNT(*) as jml
FROM rekomtek
WHERE status='Ditolak'
")->fetch_assoc()['jml'];
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Rekap Triwulan</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>

body{
    background:#f4f6f9;
    font-family:'Segoe UI',sans-serif;
}

.sidebar{
    width:250px;
    height:100vh;
    position:fixed;
    left:0;
    top:0;
    background:#003b7a;
    padding-top:20px;
}

.sidebar h4{
    color:white;
    text-align:center;
    margin-bottom:30px;
    font-weight:bold;
}

.sidebar a{
    display:block;
    color:white;
    padding:14px 20px;
    text-decoration:none;
    transition:.2s;
}

.sidebar a:hover{
    background:rgba(255,255,255,.15);
}

.sidebar .badge{
    float:right;
}

.sidebar .active{
    background:rgba(255,255,255,.15);
}

.main{
    margin-left:250px;
    padding:30px;
}

.table td,
.table th{
    vertical-align:middle;
    white-space:nowrap;
}
</style>

</head>

<body>

<div class="sidebar">

<h4>📁 Admin Laporan Triwulan</h4>

<a href="laporan_masuk">
<i class="fa fa-file-alt"></i>
Laporan Masuk
<span class="badge bg-warning text-dark">
<?= $menunggu ?>
</span>
</a>

<a href="laporan_ditolak">
<i class="fa fa-times-circle"></i>
Laporan Ditolak
<span class="badge bg-danger">
<?= $ditolak ?>
</span>
</a>

<a href="laporan_diterima">
<i class="fa fa-check-circle"></i>
Laporan Diterima
<span class="badge bg-success">
<?= $diterima ?>
</span>
</a>

<a href="rekap_triwulan">

<div class="menu-left">
<i class="fa fa-clipboard-list"></i>
Laporan Triwulan

</div>

<a href="masa_berlaku_sk">
<i class="fa fa-calendar-times"></i>
SK Perlu Perpanjangan 
</a>

</a>

<a href="master_sk">
<i class="fa fa-database"></i>
Master SK
</a>

<a href="/login">
<i class="fa fa-sign-out-alt"></i>
Logout
</a>

</div>

<div class="main">
<h3 class="mb-4">📋 Rekap Triwulan</h3>

<form method="GET" class="row mb-4">

    <div class="col-md-3">

        <select name="tahun" class="form-select">

            <?php

            $tahunList = mysqli_query($conn,"
            SELECT DISTINCT tahun_laporan
            FROM rekomtek
            ORDER BY tahun_laporan DESC
            ");

            while($t = mysqli_fetch_assoc($tahunList)){
            ?>

            <option value="<?= $t['tahun_laporan'] ?>"
            <?= ($tahun == $t['tahun_laporan']) ? 'selected' : '' ?>>

            <?= $t['tahun_laporan'] ?>

            </option>

            <?php } ?>

        </select>

    </div>

   <div class="col-md-4">
    <div class="d-flex gap-2">

        <button type="submit" class="btn btn-primary">
            Tampilkan
        </button>

        <a href="export_rekap_triwulan.php?tahun=<?= $tahun ?>"
           class="btn btn-success">
           Export Excel
        </a>

    </div>
</div>

</form>

<!-- KARTU STATISTIK -->

<div class="row mb-4">

<div class="col-md-3">
<div class="card shadow">
<div class="card-body text-center">
<h6>TW I</h6>
<h4><?= $tw1 ?>/<?= $totalSK ?></h4>
<strong>
<?= ($totalSK>0)?round(($tw1/$totalSK)*100,1):0 ?>%
</strong>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card shadow">
<div class="card-body text-center">
<h6>TW II</h6>
<h4><?= $tw2 ?>/<?= $totalSK ?></h4>
<strong>
<?= ($totalSK>0)?round(($tw2/$totalSK)*100,1):0 ?>%
</strong>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card shadow">
<div class="card-body text-center">
<h6>TW III</h6>
<h4><?= $tw3 ?>/<?= $totalSK ?></h4>
<strong>
<?= ($totalSK>0)?round(($tw3/$totalSK)*100,1):0 ?>%
</strong>
</div>
</div>
</div>

<div class="col-md-3">
<div class="card shadow">
<div class="card-body text-center">
<h6>TW IV</h6>
<h4><?= $tw4 ?>/<?= $totalSK ?></h4>
<strong>
<?= ($totalSK>0)?round(($tw4/$totalSK)*100,1):0 ?>%
</strong>
</div>
</div>
</div>

</div>

<!-- TABEL -->

<div class="card shadow">

<div class="card-body">

<div class="table-responsive">

<table class="table table-bordered table-striped">

<thead class="table-dark text-center">

<tr>
<th>No</th>
<th>Nomor SK</th>
<th>Nama Perusahaan</th>
<th>TW I</th>
<th>TW II</th>
<th>TW III</th>
<th>TW IV</th>
<th>Progress</th>
</tr>

</thead>

<tbody>

<?php

mysqli_data_seek($qPerusahaan,0);

$no=1;

while($d = mysqli_fetch_assoc($qPerusahaan)){

    $sk = $d['nomor_sk'];

    $jml = 0;

    if(isset($dataTriwulan[$sk]['Triwulan I'])) $jml++;
    if(isset($dataTriwulan[$sk]['Triwulan II'])) $jml++;
    if(isset($dataTriwulan[$sk]['Triwulan III'])) $jml++;
    if(isset($dataTriwulan[$sk]['Triwulan IV'])) $jml++;

    $progress = ($jml/4)*100;
?>

<tr>

<td><?= $no++ ?></td>

<td><?= $d['nomor_sk'] ?></td>

<td><?= $d['nama_perusahaan'] ?></td>

<td class="text-center">
<?= isset($dataTriwulan[$sk]['Triwulan I']) ? '✅' : '❌' ?>
</td>

<td class="text-center">
<?= isset($dataTriwulan[$sk]['Triwulan II']) ? '✅' : '❌' ?>
</td>

<td class="text-center">
<?= isset($dataTriwulan[$sk]['Triwulan III']) ? '✅' : '❌' ?>
</td>

<td class="text-center">
<?= isset($dataTriwulan[$sk]['Triwulan IV']) ? '✅' : '❌' ?>
</td>

<td width="180">

<div class="progress">

<?php
$warna = 'bg-danger';

if($progress >= 100){
    $warna = 'bg-success';
}
elseif($progress >= 75){
    $warna = 'bg-info';
}
elseif($progress >= 50){
    $warna = 'bg-warning';
}
?>

<div
class="progress-bar <?= $warna ?>"
style="width:<?= $progress ?>%">

<?= $progress ?>%

</div>

</div>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

