<?php
// viewPatient.php

require_once __DIR__ . '/../../dripend/includes/db.php';

// Validate ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
    die("Invalid patient reference.");
}

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

/** Lookup helper */
function getName($conn, $table, $id, $field){
    if(!$id) return '';
    $stmt = $conn->prepare("SELECT $field FROM $table WHERE id=? LIMIT 1");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc();
    return $row[$field] ?? '';
}

// Fetch patient
$stmt = $conn->prepare("SELECT * FROM patient WHERE id=? LIMIT 1");
$stmt->bind_param("i", $patient_id);
$stmt->execute();
$result = $stmt->get_result();

if (!$result || $result->num_rows === 0) {
    die("Patient not found.");
}

$p = $result->fetch_assoc();

// Resolve lookups
$prefix        = getName($conn,'prefix',$p['prefix'],'prefix_name');
$gender        = getName($conn,'gender',$p['gender_id'],'gender_name');
$staff         = getName($conn,'staff_status',$p['staff_status_id'],'status_name');
$child         = getName($conn,'child_status',$p['child_status_id'],'child_name');
$insurance     = getName($conn,'insurance_type',$p['other_insurance_id'],'insurance_name');
$occupation    = getName($conn,'occupation',$p['occupation_id'],'occupation_name');
$language      = getName($conn,'languages',$p['language_id'],'language_name');
$education     = getName($conn,'education_level',$p['education_id'],'education_name');
$marital       = getName($conn,'marital_status',$p['marital_status_id'],'status_name');
$relation      = getName($conn,'emergency_relation',$p['emergency_relation_id'],'relation_name');
$religion      = getName($conn,'religion',$p['religion'],'religion_name');

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>View Patient</title>

<style>
body{
    margin:0;
    font-family:Segoe UI, Arial;
    background:rgba(0,0,0,0.6);
}

.modal{
    display:flex;
    align-items:center;
    justify-content:center;
    height:100vh;
}

.modal-card{
    background:#fff;
    width:90%;
    max-width:900px;
    border-radius:10px;
    padding:20px;
    overflow-y:auto;
    max-height:90vh;
}

.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:15px;
}

.header h2{
    color:#023e8a;
}

.btn{
    padding:8px 14px;
    border:none;
    border-radius:6px;
    cursor:pointer;
    color:#fff;
}

.btn-close{ background:#6c757d; }
.btn-edit{ background:#0077b6; }

.section{
    margin-top:15px;
    padding:10px;
    background:#f1f5f9;
    border-radius:6px;
    font-weight:bold;
}

.grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
    gap:10px;
    margin-top:10px;
}

.field{
    background:#fafafa;
    padding:8px;
    border-radius:6px;
}

.label{
    font-size:12px;
    color:#555;
}

.value{
    font-weight:600;
}
</style>
</head>

<body>

<div class="modal">
<div class="modal-card">

<div class="header">
    <h2>Patient Details</h2>
    <div>
        <button class="btn btn-edit" onclick="window.location.href='editPatient.php?id=<?php echo $patient_id; ?>'">Edit</button>
        <button class="btn btn-close" onclick="closeModal()">Close</button>
    </div>
</div>

<div class="section">Basic Information</div>
<div class="grid">
<div class="field"><div class="label">Patient No</div><div class="value"><?php echo $p['patient_no']; ?></div></div>
<div class="field"><div class="label">Prefix</div><div class="value"><?php echo $prefix; ?></div></div>
<div class="field"><div class="label">First Name</div><div class="value"><?php echo $p['first_name']; ?></div></div>
<div class="field"><div class="label">Middle Name</div><div class="value"><?php echo $p['middle_name']; ?></div></div>
<div class="field"><div class="label">Last Name</div><div class="value"><?php echo $p['last_name']; ?></div></div>
<div class="field"><div class="label">Gender</div><div class="value"><?php echo $gender; ?></div></div>
<div class="field"><div class="label">Mobile</div><div class="value"><?php echo $p['mobile']; ?></div></div>
<div class="field"><div class="label">DOB</div><div class="value"><?php echo $p['dob']; ?></div></div>
<div class="field"><div class="label">Age</div><div class="value"><?php echo $p['age']; ?></div></div>
</div>

<div class="section">Insurance Details</div>
<div class="grid">
<div class="field"><div class="label">NHIS No</div><div class="value"><?php echo $p['nhis_no']; ?></div></div>
<div class="field"><div class="label">Insurance</div><div class="value"><?php echo $insurance; ?></div></div>
<div class="field"><div class="label">Insurance No</div><div class="value"><?php echo $p['other_insurance_no']; ?></div></div>
<div class="field"><div class="label">MOH No</div><div class="value"><?php echo $p['moh_no']; ?></div></div>
<div class="field"><div class="label">Ghana Card</div><div class="value"><?php echo $p['ghana_card_no']; ?></div></div>
</div>

<div class="section">Socio-Demographic</div>
<div class="grid">
<div class="field"><div class="label">Occupation</div><div class="value"><?php echo $occupation; ?></div></div>
<div class="field"><div class="label">Language</div><div class="value"><?php echo $language; ?></div></div>
<div class="field"><div class="label">Education</div><div class="value"><?php echo $education; ?></div></div>
<div class="field"><div class="label">Marital Status</div><div class="value"><?php echo $marital; ?></div></div>
<div class="field"><div class="label">Religion</div><div class="value"><?php echo $religion; ?></div></div>
</div>

<div class="section">Emergency Contact</div>
<div class="grid">
<div class="field"><div class="label">Name</div><div class="value"><?php echo $p['emergency_name']; ?></div></div>
<div class="field"><div class="label">Number</div><div class="value"><?php echo $p['emergency_number']; ?></div></div>
<div class="field"><div class="label">Address</div><div class="value"><?php echo $p['emergency_address']; ?></div></div>
<div class="field"><div class="label">Relation</div><div class="value"><?php echo $relation; ?></div></div>
</div>

</div>
</div>

<script>
function closeModal(){
    window.location.href='totalPatientReport.php';
}
</script>

</body>
</html>