'error','message'=>'ไม่สามารถดำเนินการได้'); switch($action){ case 'fetch': try { $stmt = $pdo->prepare("SELECT ics.*, ic.first_name, ic.last_name, pt.title as participant_type_title, gt.title as guidance_type_title, st.title as service_success_title, app.title as follow_up_title FROM individual_counseling_service ics LEFT JOIN individual_counseling ic ON ics.individual_counseling_id = ic.id LEFT JOIN participant_types pt ON ics.participant_type_id = pt.id LEFT JOIN guidance_types gt ON ics.guidance_type_id = gt.id LEFT JOIN success_types st ON ics.service_success_id = st.id LEFT JOIN appointments app ON ics.follow_up_id = app.id ORDER BY ics.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': // ตอนที่ 3: ข้อมูลการขอรับบริการ $individual_counseling_id = isset($_POST['individual_counseling_id']) ? intval($_POST['individual_counseling_id']) : NULL; $service_request_number = isset($_POST['service_request_number']) ? intval($_POST['service_request_number']) : NULL; $service_request_date = isset($_POST['service_request_date']) ? trim($_POST['service_request_date']) : ''; $service_request_details = isset($_POST['service_request_details']) ? trim($_POST['service_request_details']) : ''; $recommendations = isset($_POST['recommendations']) ? trim($_POST['recommendations']) : ''; // วิเคราะห์ประเด็นการขอรับบริการ $education_issues = isset($_POST['educationIssues']) ? json_encode($_POST['educationIssues']) : NULL; $education_other = isset($_POST['education_other']) ? trim($_POST['education_other']) : ''; $career_issues = isset($_POST['careerIssues']) ? json_encode($_POST['careerIssues']) : NULL; $career_other = isset($_POST['career_other']) ? trim($_POST['career_other']) : ''; $personal_issues = isset($_POST['personalIssues']) ? json_encode($_POST['personalIssues']) : NULL; $personal_other = isset($_POST['personal_other']) ? trim($_POST['personal_other']) : ''; // ตอนที่ 4: ผลการให้บริการ (ปรับปรุงใหม่) $service_success_id = isset($_POST['serviceSuccess']) ? intval($_POST['serviceSuccess']) : NULL; $service_failure_reason = isset($_POST['service_failure_reason']) ? trim($_POST['service_failure_reason']) : ''; // ความช่วยเหลือ/ส่งต่อ $referral_services = isset($_POST['referralServices']) ? json_encode($_POST['referralServices']) : NULL; $referral_other = isset($_POST['referral_other']) ? trim($_POST['referral_other']) : ''; // นัดหมายติดตาม $follow_up_id = isset($_POST['followUp']) ? intval($_POST['followUp']) : NULL; $follow_up_date = isset($_POST['follow_up_date']) ? trim($_POST['follow_up_date']) : NULL; // ผู้ปฏิบัติหน้าที่ $counselor_first_name = isset($_POST['counselor_first_name']) ? trim($_POST['counselor_first_name']) : ''; $counselor_last_name = isset($_POST['counselor_last_name']) ? trim($_POST['counselor_last_name']) : ''; $counselor_position = isset($_POST['counselor_position']) ? trim($_POST['counselor_position']) : ''; $counselor_agency = isset($_POST['counselor_agency']) ? trim($_POST['counselor_agency']) : ''; $counselor_phone = isset($_POST['counselor_phone']) ? trim($_POST['counselor_phone']) : ''; // ข้อมูล session $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $schoolID = isset($_POST['schoolID']) ? trim($_POST['schoolID']) : ''; $leader = isset($_POST['leader']) ? trim($_POST['leader']) : ''; // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'individual_counseling_id' => $individual_counseling_id, 'service_request_number' => $service_request_number, 'service_request_date' => $service_request_date, 'service_request_details' => $service_request_details, 'education_issues' => $education_issues, 'education_other' => $education_other, 'career_issues' => $career_issues, 'career_other' => $career_other, 'personal_issues' => $personal_issues, 'personal_other' => $personal_other, 'recommendations' => $recommendations, 'service_success_id' => $service_success_id, 'service_failure_reason' => $service_failure_reason, 'referral_services' => $referral_services, 'referral_other' => $referral_other, 'follow_up_id' => $follow_up_id, 'follow_up_date' => $follow_up_date, 'counselor_first_name' => $counselor_first_name, 'counselor_last_name' => $counselor_last_name, 'counselor_position' => $counselor_position, 'counselor_agency' => $counselor_agency, 'counselor_phone' => $counselor_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_service (individual_counseling_id, service_request_number, service_request_date, service_request_details, education_issues, education_other, career_issues, career_other, personal_issues, personal_other, recommendations, service_success_id, service_failure_reason, referral_services, referral_other, follow_up_id, follow_up_date, counselor_first_name, counselor_last_name, counselor_position, counselor_agency, counselor_phone, form_data_json, username, schoolID, leader) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); if($stmt->execute(array($individual_counseling_id, $service_request_number, $service_request_date, $service_request_details, $education_issues, $education_other, $career_issues, $career_other, $personal_issues, $personal_other, $recommendations, $service_success_id, $service_failure_reason, $referral_services, $referral_other, $follow_up_id, $follow_up_date, $counselor_first_name, $counselor_last_name, $counselor_position, $counselor_agency, $counselor_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' $individual_counseling_id = isset($_POST['individual_counseling_id']) ? intval($_POST['individual_counseling_id']) : NULL; $service_request_number = isset($_POST['service_request_number']) ? intval($_POST['service_request_number']) : NULL; $service_request_date = isset($_POST['service_request_date']) ? trim($_POST['service_request_date']) : ''; $service_request_details = isset($_POST['service_request_details']) ? trim($_POST['service_request_details']) : ''; $recommendations = isset($_POST['recommendations']) ? trim($_POST['recommendations']) : ''; // วิเคราะห์ประเด็นการขอรับบริการ $education_issues = isset($_POST['educationIssues']) ? json_encode($_POST['educationIssues']) : NULL; $education_other = isset($_POST['education_other']) ? trim($_POST['education_other']) : ''; $career_issues = isset($_POST['careerIssues']) ? json_encode($_POST['careerIssues']) : NULL; $career_other = isset($_POST['career_other']) ? trim($_POST['career_other']) : ''; $personal_issues = isset($_POST['personalIssues']) ? json_encode($_POST['personalIssues']) : NULL; $personal_other = isset($_POST['personal_other']) ? trim($_POST['personal_other']) : ''; // ตอนที่ 4: ผลการให้บริการ (ปรับปรุงใหม่) $service_success_id = isset($_POST['serviceSuccess']) ? intval($_POST['serviceSuccess']) : NULL; $service_failure_reason = isset($_POST['service_failure_reason']) ? trim($_POST['service_failure_reason']) : ''; // ความช่วยเหลือ/ส่งต่อ $referral_services = isset($_POST['referralServices']) ? json_encode($_POST['referralServices']) : NULL; $referral_other = isset($_POST['referral_other']) ? trim($_POST['referral_other']) : ''; // นัดหมายติดตาม $follow_up_id = isset($_POST['followUp']) ? intval($_POST['followUp']) : NULL; $follow_up_date = isset($_POST['follow_up_date']) ? trim($_POST['follow_up_date']) : NULL; // ผู้ปฏิบัติหน้าที่ $counselor_first_name = isset($_POST['counselor_first_name']) ? trim($_POST['counselor_first_name']) : ''; $counselor_last_name = isset($_POST['counselor_last_name']) ? trim($_POST['counselor_last_name']) : ''; $counselor_position = isset($_POST['counselor_position']) ? trim($_POST['counselor_position']) : ''; $counselor_agency = isset($_POST['counselor_agency']) ? trim($_POST['counselor_agency']) : ''; $counselor_phone = isset($_POST['counselor_phone']) ? trim($_POST['counselor_phone']) : ''; // ข้อมูล session $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $schoolID = isset($_POST['schoolID']) ? trim($_POST['schoolID']) : ''; $leader = isset($_POST['leader']) ? trim($_POST['leader']) : ''; // Validation - เหลือเฉพาะ ID if($id <= 0) { $res['message']='ID ไม่ถูกต้อง'; echo json_encode($res); exit; } // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'individual_counseling_id' => $individual_counseling_id, 'service_request_number' => $service_request_number, 'service_request_date' => $service_request_date, 'service_request_details' => $service_request_details, 'education_issues' => $education_issues, 'education_other' => $education_other, 'career_issues' => $career_issues, 'career_other' => $career_other, 'personal_issues' => $personal_issues, 'personal_other' => $personal_other, 'recommendations' => $recommendations, 'service_success_id' => $service_success_id, 'service_failure_reason' => $service_failure_reason, 'referral_services' => $referral_services, 'referral_other' => $referral_other, 'follow_up_id' => $follow_up_id, 'follow_up_date' => $follow_up_date, 'counselor_first_name' => $counselor_first_name, 'counselor_last_name' => $counselor_last_name, 'counselor_position' => $counselor_position, 'counselor_agency' => $counselor_agency, 'counselor_phone' => $counselor_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_service SET individual_counseling_id=?, service_request_number=?, service_request_date=?, service_request_details=?, education_issues=?, education_other=?, career_issues=?, career_other=?, personal_issues=?, personal_other=?, recommendations=?, service_success_id=?, service_failure_reason=?, referral_services=?, referral_other=?, follow_up_id=?, follow_up_date=?, counselor_first_name=?, counselor_last_name=?, counselor_position=?, counselor_agency=?, counselor_phone=?, form_data_json=?, username=?, schoolID=?, leader=?, updated_at=CURRENT_TIMESTAMP WHERE id=?"); if($stmt->execute(array($individual_counseling_id, $service_request_number, $service_request_date, $service_request_details, $education_issues, $education_other, $career_issues, $career_other, $personal_issues, $personal_other, $recommendations, $service_success_id, $service_failure_reason, $referral_services, $referral_other, $follow_up_id, $follow_up_date, $counselor_first_name, $counselor_last_name, $counselor_position, $counselor_agency, $counselor_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_service 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_service 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; } ?>