<?php
require "config.php";
if (file_exists("visitor.php")) {
    include "visitor.php";
}

/* ===== STATISTIK ===== */
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime('-1 day'));

$todayCount = $conn->query("SELECT COUNT(DISTINCT session_id) t FROM visitors WHERE visit_date='$today'")->fetch_assoc()['t'] ?? 0;
$yesterdayCount = $conn->query("SELECT COUNT(DISTINCT session_id) t FROM visitors WHERE visit_date='$yesterday'")->fetch_assoc()['t'] ?? 0;
$totalVisitor = $conn->query("SELECT COUNT(DISTINCT session_id) t FROM visitors")->fetch_assoc()['t'] ?? 0;

$online = $conn->query("
SELECT COUNT(DISTINCT session_id) t 
FROM visitors 
WHERE created_at >= NOW() - INTERVAL 5 MINUTE
")->fetch_assoc()['t'] ?? 0;

/* ===== CEK DATA ===== */
$data = null;
$kode = '';

if(isset($_POST['cek']) && !empty($_POST['kode'])){
    $kode = $_POST['kode'];
}
elseif(isset($_GET['kode']) && !empty($_GET['kode'])){
    $kode = $_GET['kode'];
}

if(!empty($kode)){

    // CEK KE TABEL PENGAJUAN SDA
    $data = $conn->query("
        SELECT *, 'sda' as jenis_layanan
        FROM pengajuan
        WHERE kode_tiket='$kode'
    ")->fetch_assoc();

    // JIKA TIDAK ADA, CEK KE REKOMTEK
    if(!$data){

        $data = $conn->query("
            SELECT *, 'rekomtek' as jenis_layanan
            FROM rekomtek
            WHERE kode_tiket='$kode'
        ")->fetch_assoc();

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

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Cek Status Pengajuan</title>

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

<style>

body{
    font-family:'Segoe UI',sans-serif;
    background:#f6f8fb;
    min-height:100vh;
    display:flex;
    flex-direction:column;
}

.main-content{
    flex:1;
    display:flex;
    align-items:center;
    justify-content:center;
}

.navbar{
    background:#003b7a;
    padding:14px 0;
}

.navbar .nav-link{
    color:white!important;
    font-weight:600;
}

/* ===== FOOTER ===== */

.footer-bws {
    background: linear-gradient(135deg, #003b7a, #1b4f72);
    color: #fff;
}

.footer-bws .row{
    margin-left:0;
    margin-right:0;
}

.footer-title{
    font-weight:800;
    border-left:4px solid #00c6ff;
    padding-left:12px;
    font-size: clamp(16px, 1.5vw, 18px);
}

.footer-bws{
    width:100%;
    margin:0;
}

.footer-bws .container-fluid{
    padding-left:40px;
    padding-right:40px;
}

.footer-bws .row{
    margin-left:0 !important;
    margin-right:0 !important;
}

.sosmed {
    display: flex;
    gap: 15px;
    margin-top: 10px;
}

.sosmed a {
    color: white;
    font-size: 22px;
    text-decoration: none;
    transition: 0.3s;
}

.sosmed a:hover {
    transform: scale(1.2);
    color: #00c6ff;
}

.visitor-box {
    background: rgba(255,255,255,0.1);
    padding: 10px;
    border-radius: 8px;
    font-size: 13px;
}

/* ===== TRACKING STATUS ===== */

.tracking-wrapper{
    margin-top:30px;
    margin-bottom:30px;
}

.tracking{
    display:flex;
    justify-content:space-between;
    position:relative;
}

.tracking::before{
    content:'';
    position:absolute;
    top:22px;
    left:8%;
    width:84%;
    height:4px;
    background:#dcdcdc;
    z-index:1;
}

.tracking-step{
    width:25%;
    text-align:center;
    position:relative;
    z-index:2;
}

.tracking-step .icon{
    width:50px;
    height:50px;
    border-radius:50%;
    background:#dcdcdc;
    color:#fff;
    display:flex;
    align-items:center;
    justify-content:center;
    margin:auto;
    font-size:20px;
    border:4px solid #fff;
    box-shadow:0 2px 10px rgba(0,0,0,0.15);
}

.tracking-step.active .icon{
    background:#28a745;
}

.tracking-step.reject .icon{
    background:#dc3545;
}

.tracking-step .label{
    margin-top:10px;
    font-size:14px;
    font-weight:600;
}

.tracking-step .date{
    font-size:12px;
    color:#777;
}

@media(max-width:768px){

.tracking{
    flex-wrap:wrap;
}

.tracking::before{
    display:none;
}

.tracking-step{
    width:50%;
    margin-bottom:20px;
}
.tracking.rekomtek::before{
    left:16%;
    width:68%;
}
}

</style>
</head>

<body>

<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg">

<div class="container">

<a class="navbar-brand text-white fw-bold d-flex align-items-center" href="index">

<img src="assets/bws1.png" style="height:60px; margin-right:10px;">

<div style="font-size:14px;">
Balai Wilayah Sungai Kalimantan IV Samarinda<br>
Kementerian Pekerjaan Umum
</div>

</a>

<button class="navbar-toggler bg-light" data-bs-toggle="collapse" data-bs-target="#nav">
☰
</button>

<div class="collapse navbar-collapse" id="nav">

<ul class="navbar-nav ms-auto">

<li class="nav-item">
<a class="nav-link" href="index">Beranda</a>
</li>

<li class="nav-item">
<a class="nav-link" href="cek">Cek Pengajuan</a>
</li>

</ul>

</div>
</div>
</nav>

<!-- CONTENT -->
<div class="container mt-5 mb-5">

<div class="card shadow mx-auto" style="max-width:750px;">

<div class="card-header bg-success text-white text-center">
<h5 class="mb-0">Cek Status Pengajuan</h5>
</div>

<div class="card-body">

<form method="POST">

<input 
class="form-control" 
name="kode" 
value="<?= $kode ?? '' ?>" 
placeholder="Masukkan Kode Tiket" 
required>

<button class="btn btn-success mt-3 w-100" name="cek">
Cek
</button>
<?php if(isset($_GET['jenis']) && $_GET['jenis']=='sda'): ?>
<a href="form_sda" class="btn btn-primary w-100 mt-2">
    Kembali ke Form Layanan SDA
</a>
<?php endif; ?>

<?php if(isset($_GET['jenis']) && $_GET['jenis']=='rekomtek'): ?>
<a href="form_rekomtek" class="btn btn-primary w-100 mt-2">
    Kembali ke Form Laporan Triwulan
</a>
<?php endif; ?>
</form>

<?php if(isset($_POST['cek']) && !$data){ ?>

<div class="alert alert-danger mt-3 text-center">
Kode tiket tidak ditemukan ❌
</div>

<?php } ?>

<?php if($data){ 

$status = $data['status'] ?? '';

$step1 = '';
$step2 = '';
$step3 = '';
$step4 = '';

if($status == 'Menunggu' || empty($status)){

    $step1 = 'active';

}
elseif($status == 'Diterima'){

    // KHUSUS REKOMTEK
    if($data['jenis_layanan'] == 'rekomtek'){

        $step1 = 'active';
        $step2 = 'active';
        $step4 = 'active';

    }else{

        // KHUSUS SDA
        $step1 = 'active';
        $step2 = 'active';

        if(!empty($data['no_surat'])){
            $step3 = 'active';
        }

    }

}

elseif($status == 'Selesai'){

    // selesai semua
    $step1 = 'active';
    $step2 = 'active';
    $step3 = 'active';
    $step4 = 'active';

}
elseif($status == 'Ditolak'){

    $step1 = 'active';
    $step2 = 'reject';

}

?>

<div class="mt-4">

<h5 class="mb-3">

<?php
if($data['jenis_layanan'] == 'rekomtek'){
    echo $data['nama_pemohon'];
} else {
    echo $data['nama'];
}
?>

</h5>
<p>
<b>Jenis Layanan :</b>

<?php
if($data['jenis_layanan'] == 'rekomtek'){
    echo "<span class='badge bg-primary'>REKOMTEK</span>";
} else {
    echo "<span class='badge bg-success'>SDA</span>";
}
?>
</p>
<!-- ===== TRACKING ===== -->

<div class="tracking-wrapper">

<?php if($data['jenis_layanan'] == 'rekomtek'){ ?>

<!-- =========================
     TRACKING REKOMTEK
========================= -->

<div class="tracking">

<!-- STEP 1 -->
<div class="tracking-step <?= $step1 ?>">

<div class="icon">
<i class="fa fa-file"></i>
</div>

<div class="label">
Pengajuan
</div>

<div class="date">
Masuk
</div>

</div>

<!-- STEP 2 -->
<div class="tracking-step <?= $step2 ?>">

<div class="icon">
<i class="fa fa-search"></i>
</div>

<div class="label">
Verifikasi
</div>

<div class="date">
Admin
</div>

</div>

<!-- STEP 3 -->
<div class="tracking-step <?= $step4 ?>">

<div class="icon">
<i class="fa fa-check"></i>
</div>

<div class="label">
Selesai
</div>

<div class="date">
Final
</div>

</div>

</div>

<?php } else { ?>

<!-- =========================
     TRACKING SDA
========================= -->

<div class="tracking rekomtek">

<!-- STEP 1 -->
<div class="tracking-step <?= $step1 ?>">

<div class="icon">
<i class="fa fa-file"></i>
</div>

<div class="label">
Pengajuan
</div>

<div class="date">
Masuk
</div>

</div>

<!-- STEP 2 -->
<div class="tracking-step <?= $step2 ?>">

<div class="icon">
<i class="fa fa-search"></i>
</div>

<div class="label">
Verifikasi
</div>

<div class="date">
Admin
</div>

</div>

<!-- STEP 3 -->
<div class="tracking-step <?= $step3 ?>">

<div class="icon">
<i class="fa fa-signature"></i>
</div>

<div class="label">
Proses TNDE
</div>

<div class="date">
Kepegawaian
</div>

</div>

<!-- STEP 4 -->
<div class="tracking-step <?= $step4 ?>">

<div class="icon">
<i class="fa fa-check"></i>
</div>

<div class="label">
Selesai
</div>

<div class="date">
Final
</div>

</div>

</div>

<?php } ?>

</div>

<!-- ===== STATUS ===== -->

<p>
<b>Status :</b>

<?php

if($status == 'Ditolak'){

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

}
elseif($status == 'Diproses'){

    echo "<span class='badge bg-primary'>Menunggu TTD</span>";

}
elseif($status == 'Diterima'){

    if($data['jenis_layanan'] == 'rekomtek'){

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

    }else{

        echo "<span class='badge bg-success'>Diterima</span>";

    }

}
elseif($status == 'Diterima'){

    if($data['jenis_layanan'] == 'rekomtek'){

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

    }else{

        echo "<span class='badge bg-success'>Diterima</span>";

    }

}
elseif($status == 'Selesai'){

    echo "<span class='badge bg-success'>Diterima</span>";

}
else{

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

}
?>

</p>

<!-- MENUNGGU -->
<?php if($status == 'Menunggu' || empty($status)){ ?>

<div class="alert alert-info">
Pengajuan masih diproses.
</div>

<?php } ?>

<!-- DIPROSES -->
<?php if($status == 'Diproses'){ ?>

<div class="alert alert-primary">
Surat balasan sedang menunggu tanda tangan.
</div>

<?php } ?>

<!-- DITOLAK -->
<?php if($status == 'Ditolak'){ ?>

<div class="alert alert-danger">

<b>Alasan Penolakan :</b>
<br>

<?php
if($data['jenis_layanan'] == 'rekomtek'){

    echo nl2br($data['catatan_admin']);

} else {

    echo nl2br($data['pesan_balasan']);

}
?>

</div>

<div class="alert alert-warning">

Silakan perbaiki kembali berkas sesuai catatan admin.

</div>

<?php
// tentukan link perbaikan
if($data['jenis_layanan'] == 'rekomtek'){

    $linkPerbaikan = "form_rekomtek.php?kode=".$data['kode_tiket'];

} else {

    $linkPerbaikan = "form_sda.php?kode=".$data['kode_tiket'];

}
?>

<a 
href="<?= $linkPerbaikan ?>" 
class="btn btn-danger w-100">

Perbaiki Data

</a>

<?php } ?>

<!-- DIPROSES -->
<?php if($status == 'Diproses'){ ?>

<div class="alert alert-primary">
Surat balasan sedang menunggu tanda tangan.
</div>

<?php } ?>

<!-- SURVEI DULU -->
<?php if(

(
$status == 'Selesai'
||
(
$status == 'Diterima'
&& $data['jenis_layanan'] == 'rekomtek'
)
)

&& $data['is_survei'] == 0

){ ?>

<div class="alert alert-warning mt-3">

<?php if($data['jenis_layanan'] == 'rekomtek'){ ?>

    Silakan isi survei terlebih dahulu sebelum melihat hasil verifikasi.

<?php } else { ?>

    Silakan isi survei terlebih dahulu sebelum melihat surat balasan pengajuan.

<?php } ?>

</div>

<a href="survei.php?kode=<?= $data['kode_tiket'] ?>&from=cek"
class="btn btn-warning w-100">
Isi Survei

</a>

<?php } ?>

<!-- BALASAN MUNCUL SETELAH SURVEI -->
<?php if(

(
$status == 'Selesai'
||
(
$status == 'Diterima'
&& $data['jenis_layanan'] == 'rekomtek'
)
)

&& $data['is_survei'] == 1
&& $status != 'Ditolak'

){ ?>

<?php if($data['jenis_layanan'] == 'rekomtek'){ ?>

<div class="alert alert-success mt-3">

<b>Dokumen selesai diproses.</b><br>
Dokumen yang anda kirim sudah sesuai dan telah diverifikasi admin.

</div>

<?php } ?>

<?php if(!empty($data['pesan_balasan'])){ ?>

<div class="alert alert-success mt-3">

<b>Balasan Admin :</b><br>
<?= nl2br($data['pesan_balasan']) ?>

</div>

<?php } ?>

<?php if(!empty($data['file_balasan'])){ ?>

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

<?php if($data['file_balasan'] != ""){ ?>

<a 
href="uploads/<?= urlencode($data['file_balasan']) ?>" 
class="btn btn-success px-4"
download>

📄 Download Surat Balasan

</a>

<?php } ?>


<?php if($data['file_lampiran'] != ""){ ?>

<a 
href="uploads/<?= urlencode($data['file_lampiran']) ?>" 
class="btn btn-warning px-4"
download>

📎 Download Lampiran

</a>

<?php } ?>

</div>

<?php } ?>

<?php } ?>

</div>

<?php } ?>

</div>
</div>

</div>

<!-- FOOTER -->
<footer class="footer-bws">

<div class="container-fluid py-5">

<div class="row text-white">

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

<h6 class="footer-title">Profil</h6>

<p class="small mt-2">

Balai Wilayah Sungai Kalimantan IV Samarinda<br>
Jl. MT Haryono No. 36, Kota Samarinda,<br>
Kalimantan Timur - Indonesia - 75123

</p>

</div>

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

<h6 class="footer-title">Kontak</h6>

<p class="small mt-2">

📞 (0541) 2088390<br>
✉️ bws.kalimantan.iv.samarinda@pu.go.id

</p>

</div>

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

<h6 class="footer-title">Sosial Media</h6>

<div class="sosmed">

<a href="https://www.facebook.com/profile.php?id=100078698323903" target="_blank">
<i class="fab fa-facebook"></i>
</a>

<a href="https://www.instagram.com/pu_sda_kalimantan4" target="_blank">
<i class="fab fa-instagram"></i>
</a>

<a href="https://www.youtube.com/@pusdakalimantan4samarinda" target="_blank">
<i class="fab fa-youtube"></i>
</a>

<a href="https://x.com/pu_sda_bwsk4/" target="_blank">
<i class="fab fa-x-twitter"></i>
</a>

<a href="https://www.tiktok.com/@pu_sda_kalimantan5" target="_blank">
<i class="fab fa-tiktok"></i>
</a>

</div>
</div>

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

<h6 class="footer-title">
Statistik Pengunjung
</h6>

<div class="visitor-box mt-2">

<div>Hari ini : <strong><?= $todayCount ?></strong></div>
<div>Kemarin : <?= $yesterdayCount ?></div>
<div>Total : <?= $totalVisitor ?></div>
<div>Online : <span id="online"><?= $online ?></span></div>

</div>
</div>

</div>

<hr>

<div class="text-center small">

Hak Cipta ©2024 Balai Wilayah Sungai Kalimantan IV Samarinda
Direktorat Jenderal Sumber Daya Air
Kementerian Pekerjaan Umum Republik Indonesia

</div>

</div>

</footer>

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

<script>

setInterval(() => {

fetch('online.php')
.then(res => res.text())
.then(data => document.getElementById('online').innerText = data);

}, 5000);

</script>

</body>
</html>