Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,266 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'config.php';
checkAdmin();
$topbar_text = getSystemSetting('topbar_text', 'ระบบตรวจสอบข้อมูล HOSxP');
$footer_text = getSystemSetting('footer_text', 'หน่วยงานสุขภาพดิจิทัล โรงพยาบาลเกาะสมุย');
$log_db = getDB(DB_LOG_HOST, DB_LOG_NAME, DB_LOG_USER, DB_LOG_PASS);
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>จัดการชุดข้อมูล (Export Profiles) - <?php echo htmlspecialchars($topbar_text); ?></title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="icon" href="assets/img/logo.png" type="image/png">
<style>
body { font-family: 'Sarabun', sans-serif; }
/* Custom DataTables Styling */
.dataTables_wrapper { padding: 1rem 0; }
.dataTables_wrapper .dataTables_filter input {
border: 1px solid #d1d5db; border-radius: 0.5rem; padding: 0.35rem 0.75rem; margin-left: 0.5rem; outline: none; transition: all 0.2s;
}
.dataTables_wrapper .dataTables_filter input:focus {
border-color: #10b981; box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.2);
}
.dataTables_wrapper .dataTables_length select {
border: 1px solid #d1d5db; border-radius: 0.5rem; padding: 0.25rem 1.5rem 0.25rem 0.75rem; margin: 0 0.5rem; outline: none;
}
.dataTables_wrapper .dataTables_filter,
.dataTables_wrapper .dataTables_length {
margin-bottom: 1rem;
}
table.dataTable.no-footer { border-bottom: 1px solid #e5e7eb; margin-top: 1rem !important; margin-bottom: 1rem !important; }
table.dataTable thead th {
border-bottom: 2px solid #e5e7eb; background-color: #f3f4f6; color: #374151; font-weight: 600; padding: 0.75rem 1rem; white-space: nowrap; text-align: center;
}
table.dataTable tbody td {
border-bottom: 1px solid #f3f4f6; padding: 1rem; color: #4b5563; vertical-align: top;
}
table.dataTable tbody tr:hover { background-color: #f9fafb; }
.dataTables_wrapper .dataTables_paginate .paginate_button {
padding: 0.35rem 0.85rem; margin-left: 0.25rem; border: 1px solid #d1d5db; border-radius: 0.5rem; background: white; color: #374151 !important; transition: all 0.2s; font-size: 0.875rem;
}
.dataTables_wrapper .dataTables_paginate .paginate_button.current,
.dataTables_wrapper .dataTables_paginate .paginate_button.current:hover {
background: #10b981 !important; color: white !important; border-color: #10b981; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.dataTables_wrapper .dataTables_paginate .paginate_button:not(.current):hover {
background: #f3f4f6 !important; border-color: #d1d5db; color: #111827 !important;
}
.dataTables_wrapper .dataTables_info { color: #6b7280; font-size: 0.875rem; padding-top: 0.85rem; }
</style>
</head>
<body class="bg-gray-100 flex flex-col min-h-screen">
<?php include 'topbar.php'; ?>
<div class="max-w-7xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
<div class="mb-4">
<a href="settings.php" class="text-blue-600 hover:text-blue-800 text-sm font-medium flex items-center">
<svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
กลับไปหน้าตั้งค่าระบบ
</a>
</div>
<div class="bg-white shadow rounded-lg mb-8">
<div class="px-6 py-5 border-b border-gray-200 flex justify-between items-center">
<div>
<h3 class="text-lg font-medium text-gray-900">จัดการชุดข้อมูล (Export Profiles)</h3>
<p class="mt-1 text-sm text-gray-500">ตั้งค่าคอลัมน์ Excel และคำสั่ง SQL ที่ใช้ในการค้นหา</p>
</div>
<button onclick="openModal()" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded text-sm flex items-center">
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
เพิ่มชุดข้อมูลใหม่
</button>
</div>
<div class="px-6 py-5 overflow-x-auto">
<table id="profilesTable" class="display w-full text-sm">
<thead>
<tr>
<th class="w-10 text-center">ลำดับ</th>
<th>ชื่อชุดข้อมูล</th>
<th>คอลัมน์ Excel</th>
<th>ผู้สร้าง</th>
<th>วันที่สร้าง</th>
<th class="w-24">จัดการ</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<!-- Modal Form -->
<div id="profileModal" class="fixed inset-0 z-50 hidden overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">&#8203;</span>
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-3xl w-full">
<form id="profileForm">
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<h3 class="text-lg leading-6 font-medium text-gray-900 mb-4" id="modal-title">เพิ่มชุดข้อมูล</h3>
<input type="hidden" id="profile_id" name="id">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="profile_name">ชื่อชุดข้อมูล</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="profile_name" name="profile_name" type="text" required placeholder="เช่น ค้นหาด้วย PID">
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="excel_columns">คอลัมน์ Excel (คั่นด้วยคอมม่า)</label>
<input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="excel_columns" name="excel_columns" type="text" required placeholder="เช่น pid, hn">
<p class="text-xs text-gray-500 mt-1">ตั้งชื่อให้ตรงกับหัวคอลัมน์ในแถวแรกของไฟล์ Excel (พิมพ์เล็ก/ใหญ่มีผลเมื่อเรียกใช้ใน SQL)</p>
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="sql_query">คำสั่ง SQL</label>
<textarea class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline font-mono text-xs" id="sql_query" name="sql_query" rows="6" required placeholder="SELECT * FROM person WHERE cid = :pid"></textarea>
<div class="text-xs text-gray-500 mt-2 space-y-1">
<p><b>ตัวอย่าง 1 คอลัมน์:</b> <code>SELECT * FROM person p WHERE p.cid = :pid</code></p>
<p><b>ตัวอย่าง 2 คอลัมน์:</b> <code>SELECT * FROM person p WHERE p.cid = :pid AND p.patient_hn = :hn</code></p>
<p class="text-red-500 font-semibold">* ใช้ <code>:ชื่อคอลัมน์</code> ในคำสั่ง SQL ระบบจะดึงข้อมูลจากไฟล์ Excel แต่ละบรรทัดมาแทนที่อัตโนมัติ</p>
<p class="text-red-500 font-semibold">* ชื่อคอลัมน์ผลลัพธ์ (Header) จะถูกสร้างตามชื่อที่ SELECT ออกมา สามารถใช้ <code>AS ชื่อคอลัมน์</code> เพื่อกำหนดชื่อให้สวยงามได้</p>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<button type="submit" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none sm:ml-3 sm:w-auto sm:text-sm">
บันทึกข้อมูล
</button>
<button type="button" onclick="closeModal()" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm">
ยกเลิก
</button>
</div>
</form>
</div>
</div>
</div>
<?php include 'footer.php'; ?>
<!-- jQuery, DataTables, and SweetAlert2 -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
let table;
$(document).ready(function() {
table = $('#profilesTable').DataTable({
"language": {
"url": "//cdn.datatables.net/plug-ins/1.13.6/i18n/th.json"
},
"ajax": "api_profiles.php?action=list",
"columns": [
{ "data": null, "orderable": false, "searchable": false, "className": "text-center align-middle", "render": function(data, type, row, meta) {
return meta.row + 1;
}},
{ "data": "profile_name", "className": "align-middle", "render": function(data) {
return '<div class="font-semibold text-gray-800 whitespace-nowrap">' + data + '</div>';
}},
{ "data": "excel_columns", "className": "text-center align-middle", "render": function(data) {
let cols = data.split(',').map(c => c.trim());
return '<div class="flex flex-wrap justify-center gap-1">' + cols.map(c => '<span class="bg-gray-100 border border-gray-200 text-gray-600 px-2 py-0.5 rounded text-xs">' + c + '</span>').join('') + '</div>';
}},
{ "data": "creator_name", "className": "text-center align-middle", "render": function(data) {
return '<div class="whitespace-nowrap text-gray-600 text-sm">' + (data ? data : '<span class="text-gray-400">-</span>') + '</div>';
}},
{ "data": "created_at", "className": "text-center align-middle", "render": function(data) {
return '<div class="whitespace-nowrap text-gray-500 text-sm">' + data + '</div>';
}},
{ "data": null, "orderable": false, "className": "text-center align-middle", "render": function(data, type, row) {
return `
<div class="flex items-center justify-center space-x-2">
<button onclick="editProfile(${row.id})" class="text-blue-500 hover:text-blue-700 hover:bg-blue-50 p-1.5 rounded-md transition-colors" 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="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
</button>
<button onclick="deleteProfile(${row.id})" class="text-red-500 hover:text-red-700 hover:bg-red-50 p-1.5 rounded-md transition-colors" 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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
</button>
</div>
`;
}}
],
"order": [] // Disable default sort to keep SQL order (descending ID)
});
$('#profileForm').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'api_profiles.php?action=save',
type: 'POST',
data: $(this).serialize(),
success: function(res) {
if (res.status === 'success') {
Swal.fire('สำเร็จ', res.message, 'success');
closeModal();
table.ajax.reload();
} else {
Swal.fire('ข้อผิดพลาด', res.message, 'error');
}
}
});
});
});
function openModal() {
$('#profileForm')[0].reset();
$('#profile_id').val('');
$('#modal-title').text('เพิ่มชุดข้อมูล');
$('#profileModal').removeClass('hidden');
}
function closeModal() {
$('#profileModal').addClass('hidden');
}
function editProfile(id) {
$.get('api_profiles.php?action=get&id=' + id, function(res) {
if (res.status === 'success') {
$('#profile_id').val(res.data.id);
$('#profile_name').val(res.data.profile_name);
$('#excel_columns').val(res.data.excel_columns);
$('#sql_query').val(res.data.sql_query);
$('#modal-title').text('แก้ไขชุดข้อมูล');
$('#profileModal').removeClass('hidden');
}
});
}
function deleteProfile(id) {
Swal.fire({
title: 'ยืนยันการลบ?',
text: "หากลบแล้วจะไม่สามารถกู้คืนได้ (ประวัติที่เคยใช้ชุดข้อมูลนี้จะกลายเป็นค่าว่าง)",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#3085d6',
confirmButtonText: 'ใช่, ลบเลย!',
cancelButtonText: 'ยกเลิก'
}).then((result) => {
if (result.isConfirmed) {
$.post('api_profiles.php?action=delete', { id: id }, function(res) {
if (res.status === 'success') {
Swal.fire('ลบสำเร็จ!', res.message, 'success');
table.ajax.reload();
} else {
Swal.fire('ข้อผิดพลาด', res.message, 'error');
}
});
}
});
}
</script>
</body>
</html>