tida
<?php
require "config.php";

if (file_exists("visitor.php")) {
    include "visitor.php";
}
/* =========================
   FUNCTION WHATSAPP
========================= */
function kirimWA($target, $pesan){

    $token = "Khh5TsTudPJ8FgesH2FV";

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.fonnte.com/send',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => array(
            'target' => $target,
            'message' => $pesan,
        ),
        CURLOPT_HTTPHEADER => array(
            "Authorization: $token"
        ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    return $response;
}
/* =========================
   MODE EDIT / PERBAIKI
========================= */

$edit = null;

if(isset($_GET['kode'])){

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

    $edit = $conn->query("
    SELECT *
    FROM rekomtek
    WHERE kode_tiket='$kode'
    ")->fetch_assoc();

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

    $nama_pemohon      = htmlspecialchars($_POST['nama_pemohon']);
    $nama_perusahaan   = htmlspecialchars($_POST['nama_perusahaan']);
    $nomor_handphone   = htmlspecialchars($_POST['nomor_handphone']);
    $alamat_perusahaan = htmlspecialchars($_POST['alamat_perusahaan']);
    $jenis_izin        = htmlspecialchars($_POST['jenis_izin']);
    $nomor_sk          = htmlspecialchars($_POST['nomor_sk']);
    $jenis_kegiatan    = htmlspecialchars($_POST['jenis_kegiatan']);
    $sumber_air        = htmlspecialchars($_POST['sumber_air']);
    $laporan_triwulan  = htmlspecialchars($_POST['laporan_triwulan']);
    $tahun_laporan     = htmlspecialchars($_POST['tahun_laporan']);
    /* SIMPAN MASTER SK */

$cekMaster = $conn->query("
SELECT id FROM master_sk
WHERE nomor_sk='$nomor_sk'
");

if($cekMaster->num_rows == 0){

    $stmtMaster = $conn->prepare("
    INSERT INTO master_sk (
        nomor_sk,
        nama_perusahaan,
        sumber_air
    ) VALUES (?, ?, ?)
    ");

    $stmtMaster->bind_param(
        "sss",
        $nomor_sk,
        $nama_perusahaan,
        $sumber_air
    );

    $stmtMaster->execute();
}

/* =========================
   CEK DUPLIKAT LAPORAN
========================= */

if(!$edit){

    $cekDuplikat = $conn->prepare("
    SELECT id
    FROM rekomtek
    WHERE nomor_sk = ?
    AND laporan_triwulan_ke = ?
    AND tahun_laporan = ?
    LIMIT 1
    ");

    $cekDuplikat->bind_param(
        "sss",
        $nomor_sk,
        $laporan_triwulan,
        $tahun_laporan
    );

    $cekDuplikat->execute();

    $hasilDuplikat = $cekDuplikat->get_result();

    if($hasilDuplikat->num_rows > 0){

      echo "
<script>
alert('Nomor SK ini sudah mengirim laporan $laporan_triwulan Tahun $tahun_laporan');
window.history.back();
</script>
";
exit;
    }
}
    /* =========================
       UPLOAD FILE PDF
    ========================= */

    $file_laporan = $edit['file_laporan'] ?? "";

    if(!empty($_FILES['file_laporan']['name'])){

        $ext  = strtolower(pathinfo($_FILES['file_laporan']['name'], PATHINFO_EXTENSION));
        $size = $_FILES['file_laporan']['size'];

        // VALIDASI PDF
        if($ext != "pdf"){
            die("
            <script>
            alert('File harus PDF');
            window.history.back();
            </script>
            ");
        }

        // VALIDASI MAX 15 MB
        if($size > 15 * 1024 * 1024){
            die("
            <script>
            alert('Ukuran file maksimal 15 MB');
            window.history.back();
            </script>
            ");
        }

        $file_laporan = time()."_".preg_replace(
            "/[^a-zA-Z0-9._-]/",
            "",
            $_FILES['file_laporan']['name']
        );

        move_uploaded_file(
            $_FILES['file_laporan']['tmp_name'],
            "uploads/".$file_laporan
        );
    }

    /* =========================
       GENERATE KODE TIKET
    ========================= */

    function generateKode($length = 6){

        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';

        return substr(str_shuffle($chars), 0, $length);
    }

    $tahun = date("Y");

    do {

        $random = generateKode(6);

        $kode_tiket = "RKT/".$tahun."/".$random;

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

    } while($cek->num_rows > 0);

    /* =========================
       INSERT DATABASE
    ========================= */

    $status = "Menunggu";

    if($edit){

    $stmt = $conn->prepare("
    UPDATE rekomtek SET
        nama_pemohon=?,
        nama_perusahaan=?,
        nomor_handphone=?,
        alamat_perusahaan=?,
        jenis_izin=?,
        nomor_sk=?,
        jenis_kegiatan=?,
        sumber_air=?,
        laporan_triwulan_ke=?,
        tahun_laporan=?,
        file_laporan=?,
        status='Menunggu'
    WHERE kode_tiket=?
    ");

    $stmt->bind_param(
        "ssssssssssss",
        $nama_pemohon,
        $nama_perusahaan,
        $nomor_handphone,
        $alamat_perusahaan,
        $jenis_izin,
        $nomor_sk,
        $jenis_kegiatan,
        $sumber_air,
        $laporan_triwulan,
        $tahun_laporan,
        $file_laporan,
        $kode
    );

} else {

    $stmt = $conn->prepare("
    INSERT INTO rekomtek (
        kode_tiket,
        nama_pemohon,
        nama_perusahaan,
        nomor_handphone,
        alamat_perusahaan,
        jenis_izin,
        nomor_sk,
        jenis_kegiatan,
        sumber_air,
        laporan_triwulan_ke,
        tahun_laporan,
        file_laporan,
        status,
        created_at
    ) VALUES (
        ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()
    )
    ");

    $stmt->bind_param(
    "sssssssssssss",
    $kode_tiket,
    $nama_pemohon,
    $nama_perusahaan,
    $nomor_handphone,
    $alamat_perusahaan,
    $jenis_izin,
    $nomor_sk,
    $jenis_kegiatan,
    $sumber_air,
    $laporan_triwulan,
    $tahun_laporan,
    $file_laporan,
    $status
);

}

$stmt->execute();

$success_kode = $edit ? $kode : $kode_tiket;

/* =========================
   KIRIM WA HANYA SAAT INSERT
========================= */

if(!$edit){

    /* ===== WA KE ADMIN ===== */

    $pesanAdmin = "📥 Laporan Triwulan Baru\n\n";

    $pesanAdmin .= "Kode Tiket:\n".$kode_tiket."\n\n";
    $pesanAdmin .= "Nama Pemohon:\n".$nama_pemohon."\n\n";
    $pesanAdmin .= "Perusahaan:\n".$nama_perusahaan."\n\n";
    $pesanAdmin .= "Nomor SK:\n".$nomor_sk."\n\n";
    $pesanAdmin .= "Jenis Izin:\n".$jenis_izin."\n\n";
    $pesanAdmin .= "Triwulan:\n".$laporan_triwulan."\n\n";
    $pesanAdmin .= "Tahun:\n".$tahun_laporan."\n\n";
    $pesanAdmin .= "Tanggal:\n".date('d-m-Y H:i');

    kirimWA("6285142821420", $pesanAdmin);

    /* ===== WA KE PEMOHON ===== */

    $hpUser = preg_replace('/[^0-9]/', '', $nomor_handphone);

    if(substr($hpUser,0,1) == '0'){
        $hpUser = '62'.substr($hpUser,1);
    }

    if(strlen($hpUser) >= 10){

        $linkCek = "https://petesa.bwskalimantan4.web.id/cek.php?kode=".urlencode($kode_tiket);

        
        $pesanUser .= "Yth. Bapak/Ibu ".$nama_pemohon.",\n\n";

        $pesanUser .= "Laporan Triwulan telah berhasil diterima dan saat ini berstatus Menunggu Verifikasi.\n\n";

        $pesanUser .= "Kode Tiket:\n".$kode_tiket."\n\n";
        $pesanUser .= "Nomor SK:\n".$nomor_sk."\n\n";
        $pesanUser .= "Pemegang Izin:\n".$nama_perusahaan."\n\n";

        $pesanUser .= "Cek status laporan melalui link berikut:\n";
        $pesanUser .= $linkCek."\n\n";
        
        $pesanUser .= "Untuk informasi lebih lanjut:\n\n";
        $pesanUser .= "📱 WA: +62 823-5772-5758\n\n";
        $pesanUser .= "📧 Email: rekomendasitekniswsmahakam@gmail.com\n\n";
 
        $pesanUser .= "Mohon tidak membalas pesan otomatis ini.\n\n";
        $pesanUser .= "Terima kasih.\n";
        
        $pesanUser .= "Balai Wilayah Sungai Kalimantan IV Samarinda";

        kirimWA($hpUser, $pesanUser);
    }

}

}

/* =========================
   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>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>PETESA REKOMTEK</title>

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

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

<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;
}

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

.form-control,
.form-select{
    border-radius:10px;
    padding:10px;
}

.btn-success{
    padding:12px;
    font-weight:bold;
    border-radius:10px;
}

.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;
}

/* POPUP */

.popup-overlay{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background:rgba(0,0,0,0.6);
    display:none;
    justify-content:center;
    align-items:center;
    z-index:9999;
}

.popup-box{
    background:#fff;
    padding:30px;
    width:420px;
    border-radius:12px;
    text-align:center;
    box-shadow:0 15px 40px rgba(0,0,0,0.3);
}

.kode-box{
    margin:15px 0;
    display:flex;
    justify-content:center;
    gap:10px;
}

.kode-box span{
    font-size:24px;
    font-weight:bold;
}

.note{
    color:red;
    font-size:16px;
    font-weight:bold;
}

.note2{
    background:#ffffff;
    color:#0b3d91;
    padding:10px 18px;
    border:2px solid #0b3d91;
    border-radius:8px;
    font-size:12px;
    font-weight:bold;
}

#btnCopy{
    padding:6px 12px;
    border:none;
    background:#eee;
    border-radius:5px;
}

</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>

<!-- HEADER -->

<div class="container mt-5 mb-4">

<div class="text-center">

<h4 class="fw-bold text-primary">
LAPORAN TRIWULAN PEMEGANG PERIZINAN SUMBER DAYA AIR WILAYAH SUNGAI MAHAKAM 
BALAI WILAYAH SUNGAI KALIMANTAN IV SAMARINDA
</h3>

<p class="fw-bold mt-4">
Yth. Bapak/Ibu Pengguna Layanan
Balai Wilayah Sungai Kalimantan IV Samarinda
</p>

<p>
Berdasarkan ketentuan sebagaimana diatur dalam
Undang-Undang Nomor 17 Tahun 2019 tentang
Sumber Daya Air dan Surat Edaran Nomor 14/SE/Da/2025
tentang Pedoman Pengawasan Pengguna Sumber Daya Air,
dimohon kepada perusahaan untuk menyampaikan
laporan triwulanan pemanfaatan air secara berkala.
</p>

<p class="fw-bold">
- Balai Wilayah Sungai Kalimantan IV Samarinda -
</p>

</div>
</div>

<!-- FORM -->

<div class="container mb-5">

<div class="card shadow">

<div class="card-header bg-primary text-white py-3">
<h4 class="mb-0">
Form Pengumpulan Laporan Triwulan
</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 Pemohon
<span class="text-danger">*</span>
</label>

<input
type="text"
name="nama_pemohon"
class="form-control"
placeholder="Masukkan nama pemohon"
value="<?= $edit['nama_pemohon'] ?? '' ?>"
required>
</div>

<div class="col-md-6 mb-3">
<label>
Nomor SK
<span class="text-danger">*</span>
</label>

<input
type="text"
name="nomor_sk"
id="nomor_sk"
class="form-control"
placeholder="Masukkan nomor SK"
list="list_sk"
autocomplete="off"
value="<?= $edit['nomor_sk'] ?? '' ?>"
required>

<datalist id="list_sk"></datalist>
</div>

<div class="col-md-6 mb-3">
<label>
Nomor WhatsApp
<span class="text-danger">*</span>
</label>

<input
    type="text"
    name="nomor_handphone"
    class="form-control"
    placeholder="08xxxxxxxxxx"
    maxlength="13"
    value="<?= $edit['nomor_handphone'] ?? '' ?>"
    oninput="this.value = this.value.replace(/[^0-9]/g, '')"
    required>
</div>

<div class="col-md-6 mb-3">
<label>
Sumber Air
<span class="text-danger">*</span>
</label>

<input
type="text"
name="sumber_air"
id="sumber_air"
class="form-control"
placeholder="Masukkan sumber air"
value="<?= $edit['sumber_air'] ?? '' ?>"
required>
</div>

<div class="col-md-6 mb-3">
<label>
Jenis Izin
<span class="text-danger">*</span>
</label>

<select
name="jenis_izin"
id="jenis_izin"
class="form-select"
required>

<option value="">
-- Pilih Jenis Izin --
</option>

<!-- USAHA -->
<optgroup label="Usaha">

<option value="Usaha - Pengambilan Air Permukaan"
<?= (($edit['jenis_izin'] ?? '') == 'Usaha - Pengambilan Air Permukaan') ? 'selected' : '' ?>>
Pengambilan Air Permukaan
</option>

<option value="Usaha - Konstruksi di Sumber Air"
<?= (($edit['jenis_izin'] ?? '') == 'Usaha - Konstruksi di Sumber Air') ? 'selected' : '' ?>>
Konstruksi di Sumber Air
</option>

</optgroup>

<!-- BUKAN USAHA -->
<optgroup label="Bukan Usaha">

<option value="Bukan Usaha - Pengambilan Air Permukaan"
<?= (($edit['jenis_izin'] ?? '') == 'Bukan Usaha - Pengambilan Air Permukaan') ? 'selected' : '' ?>>
Pengambilan Air Permukaan
</option>

<option value="Bukan Usaha - Konstruksi di Sumber Air"
<?= (($edit['jenis_izin'] ?? '') == 'Bukan Usaha - Konstruksi di Sumber Air') ? 'selected' : '' ?>>
Konstruksi di Sumber Air
</option>

</optgroup>

<!-- ALUR SUNGAI -->
<optgroup label="Alur Sungai">

<option value="Pengalihan Alur Sungai"
<?= (($edit['jenis_izin'] ?? '') == 'Pengalihan Alur Sungai') ? 'selected' : '' ?>>
Pengalihan Alur Sungai
</option>

</optgroup>

<!-- PEMANFAATAN IRIGASI -->
<optgroup label="Pemanfaatan Irigasi">

<option value="Air dan/atau Daya Air Irigasi"
<?= (($edit['jenis_izin'] ?? '') == 'Air dan/atau Daya Air Irigasi') ? 'selected' : '' ?>>
Air dan/atau Daya Air Irigasi
</option>

<option value="Jaringan Irigasi"
<?= (($edit['jenis_izin'] ?? '') == 'Jaringan Irigasi') ? 'selected' : '' ?>>
Jaringan Irigasi
</option>

<option value="Sempadan Irigasi"
<?= (($edit['jenis_izin'] ?? '') == 'Sempadan Irigasi') ? 'selected' : '' ?>>
Sempadan Irigasi
</option>

</optgroup>

</select>
</div>

<div class="col-md-6 mb-3">
<label>
Nama Perusahaan
<span class="text-danger">*</span>
</label>

<input
type="text"
name="nama_perusahaan"
id="nama_perusahaan"
class="form-control"
placeholder="Masukkan nama perusahaan"
value="<?= $edit['nama_perusahaan'] ?? '' ?>"
required>
</div>

<div class="col-md-6 mb-3">

<label>
Jenis Kegiatan
<span class="text-danger">*</span>
</label>

<select
name="jenis_kegiatan"
id="jenis_kegiatan"
class="form-select"
required>

<option value="">
-- Pilih Jenis Kegiatan --
</option>

<?php if(isset($edit) && !empty($edit['jenis_kegiatan'])){ ?>

<option value="<?= $edit['jenis_kegiatan'] ?>" selected>
<?= $edit['jenis_kegiatan'] ?>
</option>

<?php } ?>

</select>

<!-- INPUT MANUAL -->
<input
type="text"
name="jenis_kegiatan_manual"
id="jenis_kegiatan_manual"
class="form-control mt-2"
placeholder="Tulis jenis kegiatan lainnya..."
value="<?= $edit['jenis_kegiatan'] ?? '' ?>"
style="display:none;">

</div>

<div class="col-md-6 mb-3">
<label>
Alamat Perusahaan
<span class="text-danger">*</span>
</label>

<textarea
name="alamat_perusahaan"
class="form-control"
required><?= $edit['alamat_perusahaan'] ?? '' ?></textarea>
</div>

<div class="col-md-6 mb-3">
<label>
Laporan Triwulan Ke
<span class="text-danger">*</span>
</label>

<select
name="laporan_triwulan"
class="form-select"
required>

<option value="">
-- Pilih Triwulan --
</option>

<option value="Triwulan I"
<?= (($edit['laporan_triwulan_ke'] ?? '') == 'Triwulan I (Januari-Maret)') ? 'selected' : '' ?>>
Triwulan I (Januari-Maret)
</option>
<option value="Triwulan II"
<?= (($edit['laporan_triwulan_ke'] ?? '') == 'Triwulan II (April-Juni)') ? 'selected' : '' ?>>
Triwulan II (April-Juni)
</option>
<option value="Triwulan III"
<?= (($edit['laporan_triwulan_ke'] ?? '') == 'Triwulan III (Juli-September)') ? 'selected' : '' ?>>
Triwulan III (Juli-September)
</option>
<option value="Triwulan IV"
<?= (($edit['laporan_triwulan_ke'] ?? '') == 'Triwulan IV (Oktober-Desember)') ? 'selected' : '' ?>>
Triwulan IV (Oktober-Desember)
</option>

</select>
</div>

<div class="col-md-6 mb-3">

<label>
    Tahun Laporan
    <span class="text-danger">*</span>
</label>

<input
    type="text"
    name="tahun_laporan"
    id="tahun_laporan"
    class="form-control"
    value="<?= $edit['tahun_laporan'] ?? '' ?>"
    placeholder="Pilih Tahun"
    autocomplete="off"
    required>
</select>

</div>

<div class="col-md-12 mb-3">

<label>
Upload Dokumen Triwulan
<span class="text-danger">*</span>
</label>
<small class="text-danger">
Format PDF, ukuran maksimal 15 MB.
</small>

<?php if(!empty($edit['file_laporan'])): ?>

<div class="mb-2">

File lama:
<a href="uploads/<?= $edit['file_laporan'] ?>"
target="_blank">

<?= $edit['file_laporan'] ?>

</a>

</div>

<?php endif; ?>

<input
type="file"
name="file_laporan"
class="form-control"
accept=".pdf,application/pdf"
<?= !$edit ? 'required' : '' ?>>

<!-- Contoh Dokumen -->
<div class="alert alert-info mt-3">
    <strong>
        <i class="fas fa-file-word text-primary"></i>
        Contoh Dokumen Laporan Triwulan
    </strong>

    <p class="mb-2 mt-2">
        Silakan unduh atau buka contoh dokumen sebagai acuan penyusunan laporan.
    </p>

    <a href="assets/laporan_triwulan.docx"
       download
       class="btn btn-sm btn-success">
        <i class="fas fa-download"></i> Download Contoh
    </a>
</div>

</div>

<button
type="submit"
name="kirim"
class="btn btn-success w-100"
id="btnSubmit">

Kirim Laporan

</button>

</form>

<p class="fw-bold mt-4">
Apabila terdapat hal yang perlu dikonfirmasi lebih lanjut dapat menghubungi:
</p>

<p>
Nomor HP/WA: 0823 5772 5758 <br>
Email: rekomendasitekniswsmahakam@gmail.com
</p>

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

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<!-- FOOTER -->
<footer class="footer-bws">
<div class="container-fluid py-5">
<div class="row text-white">

    <!-- PROFIL (LEBIH LEBAR) -->
    <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>

    <!-- SOSIAL MEDIA -->
    <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" rel="noopener noreferrer">
    <i class="fab fa-facebook"></i>
    </a>
             <!-- Instagram -->
    <a href="https://www.instagram.com/pu_sda_kalimantan4" target="_blank" rel="noopener noreferrer">
        <i class="fab fa-instagram"></i>
    </a>

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

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

    <!-- TikTok -->
    <a href="https://www.tiktok.com/@pu_sda_kalimantan5" target="_blank" rel="noopener noreferrer">
        <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,. All Rights Reserved
</div>

</div>
</footer>

<!-- POPUP -->

<div id="popupSukses" class="popup-overlay">

<div class="popup-box">

<h4>Laporan Berhasil Dikirim</h4>

<p>Kode Tiket Anda:</p>

<div class="kode-box">

<span id="kodeTiket"></span>

<button id="btnCopy">
Copy
</button>

</div>

<p class="note">
Simpan kode tiket untuk cek status laporan
</p>

<p class="note2">
Laporan akan diproses maksimal 14 hari kerja
</p>

<button
class="btn btn-primary mt-3"
onclick="tutupPopup()">

OK

</button>

</div>
</div>

<!-- JS -->

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

<script>

/* VALIDASI */

(() => {

  const forms = document.querySelectorAll('.needs-validation');

  forms.forEach(form => {

    form.addEventListener('submit', e => {

      if (!form.checkValidity()) {

        e.preventDefault();
        e.stopPropagation();

        alert('Harap isi semua field wajib');

      }

      form.classList.add('was-validated');

    });

  });

})();

/* LOADING BUTTON */

document.querySelector('form').addEventListener('submit', function(){

    document.getElementById('btnSubmit').innerHTML =
    'Mengirim...';

});

/* ONLINE */

setInterval(() => {

fetch('online.php')

.then(res => res.text())

.then(data => document.getElementById('online').innerText = data);

}, 5000);

/* POPUP */

function tampilPopup(kode){

    document.getElementById("kodeTiket").innerText = kode;

    document.getElementById("popupSukses").style.display = "flex";

    let btn = document.getElementById("btnCopy");

    btn.innerText = "Copy";

    btn.onclick = function(){

        navigator.clipboard.writeText(kode).then(() => {

            btn.innerText = "Copied ✅";

        });

    }
}

/* TUTUP */

function tutupPopup() {
    window.location = "cek.php?jenis=rekomtek";
}

/* =========================
   DROPDOWN BERTINGKAT
========================= */

const kegiatan = {

"Usaha": [
"Pembangkit tenaga listrik",
"Pemanfaatan ruang pada Sumber Air dan/atau sempadan Sumber Air untuk kegiatan konstruksi",
"Transportasi",
"Olahraga",
"Pariwisata",
"Perikanan",
"Industri",
"Makanan dan Minuman",
"Perhotelan",
"Perkebunan",
"Sistem Penyediaan Air Minum (SPAM)",
"Air Minum Dalam Kemasan",
"Pertambangan",
"Kebutuhan Domestik",
"Penunjang Kegiatan Usaha"
],

"Bukan Usaha": [
"Pemenuhan Kebutuhan Pokok Sehari-Hari",
"Pertanian Rakyat",
"Pembangkit listrik tenaga yang bukan usaha",
"Pemanfaatan ruang pada Sumber Air dan/atau sempadan sumber air untuk kegiatan konstruksi bagi kepentingan umum",
"Pemanfaatan ruang pada sumber air untuk pembangkit listrik tenaga surya terapung",
"Wisata / Olahraga Air yang bukan usaha",
"Pemanfaatan SDA untuk Penelitian, Pengembangan dan Pendidikan",
"Penggunaan Air untuk Taman Kota, Rumah Ibadah, Fasilitas Umum & Fasilitas Sosial Lainnya"
],

"Alur Sungai": [
"Pengalihan Alur Sungai"
],

"Pemanfaatan Irigasi": [
"Pengambilan Air Irigasi dan/atau Daya Air Irigasi",
"Pembangunan Jaringan Irigasi",
"Peningkatan Jaringan Irigasi (mengubah jaringan irigasi yang ada)",
"Rehabilitasi Jaringan Irigasi",
"Pemanfaatan Jaringan Irigasi (di atas / di bawah jaringan irigasi termasuk sempadan irigasi)"
]

};



/* SAAT JENIS IZIN DIPILIH */

document.getElementById("jenis_izin")
.addEventListener("change", function(){

    let value = this.value;
    let izin = "";

    // KELOMPOK USAHA
    if(value.includes("Usaha -")){
        izin = "Usaha";
    }

    // KELOMPOK BUKAN USAHA
    else if(value.includes("Bukan Usaha -")){
        izin = "Bukan Usaha";
    }

    // KELOMPOK ALUR SUNGAI
    else if(value == "Pengalihan Alur Sungai"){
        izin = "Alur Sungai";
    }

    // KELOMPOK IRIGASI
    else if(
        value == "Air dan/atau Daya Air Irigasi" ||
        value == "Jaringan Irigasi" ||
        value == "Sempadan Irigasi"
    ){
        izin = "Pemanfaatan Irigasi";
    }

    let kegiatanSelect =
    document.getElementById("jenis_kegiatan");

    kegiatanSelect.innerHTML =
    '<option value="">-- Pilih Jenis Kegiatan --</option>';

    if(kegiatan[izin]){

        kegiatan[izin].forEach(function(item){

            let option = document.createElement("option");

            option.value = item;
            option.textContent = item;

            kegiatanSelect.appendChild(option);

        });

    }

    // OPSI LAINNYA
    let optionLain = document.createElement("option");

    optionLain.value = "lainnya";
    optionLain.textContent = "Lainnya...";

    kegiatanSelect.appendChild(optionLain);

});

/* JIKA PILIH LAINNYA */

document.getElementById("jenis_kegiatan")
.addEventListener("change", function(){

    let inputManual =
    document.getElementById("jenis_kegiatan_manual");

    if(this.value == "lainnya"){

        inputManual.style.display = "block";
        inputManual.required = true;

    } else {

        inputManual.style.display = "none";
        inputManual.required = false;
        inputManual.value = "";

    }

});
const nomorSk = document.getElementById("nomor_sk");

nomorSk.addEventListener("keyup", function(){

    let keyword = this.value;

    fetch("get_sk.php?q=" + keyword)

    .then(res => res.json())

    .then(data => {

        let list = document.getElementById("list_sk");

        list.innerHTML = "";

        data.forEach(item => {

            let option = document.createElement("option");

            option.value = item.nomor_sk;

            list.appendChild(option);

        });

    });

});

/* AUTO ISI */
nomorSk.addEventListener("change", function(){

    let keyword = this.value;

    fetch("get_sk.php?q=" + keyword)

    .then(res => res.json())

    .then(data => {

        data.forEach(item => {

            if(item.nomor_sk == keyword){

                document.getElementById("nama_perusahaan").value =
                item.nama_perusahaan;

                document.getElementById("sumber_air").value =
                item.sumber_air;

            }

        });

    });

});
</script>

<?php if(isset($success_kode)): ?>

<script>

window.onload = function(){

    tampilPopup("<?= $success_kode ?>");

}

</script>

<?php endif; ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-datepicker@1.10.0/dist/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-datepicker@1.10.0/dist/js/bootstrap-datepicker.min.js"></script>

<script>
$('#tahun_laporan').datepicker({
    format: "yyyy",
    viewMode: "years",
    minViewMode: "years",
    autoclose: true
});
</script>
<script>
/* VALIDASI FILE PDF MAX 15 MB */

const fileLaporan = document.querySelector('[name="file_laporan"]');

if(fileLaporan){

    fileLaporan.addEventListener('change', function(){

        const file = this.files[0];

        if(file && file.size > 15 * 1024 * 1024){

            alert('Ukuran file maksimal 15 MB');

            this.value = ''; // hapus file yang dipilih

        }

    });

}

/* CEK LAGI SAAT SUBMIT */

document.querySelector('form').addEventListener('submit', function(e){

    const file = document.querySelector('[name="file_laporan"]').files[0];

    if(file && file.size > 15 * 1024 * 1024){

        alert('Ukuran file maksimal 15 MB');

        e.preventDefault();

        return false;

    }

});
</script>
</body>
</html>

