2025-12-16 10:24:16 +05:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import '../config/api_config.dart';
|
|
|
|
|
|
|
|
|
|
class ChatService {
|
|
|
|
|
final Dio dio;
|
|
|
|
|
|
|
|
|
|
ChatService(this.dio);
|
|
|
|
|
|
|
|
|
|
/// Start chat / get ticket
|
|
|
|
|
Future<Map<String, dynamic>> startChat() async {
|
|
|
|
|
final res = await dio.get('/user/chat/start');
|
|
|
|
|
return Map<String, dynamic>.from(res.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get all messages
|
|
|
|
|
Future<List<dynamic>> getMessages(int ticketId) async {
|
|
|
|
|
final res = await dio.get('/user/chat/messages/$ticketId');
|
|
|
|
|
return res.data['messages'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send message (text or file)
|
|
|
|
|
Future<void> sendMessage(
|
|
|
|
|
int ticketId, {
|
|
|
|
|
String? message,
|
|
|
|
|
String? filePath,
|
|
|
|
|
}) async {
|
|
|
|
|
final form = FormData();
|
|
|
|
|
|
|
|
|
|
if (message != null) form.fields.add(MapEntry('message', message));
|
|
|
|
|
if (filePath != null) {
|
|
|
|
|
form.files.add(
|
|
|
|
|
MapEntry(
|
|
|
|
|
'file',
|
|
|
|
|
await MultipartFile.fromFile(filePath),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await dio.post('/user/chat/send/$ticketId', data: form);
|
|
|
|
|
}
|
2025-12-16 10:24:16 +05:30
|
|
|
|
|
|
|
|
// ---------------------------
|
|
|
|
|
// SEND FILE (image/video/pdf/excel)
|
|
|
|
|
// ---------------------------
|
|
|
|
|
Future<void> sendFile(int ticketId, File file) async {
|
|
|
|
|
final formData = FormData.fromMap({
|
|
|
|
|
'file': await MultipartFile.fromFile(
|
|
|
|
|
file.path,
|
|
|
|
|
filename: file.path.split('/').last,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await dio.post(
|
|
|
|
|
"/user/chat/send/$ticketId",
|
|
|
|
|
|
|
|
|
|
data: formData,
|
|
|
|
|
options: Options(
|
|
|
|
|
headers: {'Content-Type': 'multipart/form-data'},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-12-15 11:10:52 +05:30
|
|
|
}
|