false, 'ERROR' => 'BusinessID is required']); } try { $taskTypeID = (int) ($data['TaskTypeID'] ?? 0); if ($taskTypeID > 0) { // Service bell task $servicePointID = (int) ($data['ServicePointID'] ?? 0); $orderID = (int) ($data['OrderID'] ?? 0); $userID = (int) ($data['UserID'] ?? 0); $message = $data['Message'] ?? ''; // Get task type name for display $taskTypeQuery = queryOne("SELECT Name FROM tt_TaskTypes WHERE ID = ?", [$taskTypeID]); $taskTitle = $message; if ($taskTypeQuery && !empty(trim($taskTypeQuery['Name']))) { $taskTitle = $taskTypeQuery['Name']; } $taskDetails = $message; queryTimed(" INSERT INTO Tasks ( BusinessID, ServicePointID, TaskTypeID, OrderID, UserID, Title, Details, CreatedOn, ClaimedByUserID ) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 0) ", [ $businessID, $servicePointID, $taskTypeID, $orderID, $userID, $taskTitle, $taskDetails, ]); } else { // Legacy photo task $itemID = (int) ($data['ItemID'] ?? 0); $taskType = $data['TaskType'] ?? 'employee_photo'; $instructions = $data['Instructions'] ?? ''; $pytReward = (int) ($data['PYTReward'] ?? 0); // Get item info if itemID provided $itemName = ''; if ($itemID > 0) { $itemQuery = queryOne("SELECT Name FROM Items WHERE ID = ?", [$itemID]); if ($itemQuery) $itemName = $itemQuery['Name']; } // Create task description switch ($taskType) { case 'employee_photo': $taskDescription = "Take a photo of: $itemName"; break; case 'user_photo': $taskDescription = "Submit a photo of $itemName to earn $pytReward PYT"; break; default: $taskDescription = $instructions; } queryTimed(" INSERT INTO Tasks ( BusinessID, TaskItemID, TaskType, TaskDescription, TaskInstructions, TaskPYTReward, TaskStatus, CreatedOn ) VALUES (?, ?, ?, ?, ?, ?, 'pending', NOW()) ", [$businessID, $itemID, $taskType, $taskDescription, $instructions, $pytReward]); } $taskID = lastInsertId(); jsonResponse([ 'OK' => true, 'TASK_ID' => (int) $taskID, 'MESSAGE' => 'Task created successfully', ]); } catch (Exception $e) { jsonResponse(['OK' => false, 'ERROR' => $e->getMessage(), 'DETAIL' => '']); }