<?php
require "../config.php";

/* ================= FILTER ================= */

$where = "WHERE status='Diterima'";

/* ================= SEARCH ================= */

if(isset($_GET['search']) && $_GET['search'] != ""){

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

    $where .= " AND kode_tiket LIKE '%$s%'";
}

/* ================= FILTER TANGGAL ================= */

if(isset($_GET['filter']) && $_GET['filter'] != ''){

    $filter = $_GET['filter'];

    if($filter == "hari" && !empty($_GET['tanggal'])){

        $tgl = $_GET['tanggal'];

        $where .= " AND DATE(created_at)='$tgl'";
    }

    elseif($filter == "minggu"){

        $where .= "
        AND YEARWEEK(created_at,1)=YEARWEEK(CURDATE(),1)";
    }

    elseif($filter == "bulan"){

        $where .= "
        AND MONTH(created_at)=MONTH(CURDATE())
        AND YEAR(created_at)=YEAR(CURDATE())";
    }

    elseif($filter == "tahun"){

        $where .= "
        AND YEAR(created_at)=YEAR(CURDATE())";
    }
}

/* ================= QUERY ================= */

$q = $conn->query("
SELECT *
FROM rekomtek
$where
ORDER BY id DESC
");

/* ================= HEADER EXCEL ================= */

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

?>

<table border="1">

<tr style="background:#d9d9d9; font-weight:bold; text-align:center;">

<th>No</th>
<th>ID</th>
<th>Kode Tiket</th>
<th>Nama Pemohon</th>
<th>Nama Perusahaan</th>
<th>Nomor Handphone</th>
<th>Alamat Perusahaan</th>
<th>Jenis Izin</th>
<th>Nomor SK</th>
<th>Jenis Kegiatan</th>
<th>Sumber Air</th>
<th>Laporan Triwulan Ke</th>
<th>Tahun Laporan</th>
<th>Status</th>
<th>Tanggal Upload</th>

</tr>

<?php

$no = 1;

if($q && $q->num_rows > 0){

    while($d = $q->fetch_assoc()){

?>

<tr>

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

<td><?= htmlspecialchars($d['id']) ?></td>

<td>
<?= htmlspecialchars($d['kode_tiket']) ?>
</td>

<td>
<?= htmlspecialchars($d['nama_pemohon']) ?>
</td>

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

<td style="mso-number-format:'\@';">
<?= htmlspecialchars($d['nomor_handphone']) ?>
</td>

<td>
<?= htmlspecialchars($d['alamat_perusahaan']) ?>
</td>

<td>
<?= htmlspecialchars($d['jenis_izin']) ?>
</td>

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

<td>
<?= htmlspecialchars($d['jenis_kegiatan']) ?>
</td>

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

<td>
<?= htmlspecialchars($d['laporan_triwulan_ke']) ?>
</td>

<td>
<?= htmlspecialchars($d['tahun_laporan']) ?>
</td>

<td>
<?= htmlspecialchars($d['status']) ?>
</td>

<td>
<?= date('d-m-Y H:i', strtotime($d['created_at'])) ?>
</td>

</tr>

<?php

    }

}else{

    echo "
    <tr>
        <td colspan='17' align='center'>
            Data kosong
        </td>
    </tr>
    ";
}

?>

</table>