<?php
// Koneksi ke database
include '../koneksi.php'; // Pastikan path ini benar
include 'header.php';

// Ambil ID pasien dari parameter query
$id_pasien = isset($_GET['id']) ? mysqli_real_escape_string($koneksi, $_GET['id']) : 0;

// Query untuk mendapatkan data dari tabel 'tbl_pasien' berdasarkan ID pasien
$pasien_query = "SELECT * FROM tbl_pasien WHERE id_pasien = '$id_pasien'";
$pasien_result = mysqli_query($koneksi, $pasien_query);

// Periksa jika query gagal
if (!$pasien_result) {
    die("Query Error: " . mysqli_error($koneksi));
}

$pasien = mysqli_fetch_assoc($pasien_result);

// Jika pasien tidak ditemukan
if (!$pasien) {
    die("Data pasien tidak ditemukan.");
}
?>

<div id="wrapper">
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <div class="container-fluid">
                <h1 class="h3 mb-4 text-gray-800">Edit pasien</h1>
                
                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary">Edit Data pasien</h6>
                    </div>
                    <div class="card-body">
                        <form action="pasien_update.php" method="POST" enctype="multipart/form-data">
                            <input type="hidden" name="id_pasien" value="<?php echo $pasien['id_pasien']; ?>">
                            
                            <div class="form-group mb-3">
                                <label for="nama_pasien">Nik pasien</label>
                                <input type="text" class="form-control" id="nik_pasien" name="nik" value="<?php echo htmlspecialchars($pasien['nik']); ?>" placeholder="Masukkan nik pasien" required>
                            </div>
                            
                            <div class="form-group mb-3">
                                <label for="nama_pasien">Nama Pasien</label>
                                <input type="text" class="form-control" id="nama_pasien" name="nama_pasien" value="<?php echo htmlspecialchars($pasien['nama_pasien']); ?>" placeholder="Masukkan Nama Pasien" required>
                            </div>
                            <div class="form-group mb-3">
                                <label for="username">Username</label>
                                <input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($pasien['username']); ?>" placeholder="Masukkan Username" required>
                            </div>
                            <div class="form-group mb-3">
                                <label for="password">Nama Pasien</label>
                                <input type="text" class="form-control" id="password" name="password" value="<?php echo htmlspecialchars($pasien['password']); ?>" placeholder="Masukkan Nama Password" required>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-2">
                                        <input type="submit" class="btn btn-sm btn-primary" value="Simpan">
                                         <a href="pasien.php" class="btn btn-danger btn-sm">Kembali</a>
                                    </div>
                                    <div class="col-lg-2">
                                       
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
