policy list page new design
This commit is contained in:
parent
5ab200618c
commit
d4706f1dd0
@ -1,9 +1,11 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
|
||||
import '../../customAppBar/base_layout.dart';
|
||||
import '../../customAppBar/toastHelper.dart';
|
||||
@ -42,6 +44,7 @@ class _ClaimsOverviewDashboardState extends State<ClaimsOverviewDashboard>
|
||||
bool _isLoading = true;
|
||||
bool _isRefreshing = false;
|
||||
bool _isExportingPdf = false;
|
||||
bool _isExportingExcel = false;
|
||||
bool _sessionChecked = false;
|
||||
int _replayToken = 0;
|
||||
int _loadGeneration = 0;
|
||||
@ -455,7 +458,7 @@ class _ClaimsOverviewDashboardState extends State<ClaimsOverviewDashboard>
|
||||
}
|
||||
|
||||
Future<void> _downloadChartsPdf() async {
|
||||
if (_isExportingPdf || _isLoading) return;
|
||||
if (_isExportingPdf || _isExportingExcel || _isLoading) return;
|
||||
|
||||
setState(() => _isExportingPdf = true);
|
||||
|
||||
@ -563,6 +566,106 @@ class _ClaimsOverviewDashboardState extends State<ClaimsOverviewDashboard>
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _downloadExcel() async {
|
||||
if (_isExportingExcel || _isExportingPdf || _isLoading) return;
|
||||
final policyId = _selectedPolicyId;
|
||||
if (policyId == null || policyId.isEmpty) {
|
||||
if (!mounted) return;
|
||||
ToastHelper.showErrorToast(context, 'Select a policy to download Excel');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _isExportingExcel = true);
|
||||
try {
|
||||
final response = await _apiService.downloadClaimsCollectionReportExcel(
|
||||
clientPolicyId: policyId,
|
||||
);
|
||||
|
||||
final apiMessage = _excelDownloadErrorMessage(response);
|
||||
if (apiMessage != null) {
|
||||
if (!mounted) return;
|
||||
ToastHelper.showErrorToast(context, apiMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode != 200 || response.bodyBytes.isEmpty) {
|
||||
throw Exception('Download failed (${response.statusCode})');
|
||||
}
|
||||
|
||||
final apiContentType = response.headers['content-type'] ?? '';
|
||||
final contentDisposition =
|
||||
response.headers['content-disposition'] ?? '';
|
||||
final excelContentType =
|
||||
apiContentType.contains('spreadsheetml') ||
|
||||
apiContentType.contains('ms-excel')
|
||||
? apiContentType
|
||||
: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
final utf8FileNameMatch = RegExp(
|
||||
"filename\\*=UTF-8''([^;]+)",
|
||||
caseSensitive: false,
|
||||
).firstMatch(contentDisposition);
|
||||
final plainFileNameMatch = RegExp(
|
||||
'filename="?([^";]+)"?',
|
||||
caseSensitive: false,
|
||||
).firstMatch(contentDisposition);
|
||||
final rawFileName =
|
||||
utf8FileNameMatch?.group(1) ?? plainFileNameMatch?.group(1);
|
||||
final fileName = rawFileName != null && rawFileName.trim().isNotEmpty
|
||||
? Uri.decodeComponent(rawFileName.trim())
|
||||
: 'claims_collection_report_$policyId.xlsx';
|
||||
|
||||
final blob = html.Blob([response.bodyBytes], excelContentType);
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
final anchor = html.AnchorElement(href: url)
|
||||
..setAttribute('download', fileName)
|
||||
..click();
|
||||
html.Url.revokeObjectUrl(url);
|
||||
|
||||
await _logHrActivity('export_hr_dashboard_data_excel');
|
||||
if (!mounted) return;
|
||||
ToastHelper.showSuccessToast(
|
||||
context,
|
||||
'Claims Overview Excel downloaded',
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ToastHelper.showErrorToast(
|
||||
context,
|
||||
'Could not download Excel. Please try again.',
|
||||
);
|
||||
logDebug('Excel download failed: $e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _isExportingExcel = false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns API error message when the Excel endpoint responds with JSON
|
||||
/// `{ "status": false, "message": "..." }` instead of a file.
|
||||
String? _excelDownloadErrorMessage(http.Response response) {
|
||||
final contentType = response.headers['content-type'] ?? '';
|
||||
final body = response.body.trim();
|
||||
final looksLikeJson = contentType.contains('application/json') ||
|
||||
body.startsWith('{') ||
|
||||
body.startsWith('[');
|
||||
if (!looksLikeJson || body.isEmpty) return null;
|
||||
|
||||
try {
|
||||
final decoded = jsonDecode(body);
|
||||
if (decoded is! Map) return null;
|
||||
final status = decoded['status'];
|
||||
final isFailure = status == false ||
|
||||
status == 0 ||
|
||||
status?.toString().toLowerCase() == 'false' ||
|
||||
status?.toString().toLowerCase() == 'error';
|
||||
if (!isFailure) return null;
|
||||
final message = decoded['message']?.toString().trim();
|
||||
if (message != null && message.isNotEmpty) return message;
|
||||
return 'Could not download Excel.';
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.removeListener(_onTabChanged);
|
||||
@ -823,23 +926,55 @@ class _ClaimsOverviewDashboardState extends State<ClaimsOverviewDashboard>
|
||||
}
|
||||
|
||||
Widget _headerActions() {
|
||||
final isBusy =
|
||||
_isRefreshing || _isExportingPdf || _isExportingExcel || _isLoading;
|
||||
final isExporting = _isExportingPdf || _isExportingExcel;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(left: 4),
|
||||
child: IconButton(
|
||||
tooltip: 'Download all charts as PDF',
|
||||
icon: _isExportingPdf
|
||||
child: PopupMenuButton<String>(
|
||||
tooltip: 'Download',
|
||||
enabled: !isBusy,
|
||||
offset: const Offset(0, 36),
|
||||
onSelected: (value) {
|
||||
if (value == 'pdf') {
|
||||
_downloadChartsPdf();
|
||||
} else if (value == 'excel') {
|
||||
_downloadExcel();
|
||||
}
|
||||
},
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem(
|
||||
value: 'pdf',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.picture_as_pdf_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Text('PDF', style: GoogleFonts.poppins(fontSize: 13)),
|
||||
],
|
||||
),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'excel',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.table_chart_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Text('Excel', style: GoogleFonts.poppins(fontSize: 13)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
icon: isExporting
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.download_outlined, size: 20),
|
||||
style: IconButton.styleFrom(
|
||||
foregroundColor: ClaimsOverviewTheme.textSecondary,
|
||||
),
|
||||
onPressed: (_isRefreshing || _isExportingPdf || _isLoading)
|
||||
? null
|
||||
: _downloadChartsPdf,
|
||||
: const Icon(
|
||||
Icons.download_outlined,
|
||||
size: 20,
|
||||
color: ClaimsOverviewTheme.textSecondary,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -750,6 +750,55 @@ class ApiService {
|
||||
return _makeGetRequest(url, headers);
|
||||
}
|
||||
|
||||
/// Excel export for employee list
|
||||
/// (`downloadEmployeeListExcel?client_id=&policy_id=&branch_id=`).
|
||||
Future<http.Response> downloadEmployeeListExcel({
|
||||
required String clientId,
|
||||
required String policyId,
|
||||
required String branchId,
|
||||
required bool isPost,
|
||||
String? token,
|
||||
}) async {
|
||||
final authToken = token ?? _token;
|
||||
if (authToken == null || authToken.isEmpty) {
|
||||
await _initializeToken();
|
||||
}
|
||||
|
||||
final baseUrl = isPost ? Environment.apiUrlPost : Environment.apiUrl;
|
||||
final url = Uri.parse('${baseUrl}downloadEmployeeListExcel').replace(
|
||||
queryParameters: {
|
||||
'client_id': clientId,
|
||||
'policy_id': policyId,
|
||||
'branch_id': branchId,
|
||||
},
|
||||
);
|
||||
final headers = {
|
||||
'Authorization': 'Bearer ${token ?? _token ?? ''}',
|
||||
'APP-SIGNATURE':
|
||||
'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y',
|
||||
};
|
||||
return http.get(url, headers: headers);
|
||||
}
|
||||
|
||||
/// Excel export for claims collection report (`claims-collection-report/download-excel`).
|
||||
Future<http.Response> downloadClaimsCollectionReportExcel({
|
||||
required String clientPolicyId,
|
||||
}) async {
|
||||
if (_token == null) await _initializeToken();
|
||||
|
||||
final url = Uri.parse(
|
||||
'${Environment.apiUrlPost}claims-collection-report/download-excel',
|
||||
).replace(
|
||||
queryParameters: {'client_policy_id': clientPolicyId},
|
||||
);
|
||||
final headers = {
|
||||
'Authorization': 'Bearer ${_token ?? ''}',
|
||||
'APP-SIGNATURE':
|
||||
'nhance-2025-signature-T8f6gV9k5fK2wf5V5c4vId3b0J8z6cAm2x8Y',
|
||||
};
|
||||
return http.get(url, headers: headers);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> postHrTpaDashboard(params, token) async {
|
||||
final url = Uri.parse('${Environment.apiUrlPost}getHrTpaDashboard');
|
||||
|
||||
@ -1031,13 +1080,26 @@ class ApiService {
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getEmployeeAndDependenceToApi(
|
||||
String clintID, getPolicyNo, String empRefId, String token) async {
|
||||
String clintID, getPolicyNo, String empRefId, String token,
|
||||
{dynamic cardData, String? searchKey}) async {
|
||||
logDebug(_hrtoken);
|
||||
if (token == null) {
|
||||
await _initializeToken();
|
||||
}
|
||||
final params = <String, String>{
|
||||
'client_id': clintID,
|
||||
'client_policy_id': '$getPolicyNo',
|
||||
'client_branch_id': empRefId,
|
||||
};
|
||||
if (cardData != null) {
|
||||
params['card_data'] = cardData.toString();
|
||||
}
|
||||
if (searchKey != null && searchKey.trim().isNotEmpty) {
|
||||
params['search'] = searchKey.trim();
|
||||
}
|
||||
final url = Uri.parse(
|
||||
'${Environment.apiUrlPost}getEmployeeAndDependenceByClientId?client_id=$clintID&client_policy_id=$getPolicyNo&client_branch_id=$empRefId');
|
||||
'${Environment.apiUrlPost}getEmployeeAndDependenceByClientId')
|
||||
.replace(queryParameters: params);
|
||||
final headers = {
|
||||
'Authorization': 'Bearer $token' ?? '',
|
||||
};
|
||||
@ -1272,13 +1334,26 @@ class ApiService {
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getEmployeeAndDependenceToApiPre(
|
||||
String clintID, String getPolicyNo, String empRefId, String token) async {
|
||||
String clintID, String getPolicyNo, String empRefId, String token,
|
||||
{dynamic cardData, String? searchKey}) async {
|
||||
logDebug(_hrtoken);
|
||||
if (token == null) {
|
||||
await _initializeToken();
|
||||
}
|
||||
final url = Uri.parse(
|
||||
'${Environment.apiUrl}getEmployeeAndDependenceByClientId?client_id=$clintID&client_policy_id=$getPolicyNo&client_branch_id=$empRefId');
|
||||
final params = <String, String>{
|
||||
'client_id': clintID,
|
||||
'client_policy_id': getPolicyNo,
|
||||
'client_branch_id': empRefId,
|
||||
};
|
||||
if (cardData != null) {
|
||||
params['card_data'] = cardData.toString();
|
||||
}
|
||||
if (searchKey != null && searchKey.trim().isNotEmpty) {
|
||||
params['search'] = searchKey.trim();
|
||||
}
|
||||
final url =
|
||||
Uri.parse('${Environment.apiUrl}getEmployeeAndDependenceByClientId')
|
||||
.replace(queryParameters: params);
|
||||
final headers = {
|
||||
'Authorization': 'Bearer $token' ?? '',
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user