expense_create_page.dart 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. controller.updateAttachments(_attachmentController.toPathList());
  956. controller.updateDept(_selectedDeptId, _selectedDeptName);
  957. final ok = await controller.saveDraft();
  958. if (mounted) {
  959. if (ok) {
  960. _forcePop();
  961. } else {
  962. TDToast.showFail(l10n.get('saveFailed'), context: context);
  963. }
  964. }
  965. },
  966. onRightTap: () async {
  967. if (state.isSubmitting) return;
  968. final err = _validate(l10n, state);
  969. if (err.isNotEmpty) {
  970. TDToast.showText(err.first, context: context);
  971. return;
  972. }
  973. LoadingDialog.show(context, text: l10n.get('submitting'));
  974. try {
  975. final data = _buildSubmitData(state);
  976. final api = ref.read(expenseApiProvider);
  977. final billNo = await api.submit(data);
  978. // 上传附件(billNo 提取失败则跳过,不影响主流程)
  979. if (billNo != null) {
  980. final now = DateTime.now();
  981. final effDd = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} '
  982. '${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}.'
  983. '${now.millisecond.toString().padLeft(3, '0')}';
  984. final usr = HostAppChannel.usr;
  985. // 表头附件
  986. for (var i = 0; i < _attachmentController.files.length; i++) {
  987. final file = _attachmentController.files[i];
  988. try {
  989. await api.uploadAttachment(file.path, {
  990. 'BIL_ID': 'BX',
  991. 'BIL_NO': billNo,
  992. 'SRCITM': 0,
  993. 'ITM': i + 1,
  994. 'TAG': 1,
  995. 'EFF_DD': effDd,
  996. 'USR': usr,
  997. 'FILENAME': file.name,
  998. 'EXT': file.name.split('.').last,
  999. });
  1000. } catch (_) {
  1001. // Attachment upload failure is non-fatal
  1002. }
  1003. }
  1004. // 明细附件
  1005. for (var i = 0; i < state.expense.details.length; i++) {
  1006. final detail = state.expense.details[i];
  1007. if (detail.attachments.isEmpty) continue;
  1008. for (var j = 0; j < detail.attachments.length; j++) {
  1009. final file = File(detail.attachments[j]);
  1010. try {
  1011. if (!await file.exists()) continue;
  1012. final fileName = file.path.split('/').last;
  1013. await api.uploadAttachment(file.path, {
  1014. 'BIL_ID': 'BX',
  1015. 'BIL_NO': billNo,
  1016. 'SRCITM': i + 1,
  1017. 'ITM': j + 1,
  1018. 'TAG': 1,
  1019. 'EFF_DD': effDd,
  1020. 'USR': usr,
  1021. 'FILENAME': fileName,
  1022. 'EXT': fileName.split('.').last,
  1023. });
  1024. } catch (_) {
  1025. // Detail attachment upload failure is non-fatal
  1026. }
  1027. }
  1028. }
  1029. }
  1030. await ExpenseCreateController.deleteDraft();
  1031. if (mounted) {
  1032. LoadingDialog.hide(context);
  1033. TDToast.showSuccess(
  1034. l10n.get('submittedAwaitingApproval'),
  1035. context: context,
  1036. );
  1037. GoRouter.of(context).go('/expense/list');
  1038. }
  1039. } catch (_) {
  1040. if (mounted) {
  1041. LoadingDialog.hide(context);
  1042. TDToast.showFail(l10n.get('submitFailedRetry'), context: context);
  1043. }
  1044. }
  1045. },
  1046. );
  1047. }
  1048. Future<void> _showAddDetailDialog(
  1049. ExpenseCreateController controller, {
  1050. int? editIndex,
  1051. }) async {
  1052. if (_addingDetail) return;
  1053. _addingDetail = true;
  1054. try {
  1055. final l10n = AppLocalizations.of(context);
  1056. if (_costTypes.isEmpty) {
  1057. TDToast.showText(l10n.get('noCostTypeData'), context: context);
  1058. return;
  1059. }
  1060. final state = controller.currentState;
  1061. ExpenseDetailInputData? initialData;
  1062. if (editIndex != null) {
  1063. final d = state.expense.details[editIndex];
  1064. initialData = ExpenseDetailInputData(
  1065. category: d.expenseCategory,
  1066. categoryName: d.categoryName,
  1067. acctSubjectId: d.acctSubjectId,
  1068. acctSubjectName: d.acctSubjectName,
  1069. purpose: d.purpose,
  1070. amount: d.amount,
  1071. taxRate: d.taxRate,
  1072. projectId: d.projectId,
  1073. projectName: d.projectName,
  1074. costDeptId: d.costDeptId,
  1075. costDeptName: d.costDeptName,
  1076. customerVendorId: d.customerVendorId,
  1077. customerVendorName: d.customerVendorName,
  1078. approvedAmount: d.approvedAmount,
  1079. bankName: d.bankName,
  1080. bankAccountName: d.bankAccountName,
  1081. bankAccount: d.bankAccount,
  1082. remark: d.remark,
  1083. attachmentPaths: d.attachments,
  1084. sqMan: d.sqMan,
  1085. sqManName: d.sqManName,
  1086. aeNo: d.aeNo,
  1087. aeDd: d.aeDd,
  1088. );
  1089. }
  1090. final result = await ExpenseDetailDialog.show(
  1091. context,
  1092. categories: _dialogCategories,
  1093. projects: _dialogProjects,
  1094. costDepts: _dialogCostDepts,
  1095. customers: _dialogCustomers,
  1096. employees: _dialogEmployees,
  1097. l10n: l10n,
  1098. initialData: initialData,
  1099. checkAttachHealth: () => ref.read(expenseApiProvider).checkAttachHealth(),
  1100. );
  1101. if (result != null && mounted) {
  1102. final now = DateTime.now();
  1103. final detail = ExpenseDetailModel(
  1104. id: editIndex != null
  1105. ? state.expense.details[editIndex].id
  1106. : now.millisecondsSinceEpoch.toString(),
  1107. expenseId: '',
  1108. expenseCategory: result.category,
  1109. categoryName: result.categoryName,
  1110. purpose: result.purpose,
  1111. amount: result.taxRate > 0
  1112. ? result.amount / (1 + result.taxRate)
  1113. : result.amount,
  1114. taxRate: result.taxRate,
  1115. taxAmount: result.taxRate > 0
  1116. ? result.amount - result.amount / (1 + result.taxRate)
  1117. : 0,
  1118. totalAmount: result.amount,
  1119. projectId: result.projectId,
  1120. projectName: result.projectName,
  1121. costDeptId: result.costDeptId,
  1122. costDeptName: result.costDeptName,
  1123. acctSubjectId: result.acctSubjectId,
  1124. acctSubjectName: result.acctSubjectName,
  1125. customerVendorId: result.customerVendorId,
  1126. customerVendorName: result.customerVendorName,
  1127. approvedAmount: result.approvedAmount,
  1128. bankName: result.bankName,
  1129. bankAccountName: result.bankAccountName,
  1130. bankAccount: result.bankAccount,
  1131. sqMan: result.sqMan,
  1132. sqManName: result.sqManName,
  1133. aeNo: result.aeNo,
  1134. aeDd: result.aeDd,
  1135. remark: result.remark,
  1136. attachments: result.attachmentPaths,
  1137. createTime: now,
  1138. updateTime: now,
  1139. );
  1140. if (editIndex != null) {
  1141. controller.updateDetail(editIndex, detail);
  1142. } else {
  1143. controller.addDetail(detail);
  1144. }
  1145. controller.recalculateAmount();
  1146. }
  1147. } finally {
  1148. _addingDetail = false;
  1149. }
  1150. }
  1151. void _showCurrencyPicker(ExpenseCreateController controller, String cur) {
  1152. if (_currencies.isEmpty) {
  1153. TDToast.showText(
  1154. AppLocalizations.of(context).get('noData'),
  1155. context: context,
  1156. );
  1157. return;
  1158. }
  1159. final l10n = AppLocalizations.of(context);
  1160. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1161. final codes = _currencies.map((c) => c.curId).toList();
  1162. final labels = _currencies.map((c) => '${c.curId}/${c.name}').toList();
  1163. TDPicker.showMultiPicker(
  1164. context,
  1165. title: l10n.get('selectCurrency'),
  1166. backgroundColor: colors.bgCard,
  1167. data: [labels],
  1168. onConfirm: (s) {
  1169. if (s.isNotEmpty && s[0] is int) {
  1170. final i = s[0] as int;
  1171. if (i >= 0 && i < codes.length) {
  1172. Navigator.of(context).pop();
  1173. controller.updateCurrencyCode(codes[i]);
  1174. }
  1175. }
  1176. },
  1177. );
  1178. }
  1179. void _showTextInput(
  1180. String title,
  1181. Function(String) onConfirm, {
  1182. String initialText = '',
  1183. }) {
  1184. FocusScope.of(context).unfocus();
  1185. final l10n = AppLocalizations.of(context);
  1186. final c = TextEditingController(text: initialText);
  1187. showGeneralDialog(
  1188. context: context,
  1189. pageBuilder: (ctx, animation, secondaryAnimation) => TDInputDialog(
  1190. textEditingController: c,
  1191. title: title,
  1192. hintText: l10n.get('pleaseEnter'),
  1193. leftBtn: TDDialogButtonOptions(
  1194. title: l10n.get('cancel'),
  1195. action: () => Navigator.pop(ctx),
  1196. ),
  1197. rightBtn: TDDialogButtonOptions(
  1198. title: l10n.get('confirm'),
  1199. action: () {
  1200. onConfirm(c.text);
  1201. Navigator.pop(ctx);
  1202. },
  1203. ),
  1204. ),
  1205. );
  1206. }
  1207. Widget _label(String t, {bool required = false}) {
  1208. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1209. return Text.rich(
  1210. TextSpan(
  1211. children: [
  1212. TextSpan(
  1213. text: t,
  1214. style: TextStyle(
  1215. fontSize: AppFontSizes.subtitle,
  1216. color: colors.textSecondary,
  1217. ),
  1218. ),
  1219. if (required)
  1220. TextSpan(
  1221. text: ' *',
  1222. style: TextStyle(
  1223. fontSize: AppFontSizes.subtitle,
  1224. color: colors.danger,
  1225. ),
  1226. ),
  1227. ],
  1228. ),
  1229. );
  1230. }
  1231. List<String> _validate(AppLocalizations l10n, ExpenseCreateState state) {
  1232. final e = <String>[];
  1233. if (_purposeController.text.trim().isEmpty) {
  1234. e.add(l10n.get('enterExpenseReason'));
  1235. }
  1236. if (state.expense.details.isEmpty) {
  1237. e.add(l10n.get('addAtLeastOneDetail'));
  1238. }
  1239. return e;
  1240. }
  1241. bool _hasUnsaved(ExpenseCreateState state) =>
  1242. _purposeController.text.isNotEmpty ||
  1243. state.expense.paymentMethod.isNotEmpty ||
  1244. state.expense.currencyCode.isNotEmpty ||
  1245. _remarkController.text.isNotEmpty ||
  1246. state.expense.details.isNotEmpty ||
  1247. _attachmentController.files.isNotEmpty ||
  1248. _selectedDeptId.isNotEmpty;
  1249. void _doPop() {
  1250. final l10n = AppLocalizations.of(context);
  1251. final state = ref.read(expenseCreateProvider(widget.editId));
  1252. if (_hasUnsaved(state)) {
  1253. _showConfirmDialog(
  1254. l10n.get('confirmExit'),
  1255. l10n.get('unsavedContentWarning'),
  1256. l10n.get('continueEditing'),
  1257. l10n.get('discardAndExit'),
  1258. () async {
  1259. try {
  1260. await ExpenseCreateController.deleteDraft();
  1261. } catch (_) {}
  1262. if (!mounted) return;
  1263. setState(() {
  1264. _selectedDeptId = '';
  1265. _selectedDeptName = '';
  1266. });
  1267. ref.read(expenseCreateProvider(widget.editId).notifier).reset();
  1268. _forcePop();
  1269. },
  1270. );
  1271. } else {
  1272. _forcePop();
  1273. }
  1274. }
  1275. void _forcePop() {
  1276. _isPoppingToNative = true;
  1277. SystemNavigator.pop();
  1278. }
  1279. void _showConfirmDialog(
  1280. String title,
  1281. String content,
  1282. String leftText,
  1283. String rightText,
  1284. VoidCallback onConfirm,
  1285. ) {
  1286. FocusScope.of(context).unfocus();
  1287. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1288. showDialog(
  1289. context: context,
  1290. useRootNavigator: true,
  1291. builder: (ctx) => TDAlertDialog(
  1292. title: title,
  1293. content: content,
  1294. buttonStyle: TDDialogButtonStyle.text,
  1295. leftBtn: TDDialogButtonOptions(
  1296. title: leftText,
  1297. titleColor: colors.primary,
  1298. action: () => Navigator.pop(ctx),
  1299. ),
  1300. rightBtn: TDDialogButtonOptions(
  1301. title: rightText,
  1302. titleColor: colors.danger,
  1303. action: () {
  1304. Navigator.pop(ctx);
  1305. onConfirm();
  1306. },
  1307. ),
  1308. ),
  1309. );
  1310. }
  1311. Widget _buildPageFooter() {
  1312. final l10n = AppLocalizations.of(context);
  1313. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1314. return Center(
  1315. child: Padding(
  1316. padding: const EdgeInsets.only(bottom: 16),
  1317. child: Row(
  1318. mainAxisSize: MainAxisSize.min,
  1319. children: [
  1320. Icon(
  1321. Icons.rocket_launch_outlined,
  1322. size: 16,
  1323. color: colors.textPlaceholder,
  1324. ),
  1325. const SizedBox(width: 6),
  1326. Text(
  1327. l10n.get('pageFooter'),
  1328. style: TextStyle(
  1329. fontSize: AppFontSizes.caption,
  1330. color: colors.textPlaceholder,
  1331. ),
  1332. ),
  1333. ],
  1334. ),
  1335. ),
  1336. );
  1337. }
  1338. String _today() {
  1339. final n = DateTime.now();
  1340. return '${n.year}-${n.month.toString().padLeft(2, '0')}-${n.day.toString().padLeft(2, '0')}';
  1341. }
  1342. }