'error','message'=>'ไม่สามารถดำเนินการได้'); switch($action){ case 'fetch': try { $stmt = $pdo->prepare("SELECT ic.*, p.title as prefix_title, g.title as gender_title, o.title as occupation_title, ms.title as marital_status_title, st.title as service_type_title, r.title as emergency_relation_title FROM individual_counseling ic LEFT JOIN prefixs p ON ic.prefix_id = p.id LEFT JOIN genders g ON ic.gender_id = g.id LEFT JOIN occupations o ON ic.occupation_id = o.id LEFT JOIN statuses ms ON ic.marital_status_id = ms.id LEFT JOIN service_types st ON ic.service_type_id = st.id LEFT JOIN relationships r ON ic.emergency_relation_id = r.id ORDER BY ic.created_at DESC"); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode(array("status"=>"success", "data"=>$data)); } catch (PDOException $e) { echo json_encode(array("status"=>"error", "message"=>"เกิดข้อผิดพลาด: " . $e->getMessage())); } exit; case 'add': // รับข้อมูลจากฟอร์ม $prefix_id = isset($_POST['prefix']) ? intval($_POST['prefix']) : NULL; $first_name = isset($_POST['first_name']) ? trim($_POST['first_name']) : ''; $last_name = isset($_POST['last_name']) ? trim($_POST['last_name']) : ''; $birth_date = isset($_POST['birth_date']) ? trim($_POST['birth_date']) : ''; $gender_id = isset($_POST['gender']) ? intval($_POST['gender']) : 0; $ethnicity = isset($_POST['ethnicity']) ? trim($_POST['ethnicity']) : ''; $nationality = isset($_POST['nationality']) ? trim($_POST['nationality']) : ''; $religion = isset($_POST['religion']) ? trim($_POST['religion']) : ''; $blood_type = isset($_POST['blood_type']) ? trim($_POST['blood_type']) : NULL; $occupation_id = isset($_POST['occupation_id']) ? intval($_POST['occupation_id']) : 0; $occupation_other = isset($_POST['occupation_other']) ? trim($_POST['occupation_other']) : ''; $marital_status_id = isset($_POST['marital_status']) ? intval($_POST['marital_status']) : NULL; // ที่อยู่ $house_number = isset($_POST['house_number']) ? trim($_POST['house_number']) : ''; $village_number = isset($_POST['village_number']) ? trim($_POST['village_number']) : ''; $building = isset($_POST['building']) ? trim($_POST['building']) : ''; $street = isset($_POST['street']) ? trim($_POST['street']) : ''; $subdistrict = isset($_POST['subdistrict']) ? trim($_POST['subdistrict']) : ''; $district = isset($_POST['district']) ? trim($_POST['district']) : ''; $province = isset($_POST['province']) ? trim($_POST['province']) : ''; // ข้อมูลติดต่อ $phone = isset($_POST['phone']) ? trim($_POST['phone']) : ''; $line_id = isset($_POST['line_id']) ? trim($_POST['line_id']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $service_type_id = isset($_POST['service_type']) ? intval($_POST['service_type']) : 0; // ผู้ติดต่อฉุกเฉิน $emergency_first_name = isset($_POST['emergency_first_name']) ? trim($_POST['emergency_first_name']) : ''; $emergency_last_name = isset($_POST['emergency_last_name']) ? trim($_POST['emergency_last_name']) : ''; $emergency_relation_id = isset($_POST['emergency_relation']) ? intval($_POST['emergency_relation']) : NULL; $emergency_phone = isset($_POST['emergency_phone']) ? trim($_POST['emergency_phone']) : ''; // ข้อมูล session $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $schoolID = isset($_POST['schoolID']) ? trim($_POST['schoolID']) : ''; $leader = isset($_POST['leader']) ? trim($_POST['leader']) : ''; // Validation if($first_name === '') { $res['message']='กรุณากรอกชื่อ'; echo json_encode($res); exit; } if($last_name === '') { $res['message']='กรุณากรอกนามสกุล'; echo json_encode($res); exit; } if($birth_date === '') { $res['message']='กรุณาเลือกวันเกิด'; echo json_encode($res); exit; } if($gender_id <= 0) { $res['message']='กรุณาเลือกเพศ'; echo json_encode($res); exit; } if($ethnicity === '') { $res['message']='กรุณากรอกเชื้อชาติ'; echo json_encode($res); exit; } if($nationality === '') { $res['message']='กรุณากรอกสัญชาติ'; echo json_encode($res); exit; } if($occupation_id <= 0) { $res['message']='กรุณาเลือกอาชีพ'; echo json_encode($res); exit; } if($street === '') { $res['message']='กรุณากรอกถนน'; echo json_encode($res); exit; } if($subdistrict === '') { $res['message']='กรุณากรอกตำบล'; echo json_encode($res); exit; } if($district === '') { $res['message']='กรุณากรอกอำเภอ'; echo json_encode($res); exit; } if($province === '') { $res['message']='กรุณากรอกจังหวัด'; echo json_encode($res); exit; } if($phone === '') { $res['message']='กรุณากรอกหมายเลขโทรศัพท์'; echo json_encode($res); exit; } if($service_type_id <= 0) { $res['message']='กรุณาเลือกประเภทผู้รับบริการ'; echo json_encode($res); exit; } // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'prefix_id' => $prefix_id, 'first_name' => $first_name, 'last_name' => $last_name, 'birth_date' => $birth_date, 'gender_id' => $gender_id, 'ethnicity' => $ethnicity, 'nationality' => $nationality, 'religion' => $religion, 'blood_type' => $blood_type, 'occupation_id' => $occupation_id, 'occupation_other' => $occupation_other, 'marital_status_id' => $marital_status_id, 'house_number' => $house_number, 'village_number' => $village_number, 'building' => $building, 'street' => $street, 'subdistrict' => $subdistrict, 'district' => $district, 'province' => $province, 'phone' => $phone, 'line_id' => $line_id, 'email' => $email, 'service_type_id' => $service_type_id, 'emergency_first_name' => $emergency_first_name, 'emergency_last_name' => $emergency_last_name, 'emergency_relation_id' => $emergency_relation_id, 'emergency_phone' => $emergency_phone, 'username' => $username, 'schoolID' => $schoolID, 'leader' => $leader ); // ตรวจสอบว่า json_encode สำเร็จ $form_data_json = json_encode($form_data); if ($form_data_json === false) { $form_data_json = '{}'; } try { $stmt = $pdo->prepare("INSERT INTO individual_counseling (prefix_id, first_name, last_name, birth_date, gender_id, ethnicity, nationality, religion, blood_type, occupation_id, occupation_other, marital_status_id, house_number, village_number, building, street, subdistrict, district, province, phone, line_id, email, service_type_id, emergency_first_name, emergency_last_name, emergency_relation_id, emergency_phone, form_data_json, username, schoolID, leader) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); if($stmt->execute(array($prefix_id, $first_name, $last_name, $birth_date, $gender_id, $ethnicity, $nationality, $religion, $blood_type, $occupation_id, $occupation_other, $marital_status_id, $house_number, $village_number, $building, $street, $subdistrict, $district, $province, $phone, $line_id, $email, $service_type_id, $emergency_first_name, $emergency_last_name, $emergency_relation_id, $emergency_phone, $form_data_json, $username, $schoolID, $leader))){ $res['status']='success'; $res['message']='บันทึกข้อมูลสำเร็จ'; $res['id'] = $pdo->lastInsertId(); } } catch (PDOException $e) { $res['message'] = 'เกิดข้อผิดพลาด: ' . $e->getMessage(); } echo json_encode($res); exit; case 'edit': $id = isset($_POST['id']) ? intval($_POST['id']) : 0; // รับข้อมูลจากฟอร์ม (เหมือนกับ case 'add') $prefix_id = isset($_POST['prefix']) ? intval($_POST['prefix']) : NULL; $first_name = isset($_POST['first_name']) ? trim($_POST['first_name']) : ''; $last_name = isset($_POST['last_name']) ? trim($_POST['last_name']) : ''; $birth_date = isset($_POST['birth_date']) ? trim($_POST['birth_date']) : ''; $gender_id = isset($_POST['gender']) ? intval($_POST['gender']) : 0; $ethnicity = isset($_POST['ethnicity']) ? trim($_POST['ethnicity']) : ''; $nationality = isset($_POST['nationality']) ? trim($_POST['nationality']) : ''; $religion = isset($_POST['religion']) ? trim($_POST['religion']) : ''; $blood_type = isset($_POST['blood_type']) ? trim($_POST['blood_type']) : NULL; $occupation_id = isset($_POST['occupation_id']) ? intval($_POST['occupation_id']) : 0; $occupation_other = isset($_POST['occupation_other']) ? trim($_POST['occupation_other']) : ''; $marital_status_id = isset($_POST['marital_status']) ? intval($_POST['marital_status']) : NULL; // ที่อยู่ $house_number = isset($_POST['house_number']) ? trim($_POST['house_number']) : ''; $village_number = isset($_POST['village_number']) ? trim($_POST['village_number']) : ''; $building = isset($_POST['building']) ? trim($_POST['building']) : ''; $street = isset($_POST['street']) ? trim($_POST['street']) : ''; $subdistrict = isset($_POST['subdistrict']) ? trim($_POST['subdistrict']) : ''; $district = isset($_POST['district']) ? trim($_POST['district']) : ''; $province = isset($_POST['province']) ? trim($_POST['province']) : ''; // ข้อมูลติดต่อ $phone = isset($_POST['phone']) ? trim($_POST['phone']) : ''; $line_id = isset($_POST['line_id']) ? trim($_POST['line_id']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $service_type_id = isset($_POST['service_type']) ? intval($_POST['service_type']) : 0; // ผู้ติดต่อฉุกเฉิน $emergency_first_name = isset($_POST['emergency_first_name']) ? trim($_POST['emergency_first_name']) : ''; $emergency_last_name = isset($_POST['emergency_last_name']) ? trim($_POST['emergency_last_name']) : ''; $emergency_relation_id = isset($_POST['emergency_relation']) ? intval($_POST['emergency_relation']) : NULL; $emergency_phone = isset($_POST['emergency_phone']) ? trim($_POST['emergency_phone']) : ''; // ข้อมูล session $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $schoolID = isset($_POST['schoolID']) ? trim($_POST['schoolID']) : ''; $leader = isset($_POST['leader']) ? trim($_POST['leader']) : ''; // Validation if($id <= 0) { $res['message']='ID ไม่ถูกต้อง'; echo json_encode($res); exit; } if($first_name === '') { $res['message']='กรุณากรอกชื่อ'; echo json_encode($res); exit; } if($last_name === '') { $res['message']='กรุณากรอกนามสกุล'; echo json_encode($res); exit; } if($birth_date === '') { $res['message']='กรุณาเลือกวันเกิด'; echo json_encode($res); exit; } if($gender_id <= 0) { $res['message']='กรุณาเลือกเพศ'; echo json_encode($res); exit; } if($service_type_id <= 0) { $res['message']='กรุณาเลือกประเภทผู้รับบริการ'; echo json_encode($res); exit; } // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'prefix_id' => $prefix_id, 'first_name' => $first_name, 'last_name' => $last_name, 'birth_date' => $birth_date, 'gender_id' => $gender_id, 'ethnicity' => $ethnicity, 'nationality' => $nationality, 'religion' => $religion, 'blood_type' => $blood_type, 'occupation_id' => $occupation_id, 'occupation_other' => $occupation_other, 'marital_status_id' => $marital_status_id, 'house_number' => $house_number, 'village_number' => $village_number, 'building' => $building, 'street' => $street, 'subdistrict' => $subdistrict, 'district' => $district, 'province' => $province, 'phone' => $phone, 'line_id' => $line_id, 'email' => $email, 'service_type_id' => $service_type_id, 'emergency_first_name' => $emergency_first_name, 'emergency_last_name' => $emergency_last_name, 'emergency_relation_id' => $emergency_relation_id, 'emergency_phone' => $emergency_phone, 'username' => $username, 'schoolID' => $schoolID, 'leader' => $leader ); // ตรวจสอบว่า json_encode สำเร็จ $form_data_json = json_encode($form_data); if ($form_data_json === false) { $form_data_json = '{}'; } try { $stmt = $pdo->prepare("UPDATE individual_counseling SET prefix_id=?, first_name=?, last_name=?, birth_date=?, gender_id=?, ethnicity=?, nationality=?, religion=?, blood_type=?, occupation_id=?, occupation_other=?, marital_status_id=?, house_number=?, village_number=?, building=?, street=?, subdistrict=?, district=?, province=?, phone=?, line_id=?, email=?, service_type_id=?, emergency_first_name=?, emergency_last_name=?, emergency_relation_id=?, emergency_phone=?, form_data_json=?, username=?, schoolID=?, leader=?, updated_at=CURRENT_TIMESTAMP WHERE id=?"); if($stmt->execute(array($prefix_id, $first_name, $last_name, $birth_date, $gender_id, $ethnicity, $nationality, $religion, $blood_type, $occupation_id, $occupation_other, $marital_status_id, $house_number, $village_number, $building, $street, $subdistrict, $district, $province, $phone, $line_id, $email, $service_type_id, $emergency_first_name, $emergency_last_name, $emergency_relation_id, $emergency_phone, $form_data_json, $username, $schoolID, $leader, $id))){ $res['status']='success'; $res['message']='แก้ไขข้อมูลสำเร็จ'; } } catch (PDOException $e) { $res['message'] = 'เกิดข้อผิดพลาด: ' . $e->getMessage(); } echo json_encode($res); exit; case 'delete': $id = intval($_POST['id']); if($id <= 0){ $res['message']='ID ไม่ถูกต้อง'; echo json_encode($res); exit; } try { $stmt = $pdo->prepare("DELETE FROM individual_counseling WHERE id=?"); if($stmt->execute(array($id))){ $res['status']='success'; $res['message']='ลบข้อมูลสำเร็จ'; } } catch (PDOException $e) { $res['message'] = 'เกิดข้อผิดพลาด: ' . $e->getMessage(); } echo json_encode($res); exit; case 'get': $id = intval($_GET['id']); if($id <= 0){ $res['message']='ID ไม่ถูกต้อง'; echo json_encode($res); exit; } try { $stmt = $pdo->prepare("SELECT * FROM individual_counseling WHERE id=?"); $stmt->execute(array($id)); $data = $stmt->fetch(PDO::FETCH_ASSOC); if($data){ echo json_encode(array("status"=>"success", "data"=>$data)); } else { echo json_encode(array("status"=>"error", "message"=>"ไม่พบข้อมูล")); } } catch (PDOException $e) { echo json_encode(array("status"=>"error", "message"=>"เกิดข้อผิดพลาด: " . $e->getMessage())); } exit; default: echo json_encode($res); exit; } ?>