<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once "../config/db.php";
require_once "../inc/auth_admin.php";

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

/* =========================
   AMBIL DATA PENGHUNI
========================= */
$stmt = $conn->prepare("
SELECT penghuni.*, kamar.nomor_kamar
FROM penghuni
JOIN kamar ON penghuni.kamar_id = kamar.id
WHERE penghuni.id=?
");

$stmt->bind_param("i",$id);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();

/* =========================
   AMBIL KAMAR KOSONG
========================= */
$kamar_kosong = $conn->query("SELECT * FROM kamar WHERE status='kosong'");

/* =========================
   UPDATE DATA / PINDAH KAMAR
========================= */
if(isset($_POST['update'])){

$nama = $_POST['nama'];
$no_hp = $_POST['no_hp'];
$ktp = $_POST['ktp'];
$kamar_baru = intval($_POST['kamar_id']);
$kamar_lama = $data['kamar_id'];

$stmt = $conn->prepare("
UPDATE penghuni 
SET nama=?, no_hp=?, ktp=?, kamar_id=? 
WHERE id=?
");

$stmt->bind_param("sssii",$nama,$no_hp,$ktp,$kamar_baru,$id);
$stmt->execute();

/* =========================
   JIKA PINDAH KAMAR
========================= */
if($kamar_baru != $kamar_lama){

$conn->query("UPDATE kamar SET status='kosong' WHERE id='$kamar_lama'");
$conn->query("UPDATE kamar SET status='terisi' WHERE id='$kamar_baru'");

$conn->query("
UPDATE riwayat_kamar 
SET tanggal_keluar=CURDATE() 
WHERE penghuni_id='$id' 
AND tanggal_keluar IS NULL
");

$stmt2 = $conn->prepare("
INSERT INTO riwayat_kamar
(penghuni_id,kamar_id,tanggal_masuk)
VALUES (?,?,CURDATE())
");

$stmt2->bind_param("ii",$id,$kamar_baru);
$stmt2->execute();

}

header("Location: penghuni.php");
exit;

}
?>

<?php include "../inc/header.php"; ?>
<?php include "../inc/navbar_admin.php"; ?>

<div class="container">

<div class="section">

<h3>Edit Data Penghuni</h3>

<div class="info-box">
<b>Kamar Saat Ini:</b> <?= htmlspecialchars($data['nomor_kamar']) ?><br>
</div>

<form method="POST">

<label>Nama Penghuni</label>
<input type="text" name="nama" value="<?= htmlspecialchars($data['nama']) ?>" required>

<label>No HP</label>
<input type="text" name="no_hp" value="<?= htmlspecialchars($data['no_hp']) ?>" required>

<label>No KTP</label>
<input type="text" name="ktp" value="<?= htmlspecialchars($data['ktp']) ?>" required>

<label>Pindah Kamar</label>

<select name="kamar_id">

<option value="<?= $data['kamar_id'] ?>">
Tetap di kamar <?= $data['nomor_kamar'] ?>
</option>

<?php while($k = $kamar_kosong->fetch_assoc()): ?>

<option value="<?= $k['id'] ?>">
<?= $k['nomor_kamar'] ?>
</option>

<?php endwhile; ?>

</select>

<div class="btn-group">
<button name="update">Update Data</button>
<a href="penghuni.php" class="btn-cancel">Batal</a>
</div>

</form>

</div>

</div>

<style>

.section{
background:white;
padding:25px;
border-radius:10px;
margin:auto;
max-width:600px;
box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

.section h3{
margin-top:0;
margin-bottom:20px;
}

label{
display:block;
margin-bottom:6px;
font-weight:bold;
font-size:14px;
}

input,select{
width:100%;
padding:10px;
margin-bottom:15px;
border:1px solid #ddd;
border-radius:6px;
font-size:14px;
}

button{
padding:10px 18px;
background:#2563eb;
color:white;
border:none;
border-radius:6px;
cursor:pointer;
}

button:hover{
background:#1e40af;
}

.btn-cancel{
margin-left:10px;
text-decoration:none;
color:#555;
font-size:14px;
}

.info-box{
background:#f1f5f9;
padding:12px;
border-radius:6px;
margin-bottom:15px;
font-size:14px;
}

.btn-group{
margin-top:10px;
}

/* MOBILE */

@media(max-width:768px){

.container{
padding:15px;
}

.section{
max-width:100%;
padding:20px;
}

button{
width:100%;
margin-bottom:10px;
}

.btn-cancel{
display:block;
margin-left:0;
margin-top:5px;
}

}

</style>

<?php include "../inc/footer.php"; ?>