expense_apply_detail_page.dart 26 KB

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