นี่คือ Prompt ฉบับละเอียด (Master Prompt) ที่เขียนขึ้นเพื่อสั่งงาน AI (เช่น ChatGPT, Claude หรือ Copilot) ให้เขียนโค้ดชุดนี้โดยเฉพาะครับ

ผมได้เขียนเป็นภาษาอังกฤษในส่วนของ Prompt เพราะ AI Model สายเขียนโค้ดจะเข้าใจ Logic และ Technical terms ภาษาอังกฤษได้แม่นยำกว่าครับ แต่ผมใส่รายละเอียด Business Logic ของคุณลงไปครบถ้วนครับ

📋 ส่วนที่ 1: Prompt สำหรับสั่ง AI (Copy ส่วนนี้ไปใช้ได้เลย)

System Role: Act as a Senior PHP Security Architect and Full-Stack Developer. Your task is to build a secure Hospital Appointment System using Native PHP (PDO), MySQL, Tailwind CSS, and Alpine.js.

1. Technology Stack & Environment:

  • Backend: PHP 8.0+ (No frameworks, pure PHP).
  • Database: MySQL/MariaDB (Use PDO driver).
  • Frontend: HTML5, Tailwind CSS (CDN), Alpine.js (CDN).
  • Design: Professional Medical Theme (White & Green palette).
  • Routing: Custom routing via index.php and .htaccess (Hide .php extensions).

2. Security Requirements (Strict Enforcement):

  • SQL Injection: Use PDO Prepared Statements for ALL queries.
  • XSS: Sanitize all output with htmlspecialchars() or a custom wrapper.
  • CSRF: Implement a Token-based protection for all POST requests.
  • Path Traversal (LFI): Validate $_GET['page'] against a whitelist of allowed files.
  • Brute Force: Lock account/IP after 5 failed login attempts for 15 minutes (Track in DB).
  • Session Security: Use session_regenerate_id(true), cookie_httponly, cookie_secure (if https), and prevent Session Fixation.

3. User Roles & Permissions:

  • Super Admin:
  • Manage all Hospitals (CRUD).
  • Manage Users (Assign Admin/Officer to hospitals).
  • Default Account: User: super_admin, Pass: 123456.
  • Admin (Hospital Level):
  • Manage settings for their hospital only.
  • Toggle Booking System: Can Open/Close booking status for their hospital.
  • Officer (Hospital Level):
  • View appointment lists/Check-in patients.
  • Citizen (General User):
  • Register/Login.
  • Book appointments.
  • Cancel Rule: Can only cancel an appointment within 30 minutes of creation.

4. Business Logic Details:

  • Multi-Hospital: The system must handle multiple hospitals. Appointments are linked to hospital_id.
  • Cancellation Logic: When a citizen views their appointments, calculate (Current Time - Created Time). If > 30 mins, hide/disable the "Cancel" button.
  • 404 Handling: If a route/hospital ID is invalid, redirect to a custom 404.php.

5. Deliverables: Please generate the following code:

  1. SQL Schema (db.sql): Tables for hospitals, users (with role enum), appointments, login_attempts. Include the default Super Admin insert (Hash the password!).
  2. Configuration (db.php & config.php): PDO connection and Security constants.
  3. Router (index.php & .htaccess): The logic to handle clean URLs.
  4. Security Helper (functions.php): CSRF check, Input cleaning, Time difference calculator.
  5. Views:
  • login.php (Unified login page).
  • dashboard.php (Dynamic layout based on user Role).
  • booking.php (Citizen booking form with Alpine.js).
  • 404.php.

📋 ส่วนที่ 2: โครงสร้างฐานข้อมูลและการออกแบบ (System Design)

เพื่อให้คุณเข้าใจภาพรวมของระบบที่จะได้มา นี่คือการออกแบบฐานข้อมูลที่ระบบควรจะเป็นครับ

คำอธิบายตาราง (Database Schema):

  1. hospitals:
  • id (PK)
  • name
  • booking_status (enum: 'open', 'closed') -- สำหรับให้ Admin ปิดระบบจอง
  1. users:
  • id (PK)
  • hospital_id (FK, Nullable) -- Super Admin เป็น NULL, Admin/Officer ผูกกับ ID รพ.
  • username
  • password (VARCHAR 255 สำหรับเก็บ Hash)
  • role (enum: 'super_admin', 'admin', 'officer', 'citizen')
  1. appointments:
  • id (PK)
  • user_id (FK - ผู้จอง)
  • hospital_id (FK - จองที่ไหน)
  • status (pending, confirmed, cancelled)
  • created_at (DATETIME) -- สำคัญมาก! ใช้เช็คกฎ 30 นาที
  1. login_attempts:
  • ip_address
  • attempts_count
  • last_attempt_at -- ใช้ป้องกัน Brute Force

📋 ส่วนที่ 3: คำแนะนำในการนำโค้ดไปใช้

เมื่อ AI เขียนโค้ดมาให้แล้ว ให้คุณตรวจสอบจุดสำคัญดังนี้ครับ:

  1. การตั้งค่า .htaccess: เพื่อให้ URL สวยงามและซ่อน .php ต้องแน่ใจว่า Server (Apache) เปิดใช้งาน mod_rewrite แล้ว ไฟล์ .htaccess ควรมีหน้าตาประมาณนี้:
  2. Apache

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
  1. Logic กฎ 30 นาที (PHP): ในไฟล์ที่แสดงประวัติการจอง ตรวจสอบว่ามีโค้ดทำนองนี้หรือไม่:
  2. PHP

$created_time = strtotime($appointment['created_at']);
$time_limit = $created_time + (30 * 60); // บวก 30 นาที

if (time() < $time_limit) {
    // แสดงปุ่มยกเลิก
} else {
    // ซ่อนปุ่ม หรือแสดงข้อความว่าหมดเวลา
}
  1. ความปลอดภัย:
  • ห้ามเห็น query ดิบๆ เช่น SELECT * FROM users WHERE name = '$name'
  • ต้องเห็น prepare() และ execute(['name' => $name]) เท่านั้น

ต้องการให้ผม Gen โค้ดส่วนไหนให้ก่อนเป็นตัวอย่างไหมครับ? (เช่น ส่วน Router หรือ ส่วน Database Connection)