expense_apply_detail_page.dart 26 KB

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