expense_apply_detail_page.dart 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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(
  250. text,
  251. style: TextStyle(
  252. fontSize: 13,
  253. fontWeight: FontWeight.w600,
  254. color: color,
  255. ),
  256. ),
  257. ],
  258. ),
  259. );
  260. final canTap =
  261. approvalStatus != null &&
  262. approvalStatus.isNotEmpty &&
  263. approvalText.isNotEmpty;
  264. if (canTap) {
  265. return GestureDetector(onTap: () => _showAuditTrail('AE'), child: tag);
  266. }
  267. return tag;
  268. }
  269. Future<void> _showAuditTrail(String billId) async {
  270. await HostAppChannel.showAuditTrail(billId, widget.billNo);
  271. }
  272. // ═══ 基本信息 ═══
  273. Widget _buildBasicInfoSection(
  274. ExpenseApplyModel app,
  275. AppLocalizations l10n,
  276. AppColorsExtension colors,
  277. ) {
  278. return FormSection(
  279. title: l10n.get('basicInfo'),
  280. leadingIcon: Icons.info_outline,
  281. trailing: _buildStatusTag(l10n),
  282. children: [
  283. FormFieldRow(
  284. label: l10n.get('expenseApplyNo'),
  285. value: app.expenseApplyNo,
  286. readOnly: true,
  287. showArrow: false,
  288. ),
  289. const SizedBox(height: 16),
  290. FormFieldRow(
  291. label: l10n.get('date'),
  292. value: du.DateUtils.formatDate(app.createTime),
  293. readOnly: true,
  294. showArrow: false,
  295. ),
  296. const SizedBox(height: 16),
  297. FormFieldRow(
  298. label: l10n.get('applyDept'),
  299. value: app.deptId.isNotEmpty
  300. ? '${app.deptId}${app.deptName.isNotEmpty ? '/${app.deptName}' : ''}'
  301. : '-',
  302. readOnly: true,
  303. showArrow: false,
  304. ),
  305. const SizedBox(height: 16),
  306. Row(
  307. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  308. children: [
  309. Text(
  310. l10n.get('emergencyLevel'),
  311. style: TextStyle(
  312. fontSize: AppFontSizes.subtitle,
  313. color: colors.textSecondary,
  314. ),
  315. ),
  316. _buildUrgencyChip(app.urgency, l10n, colors),
  317. ],
  318. ),
  319. const SizedBox(height: 16),
  320. FormFieldRow(
  321. label: l10n.get('applyReason'),
  322. value: app.purpose.isNotEmpty ? app.purpose : '-',
  323. readOnly: true,
  324. showArrow: false,
  325. bold: true,
  326. showMoreOnOverflow: true,
  327. ),
  328. const SizedBox(height: 16),
  329. FormFieldRow(
  330. label: l10n.get('remark'),
  331. value: app.remark.isNotEmpty ? app.remark : '-',
  332. readOnly: true,
  333. showArrow: false,
  334. bold: false,
  335. showMoreOnOverflow: true,
  336. ),
  337. ],
  338. );
  339. }
  340. // ═══ 费用明细 ═══
  341. Widget _buildExpenseDetailSection(
  342. ExpenseApplyModel app,
  343. AppLocalizations l10n,
  344. AppColorsExtension colors,
  345. ) {
  346. return FormSection(
  347. title: l10n.get('expenseDetails'),
  348. leadingIcon: Icons.receipt_long_outlined,
  349. children: [
  350. if (app.details.isEmpty)
  351. Padding(
  352. padding: const EdgeInsets.symmetric(vertical: 8),
  353. child: Text(
  354. l10n.get('noDetailData'),
  355. style: TextStyle(
  356. fontSize: AppFontSizes.body,
  357. color: colors.textPlaceholder,
  358. ),
  359. ),
  360. )
  361. else
  362. ...app.details.asMap().entries.map((e) {
  363. final d = e.value;
  364. final catLabel = d.categoryName.isNotEmpty
  365. ? '${d.expenseCategory}/${d.categoryName}'
  366. : d.expenseCategory;
  367. return GestureDetector(
  368. onTap: () => _showExpenseDetailDialog(context, d),
  369. child: Container(
  370. margin: const EdgeInsets.symmetric(vertical: 8),
  371. padding: const EdgeInsets.all(12),
  372. decoration: BoxDecoration(
  373. color: colors.bgPage,
  374. borderRadius: BorderRadius.circular(8),
  375. ),
  376. child: Row(
  377. crossAxisAlignment: CrossAxisAlignment.center,
  378. children: [
  379. Expanded(
  380. child: Column(
  381. crossAxisAlignment: CrossAxisAlignment.start,
  382. children: [
  383. Row(
  384. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  385. children: [
  386. Expanded(
  387. child: Text(
  388. catLabel,
  389. maxLines: 1,
  390. overflow: TextOverflow.ellipsis,
  391. style: TextStyle(
  392. fontSize: AppFontSizes.subtitle,
  393. color: colors.textPrimary,
  394. ),
  395. ),
  396. ),
  397. const SizedBox(width: 12),
  398. Text(
  399. formatAmount(d.estimatedAmount),
  400. style: TextStyle(
  401. fontSize: AppFontSizes.caption,
  402. fontWeight: FontWeight.w600,
  403. color: colors.amountPrimary,
  404. ),
  405. ),
  406. ],
  407. ),
  408. if (d.acctSubjectId.isNotEmpty) ...[
  409. const SizedBox(height: 4),
  410. Text(
  411. '${l10n.get('acctSubject')}: ${d.acctSubjectId}${d.acctSubjectName.isNotEmpty ? '/${d.acctSubjectName}' : ''}',
  412. maxLines: 1,
  413. overflow: TextOverflow.ellipsis,
  414. style: TextStyle(
  415. fontSize: AppFontSizes.caption,
  416. color: colors.textSecondary,
  417. ),
  418. ),
  419. ],
  420. if (d.sqMan.isNotEmpty) ...[
  421. const SizedBox(height: 4),
  422. Text(
  423. '${l10n.get('applicant')}: ${d.sqMan}${d.sqName.isNotEmpty ? '/${d.sqName}' : ''}',
  424. maxLines: 1,
  425. overflow: TextOverflow.ellipsis,
  426. style: TextStyle(
  427. fontSize: AppFontSizes.caption,
  428. color: colors.textSecondary,
  429. ),
  430. ),
  431. ],
  432. if (d.projectId.isNotEmpty) ...[
  433. const SizedBox(height: 4),
  434. Text(
  435. '${l10n.get('project')}: ${d.projectId}${d.projectName.isNotEmpty ? '/${d.projectName}' : ''}',
  436. maxLines: 1,
  437. overflow: TextOverflow.ellipsis,
  438. style: TextStyle(
  439. fontSize: AppFontSizes.caption,
  440. color: colors.textSecondary,
  441. ),
  442. ),
  443. ],
  444. if (d.costDeptId.isNotEmpty) ...[
  445. const SizedBox(height: 4),
  446. Text(
  447. '${l10n.get('costDept')}: ${d.costDeptId}${d.costDeptName.isNotEmpty ? '/${d.costDeptName}' : ''}',
  448. maxLines: 1,
  449. overflow: TextOverflow.ellipsis,
  450. style: TextStyle(
  451. fontSize: AppFontSizes.caption,
  452. color: colors.textSecondary,
  453. ),
  454. ),
  455. ],
  456. if (d.estimatedStartDate != null) ...[
  457. const SizedBox(height: 4),
  458. Text(
  459. '${l10n.get('estimatedDate')}: ${du.DateUtils.formatDate(d.estimatedStartDate!)}${d.estimatedEndDate != null ? ' ~ ${du.DateUtils.formatDate(d.estimatedEndDate!)}' : ''}',
  460. maxLines: 1,
  461. overflow: TextOverflow.ellipsis,
  462. style: TextStyle(
  463. fontSize: AppFontSizes.caption,
  464. color: colors.textSecondary,
  465. ),
  466. ),
  467. ],
  468. if (d.bxNo.isNotEmpty) ...[
  469. const SizedBox(height: 4),
  470. Text(
  471. '${l10n.get('expenseNo')}: ${d.bxNo}',
  472. maxLines: 1,
  473. overflow: TextOverflow.ellipsis,
  474. style: TextStyle(
  475. fontSize: AppFontSizes.caption,
  476. color: colors.textSecondary,
  477. ),
  478. ),
  479. ],
  480. if (d.remark.isNotEmpty) ...[
  481. const SizedBox(height: 4),
  482. Text(
  483. d.remark,
  484. maxLines: 2,
  485. overflow: TextOverflow.ellipsis,
  486. style: TextStyle(
  487. fontSize: AppFontSizes.caption,
  488. color: colors.textSecondary,
  489. ),
  490. ),
  491. ],
  492. ],
  493. ),
  494. ),
  495. ],
  496. ),
  497. ),
  498. );
  499. }),
  500. if (app.details.isNotEmpty) ...[
  501. const SizedBox(height: 8),
  502. Row(
  503. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  504. children: [
  505. Text(
  506. l10n.get('total'),
  507. style: TextStyle(
  508. fontSize: AppFontSizes.body,
  509. fontWeight: FontWeight.w600,
  510. color: colors.textPrimary,
  511. ),
  512. ),
  513. Text(
  514. formatAmount(
  515. app.details.fold<double>(
  516. 0,
  517. (sum, d) => sum + d.estimatedAmount,
  518. ),
  519. ),
  520. style: TextStyle(
  521. fontSize: AppFontSizes.subtitle,
  522. fontWeight: FontWeight.w700,
  523. color: colors.amountPrimary,
  524. ),
  525. ),
  526. ],
  527. ),
  528. ],
  529. ],
  530. );
  531. }
  532. // ═══ 附件 ═══
  533. Widget _buildAttachmentSection(
  534. AppLocalizations l10n,
  535. AppColorsExtension colors,
  536. ) {
  537. final children = <Widget>[];
  538. if (!_attachAvailable) {
  539. children.add(
  540. Text(
  541. l10n.get('attachServiceUnavailable'),
  542. style: TextStyle(
  543. fontSize: AppFontSizes.body,
  544. color: colors.textPlaceholder,
  545. ),
  546. ),
  547. );
  548. } else if (!_billFileRights.canBrowseAttachments) {
  549. children.add(
  550. Text(
  551. l10n.get('noAttachmentPermission'),
  552. style: TextStyle(
  553. fontSize: AppFontSizes.body,
  554. color: colors.textPlaceholder,
  555. ),
  556. ),
  557. );
  558. } else if (_attachments.isEmpty) {
  559. children.add(
  560. Text(
  561. l10n.get('noAttachment'),
  562. style: TextStyle(
  563. fontSize: AppFontSizes.body,
  564. color: colors.textPlaceholder,
  565. ),
  566. ),
  567. );
  568. } else {
  569. children.addAll(_attachments.map((a) => _buildAttachmentRow(a, colors)));
  570. }
  571. return FormSection(
  572. title: l10n.get('attachments'),
  573. leadingIcon: Icons.attach_file_outlined,
  574. children: children,
  575. );
  576. }
  577. Widget _buildAttachmentRow(BillAttachment a, AppColorsExtension colors) {
  578. final isImage = [
  579. 'jpg',
  580. 'jpeg',
  581. 'png',
  582. 'gif',
  583. 'bmp',
  584. 'webp',
  585. ].contains(a.ext.toLowerCase());
  586. return GestureDetector(
  587. onTap: () => _openAttachment(a),
  588. child: Container(
  589. margin: const EdgeInsets.symmetric(vertical: 4),
  590. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
  591. decoration: BoxDecoration(
  592. color: colors.bgPage,
  593. borderRadius: BorderRadius.circular(8),
  594. ),
  595. child: Row(
  596. children: [
  597. if (isImage)
  598. _AttachmentThumbnail(
  599. api: ref.read(expenseApplyApiProvider),
  600. attachment: a,
  601. size: 40,
  602. )
  603. else
  604. Icon(_fileTypeIcon(a.ext), size: 40, color: colors.primary),
  605. const SizedBox(width: 10),
  606. Expanded(
  607. child: Text(
  608. a.fileName,
  609. maxLines: 1,
  610. overflow: TextOverflow.ellipsis,
  611. style: TextStyle(
  612. fontSize: AppFontSizes.body,
  613. color: colors.textPrimary,
  614. ),
  615. ),
  616. ),
  617. const SizedBox(width: 8),
  618. GestureDetector(
  619. onTap: () => AttachmentDownloadHelper.downloadAndSave(
  620. context,
  621. a,
  622. ref.read(expenseApplyApiProvider).downloadAttachment,
  623. ),
  624. child: Icon(
  625. Icons.download_outlined,
  626. size: 22,
  627. color: colors.primary,
  628. ),
  629. ),
  630. ],
  631. ),
  632. ),
  633. );
  634. }
  635. Future<void> _openAttachment(BillAttachment a) async {
  636. final l10n = AppLocalizations.of(context);
  637. final ext = a.ext.toLowerCase();
  638. final isImage = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].contains(ext);
  639. if (isImage) {
  640. // 图片 → 弹窗预览,内部自动下载并显示 loading
  641. final api = ref.read(expenseApplyApiProvider);
  642. AttachmentPreview.show(
  643. context,
  644. loader: api.downloadAttachment(a.id),
  645. fileName: a.fileName,
  646. loadingText: l10n.get('loading'),
  647. );
  648. return;
  649. }
  650. // 非图片 → 下载后调用系统工具打开
  651. try {
  652. LoadingDialog.show(context, text: l10n.get('downloading'));
  653. final api = ref.read(expenseApplyApiProvider);
  654. final bytes = await api.downloadAttachment(a.id);
  655. if (!mounted) return;
  656. LoadingDialog.hide(context);
  657. if (bytes == null) {
  658. TDToast.showText(l10n.get('downloadFailed'), context: context);
  659. return;
  660. }
  661. final dir = await getTemporaryDirectory();
  662. final file = File('${dir.path}/${a.fileName}');
  663. await file.writeAsBytes(bytes);
  664. await OpenFilex.open(file.path);
  665. } catch (_) {
  666. if (mounted) LoadingDialog.hide(context);
  667. if (mounted) TDToast.showText(l10n.get('openFailed'), context: context);
  668. }
  669. }
  670. IconData _fileTypeIcon(String ext) {
  671. switch (ext.toLowerCase()) {
  672. case 'pdf':
  673. return Icons.picture_as_pdf;
  674. case 'doc':
  675. case 'docx':
  676. return Icons.description;
  677. case 'xls':
  678. case 'xlsx':
  679. return Icons.table_chart;
  680. case 'jpg':
  681. case 'jpeg':
  682. case 'png':
  683. case 'gif':
  684. case 'bmp':
  685. return Icons.image_outlined;
  686. default:
  687. return Icons.insert_drive_file;
  688. }
  689. }
  690. Widget _buildPageFooter(AppColorsExtension colors) {
  691. final l10n = AppLocalizations.of(context);
  692. return Center(
  693. child: Padding(
  694. padding: const EdgeInsets.only(bottom: 16),
  695. child: Row(
  696. mainAxisSize: MainAxisSize.min,
  697. children: [
  698. Icon(
  699. Icons.rocket_launch_outlined,
  700. size: 16,
  701. color: colors.textPlaceholder,
  702. ),
  703. const SizedBox(width: 6),
  704. Text(
  705. l10n.get('pageFooter'),
  706. style: TextStyle(
  707. fontSize: AppFontSizes.caption,
  708. color: colors.textPlaceholder,
  709. ),
  710. ),
  711. ],
  712. ),
  713. ),
  714. );
  715. }
  716. Widget _buildUrgencyChip(
  717. String urgency,
  718. AppLocalizations l10n,
  719. AppColorsExtension colors,
  720. ) {
  721. final (label, color) = switch (urgency) {
  722. '3' || 'critical' => (l10n.get('critical'), colors.danger),
  723. '2' || 'urgent' => (l10n.get('urgent'), colors.warning),
  724. '1' || 'normal' => (l10n.get('normal'), colors.primary),
  725. _ => (l10n.get('normal'), colors.primary),
  726. };
  727. return Container(
  728. padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
  729. decoration: BoxDecoration(
  730. color: color.withValues(alpha: 0.1),
  731. borderRadius: BorderRadius.circular(4),
  732. border: Border.all(color: color, width: 0.5),
  733. ),
  734. child: Text(
  735. label,
  736. style: TextStyle(
  737. fontSize: 11,
  738. fontWeight: FontWeight.w500,
  739. color: color,
  740. ),
  741. ),
  742. );
  743. }
  744. void _showExpenseDetailDialog(
  745. BuildContext context,
  746. ExpenseApplyDetailModel d,
  747. ) {
  748. ExpenseApplyDetailViewDialog.show(context, d);
  749. }
  750. }
  751. /// 附件缩略图 — 自动调用 DownloadAttachment 加载图片
  752. class _AttachmentThumbnail extends StatefulWidget {
  753. final ExpenseApplyApi api;
  754. final BillAttachment attachment;
  755. final double size;
  756. const _AttachmentThumbnail({
  757. required this.api,
  758. required this.attachment,
  759. required this.size,
  760. });
  761. @override
  762. State<_AttachmentThumbnail> createState() => _AttachmentThumbnailState();
  763. }
  764. class _AttachmentThumbnailState extends State<_AttachmentThumbnail> {
  765. Uint8List? _bytes;
  766. bool _loading = true;
  767. @override
  768. void initState() {
  769. super.initState();
  770. _load();
  771. }
  772. Future<void> _load() async {
  773. try {
  774. final bytes = await widget.api.downloadAttachment(widget.attachment.id);
  775. if (mounted) {
  776. setState(() {
  777. _bytes = bytes;
  778. _loading = false;
  779. });
  780. }
  781. } catch (_) {
  782. if (mounted) setState(() => _loading = false);
  783. }
  784. }
  785. @override
  786. Widget build(BuildContext context) {
  787. if (_loading) {
  788. return SizedBox(
  789. width: widget.size,
  790. height: widget.size,
  791. child: const Center(
  792. child: SizedBox(
  793. width: 16,
  794. height: 16,
  795. child: CircularProgressIndicator(strokeWidth: 2),
  796. ),
  797. ),
  798. );
  799. }
  800. if (_bytes != null) {
  801. return ClipRRect(
  802. borderRadius: BorderRadius.circular(4),
  803. child: Image.memory(
  804. _bytes!,
  805. width: widget.size,
  806. height: widget.size,
  807. fit: BoxFit.cover,
  808. ),
  809. );
  810. }
  811. return Icon(
  812. Icons.broken_image,
  813. size: widget.size * 0.6,
  814. color: Colors.grey,
  815. );
  816. }
  817. }