expense_create_page.dart 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_riverpod/flutter_riverpod.dart';
  6. import 'package:tdesign_flutter/tdesign_flutter.dart';
  7. import 'package:go_router/go_router.dart';
  8. import '../../shared/widgets/nav_bar_config.dart';
  9. import '../../core/utils/responsive.dart';
  10. import '../../shared/widgets/form_section.dart';
  11. import '../../shared/widgets/form_field_row.dart';
  12. import 'expense_api.dart';
  13. import 'expense_create_controller.dart';
  14. import '../../core/i18n/app_localizations.dart';
  15. import 'expense_model.dart';
  16. import '../../core/theme/app_colors.dart';
  17. import '../../core/theme/app_colors_extension.dart';
  18. import '../../core/navigation/host_app_channel.dart';
  19. import '../../core/data/mock_api_data.dart';
  20. import 'widgets/expense_detail_dialog.dart';
  21. import '../../shared/widgets/action_bar.dart';
  22. import '../../shared/widgets/loading_dialog.dart';
  23. import '../../shared/widgets/attachment_picker.dart';
  24. import 'expense_apply_import_page.dart';
  25. class ExpenseCreatePage extends ConsumerStatefulWidget {
  26. final String? editId;
  27. const ExpenseCreatePage({super.key, this.editId});
  28. @override
  29. ConsumerState<ExpenseCreatePage> createState() => _ExpenseCreatePageState();
  30. }
  31. class _ExpenseCreatePageState extends ConsumerState<ExpenseCreatePage>
  32. with WidgetsBindingObserver {
  33. final _purposeController = TextEditingController();
  34. final _purposeFocus = FocusNode();
  35. final _remarkController = TextEditingController();
  36. final _remarkFocus = FocusNode();
  37. final _scrollCtrl = ScrollController();
  38. final _detailsSectionKey = GlobalKey();
  39. late final AttachmentPickerController _attachmentController;
  40. bool _attachAvailable = false;
  41. late Future<bool> _draftFuture;
  42. bool _draftHandled = false;
  43. bool _isPoppingToNative = false;
  44. // ── 参考数据(从 API 加载) ──
  45. List<CostTypeItem> _costTypes = [];
  46. List<ProjectCodeItem> _projects = [];
  47. List<DepartmentItem> _departments = [];
  48. List<CustomerItem> _customers = [];
  49. List<CurrencyItem> _currencies = [];
  50. List<EmployeeItem> _employees = [];
  51. bool _refDataLoading = true;
  52. bool _addingDetail = false;
  53. // ── 报销部门 ──
  54. String _selectedDeptId = '';
  55. String _selectedDeptName = '';
  56. @override
  57. void initState() {
  58. super.initState();
  59. WidgetsBinding.instance.addObserver(this);
  60. SystemChrome.setSystemUIOverlayStyle(
  61. const SystemUiOverlayStyle(
  62. statusBarColor: Colors.transparent,
  63. statusBarIconBrightness: Brightness.dark,
  64. ),
  65. );
  66. _attachmentController = AttachmentPickerController(maxCount: 9)
  67. ..addListener(() => setState(() {}));
  68. _checkAttachHealth();
  69. _purposeFocus.addListener(() => _ensureVisible(_purposeFocus));
  70. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  71. _costTypes = [];
  72. _projects = [];
  73. _departments = [];
  74. _customers = [];
  75. _employees = [];
  76. _refDataLoading = true;
  77. _refDataFuture = null;
  78. _draftFuture = widget.editId == null
  79. ? ExpenseCreateController.hasDraft()
  80. : Future.value(false);
  81. _loadRefData();
  82. }
  83. Future<void>? _refDataFuture;
  84. Future<void> _loadRefData() async {
  85. if (_refDataFuture != null) return _refDataFuture!;
  86. final completer = Completer<void>();
  87. _refDataFuture = completer.future;
  88. try {
  89. final api = ref.read(expenseApiProvider);
  90. final results = await Future.wait([
  91. api.getCostTypes(),
  92. api.getProjectCodes(),
  93. api.getDepartments(),
  94. api.getCustomers(),
  95. api.getCurrencies(),
  96. api.getEmployees(),
  97. ]);
  98. if (!mounted) {
  99. completer.complete();
  100. return;
  101. }
  102. setState(() {
  103. _costTypes = results[0] as List<CostTypeItem>;
  104. _projects = results[1] as List<ProjectCodeItem>;
  105. _departments = results[2] as List<DepartmentItem>;
  106. _customers = results[3] as List<CustomerItem>;
  107. _currencies = results[4] as List<CurrencyItem>;
  108. _employees = results[5] as List<EmployeeItem>;
  109. _refDataLoading = false;
  110. _autoSelectDept();
  111. });
  112. completer.complete();
  113. } catch (_) {
  114. if (!mounted) {
  115. completer.complete();
  116. return;
  117. }
  118. setState(() => _refDataLoading = false);
  119. completer.complete();
  120. } finally {
  121. _refDataFuture = null;
  122. }
  123. }
  124. void _scrollToDetailSection() {
  125. WidgetsBinding.instance.addPostFrameCallback((_) {
  126. if (_scrollCtrl.hasClients && _detailsSectionKey.currentContext != null) {
  127. Scrollable.ensureVisible(
  128. _detailsSectionKey.currentContext!,
  129. alignment: 0.0,
  130. duration: const Duration(milliseconds: 300),
  131. );
  132. }
  133. });
  134. }
  135. void _ensureVisible(FocusNode node) {
  136. if (!node.hasFocus) return;
  137. WidgetsBinding.instance.addPostFrameCallback((_) {
  138. if (node.hasFocus && _scrollCtrl.hasClients) {
  139. final ctx = node.context;
  140. if (ctx != null) {
  141. Scrollable.ensureVisible(
  142. ctx,
  143. alignment: 0.3,
  144. duration: const Duration(milliseconds: 300),
  145. );
  146. }
  147. }
  148. });
  149. }
  150. @override
  151. void dispose() {
  152. WidgetsBinding.instance.removeObserver(this);
  153. _purposeController.dispose();
  154. _purposeFocus.dispose();
  155. _remarkController.dispose();
  156. _remarkFocus.dispose();
  157. _scrollCtrl.dispose();
  158. _attachmentController.dispose();
  159. super.dispose();
  160. }
  161. @override
  162. void didChangeAppLifecycleState(AppLifecycleState state) {
  163. if (state == AppLifecycleState.resumed) {
  164. FocusScope.of(context).unfocus();
  165. if (_isPoppingToNative) {
  166. _isPoppingToNative = false;
  167. HostAppChannel.refresh();
  168. // 重置表单数据
  169. _purposeController.clear();
  170. _remarkController.clear();
  171. _selectedDeptId = '';
  172. _selectedDeptName = '';
  173. _attachmentController.clear();
  174. _attachAvailable = false;
  175. _addingDetail = false;
  176. // 重置参考数据
  177. _costTypes = [];
  178. _projects = [];
  179. _departments = [];
  180. _customers = [];
  181. _employees = [];
  182. _currencies = [];
  183. _refDataFuture = null;
  184. _refDataLoading = true;
  185. // 重置草稿和控制器状态
  186. _draftHandled = false;
  187. _draftFuture = widget.editId == null
  188. ? ExpenseCreateController.hasDraft()
  189. : Future.value(false);
  190. ref.read(expenseCreateProvider(widget.editId).notifier).reset();
  191. // 重新加载
  192. _loadRefData();
  193. _checkAttachHealth();
  194. }
  195. }
  196. }
  197. @override
  198. Widget build(BuildContext context) {
  199. final controller = ref.watch(expenseCreateProvider(widget.editId).notifier);
  200. final state = ref.watch(expenseCreateProvider(widget.editId));
  201. final r = ResponsiveHelper.of(context);
  202. final l10n = AppLocalizations.of(context);
  203. setNavBarTitle(context, ref, NavBarConfig(
  204. title: widget.editId != null
  205. ? l10n.get('editExpense')
  206. : l10n.get('expenseApply'),
  207. showBack: true,
  208. onBack: () => _doPop(),
  209. ));
  210. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  211. final bottomInset = MediaQuery.of(context).padding.bottom;
  212. Widget pageContent = PopScope(
  213. canPop: false,
  214. onPopInvokedWithResult: (didPop, _) {
  215. if (didPop) return;
  216. _doPop();
  217. },
  218. child: Column(
  219. children: [
  220. Expanded(
  221. child: Align(
  222. alignment: Alignment.topCenter,
  223. child: ConstrainedBox(
  224. constraints: BoxConstraints(maxWidth: r.formMaxWidth),
  225. child: SingleChildScrollView(
  226. controller: _scrollCtrl,
  227. padding: const EdgeInsets.all(16),
  228. child: Column(
  229. crossAxisAlignment: CrossAxisAlignment.start,
  230. children: [
  231. _buildImportLink(),
  232. const SizedBox(height: 16),
  233. _buildBasicInfoSection(controller, state),
  234. const SizedBox(height: 16),
  235. Container(
  236. key: _detailsSectionKey,
  237. child: _buildDetailSection(controller, state),
  238. ),
  239. const SizedBox(height: 16),
  240. _buildAttachmentSection(controller, state),
  241. const SizedBox(height: 24),
  242. _buildPageFooter(),
  243. ],
  244. ),
  245. ),
  246. ),
  247. ),
  248. ),
  249. ColoredBox(
  250. color: colors.bgCard,
  251. child: Column(
  252. mainAxisSize: MainAxisSize.min,
  253. children: [
  254. _buildBottomButtons(controller, state),
  255. if (bottomInset > 0) SizedBox(height: bottomInset),
  256. ],
  257. ),
  258. ),
  259. ],
  260. ),
  261. );
  262. return FutureBuilder<bool>(
  263. future: _draftFuture,
  264. builder: (ctx, snapshot) {
  265. final hasDraft = snapshot.hasData && snapshot.data == true;
  266. if (hasDraft && !_draftHandled) {
  267. _draftHandled = true;
  268. WidgetsBinding.instance.addPostFrameCallback((_) {
  269. if (mounted) _showDraftDialog();
  270. });
  271. }
  272. return pageContent;
  273. },
  274. );
  275. }
  276. void _showDraftDialog() {
  277. final l10n = AppLocalizations.of(context);
  278. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  279. FocusManager.instance.primaryFocus?.unfocus();
  280. showDialog(
  281. context: context,
  282. barrierDismissible: false,
  283. builder: (ctx) => TDAlertDialog(
  284. title: l10n.get('draftFound'),
  285. content: l10n.get('draftRestorePrompt'),
  286. leftBtn: TDDialogButtonOptions(
  287. title: l10n.get('discard'),
  288. titleColor: colors.textSecondary,
  289. action: () {
  290. Navigator.pop(ctx);
  291. ExpenseCreateController.deleteDraft();
  292. },
  293. ),
  294. rightBtn: TDDialogButtonOptions(
  295. title: l10n.get('restore'),
  296. titleColor: colors.primary,
  297. action: () async {
  298. Navigator.pop(ctx);
  299. final draft = await ExpenseCreateController.loadDraft();
  300. if (draft != null && mounted) {
  301. final api = ref.read(expenseApiProvider);
  302. ref
  303. .read(expenseCreateProvider(widget.editId).notifier)
  304. .restoreFromDraft(draft, api);
  305. _purposeController.text = draft.purpose;
  306. _remarkController.text = draft.remark;
  307. if (draft.attachments.isNotEmpty) {
  308. await _attachmentController.restoreFromPaths(draft.attachments);
  309. }
  310. }
  311. },
  312. ),
  313. ),
  314. );
  315. }
  316. // ═══ API 数据 → 弹窗类型转换 ═══
  317. List<CostCategory> get _dialogCategories => _costTypes
  318. .map(
  319. (c) => CostCategory(
  320. code: c.typeNo,
  321. nameKey: c.typeName,
  322. acctSubjectId: c.accNo,
  323. acctSubjectName: c.accName,
  324. ),
  325. )
  326. .toList();
  327. List<Project> get _dialogProjects => _projects
  328. .map((p) => Project(id: int.tryParse(p.objNo) ?? 0, name: p.name))
  329. .toList();
  330. List<CostDept> get _dialogCostDepts =>
  331. _departments.map((d) => CostDept(id: d.dep, name: d.name)).toList();
  332. String _currencyLabel(String code) {
  333. final match = _currencies.where((c) => c.curId == code);
  334. return match.isNotEmpty ? '${match.first.curId}/${match.first.name}' : code;
  335. }
  336. List<CustomerVendor> get _dialogCustomers =>
  337. _customers.map((c) => CustomerVendor(id: c.cusNo, name: c.name)).toList();
  338. List<EmployeeItem> get _dialogEmployees => _employees;
  339. void _autoSelectDept() {
  340. if (_selectedDeptId.isNotEmpty) return;
  341. final dep = HostAppChannel.dep;
  342. if (dep.isEmpty) return;
  343. final match = _departments.where((d) => d.dep == dep);
  344. if (match.isNotEmpty) {
  345. _selectedDeptId = match.first.dep;
  346. _selectedDeptName = match.first.name;
  347. }
  348. }
  349. void _showDeptPicker() {
  350. if (_departments.isEmpty) {
  351. TDToast.showText(
  352. AppLocalizations.of(context).get('noData'),
  353. context: context,
  354. );
  355. return;
  356. }
  357. final l10n = AppLocalizations.of(context);
  358. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  359. final labels = _departments.map((d) => '${d.dep}/${d.name}').toList();
  360. FocusManager.instance.primaryFocus?.unfocus();
  361. TDPicker.showMultiPicker(
  362. context,
  363. title: l10n.get('expenseDept'),
  364. backgroundColor: colors.bgCard,
  365. data: [labels],
  366. onConfirm: (selected) {
  367. if (selected.isNotEmpty && selected[0] is int) {
  368. final idx = selected[0] as int;
  369. if (idx >= 0 && idx < labels.length) {
  370. Navigator.of(context).pop();
  371. setState(() {
  372. _selectedDeptId = _departments[idx].dep;
  373. _selectedDeptName = _departments[idx].name;
  374. });
  375. }
  376. }
  377. },
  378. );
  379. }
  380. Map<String, dynamic> _buildSubmitData(ExpenseCreateState state) {
  381. final expense = state.expense;
  382. return {
  383. 'HeadData': {
  384. 'BX_DD': _today(),
  385. 'DEP': _selectedDeptId,
  386. 'USR_NO': HostAppChannel.usr,
  387. 'PAY_ID': expense.paymentMethod,
  388. 'PRT_SW': 'N',
  389. 'USR': HostAppChannel.usr,
  390. 'REM': expense.remark,
  391. 'CUR_ID': expense.currencyCode,
  392. 'EXC_RTO': 1,
  393. 'REASON': expense.purpose,
  394. 'VOH_ID': expense.isGenerateVoucher ? 'T' : 'F',
  395. },
  396. 'BodyData1': expense.details.asMap().entries.map((e) {
  397. final i = e.key;
  398. final d = e.value;
  399. return {
  400. 'ITM': i + 1,
  401. 'BX_DD': _today(),
  402. 'ACC_NO': d.acctSubjectId,
  403. 'AMT': d.totalAmount,
  404. 'AMTN': d.amount,
  405. 'AMTN_SH': d.approvedAmount > 0 ? d.approvedAmount : d.totalAmount,
  406. 'REM': d.remark,
  407. 'CUST': d.customerVendorId,
  408. 'IDX_NO': d.expenseCategory,
  409. 'OBJ_NO': d.projectId.isNotEmpty ? d.projectId : '',
  410. 'TAX': d.taxAmount,
  411. 'TAX_RTO': d.taxRate,
  412. 'DEP': d.costDeptId,
  413. 'AE_NO': d.aeNo,
  414. 'AE_DD': d.aeDd,
  415. 'BNK_NO': d.bankName,
  416. 'BNK_ID': d.bankAccount,
  417. 'ACCNAME': d.bankAccountName,
  418. 'SQ_MAN': d.sqMan.isNotEmpty ? d.sqMan : HostAppChannel.usr,
  419. };
  420. }).toList(),
  421. };
  422. }
  423. Widget _buildImportLink() {
  424. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  425. final l10n = AppLocalizations.of(context);
  426. return GestureDetector(
  427. onTap: () async {
  428. final result = await GoRouter.of(
  429. context,
  430. ).push<List<ImportableItem>>('/expense/import-apply');
  431. if (result == null || result.isEmpty || !mounted) return;
  432. // 将选中的导入数据转换为明细,先移除同单号的旧数据
  433. final controller = ref.read(
  434. expenseCreateProvider(widget.editId).notifier,
  435. );
  436. final aeNos = result.map((e) => e.aeNo).toSet();
  437. for (final aeNo in aeNos) {
  438. controller.removeDetailsByAeNo(aeNo);
  439. }
  440. final now = DateTime.now();
  441. for (final item in result) {
  442. controller.addDetail(
  443. ExpenseDetailModel(
  444. id: '${now.millisecondsSinceEpoch}_${item.itm}',
  445. expenseId: '',
  446. expenseApplyId: '',
  447. expenseApplyNo: item.aeNo,
  448. expenseApplyDate: item.aeDd.isNotEmpty
  449. ? DateTime.tryParse(item.aeDd)
  450. : null,
  451. expenseCategory: item.typeNo,
  452. categoryName: item.typeName,
  453. purpose: item.rem,
  454. priority: item.priority,
  455. projectId: item.objNo,
  456. projectName: item.objName,
  457. costDeptId: item.dep,
  458. costDeptName: item.depName,
  459. acctSubjectId: item.accNo,
  460. acctSubjectName: item.accName,
  461. amount: item.amtnYj,
  462. taxRate: 0,
  463. taxAmount: 0,
  464. totalAmount: item.amtnYj,
  465. currencyCode: '',
  466. exchangeRate: 1.0,
  467. baseAmount: item.amtnYj,
  468. approvedAmount: item.amtnYj,
  469. customerVendorId: '',
  470. customerVendorName: '',
  471. bankName: '',
  472. bankAccountName: '',
  473. bankAccount: '',
  474. remark: item.rem,
  475. sortOrder: item.itm,
  476. attachments: const [],
  477. sqMan: item.sqMan,
  478. sqManName: item.sqName,
  479. aeNo: item.aeNo,
  480. aeDd: item.aeDd,
  481. createTime: now,
  482. updateTime: now,
  483. ),
  484. );
  485. }
  486. controller.recalculateAmount();
  487. TDToast.showSuccess(l10n.get('importSuccess'), context: context);
  488. _scrollToDetailSection();
  489. },
  490. child: Container(
  491. height: 44,
  492. decoration: BoxDecoration(
  493. color: colors.primaryLight,
  494. borderRadius: BorderRadius.circular(8),
  495. ),
  496. child: Row(
  497. mainAxisAlignment: MainAxisAlignment.center,
  498. children: [
  499. Icon(Icons.download, size: 14, color: colors.primary),
  500. const SizedBox(width: 8),
  501. Text(
  502. l10n.get('importApprovedPreApp'),
  503. style: TextStyle(
  504. fontSize: AppFontSizes.body,
  505. color: colors.primary,
  506. ),
  507. ),
  508. ],
  509. ),
  510. ),
  511. );
  512. }
  513. Widget _buildBasicInfoSection(
  514. ExpenseCreateController controller,
  515. ExpenseCreateState state,
  516. ) {
  517. final l10n = AppLocalizations.of(context);
  518. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  519. final expense = state.expense;
  520. return FormSection(
  521. title: l10n.get('basicInfo'),
  522. leadingIcon: Icons.info_outline,
  523. children: [
  524. FormFieldRow(
  525. label: l10n.get('date'),
  526. value: _today(),
  527. readOnly: true,
  528. showArrow: false,
  529. ),
  530. const SizedBox(height: 16),
  531. FormFieldRow(
  532. label: l10n.get('expensePersonnel'),
  533. value:
  534. HostAppChannel.usr.isNotEmpty && HostAppChannel.usrName.isNotEmpty
  535. ? '${HostAppChannel.usr}/${HostAppChannel.usrName}'
  536. : '--',
  537. readOnly: true,
  538. showArrow: false,
  539. ),
  540. const SizedBox(height: 16),
  541. FormFieldRow(
  542. label: l10n.get('expenseDept'),
  543. value: _selectedDeptName.isNotEmpty
  544. ? '$_selectedDeptId/$_selectedDeptName'
  545. : null,
  546. hint: l10n.get('pleaseSelect'),
  547. onTap: _refDataLoading ? null : () => _showDeptPicker(),
  548. ),
  549. const SizedBox(height: 16),
  550. _label(l10n.get('expenseReason'), required: true),
  551. const SizedBox(height: 8),
  552. TDTextarea(
  553. controller: _purposeController,
  554. focusNode: _purposeFocus,
  555. hintText: l10n.get('enterExpenseReason'),
  556. maxLines: 4,
  557. minLines: 1,
  558. maxLength: 500,
  559. indicator: true,
  560. padding: EdgeInsets.zero,
  561. bordered: true,
  562. backgroundColor: colors.bgPage,
  563. onChanged: (_) => controller.updatePurpose(_purposeController.text),
  564. ),
  565. const SizedBox(height: 16),
  566. FormFieldRow(
  567. label: l10n.get('paymentMethod'),
  568. value: expense.paymentMethod,
  569. hint: l10n.get('pleaseEnter'),
  570. onTap: () => _showTextInput(
  571. l10n.get('paymentMethod'),
  572. (v) => controller.updatePaymentMethod(v),
  573. initialText: expense.paymentMethod,
  574. ),
  575. onClear: () => controller.updatePaymentMethod(''),
  576. ),
  577. const SizedBox(height: 16),
  578. FormFieldRow(
  579. label: l10n.get('currency'),
  580. value: expense.currencyCode.isNotEmpty
  581. ? _currencyLabel(expense.currencyCode)
  582. : null,
  583. hint: l10n.get('selectCurrency'),
  584. onTap: () => _showCurrencyPicker(controller, expense.currencyCode),
  585. onClear: () => controller.updateCurrencyCode(''),
  586. ),
  587. const SizedBox(height: 16),
  588. Row(
  589. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  590. children: [
  591. Text(l10n.get('generateVoucher'), style: TextStyle(fontSize: AppFontSizes.subtitle, color: colors.textSecondary)),
  592. TDSwitch(
  593. isOn: expense.isGenerateVoucher,
  594. onChanged: (v) {
  595. controller.setGenerateVoucher(v);
  596. return v;
  597. },
  598. ),
  599. ],
  600. ),
  601. const SizedBox(height: 16),
  602. _label(l10n.get('remark')),
  603. const SizedBox(height: 8),
  604. TDTextarea(
  605. controller: _remarkController,
  606. focusNode: _remarkFocus,
  607. hintText: l10n.get('enterRemark'),
  608. maxLines: 3,
  609. minLines: 1,
  610. maxLength: 500,
  611. indicator: true,
  612. padding: EdgeInsets.zero,
  613. bordered: true,
  614. backgroundColor: colors.bgPage,
  615. onChanged: (_) => controller.updateRemark(_remarkController.text),
  616. ),
  617. ],
  618. );
  619. }
  620. Widget _buildDetailSection(
  621. ExpenseCreateController controller,
  622. ExpenseCreateState state,
  623. ) {
  624. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  625. final l10n = AppLocalizations.of(context);
  626. final totalApproved = state.expense.details.fold<double>(
  627. 0,
  628. (sum, d) => sum + d.approvedAmount,
  629. );
  630. return FormSection(
  631. title: l10n.get('expenseDetails'),
  632. leadingIcon: Icons.receipt_long_outlined,
  633. showAction: true,
  634. actionText: l10n.get('add'),
  635. onActionTap: () => _showAddDetailDialog(controller),
  636. children: [
  637. if (state.expense.details.isEmpty)
  638. Padding(
  639. padding: const EdgeInsets.symmetric(vertical: 8),
  640. child: Text(
  641. l10n.get('noDetailHint'),
  642. style: TextStyle(
  643. fontSize: AppFontSizes.subtitle,
  644. color: colors.textPlaceholder,
  645. ),
  646. ),
  647. )
  648. else
  649. ...state.expense.details.asMap().entries.map((entry) {
  650. final d = entry.value;
  651. return GestureDetector(
  652. onTap: () =>
  653. _showAddDetailDialog(controller, editIndex: entry.key),
  654. child: Container(
  655. margin: const EdgeInsets.symmetric(vertical: 6),
  656. padding: const EdgeInsets.all(12),
  657. decoration: BoxDecoration(
  658. color: colors.bgPage,
  659. borderRadius: BorderRadius.circular(8),
  660. ),
  661. child: Row(
  662. children: [
  663. Expanded(
  664. child: Column(
  665. crossAxisAlignment: CrossAxisAlignment.start,
  666. children: [
  667. Row(
  668. children: [
  669. Expanded(
  670. child: Text(
  671. d.categoryName.isNotEmpty
  672. ? '${d.expenseCategory}/${d.categoryName}'
  673. : d.expenseCategory,
  674. style: TextStyle(
  675. fontSize: AppFontSizes.body,
  676. fontWeight: FontWeight.w500,
  677. color: colors.textPrimary,
  678. ),
  679. ),
  680. ),
  681. Column(
  682. crossAxisAlignment: CrossAxisAlignment.end,
  683. children: [
  684. Text(
  685. '¥${d.totalAmount.toStringAsFixed(2)}',
  686. style: TextStyle(
  687. fontSize: AppFontSizes.body,
  688. fontWeight: FontWeight.w600,
  689. color: colors.amountPrimary,
  690. ),
  691. ),
  692. if (d.approvedAmount > 0)
  693. Text(
  694. '¥${d.approvedAmount.toStringAsFixed(2)}',
  695. style: TextStyle(
  696. fontSize: AppFontSizes.body,
  697. fontWeight: FontWeight.w600,
  698. color: colors.success,
  699. ),
  700. ),
  701. ],
  702. ),
  703. ],
  704. ),
  705. const SizedBox(height: 2),
  706. Text(
  707. '${l10n.get('amountExcludingTax')}: ¥${d.amount.toStringAsFixed(2)}',
  708. style: TextStyle(
  709. fontSize: AppFontSizes.caption,
  710. color: colors.textSecondary,
  711. ),
  712. ),
  713. if (d.taxAmount > 0)
  714. Text(
  715. '${l10n.get('taxAmount')}: ¥${d.taxAmount.toStringAsFixed(2)}',
  716. style: TextStyle(
  717. fontSize: AppFontSizes.caption,
  718. color: colors.textSecondary,
  719. ),
  720. ),
  721. if (d.acctSubjectName.isNotEmpty)
  722. Text(
  723. '${l10n.get('acctSubject')}: ${d.acctSubjectId}/${d.acctSubjectName}',
  724. maxLines: 1,
  725. overflow: TextOverflow.ellipsis,
  726. style: TextStyle(
  727. fontSize: AppFontSizes.caption,
  728. color: colors.textSecondary,
  729. ),
  730. ),
  731. if (d.aeNo.isNotEmpty)
  732. Text(
  733. '${l10n.get('expenseApplyNo')}: ${d.aeNo}',
  734. maxLines: 1,
  735. overflow: TextOverflow.ellipsis,
  736. style: TextStyle(
  737. fontSize: AppFontSizes.caption,
  738. color: colors.textSecondary,
  739. ),
  740. ),
  741. if (d.aeDd.isNotEmpty)
  742. Text(
  743. '${l10n.get('applyDate')}: ${d.aeDd}',
  744. style: TextStyle(
  745. fontSize: AppFontSizes.caption,
  746. color: colors.textSecondary,
  747. ),
  748. ),
  749. if (d.projectName.isNotEmpty)
  750. Text(
  751. '${l10n.get('project')}: ${d.projectId}/${d.projectName}',
  752. maxLines: 1,
  753. overflow: TextOverflow.ellipsis,
  754. style: TextStyle(
  755. fontSize: AppFontSizes.caption,
  756. color: colors.textSecondary,
  757. ),
  758. ),
  759. if (d.costDeptName.isNotEmpty)
  760. Text(
  761. '${l10n.get('dept')}: ${d.costDeptId}/${d.costDeptName}',
  762. maxLines: 1,
  763. overflow: TextOverflow.ellipsis,
  764. style: TextStyle(
  765. fontSize: AppFontSizes.caption,
  766. color: colors.textSecondary,
  767. ),
  768. ),
  769. if (d.customerVendorName.isNotEmpty)
  770. Text(
  771. '${l10n.get('customerVendor')}: ${d.customerVendorId}/${d.customerVendorName}',
  772. maxLines: 1,
  773. overflow: TextOverflow.ellipsis,
  774. style: TextStyle(
  775. fontSize: AppFontSizes.caption,
  776. color: colors.textSecondary,
  777. ),
  778. ),
  779. if (d.bankAccountName.isNotEmpty)
  780. Text(
  781. '${l10n.get('bankAccountName')}: ${d.bankAccountName}',
  782. maxLines: 1,
  783. overflow: TextOverflow.ellipsis,
  784. style: TextStyle(
  785. fontSize: AppFontSizes.caption,
  786. color: colors.textSecondary,
  787. ),
  788. ),
  789. if (d.bankName.isNotEmpty)
  790. Text(
  791. '${l10n.get('bankName')}: ${d.bankName}',
  792. maxLines: 1,
  793. overflow: TextOverflow.ellipsis,
  794. style: TextStyle(
  795. fontSize: AppFontSizes.caption,
  796. color: colors.textSecondary,
  797. ),
  798. ),
  799. if (d.bankAccount.isNotEmpty)
  800. Text(
  801. '${l10n.get('bankAccount')}: ${d.bankAccount}',
  802. maxLines: 1,
  803. overflow: TextOverflow.ellipsis,
  804. style: TextStyle(
  805. fontSize: AppFontSizes.caption,
  806. color: colors.textSecondary,
  807. ),
  808. ),
  809. if (d.sqManName.isNotEmpty)
  810. Text(
  811. '${l10n.get('applicant')}: ${d.sqManName.isNotEmpty ? d.sqManName : d.sqMan}',
  812. style: TextStyle(
  813. fontSize: AppFontSizes.caption,
  814. color: colors.textSecondary,
  815. ),
  816. ),
  817. if (d.remark.isNotEmpty)
  818. Text(
  819. '${l10n.get('remark')}: ${d.remark}',
  820. maxLines: 2,
  821. overflow: TextOverflow.ellipsis,
  822. style: TextStyle(
  823. fontSize: AppFontSizes.caption,
  824. color: colors.textSecondary,
  825. ),
  826. ),
  827. ],
  828. ),
  829. ),
  830. const SizedBox(width: 8),
  831. GestureDetector(
  832. onTap: () {
  833. controller.removeDetail(entry.key);
  834. controller.recalculateAmount();
  835. },
  836. child: Icon(
  837. Icons.close,
  838. size: 18,
  839. color: colors.textSecondary,
  840. ),
  841. ),
  842. ],
  843. ),
  844. ),
  845. );
  846. }),
  847. const SizedBox(height: 8),
  848. Row(
  849. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  850. children: [
  851. Text(
  852. l10n.get('totalExpense'),
  853. style: TextStyle(
  854. fontSize: AppFontSizes.body,
  855. fontWeight: FontWeight.w600,
  856. color: colors.textPrimary,
  857. ),
  858. ),
  859. Text(
  860. '¥${state.expense.totalAmount.toStringAsFixed(2)}',
  861. style: TextStyle(
  862. fontSize: AppFontSizes.subtitle,
  863. fontWeight: FontWeight.w700,
  864. color: colors.amountPrimary,
  865. ),
  866. ),
  867. ],
  868. ),
  869. const SizedBox(height: 4),
  870. Row(
  871. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  872. children: [
  873. Text(
  874. l10n.get('approvedTotal'),
  875. style: TextStyle(
  876. fontSize: AppFontSizes.body,
  877. fontWeight: FontWeight.w600,
  878. color: colors.textPrimary,
  879. ),
  880. ),
  881. Text(
  882. '¥${totalApproved.toStringAsFixed(2)}',
  883. style: TextStyle(
  884. fontSize: AppFontSizes.subtitle,
  885. fontWeight: FontWeight.w700,
  886. color: totalApproved > 0 ? colors.success : colors.textPrimary,
  887. ),
  888. ),
  889. ],
  890. ),
  891. ],
  892. );
  893. }
  894. Future<void> _checkAttachHealth() async {
  895. if (mounted) setState(() => _attachAvailable = false);
  896. try {
  897. final api = ref.read(expenseApiProvider);
  898. final ok = await api.checkAttachHealth();
  899. if (mounted) setState(() => _attachAvailable = ok);
  900. } catch (_) {
  901. if (mounted) setState(() => _attachAvailable = false);
  902. }
  903. }
  904. Widget _buildAttachmentSection(
  905. ExpenseCreateController controller,
  906. ExpenseCreateState state,
  907. ) {
  908. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  909. final l10n = AppLocalizations.of(context);
  910. final children = <Widget>[];
  911. if (!_attachAvailable) {
  912. children.add(Text(l10n.get('attachServiceUnavailable'),
  913. style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textPlaceholder)));
  914. } else {
  915. children.addAll([
  916. Text(l10n.get('maxAttachment'),
  917. style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textPlaceholder)),
  918. const SizedBox(height: 8),
  919. ]);
  920. }
  921. return FormSection(
  922. title: l10n.get('attachmentUpload'),
  923. leadingIcon: Icons.attach_file_outlined,
  924. children: [
  925. ...children,
  926. if (_attachAvailable)
  927. AttachmentPicker(
  928. controller: _attachmentController,
  929. maxImageSizeMB: 10,
  930. maxFileSizeMB: 20,
  931. allowedExtensions: const [
  932. 'pdf',
  933. 'doc',
  934. 'docx',
  935. 'xls',
  936. 'xlsx',
  937. 'ppt',
  938. 'pptx',
  939. 'txt',
  940. ],
  941. onFileRejected: (file, reason) {
  942. if (context.mounted) TDToast.showText(reason, context: context);
  943. },
  944. ),
  945. ],
  946. );
  947. }
  948. Widget _buildBottomButtons(
  949. ExpenseCreateController controller,
  950. ExpenseCreateState state,
  951. ) {
  952. final l10n = AppLocalizations.of(context);
  953. return ActionBar(
  954. showLeft: false,
  955. centerLabel: l10n.get('saveDraft'),
  956. rightLabel: l10n.get('submit'),
  957. centerTextOnly: true,
  958. onCenterTap: () async {
  959. if (state.isSubmitting) return;
  960. FocusScope.of(context).unfocus();
  961. controller.updateAttachments(_attachmentController.toPathList());
  962. controller.updateDept(_selectedDeptId, _selectedDeptName);
  963. final ok = await controller.saveDraft();
  964. if (mounted) {
  965. if (ok) {
  966. _forcePop();
  967. } else {
  968. TDToast.showFail(l10n.get('saveFailed'), context: context);
  969. }
  970. }
  971. },
  972. onRightTap: () async {
  973. if (state.isSubmitting) return;
  974. final err = _validate(l10n, state);
  975. if (err.isNotEmpty) {
  976. TDToast.showText(err.first, context: context);
  977. return;
  978. }
  979. FocusScope.of(context).unfocus();
  980. LoadingDialog.show(context, text: l10n.get('submitting'));
  981. try {
  982. final data = _buildSubmitData(state);
  983. final api = ref.read(expenseApiProvider);
  984. final billNo = await api.submit(data);
  985. // 上传附件(billNo 提取失败则跳过,不影响主流程)
  986. if (billNo != null) {
  987. final now = DateTime.now();
  988. final effDd = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} '
  989. '${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}.'
  990. '${now.millisecond.toString().padLeft(3, '0')}';
  991. final usr = HostAppChannel.usr;
  992. // 表头附件
  993. for (var i = 0; i < _attachmentController.files.length; i++) {
  994. final file = _attachmentController.files[i];
  995. try {
  996. await api.uploadAttachment(file.path, {
  997. 'BIL_ID': 'BX',
  998. 'BIL_NO': billNo,
  999. 'SRCITM': 0,
  1000. 'ITM': i + 1,
  1001. 'TAG': 1,
  1002. 'EFF_DD': effDd,
  1003. 'USR': usr,
  1004. 'FILENAME': file.name,
  1005. 'EXT': file.name.split('.').last,
  1006. });
  1007. } catch (_) {
  1008. // Attachment upload failure is non-fatal
  1009. }
  1010. }
  1011. // 明细附件
  1012. for (var i = 0; i < state.expense.details.length; i++) {
  1013. final detail = state.expense.details[i];
  1014. if (detail.attachments.isEmpty) continue;
  1015. for (var j = 0; j < detail.attachments.length; j++) {
  1016. final file = File(detail.attachments[j]);
  1017. try {
  1018. if (!await file.exists()) continue;
  1019. final fileName = file.path.split('/').last;
  1020. await api.uploadAttachment(file.path, {
  1021. 'BIL_ID': 'BX',
  1022. 'BIL_NO': billNo,
  1023. 'SRCITM': i + 1,
  1024. 'ITM': j + 1,
  1025. 'TAG': 1,
  1026. 'EFF_DD': effDd,
  1027. 'USR': usr,
  1028. 'FILENAME': fileName,
  1029. 'EXT': fileName.split('.').last,
  1030. });
  1031. } catch (_) {
  1032. // Detail attachment upload failure is non-fatal
  1033. }
  1034. }
  1035. }
  1036. }
  1037. await ExpenseCreateController.deleteDraft();
  1038. if (mounted) {
  1039. LoadingDialog.hide(context);
  1040. TDToast.showSuccess(
  1041. l10n.get('submittedAwaitingApproval'),
  1042. context: context,
  1043. );
  1044. GoRouter.of(context).go('/expense/list');
  1045. }
  1046. } catch (_) {
  1047. if (mounted) {
  1048. LoadingDialog.hide(context);
  1049. TDToast.showFail(l10n.get('submitFailedRetry'), context: context);
  1050. }
  1051. }
  1052. },
  1053. );
  1054. }
  1055. Future<void> _showAddDetailDialog(
  1056. ExpenseCreateController controller, {
  1057. int? editIndex,
  1058. }) async {
  1059. if (_addingDetail) return;
  1060. _addingDetail = true;
  1061. try {
  1062. final l10n = AppLocalizations.of(context);
  1063. if (_costTypes.isEmpty) {
  1064. TDToast.showText(l10n.get('noCostTypeData'), context: context);
  1065. return;
  1066. }
  1067. final state = controller.currentState;
  1068. ExpenseDetailInputData? initialData;
  1069. if (editIndex != null) {
  1070. final d = state.expense.details[editIndex];
  1071. initialData = ExpenseDetailInputData(
  1072. category: d.expenseCategory,
  1073. categoryName: d.categoryName,
  1074. acctSubjectId: d.acctSubjectId,
  1075. acctSubjectName: d.acctSubjectName,
  1076. purpose: d.purpose,
  1077. amount: d.amount,
  1078. taxRate: d.taxRate,
  1079. projectId: d.projectId,
  1080. projectName: d.projectName,
  1081. costDeptId: d.costDeptId,
  1082. costDeptName: d.costDeptName,
  1083. customerVendorId: d.customerVendorId,
  1084. customerVendorName: d.customerVendorName,
  1085. approvedAmount: d.approvedAmount,
  1086. bankName: d.bankName,
  1087. bankAccountName: d.bankAccountName,
  1088. bankAccount: d.bankAccount,
  1089. remark: d.remark,
  1090. attachmentPaths: d.attachments,
  1091. sqMan: d.sqMan,
  1092. sqManName: d.sqManName,
  1093. aeNo: d.aeNo,
  1094. aeDd: d.aeDd,
  1095. );
  1096. }
  1097. FocusManager.instance.primaryFocus?.unfocus();
  1098. final result = await ExpenseDetailDialog.show(
  1099. context,
  1100. categories: _dialogCategories,
  1101. projects: _dialogProjects,
  1102. costDepts: _dialogCostDepts,
  1103. customers: _dialogCustomers,
  1104. employees: _dialogEmployees,
  1105. l10n: l10n,
  1106. initialData: initialData,
  1107. checkAttachHealth: () => ref.read(expenseApiProvider).checkAttachHealth(),
  1108. );
  1109. if (result != null && mounted) {
  1110. final now = DateTime.now();
  1111. final detail = ExpenseDetailModel(
  1112. id: editIndex != null
  1113. ? state.expense.details[editIndex].id
  1114. : now.millisecondsSinceEpoch.toString(),
  1115. expenseId: '',
  1116. expenseCategory: result.category,
  1117. categoryName: result.categoryName,
  1118. purpose: result.purpose,
  1119. amount: result.taxRate > 0
  1120. ? result.amount / (1 + result.taxRate)
  1121. : result.amount,
  1122. taxRate: result.taxRate,
  1123. taxAmount: result.taxRate > 0
  1124. ? result.amount - result.amount / (1 + result.taxRate)
  1125. : 0,
  1126. totalAmount: result.amount,
  1127. projectId: result.projectId,
  1128. projectName: result.projectName,
  1129. costDeptId: result.costDeptId,
  1130. costDeptName: result.costDeptName,
  1131. acctSubjectId: result.acctSubjectId,
  1132. acctSubjectName: result.acctSubjectName,
  1133. customerVendorId: result.customerVendorId,
  1134. customerVendorName: result.customerVendorName,
  1135. approvedAmount: result.approvedAmount,
  1136. bankName: result.bankName,
  1137. bankAccountName: result.bankAccountName,
  1138. bankAccount: result.bankAccount,
  1139. sqMan: result.sqMan,
  1140. sqManName: result.sqManName,
  1141. aeNo: result.aeNo,
  1142. aeDd: result.aeDd,
  1143. remark: result.remark,
  1144. attachments: result.attachmentPaths,
  1145. createTime: now,
  1146. updateTime: now,
  1147. );
  1148. if (editIndex != null) {
  1149. controller.updateDetail(editIndex, detail);
  1150. } else {
  1151. controller.addDetail(detail);
  1152. }
  1153. controller.recalculateAmount();
  1154. }
  1155. } finally {
  1156. _addingDetail = false;
  1157. }
  1158. }
  1159. void _showCurrencyPicker(ExpenseCreateController controller, String cur) {
  1160. if (_currencies.isEmpty) {
  1161. TDToast.showText(
  1162. AppLocalizations.of(context).get('noData'),
  1163. context: context,
  1164. );
  1165. return;
  1166. }
  1167. final l10n = AppLocalizations.of(context);
  1168. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1169. final codes = _currencies.map((c) => c.curId).toList();
  1170. final labels = _currencies.map((c) => '${c.curId}/${c.name}').toList();
  1171. FocusManager.instance.primaryFocus?.unfocus();
  1172. TDPicker.showMultiPicker(
  1173. context,
  1174. title: l10n.get('selectCurrency'),
  1175. backgroundColor: colors.bgCard,
  1176. data: [labels],
  1177. onConfirm: (s) {
  1178. if (s.isNotEmpty && s[0] is int) {
  1179. final i = s[0] as int;
  1180. if (i >= 0 && i < codes.length) {
  1181. Navigator.of(context).pop();
  1182. controller.updateCurrencyCode(codes[i]);
  1183. }
  1184. }
  1185. },
  1186. );
  1187. }
  1188. void _showTextInput(
  1189. String title,
  1190. Function(String) onConfirm, {
  1191. String initialText = '',
  1192. }) {
  1193. FocusScope.of(context).unfocus();
  1194. FocusManager.instance.primaryFocus?.unfocus();
  1195. final l10n = AppLocalizations.of(context);
  1196. final c = TextEditingController(text: initialText);
  1197. showGeneralDialog(
  1198. context: context,
  1199. pageBuilder: (ctx, animation, secondaryAnimation) => TDInputDialog(
  1200. textEditingController: c,
  1201. title: title,
  1202. hintText: l10n.get('pleaseEnter'),
  1203. leftBtn: TDDialogButtonOptions(
  1204. title: l10n.get('cancel'),
  1205. action: () => Navigator.pop(ctx),
  1206. ),
  1207. rightBtn: TDDialogButtonOptions(
  1208. title: l10n.get('confirm'),
  1209. action: () {
  1210. onConfirm(c.text);
  1211. Navigator.pop(ctx);
  1212. },
  1213. ),
  1214. ),
  1215. );
  1216. }
  1217. Widget _label(String t, {bool required = false}) {
  1218. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1219. return Text.rich(
  1220. TextSpan(
  1221. children: [
  1222. TextSpan(
  1223. text: t,
  1224. style: TextStyle(
  1225. fontSize: AppFontSizes.subtitle,
  1226. color: colors.textSecondary,
  1227. ),
  1228. ),
  1229. if (required)
  1230. TextSpan(
  1231. text: ' *',
  1232. style: TextStyle(
  1233. fontSize: AppFontSizes.subtitle,
  1234. color: colors.danger,
  1235. ),
  1236. ),
  1237. ],
  1238. ),
  1239. );
  1240. }
  1241. List<String> _validate(AppLocalizations l10n, ExpenseCreateState state) {
  1242. final e = <String>[];
  1243. if (_purposeController.text.trim().isEmpty) {
  1244. e.add(l10n.get('enterExpenseReason'));
  1245. }
  1246. if (state.expense.details.isEmpty) {
  1247. e.add(l10n.get('addAtLeastOneDetail'));
  1248. }
  1249. return e;
  1250. }
  1251. bool _hasUnsaved(ExpenseCreateState state) =>
  1252. _purposeController.text.isNotEmpty ||
  1253. state.expense.paymentMethod.isNotEmpty ||
  1254. state.expense.currencyCode.isNotEmpty ||
  1255. _remarkController.text.isNotEmpty ||
  1256. state.expense.details.isNotEmpty ||
  1257. _attachmentController.files.isNotEmpty ||
  1258. _selectedDeptId.isNotEmpty;
  1259. void _doPop() {
  1260. final l10n = AppLocalizations.of(context);
  1261. final state = ref.read(expenseCreateProvider(widget.editId));
  1262. if (_hasUnsaved(state)) {
  1263. _showConfirmDialog(
  1264. l10n.get('confirmExit'),
  1265. l10n.get('unsavedContentWarning'),
  1266. l10n.get('continueEditing'),
  1267. l10n.get('discardAndExit'),
  1268. () async {
  1269. try {
  1270. await ExpenseCreateController.deleteDraft();
  1271. } catch (_) {}
  1272. if (!mounted) return;
  1273. setState(() {
  1274. _selectedDeptId = '';
  1275. _selectedDeptName = '';
  1276. });
  1277. ref.read(expenseCreateProvider(widget.editId).notifier).reset();
  1278. _forcePop();
  1279. },
  1280. );
  1281. } else {
  1282. _forcePop();
  1283. }
  1284. }
  1285. void _forcePop() {
  1286. FocusManager.instance.primaryFocus?.unfocus();
  1287. final router = GoRouter.of(context);
  1288. if (router.canPop()) {
  1289. router.pop();
  1290. } else {
  1291. _isPoppingToNative = true;
  1292. SystemNavigator.pop();
  1293. }
  1294. }
  1295. void _showConfirmDialog(
  1296. String title,
  1297. String content,
  1298. String leftText,
  1299. String rightText,
  1300. VoidCallback onConfirm,
  1301. ) {
  1302. FocusScope.of(context).unfocus();
  1303. FocusManager.instance.primaryFocus?.unfocus();
  1304. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1305. showDialog(
  1306. context: context,
  1307. useRootNavigator: true,
  1308. builder: (ctx) => TDAlertDialog(
  1309. title: title,
  1310. content: content,
  1311. buttonStyle: TDDialogButtonStyle.text,
  1312. leftBtn: TDDialogButtonOptions(
  1313. title: leftText,
  1314. titleColor: colors.primary,
  1315. action: () => Navigator.pop(ctx),
  1316. ),
  1317. rightBtn: TDDialogButtonOptions(
  1318. title: rightText,
  1319. titleColor: colors.danger,
  1320. action: () {
  1321. Navigator.pop(ctx);
  1322. onConfirm();
  1323. },
  1324. ),
  1325. ),
  1326. );
  1327. }
  1328. Widget _buildPageFooter() {
  1329. final l10n = AppLocalizations.of(context);
  1330. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1331. return Center(
  1332. child: Padding(
  1333. padding: const EdgeInsets.only(bottom: 16),
  1334. child: Row(
  1335. mainAxisSize: MainAxisSize.min,
  1336. children: [
  1337. Icon(
  1338. Icons.rocket_launch_outlined,
  1339. size: 16,
  1340. color: colors.textPlaceholder,
  1341. ),
  1342. const SizedBox(width: 6),
  1343. Text(
  1344. l10n.get('pageFooter'),
  1345. style: TextStyle(
  1346. fontSize: AppFontSizes.caption,
  1347. color: colors.textPlaceholder,
  1348. ),
  1349. ),
  1350. ],
  1351. ),
  1352. ),
  1353. );
  1354. }
  1355. String _today() {
  1356. final n = DateTime.now();
  1357. return '${n.year}-${n.month.toString().padLeft(2, '0')}-${n.day.toString().padLeft(2, '0')}';
  1358. }
  1359. }