<?php
define('SECURE_ACCESS', true);
include "koneksi.php";
session_start();

if(!isset($_SESSION['responden_id'])){
    header("Location: index.php");
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cek Data Kunjungan</title>

<style>
body{
font-family:Arial;
background:#f4f6f9;
margin:0;
}
.wrapper{
display:flex;
justify-content:center;
margin-top:60px;
padding:20px;
}
.container{
width:100%;
max-width:500px;
background:white;
padding:35px;
border-radius:18px;
box-shadow:0 15px 35px rgba(0,0,0,.15);
text-align:center;
}
input{
width:100%;
padding:12px;
margin-top:10px;
border-radius:8px;
border:1px solid #ccc;
}
button{
width:100%;
padding:14px;
margin-top:18px;
background:#8B0000;
color:white;
border:none;
border-radius:10px;
cursor:pointer;
}
</style>
</head>

<body>

<?php include "banner.php"; ?>

<div class="wrapper">
<div class="container">

<h2>Masukkan Nomor Identitas</h2>

<input type="text" id="nik" placeholder="KTP / SIM / Paspor" maxlength="25">

<button onclick="cek()">Cek Data</button>

<div id="hasil"></div>

</div>
</div>

<script>
function cek(){

let nik = document.getElementById("nik").value.trim();

/* VALIDASI IDENTITAS */
if(nik==""){
alert("Masukkan nomor identitas terlebih dahulu");
return;
}

/* minimal 6 karakter supaya tidak kosong */
if(nik.length < 6){
alert("Nomor identitas tidak valid");
return;
}

fetch("cek_data.php",{
method:"POST",
headers:{
"Content-Type":"application/x-www-form-urlencoded"
},
body:"nik="+encodeURIComponent(nik)
})
.then(res=>res.json())
.then(res=>{

if(res.status=="ada"){

document.getElementById("hasil").innerHTML = `
<div style="margin-top:20px;text-align:left;">
<b>Data Ditemukan:</b><br><br>
Nama : ${res.nama}<br><br>
<form method="POST" action="set_survey_session.php">
<input type="hidden" name="responden_id" value="${res.id}">
<button type="submit"
style="width:100%;padding:12px;background:#8B0000;color:white;border:none;border-radius:8px;">
Lanjut ke Survey
</button>
</form>
</div>
`;

}else{

document.getElementById("hasil").innerHTML = `
<div style="margin-top:20px;color:red;">
Data tidak ditemukan.
</div>
`;

}

})
.catch(err=>{
alert("Terjadi kesalahan koneksi");
console.log(err);
});

}
</script>

</body>
</html>
