Files
2026-09-16 23:20:08 +07:00

287 lines
16 KiB
PHP

<?php
require_once 'includes/auth.php';
require_once 'includes/db.php';
$pageTitle = 'ตารางการเดินรถ | ระบบจัดการข้อมูลยานพาหนะ โรงพยาบาลเกาะสมุย';
$pageTitleDisplay = 'ตารางการเดินรถ';
$breadcrumbs = ['ตารางการเดินรถ'];
require_once 'includes/header.php';
// DataTables CSS
echo '<link rel="stylesheet" href="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css">';
echo '<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.dataTables.min.css">';
require_once 'includes/db_intranet.php';
$selectedYear = $_GET['year'] ?? (date('Y') + 543);
$gregorianYear = $selectedYear - 543;
// ดึงข้อมูลการจองจากตาราง booking_car (Intranet)
$schedules = [];
$availableYears = [];
if ($intranet_pdo) {
try {
// ดึงรายการปีที่มีข้อมูล
$stmtYears = $intranet_pdo->query("SELECT DISTINCT YEAR(booking_car_request_date_start) as y FROM booking_car WHERE booking_car_request_date_start IS NOT NULL ORDER BY y DESC");
$years = $stmtYears->fetchAll();
foreach($years as $y) {
if($y['y']) {
$availableYears[] = $y['y'] + 543;
}
}
// ดึงข้อมูลตามปีที่เลือก
$stmt = $intranet_pdo->prepare("
SELECT *
FROM booking_car
WHERE YEAR(booking_car_request_date_start) = ?
ORDER BY booking_car_request_date_start DESC, booking_car_request_time_start DESC
");
$stmt->execute([$gregorianYear]);
$schedules = $stmt->fetchAll();
$inSamuiCount = 0;
$outSamuiCount = 0;
foreach ($schedules as $s) {
$area = trim($s['booking_car_request_area'] ?? '');
if ($area === 'ในเกาะสมุย') {
$inSamuiCount++;
} elseif ($area === 'นอกเกาะสมุย') {
$outSamuiCount++;
}
}
} catch (PDOException $e) {
$schedules = [];
$inSamuiCount = 0;
$outSamuiCount = 0;
}
}
if (empty($availableYears)) {
$availableYears = [date('Y') + 543];
}
?>
<!-- Floating Background -->
<div class="fixed top-0 left-0 w-full h-full overflow-hidden -z-10 pointer-events-none">
<div class="absolute top-[-10%] left-[-10%] w-[40%] h-[40%] bg-primary-500/10 rounded-full blur-[100px]"></div>
<div class="absolute bottom-[-10%] right-[-10%] w-[40%] h-[40%] bg-secondary/10 rounded-full blur-[100px]"></div>
</div>
<!-- Layout Wrapper -->
<div x-data="sidebarStore" class="flex h-screen overflow-hidden">
<!-- Sidebar -->
<?php include 'includes/sidebar.php'; ?>
<!-- Main Content -->
<main class="flex-1 flex flex-col min-w-0 overflow-hidden relative z-10">
<!-- Top Navbar -->
<?php include 'includes/navbar.php'; ?>
<!-- Dashboard Content -->
<div class="flex-1 overflow-y-auto p-4 pt-0 custom-scrollbar page-transition">
<div class="mb-6 flex justify-between items-end">
<div>
<h1 class="text-2xl font-bold dark:text-white tracking-tight">ตารางการเดินรถ</h1>
<p class="text-slate-500 dark:text-slate-400 mt-1">ปฏิทินและคิวการจองรถยนต์จากระบบ Intranet</p>
</div>
</div>
<div class="grid grid-cols-1 xl:grid-cols-3 gap-6">
<!-- Calendar Placeholder (Left Column) -->
<div class="xl:col-span-1 glass-card p-5 h-[850px] flex flex-col">
<div id="calendar" class="h-full"></div>
</div>
<!-- List Section (Right Column) -->
<div class="xl:col-span-2 glass-card rounded-[16px] overflow-hidden flex flex-col h-[850px]">
<div class="p-5 border-b border-slate-200 dark:border-slate-700/50 flex justify-between items-center bg-white/50 dark:bg-slate-800/50 shrink-0">
<h2 class="text-lg font-semibold dark:text-white">รายการเดินรถ (ปี <?= $selectedYear ?>)</h2>
<div class="flex items-center space-x-4">
<div class="flex space-x-3 text-sm text-slate-600 dark:text-slate-300 mr-2 border-r border-slate-200 dark:border-slate-700 pr-4">
<div class="flex items-center">
<i class="fa-solid fa-car-side mr-2" style="color: #3b82f6;"></i>
<span>ในสมุย: <strong><?= $inSamuiCount ?></strong></span>
</div>
<div class="flex items-center">
<i class="fa-solid fa-car-side mr-2" style="color: #f97316;"></i>
<span>นอกสมุย: <strong><?= $outSamuiCount ?></strong></span>
</div>
</div>
<div class="flex items-center space-x-2">
<label class="text-sm text-slate-500">เลือกปี พ.ศ.</label>
<select class="glass-input text-sm p-1.5 rounded-lg w-24" onchange="window.location.href='schedule.php?year='+this.value">
<?php foreach($availableYears as $y): ?>
<option value="<?= $y ?>" <?= $y == $selectedYear ? 'selected' : '' ?>><?= $y ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="overflow-y-auto flex-1 p-5">
<table id="scheduleTable" class="w-full text-sm text-left text-slate-500 dark:text-slate-400" style="width:100%">
<thead class="text-xs text-slate-700 uppercase bg-slate-50 dark:bg-slate-700 dark:text-slate-400">
<tr>
<th scope="col" class="px-4 py-3">วัน-เวลา</th>
<th scope="col" class="px-4 py-3">สถานที่/ไปเพื่อ</th>
<th scope="col" class="px-4 py-3 text-center">จัดการ</th>
</tr>
</thead>
<tbody>
<?php foreach($schedules as $s):
$statusText = trim($s['booking_car_request_status'] ?? 'รอพิจารณา');
$badgeBg = 'bg-slate-100';
$badgeText = 'text-slate-600';
$badgeBorder = 'border-slate-200';
if(strpos($statusText, 'อนุมัติ') !== false) {
$badgeBg = 'bg-success/10'; $badgeText = 'text-success'; $badgeBorder = 'border-success/20';
} elseif(strpos($statusText, 'ยกเลิก') !== false || strpos($statusText, 'ไม่อนุมัติ') !== false) {
$badgeBg = 'bg-danger/10'; $badgeText = 'text-danger'; $badgeBorder = 'border-danger/20';
} elseif(strpos($statusText, 'รอ') !== false) {
$badgeBg = 'bg-warning/10'; $badgeText = 'text-warning'; $badgeBorder = 'border-warning/20';
}
$startDate = strtotime($s['booking_car_request_date_start']);
$startTime = $s['booking_car_request_time_start'] ? date('H:i', strtotime($s['booking_car_request_time_start'])) : '';
$thaiYear = date('Y', $startDate) + 543;
$formattedDateTime = date('d/m/', $startDate) . $thaiYear . ($startTime ? ' ' . $startTime : '');
?>
<tr class="bg-white border-b dark:bg-slate-800 dark:border-slate-700 hover:bg-slate-50 dark:hover:bg-slate-600">
<td class="px-4 py-3 whitespace-nowrap"><span class="hidden"><?= $s['booking_car_request_date_start'] . ' ' . $s['booking_car_request_time_start'] ?></span><?= $formattedDateTime ?></td>
<td class="px-4 py-3">
<div class="font-bold text-slate-800 dark:text-white"><?= htmlspecialchars($s['booking_car_request_station'] ?? 'ไม่ระบุสถานที่') ?></div>
<div class="text-xs text-slate-500 line-clamp-1"><?= htmlspecialchars($s['booking_car_request_title'] ?? '-') ?></div>
</td>
<td class="px-4 py-3 text-center">
<button onclick="showBookingDetails(<?= htmlspecialchars(json_encode([
'station' => $s['booking_car_request_station'] ?? 'ไม่ระบุ',
'title' => $s['booking_car_request_title'] ?? '-',
'name' => $s['booking_car_request_name'] ?? 'ไม่ระบุ',
'type' => $s['booking_car_request_type'] ?? 'ไม่ระบุ',
'status' => $statusText,
'phone' => $s['booking_car_request_phone'] ?? '-',
'desc' => $s['booking_car_request_description'] ?? '-',
'date' => $formattedDateTime,
'link' => $s['booking_car_request_link_view'] ?? ''
])) ?>)" class="text-primary-600 hover:text-primary-800 bg-primary-50 hover:bg-primary-100 p-1.5 rounded-lg transition-colors">
<i class="fa-solid fa-eye"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</main>
</div>
<!-- jQuery and DataTables JS -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>
<!-- SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- FullCalendar CDN -->
<script src="https://cdn.jsdelivr.net/npm/fullcalendar@6.1.10/index.global.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Initialize DataTable
if ($.fn.DataTable.isDataTable('#scheduleTable')) {
$('#scheduleTable').DataTable().destroy();
}
$('#scheduleTable').DataTable({
responsive: true,
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.7/i18n/th.json'
},
pageLength: 10,
lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "ทั้งหมด"]],
order: [[0, "desc"]] // Sort by date descending
});
// Initialize FullCalendar
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
locale: 'th',
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth'
},
buttonText: {
today: 'วันนี้',
month: 'เดือน'
},
themeSystem: 'standard',
height: '100%',
events: 'api/schedule_events.php',
eventContent: function(arg) {
// Custom HTML to render just the car icon
let iconColor = arg.event.extendedProps.iconColor || '#94a3b8';
let html = `
<div class="flex items-center justify-center cursor-pointer hover:scale-125 transition-transform" title="${arg.event.title}">
<i class="fa-solid fa-car-side text-xl" style="color: ${iconColor}; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.2));"></i>
</div>
`;
return { html: html };
},
eventClick: function(info) {
// ป้องกันการลิงก์ออก
info.jsEvent.preventDefault();
const props = info.event.extendedProps;
showBookingDetails({
station: props.station || 'ไม่ระบุ',
title: props.purpose || '-',
name: props.name || 'ไม่ระบุ',
type: props.type || 'ไม่ระบุ',
status: props.status || 'รอพิจารณา',
phone: props.phone || '-',
desc: props.desc || '-',
date: props.date || '',
link: props.link || ''
});
}
});
calendar.render();
});
// Function for table view button
function showBookingDetails(data) {
let html = `
<div class="text-left space-y-3 text-sm">
<div><strong>วัน-เวลา:</strong> ${data.date}</div>
<div><strong>สถานที่:</strong> ${data.station}</div>
<div><strong>ไปเพื่อ:</strong> ${data.title}</div>
<div><strong>ผู้จอง:</strong> ${data.name}</div>
<div><strong>เบอร์ติดต่อ:</strong> ${data.phone}</div>
<div><strong>ประเภทรถ:</strong> ${data.type}</div>
<div><strong>รายละเอียด:</strong> ${data.desc}</div>
<div><strong>สถานะ:</strong> ${data.status}</div>
</div>
`;
Swal.fire({
title: 'รายละเอียดรายการเดินรถ',
html: html,
icon: 'info',
confirmButtonText: 'ปิด',
});
}
</script>
<?php require_once 'includes/footer.php'; ?>