expense_apply_detail_page.dart 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. import 'dart:typed_data';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:open_filex/open_filex.dart';
  6. import 'package:go_router/go_router.dart';
  7. import '../../shared/widgets/attachment_preview_page.dart';
  8. import 'package:tdesign_flutter/tdesign_flutter.dart';
  9. import 'package:flutter_riverpod/flutter_riverpod.dart';
  10. import '../../shared/widgets/loading_dialog.dart';
  11. import '../../core/utils/date_utils.dart' as du;
  12. import '../../shared/widgets/form_section.dart';
  13. import '../../shared/widgets/form_field_row.dart';
  14. import '../../shared/widgets/app_skeletons.dart';
  15. import '../../shared/models/bill_file_rights.dart';
  16. import '../../shared/widgets/attachment_download_helper.dart';
  17. import '../../shared/widgets/action_bar.dart';
  18. import '../../core/navigation/host_app_channel.dart';
  19. import 'expense_apply_model.dart';
  20. import 'widgets/expense_apply_detail_view_dialog.dart';
  21. import '../../core/i18n/app_localizations.dart';
  22. import '../../shared/models/bill_attachment.dart';
  23. import 'expense_apply_api.dart';
  24. import '../../core/theme/app_colors.dart';
  25. import '../../core/theme/app_colors_extension.dart';
  26. import '../../core/utils/amount_utils.dart';
  27. class ExpenseApplyDetailPage extends ConsumerStatefulWidget {
  28. final String billNo;
  29. final int queryId;
  30. const ExpenseApplyDetailPage({
  31. super.key,
  32. required this.billNo,
  33. this.queryId = 0,
  34. });
  35. @override
  36. ConsumerState<ExpenseApplyDetailPage> createState() =>
  37. _ExpenseApplyDetailPageState();
  38. }
  39. class _ExpenseApplyDetailPageState
  40. extends ConsumerState<ExpenseApplyDetailPage> {
  41. bool _loading = true;
  42. String? _error;
  43. ExpenseApplyModel? _data;
  44. List<BillAttachment> _attachments = [];
  45. bool _attachAvailable = false;
  46. BillFileRights _billFileRights = BillFileRights.none;
  47. Map<String, dynamic>? _billStatus;
  48. bool _statusLoading = false;
  49. bool _canEdit = false;
  50. @override
  51. void initState() {
  52. super.initState();
  53. _loadData();
  54. }
  55. @override
  56. void dispose() {
  57. super.dispose();
  58. }
  59. Future<void> _loadData() async {
  60. setState(() {
  61. _loading = true;
  62. _error = null;
  63. });
  64. try {
  65. final api = ref.read(expenseApplyApiProvider);
  66. final detail = await api.fetchDetail(widget.billNo);
  67. // Load attachments (non-critical, best-effort)
  68. bool attachAvailable = false;
  69. try {
  70. attachAvailable = await api.checkAttachHealth();
  71. debugPrint('[Attach] checkAttachHealth result: $attachAvailable');
  72. } catch (e) {
  73. debugPrint('[Attach] checkAttachHealth error: $e');
  74. attachAvailable = false;
  75. }
  76. // 加载附件权限
  77. BillFileRights billFileRights = BillFileRights.none;
  78. try {
  79. billFileRights = await api.getBillFileRights('AE');
  80. } catch (_) {}
  81. List<BillAttachment> attachments = [];
  82. if (attachAvailable && billFileRights.canBrowseAttachments) {
  83. try {
  84. attachments = await api.getAttachments('AE', widget.billNo);
  85. debugPrint('[Attach] getAttachments count: ${attachments.length}');
  86. } catch (e) {
  87. debugPrint('[Attach] getAttachments error: $e');
  88. }
  89. }
  90. debugPrint(
  91. '[Attach] final state: attachAvailable=$attachAvailable, count=${attachments.length}',
  92. );
  93. if (mounted) {
  94. setState(() {
  95. _data = detail;
  96. _attachments = attachments;
  97. _attachAvailable = attachAvailable;
  98. _billFileRights = billFileRights;
  99. _loading = false;
  100. });
  101. }
  102. // 单据状态(非关键,尽力加载)
  103. setState(() => _statusLoading = true);
  104. try {
  105. final status = await api.getBillStatus(widget.billNo);
  106. if (mounted) {
  107. setState(() {
  108. _billStatus = status;
  109. _canEdit = status['canEdit'] == true;
  110. _statusLoading = false;
  111. });
  112. }
  113. } catch (_) {
  114. if (mounted) setState(() => _statusLoading = false);
  115. }
  116. } catch (e) {
  117. if (mounted) {
  118. setState(() {
  119. _error = e.toString();
  120. _loading = false;
  121. });
  122. }
  123. }
  124. }
  125. @override
  126. Widget build(BuildContext context) {
  127. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  128. final l10n = AppLocalizations.of(context);
  129. if (_loading) {
  130. return const SkeletonDetailPage();
  131. }
  132. if (_error != null) {
  133. return Center(
  134. child: Column(
  135. mainAxisSize: MainAxisSize.min,
  136. children: [
  137. Icon(Icons.error_outline, size: 48, color: colors.danger),
  138. const SizedBox(height: 16),
  139. Padding(
  140. padding: const EdgeInsets.symmetric(horizontal: 32),
  141. child: Text(
  142. _error!,
  143. textAlign: TextAlign.center,
  144. style: TextStyle(
  145. fontSize: AppFontSizes.body,
  146. color: colors.textSecondary,
  147. ),
  148. ),
  149. ),
  150. const SizedBox(height: 16),
  151. TDButton(
  152. text: l10n.get('retry'),
  153. size: TDButtonSize.medium,
  154. onTap: _loadData,
  155. ),
  156. ],
  157. ),
  158. );
  159. }
  160. final app = _data!;
  161. return Column(
  162. children: [
  163. Expanded(
  164. child: SingleChildScrollView(
  165. physics: const AlwaysScrollableScrollPhysics(),
  166. padding: const EdgeInsets.all(16),
  167. child: Column(
  168. children: [
  169. _buildBasicInfoSection(app, l10n, colors),
  170. const SizedBox(height: 16),
  171. _buildExpenseDetailSection(app, l10n, colors),
  172. const SizedBox(height: 16),
  173. _buildAttachmentSection(l10n, colors),
  174. const SizedBox(height: 24),
  175. _buildPageFooter(colors),
  176. ],
  177. ),
  178. ),
  179. ),
  180. // TODO: 等 ERP 提供"能否修改单据"接口后,恢复为 if (_canEdit)
  181. if (false)
  182. ActionBar(
  183. showLeft: false,
  184. showCenter: false,
  185. rightLabel: l10n.get('editApply'),
  186. onRightTap: () async {
  187. final result = await GoRouter.of(
  188. context,
  189. ).push('/expense-apply/edit/${widget.billNo}');
  190. if (result == true && mounted) _loadData();
  191. },
  192. ),
  193. ],
  194. );
  195. }
  196. // ═══ 状态 tag(标题行右侧) ═══
  197. Widget? _buildStatusTag(AppLocalizations l10n) {
  198. final isTransferred = _billStatus?['isTransferred'] == true;
  199. final approvalStatus = _billStatus?['approvalStatus'] as String?;
  200. final approvalText = _billStatus?['approvalText'] as String? ?? '';
  201. IconData icon;
  202. String text;
  203. Color color;
  204. if (isTransferred) {
  205. icon = Icons.swap_horiz;
  206. text = l10n.get('statusConvertedToExpense');
  207. color = Colors.blue;
  208. } else if (approvalStatus == null || approvalStatus.isEmpty) {
  209. return null;
  210. } else {
  211. switch (approvalStatus) {
  212. case 'SHCOUNT0':
  213. icon = Icons.edit_note;
  214. color = Colors.teal;
  215. break;
  216. case 'SHCOUNT1':
  217. icon = Icons.hourglass_empty;
  218. color = Colors.blueGrey;
  219. break;
  220. case 'SHCOUNT2':
  221. icon = Icons.cancel_outlined;
  222. color = Colors.red;
  223. break;
  224. case 'SHCOUNT3':
  225. icon = Icons.undo;
  226. color = Colors.deepOrange;
  227. break;
  228. case 'SHCOUNT4':
  229. icon = Icons.check_circle_outline;
  230. color = Colors.green;
  231. break;
  232. default:
  233. return null;
  234. }
  235. text = approvalText;
  236. }
  237. final tag = Container(
  238. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
  239. decoration: BoxDecoration(
  240. color: color.withValues(alpha: 0.1),
  241. borderRadius: BorderRadius.circular(6),
  242. border: Border.all(color: color.withValues(alpha: 0.3), width: 0.5),
  243. ),
  244. child: Row(
  245. mainAxisSize: MainAxisSize.min,
  246. children: [
  247. Icon(icon, size: 18, color: color),
  248. const SizedBox(width: 4),
  249. Text(text, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: color)),
  250. ],
  251. ),
  252. );
  253. final canTap = approvalStatus != null && approvalStatus.isNotEmpty && approvalText.isNotEmpty;
  254. if (canTap) {
  255. return GestureDetector(
  256. onTap: () => _showAuditTrail('AE'),
  257. child: tag,
  258. );
  259. }
  260. return tag;
  261. }
  262. Future<void> _showAuditTrail(String billId) async {
  263. await HostAppChannel.showAuditTrail(billId, widget.billNo);
  264. }
  265. // ═══ 基本信息 ═══
  266. Widget _buildBasicInfoSection(
  267. ExpenseApplyModel app,
  268. AppLocalizations l10n,
  269. AppColorsExtension colors,
  270. ) {
  271. return FormSection(
  272. title: l10n.get('basicInfo'),
  273. leadingIcon: Icons.info_outline,
  274. trailing: _buildStatusTag(l10n),
  275. children: [
  276. FormFieldRow(
  277. label: l10n.get('expenseApplyNo'),
  278. value: app.expenseApplyNo,
  279. readOnly: true,
  280. showArrow: false,
  281. ),
  282. const SizedBox(height: 16),
  283. FormFieldRow(
  284. label: l10n.get('date'),
  285. value: du.DateUtils.formatDate(app.createTime),
  286. readOnly: true,
  287. showArrow: false,
  288. ),
  289. const SizedBox(height: 16),
  290. FormFieldRow(
  291. label: l10n.get('applyDept'),
  292. value: app.deptId.isNotEmpty
  293. ? '${app.deptId}${app.deptName.isNotEmpty ? '/${app.deptName}' : ''}'
  294. : '-',
  295. readOnly: true,
  296. showArrow: false,
  297. ),
  298. const SizedBox(height: 16),
  299. SizedBox(
  300. height: 24,
  301. child: Row(
  302. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  303. children: [
  304. Text(
  305. l10n.get('emergencyLevel'),
  306. style: TextStyle(
  307. fontSize: AppFontSizes.subtitle,
  308. color: colors.textSecondary,
  309. ),
  310. ),
  311. _buildUrgencyChip(app.urgency, l10n, colors),
  312. ],
  313. ),
  314. ),
  315. const SizedBox(height: 16),
  316. FormFieldRow(
  317. label: l10n.get('applyReason'),
  318. value: app.purpose.isNotEmpty ? app.purpose : '-',
  319. readOnly: true,
  320. showArrow: false,
  321. bold: true,
  322. ),
  323. const SizedBox(height: 16),
  324. FormFieldRow(
  325. label: l10n.get('remark'),
  326. value: app.remark.isNotEmpty ? app.remark : '-',
  327. readOnly: true,
  328. showArrow: false,
  329. bold: false,
  330. ),
  331. ],
  332. );
  333. }
  334. // ═══ 费用明细 ═══
  335. Widget _buildExpenseDetailSection(
  336. ExpenseApplyModel app,
  337. AppLocalizations l10n,
  338. AppColorsExtension colors,
  339. ) {
  340. return FormSection(
  341. title: l10n.get('expenseDetails'),
  342. leadingIcon: Icons.receipt_long_outlined,
  343. children: [
  344. if (app.details.isEmpty)
  345. Padding(
  346. padding: const EdgeInsets.symmetric(vertical: 8),
  347. child: Text(
  348. l10n.get('noDetailData'),
  349. style: TextStyle(
  350. fontSize: AppFontSizes.body,
  351. color: colors.textPlaceholder,
  352. ),
  353. ),
  354. )
  355. else
  356. ...app.details.asMap().entries.map((e) {
  357. final d = e.value;
  358. final catLabel = d.categoryName.isNotEmpty
  359. ? '${d.expenseCategory}/${d.categoryName}'
  360. : d.expenseCategory;
  361. return GestureDetector(
  362. onTap: () => _showExpenseDetailDialog(context, d),
  363. child: Container(
  364. margin: const EdgeInsets.symmetric(vertical: 8),
  365. padding: const EdgeInsets.all(12),
  366. decoration: BoxDecoration(
  367. color: colors.bgPage,
  368. borderRadius: BorderRadius.circular(8),
  369. ),
  370. child: Row(
  371. crossAxisAlignment: CrossAxisAlignment.center,
  372. children: [
  373. Expanded(
  374. child: Column(
  375. crossAxisAlignment: CrossAxisAlignment.start,
  376. children: [
  377. Row(
  378. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  379. children: [
  380. Expanded(
  381. child: Text(
  382. catLabel,
  383. maxLines: 1,
  384. overflow: TextOverflow.ellipsis,
  385. style: TextStyle(
  386. fontSize: AppFontSizes.subtitle,
  387. color: colors.textPrimary,
  388. ),
  389. ),
  390. ),
  391. const SizedBox(width: 12),
  392. Text(
  393. formatAmount(d.estimatedAmount),
  394. style: TextStyle(
  395. fontSize: AppFontSizes.caption,
  396. fontWeight: FontWeight.w600,
  397. color: colors.amountPrimary,
  398. ),
  399. ),
  400. ],
  401. ),
  402. if (d.acctSubjectId.isNotEmpty) ...[
  403. const SizedBox(height: 4),
  404. Text(
  405. '${l10n.get('acctSubject')}: ${d.acctSubjectId}${d.acctSubjectName.isNotEmpty ? '/${d.acctSubjectName}' : ''}',
  406. maxLines: 1,
  407. overflow: TextOverflow.ellipsis,
  408. style: TextStyle(
  409. fontSize: AppFontSizes.caption,
  410. color: colors.textSecondary,
  411. ),
  412. ),
  413. ],
  414. if (d.sqMan.isNotEmpty) ...[
  415. const SizedBox(height: 4),
  416. Text(
  417. '${l10n.get('applicant')}: ${d.sqMan}${d.sqName.isNotEmpty ? '/${d.sqName}' : ''}',
  418. maxLines: 1,
  419. overflow: TextOverflow.ellipsis,
  420. style: TextStyle(
  421. fontSize: AppFontSizes.caption,
  422. color: colors.textSecondary,
  423. ),
  424. ),
  425. ],
  426. if (d.projectId.isNotEmpty) ...[
  427. const SizedBox(height: 4),
  428. Text(
  429. '${l10n.get('project')}: ${d.projectId}${d.projectName.isNotEmpty ? '/${d.projectName}' : ''}',
  430. maxLines: 1,
  431. overflow: TextOverflow.ellipsis,
  432. style: TextStyle(
  433. fontSize: AppFontSizes.caption,
  434. color: colors.textSecondary,
  435. ),
  436. ),
  437. ],
  438. if (d.costDeptId.isNotEmpty) ...[
  439. const SizedBox(height: 4),
  440. Text(
  441. '${l10n.get('costDept')}: ${d.costDeptId}${d.costDeptName.isNotEmpty ? '/${d.costDeptName}' : ''}',
  442. maxLines: 1,
  443. overflow: TextOverflow.ellipsis,
  444. style: TextStyle(
  445. fontSize: AppFontSizes.caption,
  446. color: colors.textSecondary,
  447. ),
  448. ),
  449. ],
  450. if (d.estimatedStartDate != null) ...[
  451. const SizedBox(height: 4),
  452. Text(
  453. '${l10n.get('estimatedDate')}: ${du.DateUtils.formatDate(d.estimatedStartDate!)}${d.estimatedEndDate != null ? ' ~ ${du.DateUtils.formatDate(d.estimatedEndDate!)}' : ''}',
  454. maxLines: 1,
  455. overflow: TextOverflow.ellipsis,
  456. style: TextStyle(
  457. fontSize: AppFontSizes.caption,
  458. color: colors.textSecondary,
  459. ),
  460. ),
  461. ],
  462. if (d.bxNo.isNotEmpty) ...[
  463. const SizedBox(height: 4),
  464. Text(
  465. '${l10n.get('expenseNo')}: ${d.bxNo}',
  466. maxLines: 1,
  467. overflow: TextOverflow.ellipsis,
  468. style: TextStyle(
  469. fontSize: AppFontSizes.caption,
  470. color: colors.textSecondary,
  471. ),
  472. ),
  473. ],
  474. if (d.remark.isNotEmpty) ...[
  475. const SizedBox(height: 4),
  476. Text(
  477. d.remark,
  478. maxLines: 2,
  479. overflow: TextOverflow.ellipsis,
  480. style: TextStyle(
  481. fontSize: AppFontSizes.caption,
  482. color: colors.textSecondary,
  483. ),
  484. ),
  485. ],
  486. ],
  487. ),
  488. ),
  489. ],
  490. ),
  491. ),
  492. );
  493. }),
  494. if (app.details.isNotEmpty) ...[
  495. const SizedBox(height: 8),
  496. Row(
  497. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  498. children: [
  499. Text(
  500. l10n.get('total'),
  501. style: TextStyle(
  502. fontSize: AppFontSizes.body,
  503. fontWeight: FontWeight.w600,
  504. color: colors.textPrimary,
  505. ),
  506. ),
  507. Text(
  508. formatAmount(app.details.fold<double>(0, (sum, d) => sum + d.estimatedAmount)),
  509. style: TextStyle(
  510. fontSize: AppFontSizes.subtitle,
  511. fontWeight: FontWeight.w700,
  512. color: colors.amountPrimary,
  513. ),
  514. ),
  515. ],
  516. ),
  517. ],
  518. ],
  519. );
  520. }
  521. // ═══ 附件 ═══
  522. Widget _buildAttachmentSection(
  523. AppLocalizations l10n,
  524. AppColorsExtension colors,
  525. ) {
  526. final children = <Widget>[];
  527. if (!_attachAvailable) {
  528. children.add(
  529. Text(
  530. l10n.get('attachServiceUnavailable'),
  531. style: TextStyle(
  532. fontSize: AppFontSizes.body,
  533. color: colors.textPlaceholder,
  534. ),
  535. ),
  536. );
  537. } else if (!_billFileRights.canBrowseAttachments) {
  538. children.add(
  539. Text(
  540. l10n.get('noAttachmentPermission'),
  541. style: TextStyle(
  542. fontSize: AppFontSizes.body,
  543. color: colors.textPlaceholder,
  544. ),
  545. ),
  546. );
  547. } else if (_attachments.isEmpty) {
  548. children.add(
  549. Text(
  550. l10n.get('noAttachment'),
  551. style: TextStyle(
  552. fontSize: AppFontSizes.body,
  553. color: colors.textPlaceholder,
  554. ),
  555. ),
  556. );
  557. } else {
  558. children.addAll(_attachments.map((a) => _buildAttachmentRow(a, colors)));
  559. }
  560. return FormSection(
  561. title: l10n.get('attachments'),
  562. leadingIcon: Icons.attach_file_outlined,
  563. children: children,
  564. );
  565. }
  566. Widget _buildAttachmentRow(BillAttachment a, AppColorsExtension colors) {
  567. final isImage = [
  568. 'jpg',
  569. 'jpeg',
  570. 'png',
  571. 'gif',
  572. 'bmp',
  573. 'webp',
  574. ].contains(a.ext.toLowerCase());
  575. return GestureDetector(
  576. onTap: () => _openAttachment(a),
  577. child: Container(
  578. margin: const EdgeInsets.symmetric(vertical: 4),
  579. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
  580. decoration: BoxDecoration(
  581. color: colors.bgPage,
  582. borderRadius: BorderRadius.circular(8),
  583. ),
  584. child: Row(
  585. children: [
  586. if (isImage)
  587. _AttachmentThumbnail(
  588. api: ref.read(expenseApplyApiProvider),
  589. attachment: a,
  590. size: 40,
  591. )
  592. else
  593. Icon(_fileTypeIcon(a.ext), size: 40, color: colors.primary),
  594. const SizedBox(width: 10),
  595. Expanded(
  596. child: Text(
  597. a.fileName,
  598. maxLines: 1,
  599. overflow: TextOverflow.ellipsis,
  600. style: TextStyle(
  601. fontSize: AppFontSizes.body,
  602. color: colors.textPrimary,
  603. ),
  604. ),
  605. ),
  606. const SizedBox(width: 8),
  607. GestureDetector(
  608. onTap: () => AttachmentDownloadHelper.downloadAndSave(
  609. context,
  610. a,
  611. ref.read(expenseApplyApiProvider).downloadAttachment,
  612. ),
  613. child: Icon(
  614. Icons.download_outlined,
  615. size: 22,
  616. color: colors.primary,
  617. ),
  618. ),
  619. ],
  620. ),
  621. ),
  622. );
  623. }
  624. Future<void> _openAttachment(BillAttachment a) async {
  625. final l10n = AppLocalizations.of(context);
  626. final ext = a.ext.toLowerCase();
  627. final isImage = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].contains(ext);
  628. if (isImage) {
  629. // 图片 → 弹窗预览,内部自动下载并显示 loading
  630. final api = ref.read(expenseApplyApiProvider);
  631. AttachmentPreview.show(
  632. context,
  633. loader: api.downloadAttachment(a.id),
  634. fileName: a.fileName,
  635. loadingText: l10n.get('loading'),
  636. );
  637. return;
  638. }
  639. // 非图片 → 下载后调用系统工具打开
  640. try {
  641. LoadingDialog.show(context, text: l10n.get('downloading'));
  642. final api = ref.read(expenseApplyApiProvider);
  643. final bytes = await api.downloadAttachment(a.id);
  644. if (!mounted) return;
  645. LoadingDialog.hide(context);
  646. if (bytes == null) {
  647. TDToast.showText(l10n.get('downloadFailed'), context: context);
  648. return;
  649. }
  650. final dir = await getTemporaryDirectory();
  651. final file = File('${dir.path}/${a.fileName}');
  652. await file.writeAsBytes(bytes);
  653. await OpenFilex.open(file.path);
  654. } catch (_) {
  655. if (mounted) LoadingDialog.hide(context);
  656. if (mounted) TDToast.showText(l10n.get('openFailed'), context: context);
  657. }
  658. }
  659. IconData _fileTypeIcon(String ext) {
  660. switch (ext.toLowerCase()) {
  661. case 'pdf':
  662. return Icons.picture_as_pdf;
  663. case 'doc':
  664. case 'docx':
  665. return Icons.description;
  666. case 'xls':
  667. case 'xlsx':
  668. return Icons.table_chart;
  669. case 'jpg':
  670. case 'jpeg':
  671. case 'png':
  672. case 'gif':
  673. case 'bmp':
  674. return Icons.image_outlined;
  675. default:
  676. return Icons.insert_drive_file;
  677. }
  678. }
  679. Widget _buildPageFooter(AppColorsExtension colors) {
  680. final l10n = AppLocalizations.of(context);
  681. return Center(
  682. child: Padding(
  683. padding: const EdgeInsets.only(bottom: 16),
  684. child: Row(
  685. mainAxisSize: MainAxisSize.min,
  686. children: [
  687. Icon(
  688. Icons.rocket_launch_outlined,
  689. size: 16,
  690. color: colors.textPlaceholder,
  691. ),
  692. const SizedBox(width: 6),
  693. Text(
  694. l10n.get('pageFooter'),
  695. style: TextStyle(
  696. fontSize: AppFontSizes.caption,
  697. color: colors.textPlaceholder,
  698. ),
  699. ),
  700. ],
  701. ),
  702. ),
  703. );
  704. }
  705. Widget _buildUrgencyChip(
  706. String urgency,
  707. AppLocalizations l10n,
  708. AppColorsExtension colors,
  709. ) {
  710. final (label, color) = switch (urgency) {
  711. '3' || 'critical' => (l10n.get('critical'), colors.danger),
  712. '2' || 'urgent' => (l10n.get('urgent'), colors.warning),
  713. '1' || 'normal' => (l10n.get('normal'), colors.primary),
  714. _ => (l10n.get('normal'), colors.primary),
  715. };
  716. return Container(
  717. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
  718. decoration: BoxDecoration(
  719. color: color.withValues(alpha: 0.1),
  720. borderRadius: BorderRadius.circular(4),
  721. border: Border.all(color: color, width: 0.5),
  722. ),
  723. child: Text(
  724. label,
  725. style: TextStyle(
  726. fontSize: 11,
  727. fontWeight: FontWeight.w500,
  728. color: color,
  729. ),
  730. ),
  731. );
  732. }
  733. void _showExpenseDetailDialog(
  734. BuildContext context,
  735. ExpenseApplyDetailModel d,
  736. ) {
  737. ExpenseApplyDetailViewDialog.show(context, d);
  738. }
  739. }
  740. /// 附件缩略图 — 自动调用 DownloadAttachment 加载图片
  741. class _AttachmentThumbnail extends StatefulWidget {
  742. final ExpenseApplyApi api;
  743. final BillAttachment attachment;
  744. final double size;
  745. const _AttachmentThumbnail({
  746. required this.api,
  747. required this.attachment,
  748. required this.size,
  749. });
  750. @override
  751. State<_AttachmentThumbnail> createState() => _AttachmentThumbnailState();
  752. }
  753. class _AttachmentThumbnailState extends State<_AttachmentThumbnail> {
  754. Uint8List? _bytes;
  755. bool _loading = true;
  756. @override
  757. void initState() {
  758. super.initState();
  759. _load();
  760. }
  761. Future<void> _load() async {
  762. try {
  763. final bytes = await widget.api.downloadAttachment(widget.attachment.id);
  764. if (mounted) {
  765. setState(() {
  766. _bytes = bytes;
  767. _loading = false;
  768. });
  769. }
  770. } catch (_) {
  771. if (mounted) setState(() => _loading = false);
  772. }
  773. }
  774. @override
  775. Widget build(BuildContext context) {
  776. if (_loading) {
  777. return SizedBox(
  778. width: widget.size,
  779. height: widget.size,
  780. child: const Center(
  781. child: SizedBox(
  782. width: 16,
  783. height: 16,
  784. child: CircularProgressIndicator(strokeWidth: 2),
  785. ),
  786. ),
  787. );
  788. }
  789. if (_bytes != null) {
  790. return ClipRRect(
  791. borderRadius: BorderRadius.circular(4),
  792. child: Image.memory(
  793. _bytes!,
  794. width: widget.size,
  795. height: widget.size,
  796. fit: BoxFit.cover,
  797. ),
  798. );
  799. }
  800. return Icon(
  801. Icons.broken_image,
  802. size: widget.size * 0.6,
  803. color: Colors.grey,
  804. );
  805. }
  806. }