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

if(!isset($_GET['id'])){
    die("ID tidak ditemukan");
}

$id = intval($_GET['id']);

$q = $conn->query("
SELECT * FROM rekomtek
WHERE id='$id'
");

if($q->num_rows == 0){
    die("Data tidak ditemukan");
}

$d = $q->fetch_assoc();

/* =========================
   FALLBACK FIELD
========================= */

$kode     = $d['kode_tiket'] ?? '';
$pemohon  = $d['nama_pemohon'] ?? '';
$perusahaan = $d['nama_perusahaan'] ?? '';
$hp       = $d['nomor_handphone'] ?? '';
$alamat   = $d['alamat_perusahaan'] ?? '';
$izin     = $d['jenis_izin'] ?? '';
$nosk     = $d['nomor_sk'] ?? '';
$kegiatan = $d['jenis_kegiatan'] ?? '';
$sumber   = $d['sumber_air'] ?? '';
$triwulan = $d['laporan_triwulan_ke'] ?? '';
$tahun    = $d['tahun_laporan'] ?? '';
$status   = $d['status'] ?? '';
$catatan  = $d['catatan_admin'] ?? '';
$tanggal  = $d['created_at'] ?? '';

/* =========================
   FUNCTION TAMPIL FILE
========================= */

function tampilFile($file){

    if(!$file){
        return "<i>Tidak ada file</i>";
    }

    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

    $path = "../uploads/".$file;

    if(in_array($ext,['jpg','jpeg','png','gif'])){

        return "
        <img 
        src='$path'
        class='img-fluid border rounded'
        style='max-height:400px'>
        ";

    } elseif($ext == 'pdf'){

        return "
        <iframe 
        src='$path'
        width='100%'
        height='500px'>
        </iframe>
        ";

    } else {

        return "
        <a 
        href='$path'
        target='_blank'
        class='btn btn-info btn-sm'>

        Download File

        </a>
        ";

    }

}
?>

<!DOCTYPE html>
<html lang="id">
<head>

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

<title>Detail Laporan Rekomtek</title>

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

<style>

body{
    background:#f1f5f9;
    margin:0;
    padding:20px;
    font-family:'Segoe UI',sans-serif;
}

.card{
    border:none;
    border-radius:18px;
    overflow:hidden;
}

.table th{
    width:30%;
    background:#f8fafc;
}

iframe{
    border:none;
    border-radius:12px;
    width:100%;
    height:850px;
}

</style>

</head>

<body>

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

<div class="card-body">

<!-- FILE LAPORAN -->

<div class="text-center mb-4">

<h5 class="fw-bold mb-3">
File Laporan
</h5>

<div class="border rounded overflow-hidden bg-light p-2">

<?= tampilFile($d['file_laporan']) ?>

</div>

</div>

<hr>

<!-- DETAIL -->

<h5 class="text-center fw-bold mb-3">
Detail Laporan
</h5>

<div class="table-responsive">

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

<tr>
<th width="30%">Kode Tiket</th>
<td><?= $kode ?></td>
</tr>

<tr>
<th>Nama Pemohon</th>
<td><?= $pemohon ?></td>
</tr>

<tr>
<th>Nama Perusahaan</th>
<td><?= $perusahaan ?></td>
</tr>

<tr>
<th>Nomor Handphone</th>
<td><?= $hp ?></td>
</tr>

<tr>
<th>Alamat Perusahaan</th>
<td><?= $alamat ?></td>
</tr>

<tr>
<th>Jenis Izin</th>
<td><?= $izin ?></td>
</tr>

<tr>
<th>Nomor SK</th>
<td><?= $nosk ?></td>
</tr>

<tr>
<th>Jenis Kegiatan</th>
<td><?= $kegiatan ?></td>
</tr>

<tr>
<th>Sumber Air</th>
<td><?= $sumber ?></td>
</tr>

<tr>
<th>Triwulan</th>
<td><?= $triwulan ?></td>
</tr>

<tr>
<th>Tahun Laporan</th>
<td><?= $tahun ?></td>
</tr>

<tr>
<th>Tanggal Upload</th>
<td><?= $tanggal ?></td>
</tr>

<tr>
<th>Status</th>

<td>

<?php if($status == 'Menunggu'){ ?>

<span class="badge bg-warning text-dark">
Menunggu
</span>

<?php } elseif($status == 'Diterima'){ ?>

<span class="badge bg-primary">
Diterima
</span>

<?php } elseif($status == 'Selesai'){ ?>

<span class="badge bg-success">
Selesai
</span>

<?php } else { ?>

<span class="badge bg-danger">
Ditolak
</span>

<?php } ?>

</td>
</tr>

<tr>
<th>Catatan Admin</th>

<td style="white-space:pre-line;">

<?= !empty($catatan)
? nl2br($catatan)
: 'Belum ada catatan'; ?>

</td>
</tr>

</table>

</div>

<!-- BUTTON -->

<div class="text-center mt-4">

<?php

$from = $_GET['from'] ?? '';

if($from == 'admin3'){

    if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'laporan_selesai.php') !== false){

        $kembali = '../admin3/laporan_selesai.php';

    } else {

        $kembali = '../admin3/laporan_masuk.php';

    }

} else {

    $kembali = !empty($_SERVER['HTTP_REFERER'])
    ? $_SERVER['HTTP_REFERER']
    : 'laporan_masuk.php';

}

?>

<a href="<?= $kembali ?>" class="btn btn-secondary px-4">
Kembali
</a>

<?php if($status == 'Menunggu'){ ?>

<a href="../admin3/laporan_masuk.php?terima=<?= $id ?>"
class="btn btn-success px-4"
onclick="return confirm('Terima laporan ini?')">

Terima

</a>

<button
class="btn btn-danger px-4"
data-bs-toggle="modal"
data-bs-target="#modalTolak">

Tolak

</button>

<?php } ?>
</div>

</div>
</div>

</div>
```php
<!-- MODAL TOLAK -->

<div class="modal fade"
id="modalTolak"
tabindex="-1"
aria-hidden="true">

<div class="modal-dialog">

<div class="modal-content">

<form method="POST"
action="../admin3/laporan_ditolak.php">

<div class="modal-header bg-danger text-white">

<h5 class="modal-title">
Tolak Laporan
</h5>

<button
type="button"
class="btn-close btn-close-white"
data-bs-dismiss="modal">
</button>

</div>

<div class="modal-body">

<input
type="hidden"
name="id"
value="<?= $id ?>">

<textarea
name="catatan"
class="form-control"
rows="4"
required
placeholder="Masukkan alasan penolakan..."></textarea>

</div>

<div class="modal-footer">

<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal">

Batal

</button>

<button
type="submit"
class="btn btn-danger">

Tolak

</button>

</div>

</form>

</div>

</div>

</div>

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

<?php include 'layout_footer.php'; ?>