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

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

if(!isset($_SESSION['login'])){
    header("Location: /login.php");
    exit;
}

if($_SESSION['role'] != 'admin3'){
    header("Location: /login.php");
    exit;
}

$search = $_GET['search'] ?? '';
$search = $conn->real_escape_string($search);

$where = "
masa_berlaku BETWEEN CURDATE()
AND DATE_ADD(CURDATE(), INTERVAL 3 MONTH)
";

if($search != ''){

    $where .= " AND (
        nomor_sk LIKE '%$search%'
        OR nama_perusahaan LIKE '%$search%'
        OR sumber_air LIKE '%$search%'
    )";

}

$data = $conn->query("
SELECT *
FROM master_sk
WHERE masa_berlaku <= DATE_ADD(CURDATE(), INTERVAL 3 MONTH)
ORDER BY masa_berlaku ASC
");

/* ================= 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>
<head>

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

<title>Masa Berlaku SK</title>

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

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

<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;
}

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

.sidebar .badge{
    float:right;
}

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

.card{
    border-radius:15px;
}

.table td,
.table th{
    vertical-align:middle;
}

</style>

</head>

<body style="background:#f4f6f9;">

<!-- SIDEBAR -->

<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">
    <i class="fa fa-clipboard-list"></i>
    Laporan Triwulan
</a>

<a href="masa_berlaku_sk">
<i class="fa fa-calendar-times"></i>
SK Perlu Perpanjangan 
</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 fw-bold">
<i class="fa fa-calendar-times text-danger"></i>
SK Perlu Perpanjangan
</h3>

<div class="card shadow border-0">

<div class="card-body">

<div class="table-responsive">
    
<form method="GET" class="mb-3">

<div class="row">

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

<input
type="text"
name="search"
class="form-control"
placeholder="Cari nomor SK / perusahaan / sumber air..."
value="<?= $_GET['search'] ?? '' ?>">

</div>

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

<button class="btn btn-primary w-100">
<i class="fa fa-search"></i>
Cari
</button>

</div>

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

<a href="masa_berlaku_sk"
class="btn btn-secondary w-100">

Reset

</a>

</div>

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

<a href="export_sk?search=<?= urlencode($_GET['search'] ?? '') ?>"
class="btn btn-success">

    <i class="fa fa-file-excel"></i>
    Export Excel

</a>

</div>

</div>

</form>    

<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>Sumber Air</th>
<th>Masa Berlaku</th>
<th>Sisa Hari</th>
</tr>

</thead>

<tbody>

<?php
$no = 1;

while($d = $data->fetch_assoc()):

$sisa_hari = floor(
(strtotime($d['masa_berlaku']) - time()) / 86400
);
?>

<tr>
<td class="text-center">
<?= $no++ ?></td>

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

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

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

<td>
<?= date('d-m-Y', strtotime($d['masa_berlaku'])) ?>
</td>

<td>

<?php
if($sisa_hari < 0){

    echo "<span class='badge bg-dark'>
    Expired ".abs($sisa_hari)." Hari
    </span>";

}
elseif($sisa_hari <= 30){

    echo "<span class='badge bg-danger'>
    $sisa_hari Hari
    </span>";

}
elseif($sisa_hari <= 90){

    echo "<span class='badge bg-warning text-dark'>
    $sisa_hari Hari
    </span>";

}
else{

    echo "<span class='badge bg-success'>
    $sisa_hari Hari
    </span>";

}
?>

</td>

</tr>

<?php endwhile; ?>

</tbody>

</table>

</div>

</div>

</div>

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

</body>
</html>