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

/* ===== JENIS FIX ===== */
$jenis = "Data Informasi Lainnya";

/* ===== PROSES FORM ===== */
if(isset($_POST['kirim'])){

$nama     = $_POST['nama'];
$hp       = $_POST['no_hp'];
$email    = $_POST['email'];
$instansi = $_POST['instansi'];
$pekerjaan= $_POST['pekerjaan'];
$alamat   = $_POST['alamat'];
$kota     = $_POST['kota'];
$provinsi = $_POST['provinsi'];
$nik      = $_POST['nik'];
$tujuan   = $_POST['tujuan'];
$tgl      = $_POST['tanggal_permohonan'];

/* ===== UPLOAD FILE ===== */
$file_surat="";
$file_identitas="";

if($_FILES['file_surat']['name']!=""){
$file_surat=time()."_".$_FILES['file_surat']['name'];
move_uploaded_file($_FILES['file_surat']['tmp_name'],"uploads/".$file_surat);
}

if($_FILES['file_identitas']['name']!=""){
$file_identitas=time()."_".$_FILES['file_identitas']['name'];
move_uploaded_file($_FILES['file_identitas']['tmp_name'],"uploads/".$file_identitas);
}

/* ===== AUTO KODE ===== */
$tahun=date("Y");
$max=$conn->query("SELECT MAX(id) m FROM pengajuan")->fetch_assoc()['m']+1;
$kode="BWS/".$tahun."/".str_pad($max,4,"0",STR_PAD_LEFT);

/* ===== INSERT ===== */
$conn->query("
INSERT INTO pengajuan SET
kode_tiket='$kode',
nama='$nama',
no_hp='$hp',
email='$email',
instansi='$instansi',
pekerjaan='$pekerjaan',
alamat='$alamat',
kota='$kota',
provinsi='$provinsi',
nik='$nik',
jenis_permohonan='$jenis',
tujuan='$tujuan',
tanggal_permohonan='$tgl',
file_surat='$file_surat',
file_identitas='$file_identitas',
status='Menunggu',
status_surat='Menunggu'
");

echo "<script>
alert('Permohonan berhasil dikirim. Kode tiket: $kode');
location='cek_pengajuan.php';
</script>";
exit;
}

/* ===== 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;
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>PETESA</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;}
.navbar{background:#003b7a;padding:14px 0;}
.navbar .nav-link{color:white!important;font-weight:600;}

.footer-bws {
    background: linear-gradient(135deg, #0a2540, #1b4f72);
    color: #fff;
}
.footer-title {
    font-weight: 600;
    border-left: 3px solid #00c6ff;
    padding-left: 10px;
}
.visitor-box {
    background: rgba(255,255,255,0.1);
    padding: 10px;
    border-radius: 8px;
    font-size: 13px;
}
</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="#">
<img src="uploads/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.php">Beranda</a>
</li>

<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-bs-toggle="dropdown">Pengajuan</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="form_sda.php">Layanan Informasi SDA</a></li>
<li><a class="dropdown-item" href="form_hidrologi.php">Data Hidrologi</a></li>
<li><a class="dropdown-item" href="form_rekomtek.php">Rekomtek</a></li>
<li><a class="dropdown-item" href="form_lainnya.php">Data Lainnya</a></li>
</ul>
</li>

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

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

<!-- FORM -->
<div class="container mt-5">

<div class="card shadow">
<div class="card-header bg-primary text-white">
<h4>Form Layanan Informasi Lainnya</h4>
</div>

<div class="card-body">

<form method="POST" enctype="multipart/form-data" class="needs-validation" novalidate>

<div class="row">

<div class="col-md-6 mb-3">
<label>Nama Lengkap <span class="text-danger">*</span></label>
<input type="text" name="nama" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>No Handphone <span class="text-danger">*</span></label>
<input type="text" name="no_hp" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>Email <span class="text-danger">*</span></label>
<input type="email" name="email" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>Instansi <span class="text-danger">*</span></label>
<input type="text" name="instansi" class="form-control">
</div>

<div class="col-md-6 mb-3">
<label>Pekerjaan <span class="text-danger">*</span></label>
<input type="text" name="pekerjaan" class="form-control">
</div>

<div class="col-md-6 mb-3">
<label>NIK <span class="text-danger">*</span></label>
<input type="text" name="nik" class="form-control" required>
</div>

<div class="col-md-12 mb-3">
<label>Alamat <span class="text-danger">*</span></label>
<textarea name="alamat" class="form-control" required></textarea>
</div>

<div class="col-md-6 mb-3">
<label>Kota <span class="text-danger">*</span></label>
<input type="text" name="kota" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>Provinsi <span class="text-danger">*</span></label>
<input type="text" name="provinsi" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>Jenis Permohonan</label>
<input type="text" class="form-control" value="<?= $jenis ?>" readonly>
</div>

<div class="col-md-6 mb-3">
<label>Tanggal Permohonan <span class="text-danger">*</span></label>
<input type="date" name="tanggal_permohonan" class="form-control" required>
</div>

<div class="col-md-12 mb-3">
<label>Tujuan Permohonan <span class="text-danger">*</span></label>
<textarea name="tujuan" class="form-control" required></textarea>
</div>

<div class="col-md-6 mb-3">
<label>Upload Surat <span class="text-danger">*</span></label>
<input type="file" name="file_surat" class="form-control" required>
</div>

<div class="col-md-6 mb-3">
<label>Upload Identitas <span class="text-danger">*</span></label>
<input type="file" name="file_identitas" class="form-control" required>
</div>

</div>

<button name="kirim" class="btn btn-success w-100">
Kirim Permohonan
</button>

</form>

</div>
</div>

</div>

<!-- FOOTER -->
<footer class="footer-bws mt-5">
<div class="container py-5">
<div class="row">

<div class="col-md-5">
<h5>Balai Wilayah Sungai Kalimantan IV Samarinda</h5>
<p class="small mt-3">
Jl. MT Haryono Samarinda<br>
Kalimantan Timur
</p>
</div>

<div class="col-md-3">
<h6 class="footer-title">Kontak</h6>
<p class="small">📞 (0541) 2088390<br>✉️ bws@pu.go.id</p>
</div>

<div class="col-md-4">
<h6 class="footer-title">Statistik Pengunjung</h6>
<div class="visitor-box">
<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,. All Rights Reserved
</div>
</div>
</footer>

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

<script>
(() => {
  const forms = document.querySelectorAll('.needs-validation')
  forms.forEach(form => {
    form.addEventListener('submit', e => {
      if (!form.checkValidity()) {
        e.preventDefault()
        alert('Harap isi semua field wajib (*)')
      }
    })
  })
})();

setInterval(() => {
fetch('online.php')
.then(res => res.text())
.then(data => document.getElementById('online').innerText = data);
}, 5000);
</script>

</body>
</html>