'error','message'=>'ไม่สามารถดำเนินการได้'); // Debug logs removed for production switch($action){ case 'fetch': try { $stmt = $pdo->prepare("SELECT g.*, u.name as user_name FROM group_guidance g LEFT JOIN users u ON g.user_id = u.id ORDER BY g.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': // Debug: แสดงข้อมูลที่ได้รับ error_log('POST data: ' . print_r($_POST, true)); $service_date = isset($_POST['service_date']) ? trim($_POST['service_date']) : ''; $service_location = isset($_POST['service_location']) ? trim($_POST['service_location']) : ''; $participant_count = isset($_POST['participant_count']) ? intval($_POST['participant_count']) : 0; $participant_type = isset($_POST['participant_type']) ? trim($_POST['participant_type']) : ''; $guidance_type = isset($_POST['guidance_type']) ? trim($_POST['guidance_type']) : ''; $activity_method = isset($_POST['activity_method']) ? trim($_POST['activity_method']) : ''; $objectives = isset($_POST['objectives']) ? trim($_POST['objectives']) : ''; $objectives_activities = isset($_POST['objectives_activities']) ? trim($_POST['objectives_activities']) : ''; $service_details = isset($_POST['service_details']) ? trim($_POST['service_details']) : ''; $consultant_name = isset($_POST['consultant_name']) ? trim($_POST['consultant_name']) : ''; $consultant_position = isset($_POST['consultant_position']) ? trim($_POST['consultant_position']) : ''; $consultant_phone = isset($_POST['consultant_phone']) ? trim($_POST['consultant_phone']) : ''; $goal_achieved = isset($_POST['goal_achieved']) ? trim($_POST['goal_achieved']) : ''; $failure_reason = isset($_POST['failure_reason']) ? trim($_POST['failure_reason']) : ''; // Debug: แสดงค่า participant_type error_log('participant_type received: "' . $participant_type . '"'); // New session data $username = isset($_POST['username']) ? trim($_POST['username']) : ''; $schoolID = isset($_POST['schoolID']) ? trim($_POST['schoolID']) : ''; $leader = isset($_POST['leader']) ? trim($_POST['leader']) : ''; // Validation if($service_date === '') { $res['message']='กรุณาเลือกวันที่ให้บริการ'; echo json_encode($res); exit; } if($service_location === '') { $res['message']='กรุณากรอกสถานที่ให้บริการ'; echo json_encode($res); exit; } if($participant_count <= 0) { $res['message']='กรุณากรอกจำนวนผู้เข้าร่วม'; echo json_encode($res); exit; } if($consultant_name === '') { $res['message']='กรุณากรอกชื่อที่ปรึกษา'; echo json_encode($res); exit; } // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'service_date' => $service_date, 'service_location' => $service_location, 'participant_count' => $participant_count, 'participant_type' => $participant_type, 'guidance_type' => $guidance_type, 'activity_method' => $activity_method, 'objectives' => $objectives, 'objectives_activities' => $objectives_activities, 'service_details' => $service_details, 'consultant_name' => $consultant_name, 'consultant_position' => $consultant_position, 'consultant_phone' => $consultant_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 group_guidance (service_date, service_location, participant_count, participant_type, guidance_type, activity_method, objectives, objectives_activities, service_details, consultant_name, consultant_position, consultant_phone, goal_achieved, failure_reason, form_data_json, username, schoolID, leader) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); if($stmt->execute(array($service_date, $service_location, $participant_count, $participant_type, $guidance_type, $activity_method, $objectives, $objectives_activities, $service_details, $consultant_name, $consultant_position, $consultant_phone, $goal_achieved, $failure_reason, $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; $service_date = isset($_POST['service_date']) ? trim($_POST['service_date']) : ''; $service_location = isset($_POST['service_location']) ? trim($_POST['service_location']) : ''; $participant_count = isset($_POST['participant_count']) ? intval($_POST['participant_count']) : 0; $participant_type = isset($_POST['participant_type']) ? trim($_POST['participant_type']) : ''; $guidance_type = isset($_POST['guidance_type']) ? trim($_POST['guidance_type']) : ''; $activity_method = isset($_POST['activity_method']) ? trim($_POST['activity_method']) : ''; $objectives = isset($_POST['objectives']) ? trim($_POST['objectives']) : ''; $objectives_activities = isset($_POST['objectives_activities']) ? trim($_POST['objectives_activities']) : ''; $service_details = isset($_POST['service_details']) ? trim($_POST['service_details']) : ''; $consultant_name = isset($_POST['consultant_name']) ? trim($_POST['consultant_name']) : ''; $consultant_position = isset($_POST['consultant_position']) ? trim($_POST['consultant_position']) : ''; $consultant_phone = isset($_POST['consultant_phone']) ? trim($_POST['consultant_phone']) : ''; $goal_achieved = isset($_POST['goal_achieved']) ? trim($_POST['goal_achieved']) : ''; $failure_reason = isset($_POST['failure_reason']) ? trim($_POST['failure_reason']) : ''; // New session data $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($service_date === '') { $res['message']='กรุณาเลือกวันที่ให้บริการ'; echo json_encode($res); exit; } if($service_location === '') { $res['message']='กรุณากรอกสถานที่ให้บริการ'; echo json_encode($res); exit; } if($consultant_name === '') { $res['message']='กรุณากรอกชื่อที่ปรึกษา'; echo json_encode($res); exit; } // สร้าง JSON สำหรับเก็บข้อมูลฟอร์มทั้งหมด $form_data = array( 'service_date' => $service_date, 'service_location' => $service_location, 'participant_count' => $participant_count, 'participant_type' => $participant_type, 'guidance_type' => $guidance_type, 'activity_method' => $activity_method, 'objectives' => $objectives, 'objectives_activities' => $objectives_activities, 'service_details' => $service_details, 'consultant_name' => $consultant_name, 'consultant_position' => $consultant_position, 'consultant_phone' => $consultant_phone, 'goal_achieved' => $goal_achieved, 'failure_reason' => $failure_reason, '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 group_guidance SET service_date=?, service_location=?, participant_count=?, participant_type=?, guidance_type=?, activity_method=?, objectives=?, objectives_activities=?, service_details=?, consultant_name=?, consultant_position=?, consultant_phone=?, goal_achieved=?, failure_reason=?, form_data_json=?, updated_at=CURRENT_TIMESTAMP, username=?, schoolID=?, leader=? WHERE id=?"); if($stmt->execute(array($service_date, $service_location, $participant_count, $participant_type, $guidance_type, $activity_method, $objectives, $objectives_activities, $service_details, $consultant_name, $consultant_position, $consultant_phone, $goal_achieved, $failure_reason, $form_data_json, $username, $schoolID, $leader, $id))){ $res['status']='success'; $res['message']='แก้ไขข้อมูลสำเร็จ'; } } catch (PDOException $e) { error_log('PDO Error in edit mode: ' . $e->getMessage()); error_log('Parameters: ' . print_r(array($service_date, $service_location, $participant_count, $participant_type, $guidance_type, $objectives, $objectives_activities, $service_details, $consultant_name, $consultant_position, $consultant_phone, $goal_achieved, $failure_reason, $form_data_json, $username, $schoolID, $leader, $id), true)); $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 group_guidance 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 group_guidance 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; } ?>