'error','message'=>'ไม่สามารถดำเนินการได้'); switch($action){ case 'fetch': $stmt = $pdo->prepare(" SELECT s.id, s.title, s.status, s.typeGiveHelpId, t.title AS typeTitle FROM typegiveHelpSub s LEFT JOIN typeGiveHelp t ON s.typeGiveHelpId = t.id ORDER BY s.id DESC "); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode(array("data"=>$data)); exit; case 'add': $title = isset($_POST['title']) ? trim($_POST['title']) : ''; $typeId = isset($_POST['typeId']) ? intval($_POST['typeId']) : 0; if($title===''){ $res['message']='กรุณากรอกการให้ความช่วยเหลือ (พมจ.)'; echo json_encode($res); exit; } if($typeId<=0){ $res['message']='กรุณาเลือกประเภทการช่วยเหลือ'; echo json_encode($res); exit; } $stmt = $pdo->prepare("INSERT INTO typegiveHelpSub (title,status,typeGiveHelpId) VALUES (?,1,?)"); if($stmt->execute(array($title,$typeId))){ $res['status']='success'; $res['message']='เพิ่มสำเร็จ'; } echo json_encode($res); exit; case 'edit': $id = isset($_POST['id']) ? intval($_POST['id']) : 0; $title = isset($_POST['title']) ? trim($_POST['title']) : ''; $typeId = isset($_POST['typeId']) ? intval($_POST['typeId']) : 0; if ($id <= 0) { $res['message'] = 'ไม่พบรหัสข้อมูลที่ต้องการแก้ไข'; echo json_encode($res); exit; } if ($title === '') { $res['message'] = 'กรุณากรอกชื่อเรื่อง'; echo json_encode($res); exit; } if ($typeId <= 0) { $res['message'] = 'กรุณาเลือกประเภทการช่วยเหลือ'; echo json_encode($res); exit; } $stmt = $pdo->prepare("UPDATE typegiveHelpSub SET title=?, typeGiveHelpId=? WHERE id=?"); if ($stmt->execute(array($title, $typeId, $id))) { $res['status'] = 'success'; $res['message'] = 'แก้ไขสำเร็จ'; } else { $res['message'] = 'เกิดข้อผิดพลาดในการบันทึก'; } echo json_encode($res); exit; case 'delete': $id = intval($_POST['id']); if($id<=0){ echo json_encode($res); exit; } $stmt = $pdo->prepare("DELETE FROM typegiveHelpSub WHERE id=?"); if($stmt->execute(array($id))){ $res['status']='success'; $res['message']='ลบสำเร็จ'; } echo json_encode($res); exit; case 'toggle': $id = intval($_POST['id']); $status = intval($_POST['status']); if($id<=0){ echo json_encode($res); exit; } $stmt = $pdo->prepare("UPDATE typegiveHelpSub SET status=? WHERE id=?"); if($stmt->execute(array($status,$id))){ $res['status']='success'; $res['message']='อัปเดตสถานะสำเร็จ'; } echo json_encode($res); exit; // เพิ่ม endpoint สำหรับดึง typeGiveHelp ไปใช้ใน select case 'getTypeList': $stmt = $pdo->prepare("SELECT id,title FROM typeGiveHelp ORDER BY title ASC"); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($rows); exit; default: echo json_encode($res); exit; } ?>