expense_create_page.dart 46 KB

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