<?php

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

if(!isset($_SESSION['login'])){
    exit;
}

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Master_SK.xls");

$data = $conn->query("
SELECT *
FROM master_sk
ORDER BY id DESC
");

?>

<table border="1">

<tr style="background:#d9d9d9; font-weight:bold;">
    <th>No</th>
    <th>Nomor SK</th>
    <th>Sumber Air</th>
    <th>Nama Perusahaan</th>
    <th>Masa Berlaku SK</th>
    <th>Status</th>
</tr>

<?php

$no = 1;

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

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

    if($sisa_hari < 0){
        $status = "Expired";
    }elseif($sisa_hari <= 90){
        $status = "Perlu Perpanjangan";
    }else{
        $status = "Aktif";
    }

?>

<tr>

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

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

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

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

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

    <td><?= $status ?></td>

</tr>

<?php endwhile; ?>

</table>