expense_create_page.dart 48 KB

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