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,
|
2025-12-18 11:03:25 +05:30
|
|
|
String? clientId,
|
2025-12-15 11:10:52 +05:30
|
|
|
}) async {
|
|
|
|
|
final form = FormData();
|
|
|
|
|
|
|
|
|
|
if (message != null) form.fields.add(MapEntry('message', message));
|
2025-12-18 11:03:25 +05:30
|
|
|
if (clientId != null) form.fields.add(MapEntry('client_id', clientId));
|
2025-12-15 11:10:52 +05:30
|
|
|
|
|
|
|
|
await dio.post('/user/chat/send/$ticketId', data: form);
|
|
|
|
|
}
|
2025-12-16 10:24:16 +05:30
|
|
|
|
2025-12-18 11:03:25 +05:30
|
|
|
|
2025-12-16 10:24:16 +05:30
|
|
|
// ---------------------------
|
|
|
|
|
// SEND FILE (image/video/pdf/excel)
|
|
|
|
|
// ---------------------------
|
2025-12-18 11:03:25 +05:30
|
|
|
Future<Map<String, dynamic>> sendFile(
|
|
|
|
|
int ticketId,
|
|
|
|
|
File file, {
|
|
|
|
|
required Function(double) onProgress,
|
|
|
|
|
}) async {
|
2025-12-16 10:24:16 +05:30
|
|
|
final formData = FormData.fromMap({
|
|
|
|
|
'file': await MultipartFile.fromFile(
|
|
|
|
|
file.path,
|
|
|
|
|
filename: file.path.split('/').last,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-18 11:03:25 +05:30
|
|
|
final res = await dio.post(
|
2025-12-16 10:24:16 +05:30
|
|
|
"/user/chat/send/$ticketId",
|
|
|
|
|
data: formData,
|
|
|
|
|
options: Options(
|
|
|
|
|
headers: {'Content-Type': 'multipart/form-data'},
|
|
|
|
|
),
|
2025-12-18 11:03:25 +05:30
|
|
|
onSendProgress: (sent, total) {
|
|
|
|
|
if (total > 0) {
|
|
|
|
|
onProgress(sent / total);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-12-16 10:24:16 +05:30
|
|
|
);
|
2025-12-18 11:03:25 +05:30
|
|
|
|
|
|
|
|
return Map<String, dynamic>.from(res.data['message']);
|
2025-12-16 10:24:16 +05:30
|
|
|
}
|
2025-12-18 11:03:25 +05:30
|
|
|
|
2025-12-15 11:10:52 +05:30
|
|
|
}
|