|
@@ -1,21 +1,23 @@
|
|
|
|
|
+import 'dart:typed_data';
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
|
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
import '../../shared/widgets/nav_bar_config.dart';
|
|
import '../../shared/widgets/nav_bar_config.dart';
|
|
|
|
|
+import '../../shared/widgets/loading_dialog.dart';
|
|
|
import '../../core/utils/date_utils.dart' as du;
|
|
import '../../core/utils/date_utils.dart' as du;
|
|
|
import '../../shared/widgets/form_section.dart';
|
|
import '../../shared/widgets/form_section.dart';
|
|
|
import '../../shared/widgets/form_field_row.dart';
|
|
import '../../shared/widgets/form_field_row.dart';
|
|
|
import 'expense_model.dart';
|
|
import 'expense_model.dart';
|
|
|
import '../../core/i18n/app_localizations.dart';
|
|
import '../../core/i18n/app_localizations.dart';
|
|
|
-import '../../shared/widgets/loading_dialog.dart';
|
|
|
|
|
import '../../shared/models/bill_attachment.dart';
|
|
import '../../shared/models/bill_attachment.dart';
|
|
|
import '../../core/theme/app_colors.dart';
|
|
import '../../core/theme/app_colors.dart';
|
|
|
import '../../core/theme/app_colors_extension.dart';
|
|
import '../../core/theme/app_colors_extension.dart';
|
|
|
import 'expense_api.dart';
|
|
import 'expense_api.dart';
|
|
|
-import '../../shared/widgets/approval_actions.dart';
|
|
|
|
|
-import '../../shared/widgets/approval_timeline.dart';
|
|
|
|
|
-import '../../shared/models/approval_status.dart';
|
|
|
|
|
|
|
+import 'dart:io';
|
|
|
|
|
+import 'package:path_provider/path_provider.dart';
|
|
|
|
|
+import 'package:open_filex/open_filex.dart';
|
|
|
|
|
+import '../expense_apply/widgets/attachment_preview_page.dart';
|
|
|
|
|
|
|
|
class ExpenseDetailPage extends ConsumerStatefulWidget {
|
|
class ExpenseDetailPage extends ConsumerStatefulWidget {
|
|
|
final String billNo;
|
|
final String billNo;
|
|
@@ -31,9 +33,6 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
ExpenseModel? _expense;
|
|
ExpenseModel? _expense;
|
|
|
List<BillAttachment> _attachments = [];
|
|
List<BillAttachment> _attachments = [];
|
|
|
bool _attachAvailable = false;
|
|
bool _attachAvailable = false;
|
|
|
- List<ApprovalRecord> _timelineRecords = [];
|
|
|
|
|
- List<String> _timelineChain = [];
|
|
|
|
|
- String _timelineCurrentApproverId = '';
|
|
|
|
|
bool _isLoading = true;
|
|
bool _isLoading = true;
|
|
|
String? _error;
|
|
String? _error;
|
|
|
|
|
|
|
@@ -50,9 +49,15 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
super.dispose();
|
|
super.dispose();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ bool _openingFile = false;
|
|
|
|
|
+
|
|
|
@override
|
|
@override
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
if (state == AppLifecycleState.resumed) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
|
|
|
+ if (_openingFile) {
|
|
|
|
|
+ _openingFile = false;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
_attachments = [];
|
|
_attachments = [];
|
|
|
_attachAvailable = false;
|
|
_attachAvailable = false;
|
|
|
_loadData();
|
|
_loadData();
|
|
@@ -70,40 +75,18 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
final expense = await api.fetchDetail(widget.billNo);
|
|
final expense = await api.fetchDetail(widget.billNo);
|
|
|
setState(() => _expense = expense);
|
|
setState(() => _expense = expense);
|
|
|
|
|
|
|
|
- // 2. 加载审批时间线(非致命——失败时回退到 expense 模型数据)
|
|
|
|
|
|
|
+ // 2. 加载附件(非致命)
|
|
|
try {
|
|
try {
|
|
|
- final timelineData = await api.fetchApprovalTimeline('BX', widget.billNo);
|
|
|
|
|
- setState(() {
|
|
|
|
|
- _timelineRecords = (timelineData['records'] as List<dynamic>?)
|
|
|
|
|
- ?.map((e) => ApprovalRecord.fromJson(e as Map<String, dynamic>))
|
|
|
|
|
- .toList() ??
|
|
|
|
|
- [];
|
|
|
|
|
- _timelineChain = (timelineData['chain'] as List<dynamic>?)
|
|
|
|
|
- ?.map((e) => e as String)
|
|
|
|
|
- .toList() ??
|
|
|
|
|
- [];
|
|
|
|
|
- _timelineCurrentApproverId =
|
|
|
|
|
- (timelineData['currentApproverId'] as String?) ?? '';
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ _attachAvailable = await api.checkAttachHealth();
|
|
|
} catch (_) {
|
|
} catch (_) {
|
|
|
- // 回退到 expense 自带的审批数据
|
|
|
|
|
- setState(() {
|
|
|
|
|
- _timelineRecords = expense.approvalRecords;
|
|
|
|
|
- _timelineChain = expense.approvalChain;
|
|
|
|
|
- _timelineCurrentApproverId = expense.currentApproverId;
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ _attachAvailable = false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 3. 加载附件(非致命)
|
|
|
|
|
- try {
|
|
|
|
|
- if (mounted) setState(() => _attachAvailable = false);
|
|
|
|
|
- _attachAvailable = await api.checkAttachHealth();
|
|
|
|
|
- if (_attachAvailable) {
|
|
|
|
|
|
|
+ if (_attachAvailable) {
|
|
|
|
|
+ try {
|
|
|
_attachments = await api.getAttachments('BX', widget.billNo);
|
|
_attachments = await api.getAttachments('BX', widget.billNo);
|
|
|
|
|
+ } catch (_) {
|
|
|
|
|
+ _attachments = [];
|
|
|
}
|
|
}
|
|
|
- } catch (_) {
|
|
|
|
|
- _attachAvailable = false;
|
|
|
|
|
- _attachments = [];
|
|
|
|
|
}
|
|
}
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
setState(() => _error = e.toString());
|
|
setState(() => _error = e.toString());
|
|
@@ -157,118 +140,23 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
|
|
|
|
|
final expense = _expense!;
|
|
final expense = _expense!;
|
|
|
|
|
|
|
|
- return Column(
|
|
|
|
|
- children: [
|
|
|
|
|
- Expanded(
|
|
|
|
|
- child: SingleChildScrollView(
|
|
|
|
|
- physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
|
- padding: const EdgeInsets.all(16),
|
|
|
|
|
- child: Column(
|
|
|
|
|
- children: [
|
|
|
|
|
- _buildBasicInfoSection(expense, l10n, colors),
|
|
|
|
|
- const SizedBox(height: 16),
|
|
|
|
|
- _buildExpenseDetailSection(expense, l10n, colors),
|
|
|
|
|
- const SizedBox(height: 16),
|
|
|
|
|
- _buildAttachmentSection(l10n, colors),
|
|
|
|
|
- const SizedBox(height: 16),
|
|
|
|
|
- _buildApprovalSection(l10n, colors),
|
|
|
|
|
- ],
|
|
|
|
|
- ),
|
|
|
|
|
- ),
|
|
|
|
|
- ),
|
|
|
|
|
- ApprovalActions(
|
|
|
|
|
- queryId: widget.queryId,
|
|
|
|
|
- onApprove: () => _handleApprove(),
|
|
|
|
|
- onReject: () => _handleReject(),
|
|
|
|
|
- onReverseAudit: () => _handleReverseAudit(),
|
|
|
|
|
- ),
|
|
|
|
|
- ],
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // ── 审核操作 handler ──
|
|
|
|
|
-
|
|
|
|
|
- void _handleApprove() async {
|
|
|
|
|
- final l10n = AppLocalizations.of(context);
|
|
|
|
|
- final rem = await _showOpinionDialog(
|
|
|
|
|
- title: l10n.get('confirmApprove'),
|
|
|
|
|
- hint: l10n.get('approvalComment'),
|
|
|
|
|
- );
|
|
|
|
|
- if (rem == null || !mounted) return;
|
|
|
|
|
- await _doAudit('approve', rem: rem);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void _handleReject() async {
|
|
|
|
|
- final l10n = AppLocalizations.of(context);
|
|
|
|
|
- final rem = await _showOpinionDialog(
|
|
|
|
|
- title: l10n.get('confirmReject'),
|
|
|
|
|
- hint: l10n.get('rejectReason'),
|
|
|
|
|
- );
|
|
|
|
|
- if (rem == null || !mounted) return;
|
|
|
|
|
- await _doAudit('reject', rem: rem);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- void _handleReverseAudit() async {
|
|
|
|
|
- final l10n = AppLocalizations.of(context);
|
|
|
|
|
- final confirmed = await showDialog<bool>(
|
|
|
|
|
- context: context,
|
|
|
|
|
- builder: (ctx) => TDAlertDialog(
|
|
|
|
|
- title: l10n.get('withdrawConfirm'),
|
|
|
|
|
- content: l10n.get('withdrawConfirmTip'),
|
|
|
|
|
- leftBtn: TDDialogButtonOptions(title: l10n.get('cancel'), action: () => Navigator.pop(ctx, false)),
|
|
|
|
|
- rightBtn: TDDialogButtonOptions(title: l10n.get('confirm'), action: () => Navigator.pop(ctx, true)),
|
|
|
|
|
- ),
|
|
|
|
|
- );
|
|
|
|
|
- if (confirmed != true || !mounted) return;
|
|
|
|
|
- final rem = await _showOpinionDialog(
|
|
|
|
|
- title: l10n.get('withdrawConfirm'),
|
|
|
|
|
- hint: l10n.get('approvalComment'),
|
|
|
|
|
- );
|
|
|
|
|
- if (rem == null || !mounted) return;
|
|
|
|
|
- await _doAudit('reverseAudit', rem: rem);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /// 弹出审批意见输入框,返回 null 表示取消
|
|
|
|
|
- Future<String?> _showOpinionDialog({
|
|
|
|
|
- required String title,
|
|
|
|
|
- required String hint,
|
|
|
|
|
- }) async {
|
|
|
|
|
- final ctrl = TextEditingController();
|
|
|
|
|
- return showDialog<String>(
|
|
|
|
|
- context: context,
|
|
|
|
|
- builder: (ctx) => TDAlertDialog(
|
|
|
|
|
- title: title,
|
|
|
|
|
- content: hint,
|
|
|
|
|
- leftBtn: TDDialogButtonOptions(
|
|
|
|
|
- title: AppLocalizations.of(context).get('cancel'),
|
|
|
|
|
- action: () => Navigator.pop(ctx),
|
|
|
|
|
- ),
|
|
|
|
|
- rightBtn: TDDialogButtonOptions(
|
|
|
|
|
- title: AppLocalizations.of(context).get('confirm'),
|
|
|
|
|
- action: () => Navigator.pop(ctx, ctrl.text),
|
|
|
|
|
|
|
+ return Expanded(
|
|
|
|
|
+ child: SingleChildScrollView(
|
|
|
|
|
+ physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
|
+ padding: const EdgeInsets.all(16),
|
|
|
|
|
+ child: Column(
|
|
|
|
|
+ children: [
|
|
|
|
|
+ _buildBasicInfoSection(expense, l10n, colors),
|
|
|
|
|
+ const SizedBox(height: 16),
|
|
|
|
|
+ _buildExpenseDetailSection(expense, l10n, colors),
|
|
|
|
|
+ const SizedBox(height: 16),
|
|
|
|
|
+ _buildAttachmentSection(l10n, colors),
|
|
|
|
|
+ ],
|
|
|
),
|
|
),
|
|
|
),
|
|
),
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- Future<void> _doAudit(String action, {String rem = ''}) async {
|
|
|
|
|
- try {
|
|
|
|
|
- LoadingDialog.show(context);
|
|
|
|
|
- final api = ref.read(expenseApiProvider);
|
|
|
|
|
- await api.executeApproval(bilId: 'BX', bilNo: widget.billNo, action: action, rem: rem);
|
|
|
|
|
- if (mounted) {
|
|
|
|
|
- LoadingDialog.hide(context);
|
|
|
|
|
- TDToast.showSuccess(AppLocalizations.of(context).get('submitSuccess'), context: context);
|
|
|
|
|
- if (mounted) context.pop();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- if (mounted) {
|
|
|
|
|
- LoadingDialog.hide(context);
|
|
|
|
|
- TDToast.showFail(e.toString(), context: context);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// ═══ 基本信息 ═══
|
|
// ═══ 基本信息 ═══
|
|
|
Widget _buildBasicInfoSection(
|
|
Widget _buildBasicInfoSection(
|
|
|
ExpenseModel expense, AppLocalizations l10n, AppColorsExtension colors) {
|
|
ExpenseModel expense, AppLocalizations l10n, AppColorsExtension colors) {
|
|
@@ -531,39 +419,68 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Widget _buildAttachmentRow(BillAttachment a, AppColorsExtension colors) {
|
|
Widget _buildAttachmentRow(BillAttachment a, AppColorsExtension colors) {
|
|
|
- return Container(
|
|
|
|
|
- margin: const EdgeInsets.symmetric(vertical: 4),
|
|
|
|
|
- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
|
|
|
|
- decoration: BoxDecoration(
|
|
|
|
|
- color: colors.bgPage,
|
|
|
|
|
- borderRadius: BorderRadius.circular(8),
|
|
|
|
|
- ),
|
|
|
|
|
- child: Row(children: [
|
|
|
|
|
- Icon(_fileTypeIcon(a.ext), size: 24, color: colors.primary),
|
|
|
|
|
- const SizedBox(width: 10),
|
|
|
|
|
- Expanded(
|
|
|
|
|
- child: Text(a.fileName,
|
|
|
|
|
- maxLines: 1, overflow: TextOverflow.ellipsis,
|
|
|
|
|
- style: TextStyle(fontSize: AppFontSizes.body, color: colors.textPrimary)),
|
|
|
|
|
|
|
+ final isImage = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].contains(a.ext.toLowerCase());
|
|
|
|
|
+ return GestureDetector(
|
|
|
|
|
+ onTap: () => _openAttachment(a),
|
|
|
|
|
+ child: Container(
|
|
|
|
|
+ margin: const EdgeInsets.symmetric(vertical: 4),
|
|
|
|
|
+ padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
|
|
|
|
+ decoration: BoxDecoration(
|
|
|
|
|
+ color: colors.bgPage,
|
|
|
|
|
+ borderRadius: BorderRadius.circular(8),
|
|
|
),
|
|
),
|
|
|
- ]),
|
|
|
|
|
|
|
+ child: Row(children: [
|
|
|
|
|
+ if (isImage)
|
|
|
|
|
+ _ExpAttachmentThumbnail(api: ref.read(expenseApiProvider), attachment: a, size: 40)
|
|
|
|
|
+ else
|
|
|
|
|
+ Icon(_fileTypeIcon(a.ext), size: 40, color: colors.primary),
|
|
|
|
|
+ const SizedBox(width: 10),
|
|
|
|
|
+ Expanded(
|
|
|
|
|
+ child: Text(a.fileName,
|
|
|
|
|
+ maxLines: 1, overflow: TextOverflow.ellipsis,
|
|
|
|
|
+ style: TextStyle(fontSize: AppFontSizes.body, color: colors.textPrimary)),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ]),
|
|
|
|
|
+ ),
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // ═══ 审核流程 — 使用 ApprovalTimeline 组件 ═══
|
|
|
|
|
- Widget _buildApprovalSection(
|
|
|
|
|
- AppLocalizations l10n, AppColorsExtension colors) {
|
|
|
|
|
- return FormSection(
|
|
|
|
|
- title: l10n.get('approvalFlow'),
|
|
|
|
|
- leadingIcon: Icons.fact_check_outlined,
|
|
|
|
|
- children: [
|
|
|
|
|
- ApprovalTimeline(
|
|
|
|
|
- records: _timelineRecords,
|
|
|
|
|
- chain: _timelineChain,
|
|
|
|
|
- currentApproverId: _timelineCurrentApproverId,
|
|
|
|
|
- ),
|
|
|
|
|
- ],
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ Future<void> _openAttachment(BillAttachment a) async {
|
|
|
|
|
+ final l10n = AppLocalizations.of(context);
|
|
|
|
|
+ final ext = a.ext.toLowerCase();
|
|
|
|
|
+ final isImage = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].contains(ext);
|
|
|
|
|
+
|
|
|
|
|
+ if (isImage) {
|
|
|
|
|
+ // 图片 → 弹窗预览,内部自动下载并显示 loading
|
|
|
|
|
+ final api = ref.read(expenseApiProvider);
|
|
|
|
|
+ AttachmentPreview.show(context,
|
|
|
|
|
+ loader: api.downloadAttachment(a.id),
|
|
|
|
|
+ fileName: a.fileName,
|
|
|
|
|
+ loadingText: l10n.get('loading'),
|
|
|
|
|
+ );
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 非图片 → 下载后调用系统工具打开
|
|
|
|
|
+ try {
|
|
|
|
|
+ LoadingDialog.show(context, text: l10n.get('downloading'));
|
|
|
|
|
+ final api = ref.read(expenseApiProvider);
|
|
|
|
|
+ final bytes = await api.downloadAttachment(a.id);
|
|
|
|
|
+ if (!mounted) return;
|
|
|
|
|
+ LoadingDialog.hide(context);
|
|
|
|
|
+ if (bytes == null) {
|
|
|
|
|
+ TDToast.showText(l10n.get('downloadFailed'), context: context);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ final dir = await getTemporaryDirectory();
|
|
|
|
|
+ final file = File('${dir.path}/${a.fileName}');
|
|
|
|
|
+ await file.writeAsBytes(bytes);
|
|
|
|
|
+ _openingFile = true;
|
|
|
|
|
+ await OpenFilex.open(file.path);
|
|
|
|
|
+ } catch (_) {
|
|
|
|
|
+ if (mounted) LoadingDialog.hide(context);
|
|
|
|
|
+ if (mounted) TDToast.showText(l10n.get('openFailed'), context: context);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
IconData _fileTypeIcon(String ext) {
|
|
IconData _fileTypeIcon(String ext) {
|
|
@@ -576,3 +493,48 @@ class _ExpenseDetailPageState extends ConsumerState<ExpenseDetailPage>
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+/// 附件缩略图 — 自动调用 DownloadAttachment 加载图片
|
|
|
|
|
+class _ExpAttachmentThumbnail extends StatefulWidget {
|
|
|
|
|
+ final ExpenseApi api;
|
|
|
|
|
+ final BillAttachment attachment;
|
|
|
|
|
+ final double size;
|
|
|
|
|
+ const _ExpAttachmentThumbnail({required this.api, required this.attachment, required this.size});
|
|
|
|
|
+ @override
|
|
|
|
|
+ State<_ExpAttachmentThumbnail> createState() => _ExpAttachmentThumbnailState();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class _ExpAttachmentThumbnailState extends State<_ExpAttachmentThumbnail> {
|
|
|
|
|
+ Uint8List? _bytes;
|
|
|
|
|
+ bool _loading = true;
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ void initState() {
|
|
|
|
|
+ super.initState();
|
|
|
|
|
+ _load();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> _load() async {
|
|
|
|
|
+ try {
|
|
|
|
|
+ final bytes = await widget.api.downloadAttachment(widget.attachment.id);
|
|
|
|
|
+ if (mounted) setState(() { _bytes = bytes; _loading = false; });
|
|
|
|
|
+ } catch (_) {
|
|
|
|
|
+ if (mounted) setState(() => _loading = false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @override
|
|
|
|
|
+ Widget build(BuildContext context) {
|
|
|
|
|
+ if (_loading) {
|
|
|
|
|
+ return SizedBox(width: widget.size, height: widget.size,
|
|
|
|
|
+ child: const Center(child: SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2))));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (_bytes != null) {
|
|
|
|
|
+ return ClipRRect(
|
|
|
|
|
+ borderRadius: BorderRadius.circular(4),
|
|
|
|
|
+ child: Image.memory(_bytes!, width: widget.size, height: widget.size, fit: BoxFit.cover),
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ return Icon(Icons.broken_image, size: widget.size * 0.6, color: Colors.grey);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|