expense_create_page.dart 47 KB

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