Files
gravity/hosxp-webservice/components/card_patient_details.php
T
2026-09-16 23:20:08 +07:00

1087 lines
70 KiB
PHP

<?php
//@session_start();
//$login_id = $_SESSION['account'];
//$pt_lab = $_SESSION['lab_pt'];
//require("connect_web.php");
//require("connect_hosxp.php");
//require("connect_web.php");
//require("date_format.php");
//$hn = base64_decode($_GET['hn']);
//$nn = base64_decode($_GET['nn']);
$timezone = "Asia/Bangkok";
if (function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$today = date("Y-m-d H:i:s");
try {
$sql_view_hn = "INSERT INTO check_view_hn VALUES (NULL, '$login_id', '$hn','$nn', '$today');";
$result_view_hn = mysqli_query($conn2, $sql_view_hn);
} catch (Exception $e) {
// Ignore logging errors to prevent 500
}
$sql1 = "select a.*,c.name as pt from patient a left outer join ovst b on a.hn=b.hn left outer join pttype c on a.pttype=c.pttype where a.hn='$hn'";
$result1 = mysqli_query($conn1, $sql1);
$row1 = $result1 ? mysqli_fetch_array($result1) : [];
$sql2 = "SELECT p.hometel as tel, n.name as nation, r.name as religion1, img.image as img1, concat(p.addrpart,' หมู่ ',p.moopart,' ต. ',t3.name,' อ.',t2.name,' จ. ',t1.name,' ',p.po_code) as addr FROM patient p left outer join thaiaddress t1 on t1.addressid=concat(p.chwpart,'0000') left outer join thaiaddress t2 on t2.addressid=concat(p.chwpart,p.amppart,'00') left outer join thaiaddress t3 on t3.addressid=concat(p.chwpart,p.amppart,p.tmbpart) left outer join nationality n on p.nationality=n.nationality left outer join religion r on p.religion=r.religion left outer join patient_image img on p.hn=img.hn where p.hn='$hn' ";
$result2 = mysqli_query($conn1, $sql2);
$row2 = $result2 ? mysqli_fetch_array($result2) : [];
$ucid = $row1['cid'];
//SQL Check Covid19 Certificate
// $sql_covid19 = "select * from cert_request where cid='$ucid' and cert_file !='' ";
// $result_covid19 = mysqli_query($conn_covid19, $sql_covid19);
// $num_row_covid19 = mysqli_num_rows($result_covid19);
// $row_covid19 = mysqli_fetch_array($result_covid19);
// if ($num_row_covid19 != 0) {
// $cert_path = "https://www1.samuihospital.go.th/line-bot/covid19/cert/cert_view_data_doc_pc.php?cid=" . $row_covid19['cid'];
// $cert_title = '<i class="iconsweets-magnifying iconsweets-white"></i>คลิกเปิดดู';
// $cert_a_class = "badge badge-info";
// } else {
// $cert_path = "#";
// $cert_title = "ไม่พบข้อมูล";
// $cert_a_class = "";
// }
//---- Check visit OPD/IPD -------
$sql_check_visit = "select * from ovst where hn='$hn' and vstdate='$vdate'";
$result_check_visit = mysqli_query($conn1, $sql_check_visit);
$row_check_visit = $result_check_visit ? mysqli_fetch_array($result_check_visit) : false;
//----------------------------------
//---- Resolve true VN (in case $nn is an AN) -------
if ($vdate != NULL) {
$sql4 = "select a.vstdate, a.vsttime, a.vn as a_vn from ovst a where a.hn='$hn' and (a.vn='$nn' or a.an='$nn')";
$result4 = mysqli_query($conn1, $sql4);
$row4 = $result4 ? mysqli_fetch_array($result4) : [];
$vn1 = isset($row4['a_vn']) ? $row4['a_vn'] : $nn;
} else {
$vn1 = $nn;
}
//----------------------------------
//---- Check visit Report Lan Covid-19 -------
$sql_check_novel_covid19 = "select h.lab_order_number,h.report_date,h.report_time
from lab_head h left outer join lab_order lo on h.lab_order_number=lo.lab_order_number
where lo.lab_items_code in('1822','1837','1842','1856','1900','2030','2038','2044','1846','2054','2103') and (h.vn='$vn1' or h.vn='$nn') and ( lo.lab_order_result not in(' ') or lo.lab_order_result is not null ) order by lo.lab_items_code";
$result_check_novel_covid19 = mysqli_query($conn1, $sql_check_novel_covid19);
$num_row_check_novel_covid19 = $result_check_novel_covid19 ? mysqli_num_rows($result_check_novel_covid19) : 0;
$row_check_novel_covid19 = $result_check_novel_covid19 ? mysqli_fetch_array($result_check_novel_covid19) : [];
if ($num_row_check_novel_covid19 == 0) {
$novel_covid19_box = "none";
} else {
$novel_covid19_box = "table-row";
}
//----------------------------------
//---- Check Doctor Cert Covid-19 -------
$sql_check_doctor_cert_covid19 = "select * from universal_head where universal_form_id='95' and vn = '$nn'";
$result_check_doctor_cert_covid19 = mysqli_query($conn1, $sql_check_doctor_cert_covid19);
$num_row_check_doctor_cert_covid19 = $result_check_doctor_cert_covid19 ? mysqli_num_rows($result_check_doctor_cert_covid19) : 0;
$row_check_doctor_cert_covid19 = $result_check_doctor_cert_covid19 ? mysqli_fetch_array($result_check_doctor_cert_covid19) : [];
if ($num_row_check_doctor_cert_covid19 == 0) {
$doctor_cert_covid19_box = "none";
} else {
$doctor_cert_covid19_box = "table-row";
}
//----------------------------------
if ($vdate != NULL) {
// 2. Get Vitals from opdscreen
$sql_vitals = "select * from opdscreen where vn='$vn1'";
$result_vitals = mysqli_query($conn1, $sql_vitals);
$row_vitals = $result_vitals ? mysqli_fetch_array($result_vitals) : [];
$vsign = "BT : " . sprintf("%.1f", (float)@$row_vitals["temperature"]) . "&nbsp;&deg;C BP :&nbsp;" . (int)@$row_vitals["bps"] . "/" . (int)@$row_vitals["bpd"] . "&nbsp;mmHg ";
$vsign .= "PR : " . (int)@$row_vitals["pulse"] . " /min " . "RR : " . (int)@$row_vitals["rr"] . " /min" . "&nbsp;BW : " . sprintf("%.1f", (float)@$row_vitals["bw"]) . " Kg. ";
if (@$row_vitals["height"] <> 0) {
$vsign .= "Ht : " . (int)@$row_vitals["height"] . " Cm ";
}
if (@$row_vitals["fbs"] <> 0) {
$vsign .= "FBS : " . (int)@$row_vitals["fbs"] . " mg/dl";
}
if (@$row_vitals["bmi"] <> 0) {
$vsign .= "BMI : " . (int)@$row_vitals["bmi"] . " Kg/(m2)";
}
// Debug output
error_log("DEBUG SQL4: $sql4\nDEBUG vn1: $vn1\nDEBUG nn: $nn\n", 3, "debug_log.txt");
//---- Check Admit Ward-------
$sql_check_admit_ward = "SELECT ipt.an, ward.name as ward_name FROM ipt INNER JOIN ward ON ipt.ward = ward.ward WHERE ipt.vn = '$vn1' ";
$result_check_admit_ward = mysqli_query($conn1, $sql_check_admit_ward);
$num_row_check_admit_ward = $result_check_admit_ward ? mysqli_num_rows($result_check_admit_ward) : 0;
$row_check_admit_ward = $result_check_admit_ward ? mysqli_fetch_array($result_check_admit_ward) : [];
$admit_ward = $row_check_admit_ward['ward_name'];
if ($num_row_check_admit_ward == 0) {
$admit_ward_box = "none";
} else {
$admit_ward_box = "table-row";
}
//----------------------------------
$sql5 = "select agent,symptom from opd_allergy where hn='$hn'"; //แพ้ยา
$result5 = mysqli_query($conn1, $sql5);
$sql6 = "select cc_persist_disease from opd_ill_history where hn='$hn'"; //โรคประจำตัว
$result6 = mysqli_query($conn1, $sql6);
$sql7 = "select cc_operation_name from opd_ill_history where hn='$hn'"; //ประวัติผ่าตัด
$result7 = mysqli_query($conn1, $sql7);
$sql8 = "select * from ovstdiag where (vn='$vn1' or vn='$nn') order by icd10 desc,diagtype"; //diag
$result8 = mysqli_query($conn1, $sql8);
$sql9 = "select count(*) as cc from opitemrece where (vn='$vn1' or an='$nn')";
$result_sd = mysqli_query($conn1, $sql9);
$rs1 = $result_sd ? mysqli_fetch_array($result_sd) : ["cc" => 0];
$num_rows_max2 = 0;
if ($rs1["cc"] > 0) { //a
$sqlMedi = "select o.*,concat(s.name,' ',s.strength,' ',s.units) as drugname ,o.qty as sumqty, d.shortlist , i.displaycolor ,sp.* ,u.name as ustaff , ";
$sqlMedi .= "concat(sp.name1,' ',sp.name2,' ', sp.name3) as spname ";
$sqlMedi .= "from opitemrece o ";
$sqlMedi .= "left outer join s_drugitems s on s.icode=o.icode ";
$sqlMedi .= "left outer join drugusage d on d.drugusage=o.drugusage ";
$sqlMedi .= "left outer join sp_use sp on sp.sp_use = o.sp_use ";
$sqlMedi .= "left outer join opduser u on u.loginname=o.staff ";
$sqlMedi .= "left outer join drugitems i on i.icode=o.icode where (o.vn='$vn1' or o.an='$nn') ";
$sqlMedi .= "and o.sub_type in ('1','3') ";
$sqlMedi .= "order by o.order_no ";
// Force empty sql_mode right before query execution
mysqli_query($conn1, "SET SESSION sql_mode=''");
$result_sd = mysqli_query($conn1, $sqlMedi);
$num_rows_max2 = $result_sd ? mysqli_num_rows($result_sd) : 0;
}
$sql_xray = "select xr.*,xh.xray_list as list,xh.department_name as dep,u.name as xname,xt.name as xtype from xray_report xr left outer join xray_head xh on xh.vn=xr.vn left outer join opduser u on u.loginname=xr.staff left outer join xray_type xt on xt.xray_type=xr.xray_type where (xr.vn='$vn1' or xr.an='$nn') and xr.confirm='Y' ";
$result_xray = mysqli_query($conn1, $sql_xray);
$num_rows_max4 = $result_xray ? mysqli_num_rows($result_xray) : 0;
}
$birthday = $row1['birthday']; //รูปแบบการเก็บค่าข้อมูลวันเกิด
$today = date("Y-m-d"); //จุดต้องเปลี่ยน
list($byear, $bmonth, $bday) = explode("-", $birthday); //จุดต้องเปลี่ยน
list($tyear, $tmonth, $tday) = explode("-", $today); //จุดต้องเปลี่ยน
$mbirthday = mktime(0, 0, 0, $bmonth, $bday, $byear);
$mnow = mktime(0, 0, 0, $tmonth, $tday, $tyear);
$mage = ($mnow - $mbirthday);
$u_y = date("Y", $mage) - 1970;
$u_m = date("m", $mage) - 1;
$u_d = date("d", $mage) - 1;
$sql_vaccine_covid19 = "SELECT * FROM visit_immunization_samui_1 WHERE ref_hn = '$ucid' order by vaccine_plan_no asc"; //วัคซีนโควิด19
$result_vaccine_covid19 = mysqli_query($conn1, $sql_vaccine_covid19);
$num_rows_vaccine_covid19 = $result_vaccine_covid19 ? mysqli_num_rows($result_vaccine_covid19) : 0;
?>
<div class="glass-card mb-8 overflow-hidden relative">
<div class="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-emerald-400 to-emerald-600"></div>
<div class="flex flex-col md:flex-row p-8 gap-8 items-start">
<!-- Profile Image -->
<div class="w-32 h-40 flex-shrink-0 rounded-2xl overflow-hidden shadow-lg border-4 border-white/60 bg-white flex items-center justify-center text-slate-300">
<?php if ($row2['img1'] == "") {
echo '<svg class="w-12 h-12" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>';
} else {
echo '<img src="data:image/jpeg;base64,' . base64_encode($row2['img1']) . '" class="w-full h-full object-cover object-top"/>';
} ?>
</div>
<!-- Info Grid -->
<div class="flex-1 w-full">
<h3 class="text-2xl font-bold text-slate-800 mb-6 flex flex-wrap items-center gap-3">
<?php echo $row1['pname'] . "" . $row1['fname'] . " " . $row1['lname']; ?>
<span class="text-sm font-bold px-3 py-1 bg-emerald-100 text-emerald-700 rounded-full shadow-sm">HN: <?php echo htmlspecialchars($hn); ?></span>
</h3>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-y-6 gap-x-8">
<!-- Info Item -->
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">เลขบัตรประชาชน</span>
<span class="text-slate-700 font-medium text-sm"><?php echo $ucid; ?></span>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">สิทธิ์การรักษา</span>
<span class="text-slate-700 font-medium text-sm"><?php echo $row1['pt']; ?></span>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">วันเกิด</span>
<span class="text-slate-700 font-medium text-sm"><?php echo thai_date7($row1['birthday']); ?></span>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">อายุ</span>
<span class="text-slate-700 font-medium text-sm"><?php echo "$u_y ปี $u_m เดือน $u_d วัน"; ?></span>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">โทรศัพท์</span>
<span class="text-slate-700 font-medium text-sm"><?php echo ($row2['tel'] == "") ? "-" : $row2['tel']; ?></span>
</div>
<div class="flex flex-col gap-1.5">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">สัญชาติ / ศาสนา</span>
<span class="text-slate-700 font-medium text-sm"><?php echo $row2['nation'] . " / " . $row2['religion1']; ?></span>
</div>
<div class="flex flex-col gap-1.5 sm:col-span-2 lg:col-span-3">
<span class="text-xs font-bold text-emerald-500 uppercase tracking-wide">ที่อยู่</span>
<span class="text-slate-700 font-medium text-sm"><?php echo $row2['addr']; ?></span>
</div>
</div>
</div>
</div>
</div>
<div class="glass-card mb-8 overflow-hidden" style="display:<?php if ($vdate != "") { echo "none"; } else { echo "block"; } ?>">
<div class="p-6">
<h4 class="text-xl font-bold text-slate-800 mb-6 flex items-center gap-2">
<svg class="w-6 h-6 text-emerald-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"></path></svg>
ข้อมูลการฉีดวัคซีนโควิด-19
</h4>
<?php if ($num_rows_vaccine_covid19 == 0) { ?>
<div class="p-4 bg-slate-50 text-slate-500 rounded-xl text-sm border border-slate-100 flex items-center gap-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
ไม่มีข้อมูลรายการวัคซีนโควิด-19
</div>
<?php } else { ?>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<?php
$i22 = 0;
while ($i22 < $num_rows_vaccine_covid19) {
$rs_vaccine_covid19 = mysqli_fetch_array($result_vaccine_covid19);
?>
<div class="bg-white border border-emerald-100 rounded-xl p-4 shadow-sm hover:shadow-md transition-shadow relative overflow-hidden">
<div class="absolute top-0 left-0 w-1 h-full bg-emerald-400"></div>
<div class="flex justify-between items-start mb-3">
<span class="text-xs font-bold px-2 py-1 bg-emerald-50 text-emerald-600 rounded-md">เข็มที่ <?php echo $rs_vaccine_covid19["vaccine_plan_no"]; ?></span>
<span class="text-xs text-slate-400"><?php echo thai_date8(datetime_to_date($rs_vaccine_covid19["immunization_datetime"])); ?></span>
</div>
<h5 class="font-bold text-slate-800 mb-2"><?php echo $rs_vaccine_covid19["vaccine_manufacturer"]; ?></h5>
<div class="flex flex-col gap-1 text-sm text-slate-600">
<div class="flex justify-between">
<span class="text-slate-400">Lot Number:</span>
<span class="font-medium"><?php echo $rs_vaccine_covid19["lot_number"]; ?></span>
</div>
<div class="flex justify-between">
<span class="text-slate-400">Serial:</span>
<span class="font-medium"><?php echo $rs_vaccine_covid19["vaccine_serial_no"]; ?></span>
</div>
</div>
</div>
<?php $i22++; } ?>
</div>
<?php } ?>
</div>
</div>
<div class="glass-card mb-8 overflow-hidden" style="display:<?php if ($vdate == "") { echo "none"; } else { echo "block"; } ?>">
<div class="p-6">
<div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
<h4 class="text-xl font-bold text-slate-800 flex items-center gap-2">
<svg class="w-6 h-6 text-emerald-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg>
ข้อมูลการรับบริการ
</h4>
<div class="flex items-center gap-3">
<span class="px-3 py-1.5 bg-emerald-50 text-emerald-700 font-semibold rounded-lg text-sm border border-emerald-100 shadow-sm">
<?php echo thai_date7($row4['vstdate']); ?> <?php echo $row4['vsttime']; ?>
</span>
<span class="px-3 py-1.5 bg-slate-50 text-slate-600 font-medium rounded-lg text-sm border border-slate-200 shadow-sm">
VN: <?php echo htmlspecialchars($vn1); ?>
</span>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<!-- Left Column: CC/PI/PE -->
<div class="flex flex-col gap-4">
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">ข้อมูลการซักประวัติ (CC)</h5>
<p class="text-slate-700 leading-relaxed text-sm"><?php echo (!empty($row_vitals['cc']) ? nl2br(htmlspecialchars($row_vitals['cc'])) : '-'); ?></p>
</div>
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">อาการ / ประวัติ (HPI)</h5>
<p class="text-slate-700 leading-relaxed text-sm"><?php echo (!empty($row4['hpi']) ? nl2br(htmlspecialchars($row4['hpi'])) : '-'); ?></p>
</div>
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">การตรวจร่างกาย (PE)</h5>
<p class="text-slate-700 leading-relaxed text-sm"><?php echo nl2br($row4['pe'] ?? '-'); ?></p>
</div>
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-3">Vital Signs</h5>
<?php if (!empty($row_vitals)): ?>
<div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-3">
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">ความดัน (BP)</span>
<div class="font-bold text-slate-700"><?php echo (int)@$row_vitals["bps"] . "/" . (int)@$row_vitals["bpd"]; ?> <span class="text-[10px] text-slate-400 font-normal">mmHg</span></div>
</div>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">ชีพจร (PR)</span>
<div class="font-bold text-slate-700"><?php echo (int)@$row_vitals["pulse"]; ?> <span class="text-[10px] text-slate-400 font-normal">/min</span></div>
</div>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">อุณหภูมิ (BT)</span>
<div class="font-bold text-slate-700"><?php echo sprintf("%.1f", (float)@$row_vitals["temperature"]); ?> <span class="text-[10px] text-slate-400 font-normal">°C</span></div>
</div>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">หายใจ (RR)</span>
<div class="font-bold text-slate-700"><?php echo (int)@$row_vitals["rr"]; ?> <span class="text-[10px] text-slate-400 font-normal">/min</span></div>
</div>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">น้ำหนัก (BW)</span>
<div class="font-bold text-slate-700"><?php echo sprintf("%.1f", (float)@$row_vitals["bw"]); ?> <span class="text-[10px] text-slate-400 font-normal">Kg</span></div>
</div>
<?php if ((int)@$row_vitals["height"] > 0): ?>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">ส่วนสูง (Ht)</span>
<div class="font-bold text-slate-700"><?php echo (int)@$row_vitals["height"]; ?> <span class="text-[10px] text-slate-400 font-normal">Cm</span></div>
</div>
<?php endif; ?>
<?php if ((int)@$row_vitals["bmi"] > 0): ?>
<div class="bg-slate-50 p-2.5 rounded-xl border border-slate-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-slate-500 font-bold mb-1">BMI</span>
<div class="font-bold text-slate-700"><?php echo (int)@$row_vitals["bmi"]; ?></div>
</div>
<?php endif; ?>
<?php if ((int)@$row_vitals["fbs"] > 0): ?>
<div class="bg-rose-50 p-2.5 rounded-xl border border-rose-100 flex flex-col items-center justify-center text-center hover:shadow-sm transition-all">
<span class="text-[10px] text-rose-500 font-bold mb-1">น้ำตาล (FBS)</span>
<div class="font-bold text-rose-700"><?php echo (int)@$row_vitals["fbs"]; ?> <span class="text-[10px] text-rose-400 font-normal">mg/dl</span></div>
</div>
<?php endif; ?>
</div>
<?php else: ?>
<p class="text-slate-400 text-sm italic">ไม่มีข้อมูล Vital Signs</p>
<?php endif; ?>
</div>
</div>
<!-- Right Column: Medical History & Diagnosis -->
<div class="flex flex-col gap-4">
<div class="bg-white p-5 rounded-xl border border-rose-100 shadow-sm relative overflow-hidden">
<div class="absolute top-0 left-0 w-1 h-full bg-rose-400"></div>
<h5 class="text-xs font-bold text-rose-400 uppercase tracking-wider mb-2">ประวัติการแพ้ยา</h5>
<div class="text-sm">
<?php
$has_allergy = false;
if($result5) while ($row5 = mysqli_fetch_array($result5)) {
$has_allergy = true;
echo "<div class='text-rose-700 font-medium mb-1'>&bull; " . $row5['agent'] . " <span class='text-rose-500 font-normal'>(" . $row5['symptom'] . ")</span></div>";
}
if(!$has_allergy) echo "<span class='text-slate-400'>- ไม่มีประวัติแพ้ยา -</span>";
?>
</div>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">โรคประจำตัว</h5>
<div class="text-sm text-slate-700">
<?php echo $row4['pmh']; ?>
<?php if($result6) while ($row6 = mysqli_fetch_array($result6)) { echo "<div>&bull; " . $row6['cc_persist_disease'] . "</div>"; } ?>
</div>
</div>
<div class="bg-white p-5 rounded-xl border border-emerald-200 shadow-sm">
<h5 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">ประวัติการผ่าตัด</h5>
<div class="text-sm text-slate-700">
<?php
$has_op = false;
if($result7) while ($row7 = mysqli_fetch_array($result7)) {
$has_op = true;
echo "<div>&bull; " . $row7['cc_operation_name'] . "</div>";
}
if(!$has_op) echo "<span class='text-slate-400'>-</span>";
?>
</div>
</div>
</div>
<div class="bg-emerald-50/50 p-5 rounded-xl border border-emerald-100 shadow-sm relative">
<h5 class="text-xs font-bold text-emerald-500 uppercase tracking-wider mb-3 flex justify-between items-center">
วินิจฉัยเบื้องต้น (Diagnosis)
<?php if ($admit_ward_box == "table-row") { ?>
<span class="px-2 py-1 bg-amber-100 text-amber-700 rounded text-[10px]">Admit: <?php echo $admit_ward; ?></span>
<?php } ?>
</h5>
<div class="flex flex-col gap-2">
<?php
$num_rows_max1 = $result8 ? mysqli_num_rows($result8) : 0;
if ($num_rows_max1 == 0) {
echo "<div class='text-sm text-slate-400'>- ไม่มีข้อมูล Dx -</div>";
} else {
$i = 1;
while ($i <= $num_rows_max1) {
$rs2 = $result8 ? mysqli_fetch_array($result8) : [];
$dx_icd = $rs2["icd10"];
$dt_name = $rs2["doctor"];
$dx_name = ""; $dcn_name = "";
if ($dx_icd <> "") {
$result_dx = mysqli_query($conn1, "select name from icd101 where code='$dx_icd'");
if($result_dx && $rs_dx = mysqli_fetch_array($result_dx)) $dx_name = $rs_dx["name"];
}
if ($dt_name <> "") {
$result_dcn = mysqli_query($conn1, "select name from doctor where code='$dt_name'");
if($result_dcn && $rs_dcn = mysqli_fetch_array($result_dcn)) $dcn_name = $rs_dcn["name"];
}
$is_health_checkup = false;
if (preg_match('/^Z(10|00)/i', $dx_icd) || stripos($dx_name, 'Occupational') !== false || stripos($dx_name, 'checkup') !== false) {
$is_health_checkup = true;
}
echo "<div class='bg-white p-3 rounded-lg border border-emerald-100/50 flex flex-col sm:flex-row sm:items-center justify-between gap-2'>";
echo "<div class='flex items-start gap-2'>";
echo "<span class='text-[10px] font-bold text-emerald-400 mt-1 uppercase'>Dx.{$i}</span>";
echo "<div><span class='text-emerald-600 font-bold'>{$dx_icd}</span> <span class='text-emerald-600 text-sm ml-1'>{$dx_name}</span></div>";
echo "</div>";
echo "<div class='flex flex-col sm:flex-row items-end sm:items-center gap-2'>";
if ($is_health_checkup) {
echo "<button type='button' onclick='openCheckupCompareModal()' title='เปรียบเทียบผลตรวจสุขภาพย้อนหลัง' class='inline-flex items-center justify-center p-2 bg-indigo-50 text-indigo-600 hover:bg-indigo-500 hover:text-white rounded-full border border-indigo-200 transition-all duration-300 shadow-sm hover:shadow-md hover:-translate-y-0.5 active:scale-95 group focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-offset-1 shrink-0'><svg class='w-4 h-4 group-hover:scale-110 transition-transform duration-300' fill='none' stroke='currentColor' viewBox='0 0 24 24'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z'></path></svg></button>";
}
echo "<span class='text-[10px] text-slate-400 bg-slate-50 border border-slate-100 px-2 py-1 rounded whitespace-nowrap'>แพทย์: {$dcn_name}</span>";
echo "</div>";
echo "</div>";
$i++;
}
}
?>
</div>
</div>
<?php if ($doctor_cert_covid19_box == "table-row") { ?>
<div class="mt-2 flex items-center justify-end gap-2 text-sm">
<span class="text-emerald-500 font-semibold">พิมพ์ใบรับรองแพทย์ Covid-19:</span>
<select class="border border-slate-200 rounded-lg px-3 py-1.5 text-slate-700 outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500" onchange="window.open(this.value)">
<option selected disabled>--- เลือกแพทย์ ---</option>
<option value="doctor_cert_covid19.php?nn=<?php echo urlencode(encrypt_param($nn)); ?>&doctor=<?php echo urlencode(encrypt_param("1329900133434")); ?>">นพ.มรรษยุว์ อิงคภาสกร</option>
<option value="doctor_cert_covid19.php?nn=<?php echo urlencode(encrypt_param($nn)); ?>&doctor=<?php echo urlencode(encrypt_param("1840100404457")); ?>">นพ.ภควัต พรหมเพชร</option>
<option value="doctor_cert_covid19.php?nn=<?php echo urlencode(encrypt_param($nn)); ?>&doctor=<?php echo urlencode(encrypt_param("1909900457323")); ?>">พญ.จิตติมา มุสิกพันธุ์</option>
</select>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="glass-card mb-8 overflow-hidden">
<div class="p-6">
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6">
<h4 class="text-xl font-bold text-slate-800 m-0 flex items-center gap-2">
<svg class="w-6 h-6 text-emerald-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 8.5a5.5 5.5 0 00-7.778 0L5.5 14.222a5.5 5.5 0 007.778 7.778l5.722-5.722A5.5 5.5 0 0019 8.5z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.3 14.7l5.4-5.4" />
</svg>
รายการยา
</h4>
<div class="relative w-full sm:w-64">
<input type="text" id="medSearchInput" placeholder="ค้นหารายการยา..." class="w-full pl-9 pr-4 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm text-slate-700 focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:bg-white transition-all">
<svg class="w-4 h-4 text-slate-400 absolute left-3 top-2.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
</div>
</div>
<?php if ($num_rows_max2 == 0) { ?>
<div class="p-4 bg-slate-50 text-slate-500 rounded-xl text-sm border border-slate-100 flex items-center gap-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
ไม่มีข้อมูลรายการยา
</div>
<?php } else { ?>
<div class="flex flex-col gap-3 max-h-[400px] overflow-y-auto pr-2 custom-scrollbar" id="medContainer">
<?php
$i2 = 0;
while ($i2 < $num_rows_max2) {
$rs_sd = $result_sd ? mysqli_fetch_array($result_sd) : [];
?>
<div class="med-card-item shrink-0 bg-white border border-emerald-100 rounded-xl p-4 shadow-sm hover:shadow-md transition-all flex flex-col md:flex-row justify-between items-start md:items-center gap-4 relative overflow-hidden" data-med-name="<?php echo htmlspecialchars($rs_sd["drugname"]); ?>">
<div class="absolute top-0 left-0 w-1 h-full bg-emerald-400"></div>
<div class="flex-1">
<div class="flex items-center gap-2 mb-1">
<span class="text-[10px] font-bold px-2 py-0.5 bg-emerald-50 text-emerald-600 rounded-full">#<?php echo ($i2 + 1); ?></span>
<h5 class="font-bold text-slate-800 text-base"><?php echo $rs_sd["drugname"]; ?></h5>
</div>
<p class="text-sm text-slate-500 mt-1 pl-9">
<?php echo ($rs_sd["spname"] <> "") ? $rs_sd["spname"] : $rs_sd["shortlist"]; ?>
</p>
</div>
<div class="flex items-center gap-3 w-full md:w-auto pl-9 md:pl-0 shrink-0">
<div class="flex flex-col items-center md:items-end bg-slate-50 px-3 py-1.5 rounded-lg border border-slate-100 min-w-[70px]">
<span class="text-[10px] text-slate-400 font-medium uppercase">จำนวน</span>
<span class="font-semibold text-slate-700 text-[15px]"><?php echo $rs_sd["qty"]; ?></span>
</div>
<div class="flex flex-col items-center md:items-end bg-emerald-50 px-3 py-1.5 rounded-lg border border-emerald-100 min-w-[90px]">
<span class="text-[10px] text-emerald-500 font-medium uppercase">รวมราคา</span>
<span class="font-semibold text-emerald-700 text-[15px]">฿<?php echo number_format($rs_sd["sum_price"]); ?></span>
</div>
</div>
</div>
<?php $i2++; } ?>
</div>
<?php } ?>
</div>
</div>
<div class="glass-card mb-8 overflow-hidden">
<div class="p-6">
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 mb-6">
<h4 class="text-xl font-bold text-slate-800 m-0 flex items-center gap-2">
<svg class="w-6 h-6 text-purple-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19.428 15.428a2 2 0 00-1.022-.547l-2.387-.477a6 6 0 00-3.86.517l-.318.158a6 6 0 01-3.86.517L6.05 15.21a2 2 0 00-1.806.547M8 4h8l-1 1v5.172a2 2 0 00.586 1.414l5 5c1.26 1.26.367 3.414-1.415 3.414H4.828c-1.782 0-2.674-2.154-1.414-3.414l5-5A2 2 0 009 10.172V5L8 4z"></path></svg>
รายงานผลแล็บ
</h4>
<div class="flex items-center gap-2 w-full sm:w-auto">
<select id="labFilterSelect" class="pl-3 pr-8 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm text-slate-700 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:bg-white transition-all appearance-none cursor-pointer">
<option value="all">สถานะทั้งหมด</option>
<option value="abnormal">เฉพาะผิดปกติ (Abnormal)</option>
<option value="normal">ปกติ (Normal)</option>
</select>
<div class="relative w-full sm:w-64">
<input type="text" id="labSearchInput" placeholder="ค้นหารายการแล็บ..." class="w-full pl-9 pr-4 py-2 bg-slate-50 border border-slate-200 rounded-lg text-sm text-slate-700 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:bg-white transition-all">
<svg class="w-4 h-4 text-slate-400 absolute left-3 top-2.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
</div>
<button type="button" onclick="printLabReport()" class="inline-flex items-center justify-center gap-2 px-3 py-2 bg-purple-600 text-white hover:bg-purple-700 rounded-lg text-sm font-semibold transition-colors shadow-sm shrink-0" title="พิมพ์รายงานผลแล็บ">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"></path></svg>
พิมพ์
</button>
</div>
</div>
<?php
// isAbnormalLab function moved to core/utils.php
$sqlLab1 = "select * from lab_head where (vn='$vn1' or vn='$nn') order by report_date desc ";
$result_lab1 = mysqli_query($conn1, $sqlLab1);
$num_rows_lab1 = $result_lab1 ? mysqli_num_rows($result_lab1) : 0;
if ($num_rows_lab1 == 0) {
echo '<div class="p-4 bg-slate-50 text-slate-500 rounded-xl text-sm border border-slate-100 flex items-center gap-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
ไม่มีข้อมูลการส่งตรวจทางห้องปฏิบัติการ
</div>';
} else {
$i4 = 0; $lab_order_list = "";
while ($i4 < $num_rows_lab1) {
$rs_lab1 = $result_lab1 ? mysqli_fetch_array($result_lab1) : [];
$lab_order_list = ($lab_order_list == "") ? "'" . $rs_lab1["lab_order_number"] . "'" : $lab_order_list . ",'" . $rs_lab1["lab_order_number"] . "'";
$i4++;
}
if ($lab_order_list == "") $lab_order_list = "0";
// Added GROUP BY to prevent duplicates, ORDER BY i.lab_items_name ASC for alphabetical sorting
$sqlLab2 = "select l.*,i.lab_items_name,i.lab_items_unit,i.lab_items_code,i.lab_items_normal_value,e.staff,l.lab_order_number,j.lab_items_sub_group_name,h.order_date from lab_order l left outer join lab_entry_log e on e.lab_order_number=l.lab_order_number left outer join lab_items i on i.lab_items_code=l.lab_items_code left outer join lab_order_image k on k.lab_order_number=l.lab_order_number left outer join lab_items_sub_group j on j.lab_items_sub_group_code = l.lab_items_sub_group_code LEFT OUTER JOIN lab_head h ON h.lab_order_number = l.lab_order_number where l.lab_order_number in ($lab_order_list) and l.lab_order_result <>'' and i.lab_items_code not in ('332','657','1590','1592','1594','1596','1602','1604','1606','1608') GROUP BY l.lab_order_number, i.lab_items_code order by i.lab_items_name ASC";
mysqli_query($conn1, "SET SESSION sql_mode=''");
$result_lab2 = mysqli_query($conn1, $sqlLab2);
$num_rows_lab2 = $result_lab2 ? mysqli_num_rows($result_lab2) : 0;
if ($num_rows_lab2 == 0) {
echo '<div class="p-4 bg-slate-50 text-slate-500 rounded-xl text-sm border border-slate-100 flex items-center gap-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
ไม่มีรายการผลแล็บ
</div>';
} else {
echo '<div class="flex flex-col gap-3 max-h-[400px] overflow-y-auto pr-2 custom-scrollbar" id="labCardContainer">';
$i3 = 0;
while ($i3 < $num_rows_lab2) {
$rs_lab2 = $result_lab2 ? mysqli_fetch_array($result_lab2) : [];
$ln = urlencode(encrypt_param($rs_lab2["lab_order_number"]));
$result_val = $rs_lab2["lab_order_result"];
$is_image = in_array($result_val, ["ดูผลใน Image หน้าที่ 1", "ดูผลใน Image หน้าที่ 2", "ดูผลใน Image", " ดูผลใน Image", "Undetectable", "Detectable"]);
// Determine Abnormality
$is_abnormal = isAbnormalLab($result_val, $rs_lab2["lab_items_normal_value"]);
// Dynamic styling for abnormal
$borderClass = $is_abnormal ? 'border-rose-200' : 'border-purple-100';
$indicatorClass = $is_abnormal ? 'bg-rose-500' : 'bg-purple-400';
$textClass = $is_abnormal ? 'text-rose-600' : ($is_image ? 'text-emerald-600' : 'text-slate-800');
$bgClass = $is_abnormal ? 'bg-rose-50/50' : 'bg-white';
?>
<div class="lab-card-item shrink-0 <?php echo $bgClass; ?> border <?php echo $borderClass; ?> rounded-xl p-4 shadow-sm hover:shadow-md transition-all flex flex-col md:flex-row justify-between items-start md:items-center gap-4 relative overflow-hidden" data-lab-name="<?php echo htmlspecialchars($rs_lab2["lab_items_name"]); ?>" data-lab-status="<?php echo $is_abnormal ? 'abnormal' : 'normal'; ?>">
<div class="absolute top-0 left-0 w-1 h-full <?php echo $indicatorClass; ?>"></div>
<div class="flex-1 pl-2">
<div class="flex items-center gap-2 mb-1">
<h5 class="font-semibold text-slate-800 text-[15px] leading-snug"><?php echo $rs_lab2["lab_items_name"]; ?></h5>
<?php if($is_abnormal) { ?>
<span class="bg-rose-100 text-rose-700 text-[9px] font-bold px-2 py-0.5 rounded uppercase tracking-wider border border-rose-200 shadow-sm shrink-0">Abnormal</span>
<?php } ?>
</div>
<p class="text-[13px] text-slate-500 mt-1 flex items-center flex-wrap gap-2">
<?php if (!empty($rs_lab2["lab_items_sub_group_name"])) { ?>
<span class="text-[10px] font-medium px-2 py-0.5 <?php echo $is_abnormal ? 'bg-rose-100 text-rose-700 border-rose-200' : 'bg-purple-50 text-purple-600 border-purple-100'; ?> rounded-md shadow-sm border shrink-0">
<?php echo $rs_lab2["lab_items_sub_group_name"]; ?>
</span>
<?php } ?>
<span class="text-[11px] text-slate-400"><?php echo thai_date2($rs_lab2["order_date"]); ?></span>
</p>
</div>
<div class="flex items-center justify-between w-full md:w-auto gap-4 mt-3 md:mt-0 shrink-0">
<div class="flex flex-col items-start md:items-end min-w-[80px]">
<span class="text-[10px] <?php echo $is_abnormal ? 'text-rose-400' : 'text-slate-400'; ?> mb-1">Normal: <?php echo $rs_lab2["lab_items_normal_value"] ?: '-'; ?></span>
</div>
<div class="flex items-center gap-2">
<?php if ($rs_lab2["confirm"] == "Y") { ?>
<div class="flex flex-col items-center md:items-end <?php echo $is_abnormal ? 'bg-rose-100 border-rose-200' : 'bg-slate-50 border-slate-100'; ?> px-3 py-1.5 rounded-lg border min-w-[100px] shadow-sm">
<span class="text-[10px] <?php echo $is_abnormal ? 'text-rose-500' : 'text-slate-400'; ?> font-medium uppercase">Result</span>
<span class="lab-result-text font-semibold text-[15px] <?php echo $textClass; ?>">
<?php echo $result_val; ?>
<span class="text-[11px] font-normal <?php echo $is_abnormal ? 'text-rose-500' : 'text-slate-500'; ?> ml-0.5"><?php echo $rs_lab2["lab_items_unit"]; ?></span>
</span>
</div>
<?php if ($is_image) { ?>
<a href="lab_image.php?ln=<?php echo $ln; ?>" target="_blank" class="p-2 bg-emerald-50 text-emerald-600 rounded-lg border border-emerald-100 hover:bg-emerald-100 hover:shadow-md transition-all group" title="ดูผลใน Image">
<svg class="w-5 h-5 group-hover:scale-110 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
</a>
<?php } ?>
<?php } else { ?>
<div class="flex flex-col items-center justify-center bg-amber-50 px-4 py-2 rounded-lg border border-amber-100 min-w-[120px]">
<span class="text-sm font-medium text-amber-500">ยังไม่รายงาน</span>
</div>
<?php } ?>
</div>
</div>
</div>
<?php
$i3++;
}
echo '</div>';
}
}
?>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Medication Filter
const medSearchInput = document.getElementById('medSearchInput');
const medItems = document.querySelectorAll('.med-card-item');
if (medSearchInput) {
medSearchInput.addEventListener('input', function(e) {
const searchTerm = e.target.value.toLowerCase();
medItems.forEach(item => {
const medName = item.getAttribute('data-med-name').toLowerCase();
if (medName.includes(searchTerm)) {
item.style.display = 'flex';
} else {
item.style.display = 'none';
}
});
});
}
// Lab Results Filter
const labSearchInput = document.getElementById('labSearchInput');
const labFilterSelect = document.getElementById('labFilterSelect');
const labItems = document.querySelectorAll('.lab-card-item');
function filterLabs() {
const searchTerm = labSearchInput ? labSearchInput.value.toLowerCase() : '';
const statusFilter = labFilterSelect ? labFilterSelect.value : 'all';
labItems.forEach(item => {
const labName = item.getAttribute('data-lab-name').toLowerCase();
const labStatus = item.getAttribute('data-lab-status');
const matchesSearch = labName.includes(searchTerm);
const matchesStatus = (statusFilter === 'all') || (statusFilter === labStatus);
if (matchesSearch && matchesStatus) {
item.style.display = 'flex';
} else {
item.style.display = 'none';
}
});
}
if (labSearchInput) labSearchInput.addEventListener('input', filterLabs);
if (labFilterSelect) labFilterSelect.addEventListener('change', filterLabs);
});
</script>
<div class="glass-card mb-8 overflow-hidden">
<div class="p-6">
<h4 class="text-xl font-bold text-slate-800 mb-6 flex items-center gap-2">
<svg class="w-6 h-6 text-amber-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
รายการ X-Ray
</h4>
<?php if ($num_rows_max4 == 0) { ?>
<div class="p-4 bg-slate-50 text-slate-500 rounded-xl text-sm border border-slate-100 flex items-center gap-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
ไม่มีข้อมูลรายการ X-Ray
</div>
<?php } else { ?>
<div class="flex flex-col gap-3">
<?php
$i4 = 0;
while ($i4 < $num_rows_max4) {
$rs_xray = $result_xray ? mysqli_fetch_array($result_xray) : [];
?>
<div class="bg-white border border-amber-100 rounded-xl p-4 shadow-sm hover:shadow-md transition-all flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 relative overflow-hidden">
<div class="absolute top-0 left-0 w-1 h-full bg-amber-400"></div>
<div class="flex-1">
<div class="flex items-center gap-2 mb-1">
<span class="text-[10px] font-bold px-2 py-0.5 bg-amber-50 text-amber-600 rounded-full">#<?php echo ($i4 + 1); ?></span>
<h5 class="font-bold text-slate-800 text-base"><?php echo $rs_xray["list"]; ?></h5>
</div>
<div class="flex flex-wrap gap-x-4 gap-y-1 text-sm text-slate-500 mt-1 pl-9">
<span><strong class="text-slate-400 font-medium">ท่า:</strong> <?php echo $rs_xray["xtype"]; ?></span>
<span><strong class="text-slate-400 font-medium">ส่งโดย:</strong> <?php echo $rs_xray["dep"]; ?></span>
<span>
<strong class="text-slate-400 font-medium">ผล:</strong>
<?php if ($rs_xray["report_date"] == "") { echo "-"; } else { echo "<a href='xray_result.php?xn=" . urlencode(encrypt_param($rs_xray["xn"])) . "' target='_blank' class='text-emerald-500 hover:underline'>" . thai_date2($rs_xray["report_date"]) . "</a>"; } ?>
</span>
</div>
</div>
<div class="w-full sm:w-auto pl-9 sm:pl-0">
<a href="http://49.231.191.174:9000/pkg_pacs/external_interface.aspx?TYPE=V&LID=ksh&LPW=1&PID=<?php echo $hn; ?>&AN=10742<?php echo $rs_xray["xn"]; ?>" target="_blank" class="flex items-center justify-center gap-2 w-full px-4 py-2 bg-slate-800 text-white text-sm font-medium rounded-lg hover:bg-slate-700 transition-colors shadow-sm focus:ring-2 focus:ring-slate-500 focus:ring-offset-1">
<svg class="w-4 h-4 text-amber-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
ดูฟิล์ม X-Ray
</a>
</div>
</div>
<?php $i4++; } ?>
</div>
<?php } ?>
</div>
</div>
<!-- Modal เปรียบเทียบผลตรวจสุขภาพ -->
<div id="checkupCompareModal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-slate-900/40 backdrop-blur-sm" onclick="closeCheckupCompareModal()"></div>
<div class="absolute inset-4 md:inset-10 lg:inset-x-20 lg:inset-y-10 bg-white rounded-2xl shadow-2xl flex flex-col overflow-hidden animate-enter border border-slate-200">
<!-- Header -->
<div class="px-6 py-4 border-b border-slate-100 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4 bg-indigo-50/50">
<h3 class="font-bold text-xl text-indigo-800 flex items-center gap-2">
<svg class="w-6 h-6 text-indigo-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path></svg>
เปรียบเทียบผลตรวจสุขภาพย้อนหลัง
</h3>
<div class="flex items-center gap-4">
<label class="flex items-center gap-2 text-sm font-medium text-slate-600 cursor-pointer hover:text-indigo-600 transition-colors">
<input type="checkbox" id="showAllCheckups" class="w-4 h-4 text-indigo-600 border-slate-300 rounded focus:ring-indigo-500 cursor-pointer" onchange="openCheckupCompareModal()">
แสดงประวัติทั้งหมด (ไม่จำกัด 2 ปี)
</label>
<button onclick="closeCheckupCompareModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-slate-200 text-slate-500 transition-colors">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
</button>
</div>
</div>
<!-- Body -->
<div class="flex-1 min-h-0 flex flex-col p-6 bg-slate-50 relative">
<!-- Loader -->
<div id="checkupCompareLoader" class="absolute inset-0 bg-slate-50/80 backdrop-blur-sm z-10 flex flex-col items-center justify-center hidden">
<svg class="w-10 h-10 text-indigo-500 animate-spin mb-4" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>
<span class="text-indigo-600 font-medium">กำลังดึงข้อมูลประวัติการตรวจสุขภาพ...</span>
</div>
<div id="checkupCompareContent" class="hidden flex-1 min-h-0 flex flex-col">
<div class="flex justify-end mb-3 gap-2 shrink-0">
<button type="button" onclick="document.getElementById('checkupTableWrapper').scrollTo({left: 0, behavior: 'smooth'})" class="inline-flex items-center gap-1 px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs font-bold text-slate-600 hover:bg-slate-50 hover:text-indigo-600 transition-colors shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-100">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 19l-7-7 7-7m8 14l-7-7 7-7"></path></svg>
ล่าสุด (ซ้ายสุด)
</button>
<button type="button" onclick="document.getElementById('checkupTableWrapper').scrollTo({left: document.getElementById('checkupTableWrapper').scrollWidth, behavior: 'smooth'})" class="inline-flex items-center gap-1 px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs font-bold text-slate-600 hover:bg-slate-50 hover:text-indigo-600 transition-colors shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-100">
เก่าสุด (ขวาสุด)
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 5l7 7-7 7M5 5l7 7-7 7"></path></svg>
</button>
</div>
<div id="checkupTableWrapper" class="flex-1 overflow-auto rounded-xl border border-slate-200 shadow-sm bg-white relative custom-scrollbar">
<table class="w-full text-left border-collapse min-w-max" id="checkupCompareTable">
<!-- Table content will be injected by JS -->
</table>
</div>
</div>
</div>
</div>
</div>
<script>
function printLabReport() {
const labItems = Array.from(document.querySelectorAll('.lab-card-item:not([style*="display: none"])'));
if (labItems.length === 0) {
alert('ไม่มีข้อมูลรายงานผลแล็บให้พิมพ์ หรือคุณซ่อนรายการแล็บไว้ทั้งหมด');
return;
}
let orderDate = '';
const firstDateElem = labItems[0].querySelector('.text-\\[11px\\].text-slate-400');
if (firstDateElem) {
orderDate = firstDateElem.innerText;
}
const patientName = <?php echo json_encode($row1['pname'] . "" . $row1['fname'] . " " . $row1['lname']); ?>;
const patientHn = <?php echo json_encode($hn); ?>;
const patientAge = <?php echo json_encode($row1['birthday'] ? (date('Y') - date('Y', strtotime($row1['birthday']))) : '-'); ?>;
const thMonths = ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'];
const d = new Date();
const printDate = d.getDate() + ' ' + thMonths[d.getMonth()] + ' ' + (d.getFullYear() + 543) + ' เวลา ' + String(d.getHours()).padStart(2,'0') + ':' + String(d.getMinutes()).padStart(2,'0') + ' น.';
const ITEMS_PER_PAGE = 18;
const totalPages = Math.ceil(labItems.length / ITEMS_PER_PAGE);
let pagesHtml = '';
for (let pageNum = 1; pageNum <= totalPages; pageNum++) {
const startIdx = (pageNum - 1) * ITEMS_PER_PAGE;
const endIdx = startIdx + ITEMS_PER_PAGE;
const pageItems = labItems.slice(startIdx, endIdx);
let rowsHtml = '';
pageItems.forEach((item, relativeIdx) => {
const index = startIdx + relativeIdx;
const name = item.querySelector('h5') ? item.querySelector('h5').innerText : '';
const normalValRaw = item.querySelector('.text-\\[10px\\].mb-1') ? item.querySelector('.text-\\[10px\\].mb-1').innerText : '';
const normalVal = normalValRaw.replace('Normal: ', '').trim();
const resultElem = item.querySelector('.lab-result-text');
let resultText = '';
if (resultElem) {
resultText = resultElem.innerText.replace(/\n/g, ' ').trim();
}
const isAbnormal = item.dataset.labStatus === 'abnormal';
const resultColor = isAbnormal ? '#e11d48' : '#1e293b';
let abnormalFlag = isAbnormal ? '<span style="color: #e11d48; margin-left: 4px; font-weight: bold;">*</span>' : '';
rowsHtml += `
<tr class="lab-row">
<td style="padding: 8px 12px; border-bottom: 1px dashed #e2e8f0; font-size: 13px; text-align: center; color: #64748b;">
${index + 1}
</td>
<td style="padding: 8px 12px; border-bottom: 1px dashed #e2e8f0; font-size: 13px;">
<div style="font-weight: 600; color: #1e293b;">${name}</div>
</td>
<td style="padding: 8px 12px; border-bottom: 1px dashed #e2e8f0; font-size: 13px; text-align: right; color: ${resultColor}; font-weight: ${isAbnormal ? 'bold' : 'normal'};">
${resultText} ${abnormalFlag}
</td>
<td style="padding: 8px 12px; border-bottom: 1px dashed #e2e8f0; font-size: 13px; text-align: right; color: #64748b;">
${normalVal}
</td>
</tr>
`;
});
let footerHtml = '';
if (pageNum === totalPages) {
footerHtml = `
<div class="signature-box">
<div class="signature-line">
<div class="line"></div>
ผู้รายงานผล (Reporter)
</div>
</div>
`;
}
pagesHtml += `
<div class="page-container">
<div class="hospital-header">
<h1>โรงพยาบาลเกาะสมุย</h1>
<p>รายงานผลการตรวจวิเคราะห์ทางห้องปฏิบัติการ</p>
</div>
<div class="patient-box">
<div class="p-info-item"><span class="p-info-label">ชื่อ-สกุล:</span> <span class="p-info-val">${patientName}</span></div>
<div class="p-info-item"><span class="p-info-label">พิมพ์วันที่:</span> <span class="p-info-val" style="font-weight:500;">${printDate}</span></div>
<div class="p-info-item"><span class="p-info-label">HN:</span> <span class="p-info-val">${patientHn}</span></div>
<div class="p-info-item"><span class="p-info-label">วันที่สั่งตรวจ:</span> <span class="p-info-val">${orderDate}</span></div>
<div class="p-info-item"><span class="p-info-label">อายุ:</span> <span class="p-info-val">${patientAge} ปี</span></div>
<div class="p-info-item"><span class="p-info-label">หน้า:</span> <span class="p-info-val" style="font-weight:700; color:#0f172a;">${pageNum} / ${totalPages}</span></div>
</div>
<table>
<thead>
<tr>
<th style="width: 50px; text-align: center; border-radius: 6px 0 0 0;">ลำดับ</th>
<th class="left">รายการตรวจ (Test)</th>
<th class="right">ผลการตรวจ (Result)</th>
<th class="right" style="border-radius: 0 6px 0 0;">ค่าปกติ (Normal)</th>
</tr>
</thead>
<tbody>
${rowsHtml}
</tbody>
</table>
${footerHtml}
<div class="footer-note">
เอกสารนี้พิมพ์จากระบบสารสนเทศ HOSxP Data • การแปลผลควรอยู่ภายใต้ดุลยพินิจของแพทย์
</div>
</div>
`;
}
const htmlContent = `
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>รายงานผลแล็บ - ${patientName}</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Sarabun', sans-serif;
color: #0f172a;
margin: 0;
padding: 0;
background: #fff;
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
}
@page {
size: A4;
margin: 15mm;
}
.page-container {
page-break-after: always;
position: relative;
}
.page-container:last-child {
page-break-after: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
.hospital-header {
text-align: center;
margin-bottom: 20px;
}
.hospital-header h1 {
font-size: 24px;
font-weight: 700;
margin: 0 0 5px 0;
color: #0f172a;
letter-spacing: 0.5px;
}
.hospital-header p {
font-size: 14px;
color: #475569;
margin: 0;
}
.patient-box {
border: 2px solid #cbd5e1;
border-radius: 8px;
padding: 15px 15px 5px 15px;
margin-bottom: 20px;
background: #f8fafc;
}
.p-info-item {
font-size: 14px;
display: inline-block;
width: 49%;
margin-bottom: 10px;
vertical-align: top;
}
.p-info-label {
color: #64748b;
font-weight: 600;
width: 85px;
display: inline-block;
}
.p-info-val {
color: #0f172a;
font-weight: 700;
}
th {
background: #e2e8f0;
color: #1e293b;
padding: 10px 12px;
font-size: 13px;
font-weight: 700;
border-bottom: 1px solid #cbd5e1;
border-top: 1px solid #cbd5e1;
}
th.left { text-align: left; }
th.right { text-align: right; }
/* Zebra striping for rows */
.lab-row:nth-child(even) { background-color: #f8fafc; }
.signature-box {
margin-top: 50px;
text-align: right;
}
.signature-line {
width: 250px;
text-align: center;
font-size: 13px;
color: #475569;
display: inline-block;
}
.signature-line .line {
border-bottom: 1px dotted #94a3b8;
height: 30px;
margin-bottom: 10px;
}
.footer-note {
text-align: center;
font-size: 11px;
color: #94a3b8;
margin-top: 30px;
padding-top: 10px;
border-top: 1px solid #e2e8f0;
}
</style>
</head>
<body>
${pagesHtml}
</body>
</html>
`;
const printWin = window.open('', '_blank');
printWin.document.open();
printWin.document.write(htmlContent);
printWin.document.close();
// Give fonts and layout a brief moment to render before calling print
setTimeout(() => {
printWin.focus();
printWin.print();
}, 800);
}
</script>