expense_create_page.dart 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import 'package:go_router/go_router.dart';
  6. import '../../shared/widgets/nav_bar_config.dart';
  7. import '../../core/utils/responsive.dart';
  8. import '../../shared/widgets/form_section.dart';
  9. import '../../shared/widgets/form_field_row.dart';
  10. import 'expense_api.dart';
  11. import 'expense_create_controller.dart';
  12. import '../../core/i18n/app_localizations.dart';
  13. import 'expense_model.dart';
  14. import '../../core/theme/app_colors.dart';
  15. import '../../core/theme/app_colors_extension.dart';
  16. import '../../core/navigation/host_app_channel.dart';
  17. import '../../core/data/mock_api_data.dart';
  18. import 'widgets/expense_detail_dialog.dart';
  19. import '../../shared/widgets/action_bar.dart';
  20. import '../../shared/widgets/submitting_dialog.dart';
  21. import '../../shared/widgets/attachment_picker.dart';
  22. import 'expense_apply_import_page.dart';
  23. class ExpenseApplyPage extends ConsumerStatefulWidget {
  24. final String? editId;
  25. const ExpenseApplyPage({super.key, this.editId});
  26. @override
  27. ConsumerState<ExpenseApplyPage> createState() => _ExpenseApplyPageState();
  28. }
  29. class _ExpenseApplyPageState extends ConsumerState<ExpenseApplyPage>
  30. with WidgetsBindingObserver {
  31. final _purposeController = TextEditingController();
  32. final _purposeFocus = FocusNode();
  33. final _remarkController = TextEditingController();
  34. final _remarkFocus = FocusNode();
  35. final _scrollCtrl = ScrollController();
  36. late final AttachmentPickerController _attachmentController;
  37. late Future<bool> _draftFuture;
  38. bool _draftHandled = false;
  39. bool _isPoppingToNative = false;
  40. // ── 参考数据(从 API 加载) ──
  41. List<CostTypeItem> _costTypes = [];
  42. List<ProjectCodeItem> _projects = [];
  43. List<DepartmentItem> _departments = [];
  44. List<CustomerItem> _customers = [];
  45. List<CurrencyItem> _currencies = [];
  46. List<EmployeeItem> _employees = [];
  47. bool _refDataLoading = true;
  48. // ── 报销部门 ──
  49. String _selectedDeptId = '';
  50. String _selectedDeptName = '';
  51. @override
  52. void initState() {
  53. super.initState();
  54. WidgetsBinding.instance.addObserver(this);
  55. SystemChrome.setSystemUIOverlayStyle(
  56. const SystemUiOverlayStyle(
  57. statusBarColor: Colors.transparent,
  58. statusBarIconBrightness: Brightness.dark,
  59. ),
  60. );
  61. _attachmentController = AttachmentPickerController(maxCount: 9)
  62. ..addListener(() => setState(() {}));
  63. _purposeFocus.addListener(() => _ensureVisible(_purposeFocus));
  64. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  65. _draftFuture = widget.editId == null
  66. ? ExpenseCreateController.hasDraft()
  67. : Future.value(false);
  68. _loadRefData();
  69. }
  70. Future<void> _loadRefData() async {
  71. try {
  72. final api = ref.read(expenseApiProvider);
  73. final results = await Future.wait([
  74. api.getCostTypes(),
  75. api.getProjectCodes(),
  76. api.getDepartments(),
  77. api.getCustomers(),
  78. api.getCurrencies(),
  79. api.getEmployees(),
  80. ]);
  81. if (!mounted) return;
  82. setState(() {
  83. _costTypes = results[0] as List<CostTypeItem>;
  84. _projects = results[1] as List<ProjectCodeItem>;
  85. _departments = results[2] as List<DepartmentItem>;
  86. _customers = results[3] as List<CustomerItem>;
  87. _currencies = results[4] as List<CurrencyItem>;
  88. _employees = results[5] as List<EmployeeItem>;
  89. _refDataLoading = false;
  90. _autoSelectDept();
  91. });
  92. } catch (_) {
  93. if (!mounted) return;
  94. setState(() => _refDataLoading = false);
  95. }
  96. }
  97. void _ensureVisible(FocusNode node) {
  98. if (!node.hasFocus) return;
  99. WidgetsBinding.instance.addPostFrameCallback((_) {
  100. if (node.hasFocus && _scrollCtrl.hasClients) {
  101. final ctx = node.context;
  102. if (ctx != null) {
  103. Scrollable.ensureVisible(ctx, alignment: 0.3, duration: const Duration(milliseconds: 300));
  104. }
  105. }
  106. });
  107. }
  108. @override
  109. void dispose() {
  110. WidgetsBinding.instance.removeObserver(this);
  111. _purposeController.dispose();
  112. _purposeFocus.dispose();
  113. _remarkController.dispose();
  114. _remarkFocus.dispose();
  115. _scrollCtrl.dispose();
  116. _attachmentController.dispose();
  117. super.dispose();
  118. }
  119. @override
  120. void didChangeAppLifecycleState(AppLifecycleState state) {
  121. if (state == AppLifecycleState.resumed && _isPoppingToNative) {
  122. _isPoppingToNative = false;
  123. HostAppChannel.refresh();
  124. setState(() {
  125. _draftHandled = false;
  126. _draftFuture = widget.editId == null
  127. ? ExpenseCreateController.hasDraft()
  128. : Future.value(false);
  129. _refDataLoading = true;
  130. });
  131. _loadRefData();
  132. }
  133. }
  134. @override
  135. Widget build(BuildContext context) {
  136. final controller = ref.watch(expenseCreateProvider(widget.editId).notifier);
  137. final state = ref.watch(expenseCreateProvider(widget.editId));
  138. final r = ResponsiveHelper.of(context);
  139. final l10n = AppLocalizations.of(context);
  140. ref
  141. .read(navBarConfigProvider.notifier)
  142. .update(
  143. NavBarConfig(
  144. title: widget.editId != null
  145. ? l10n.get('editExpense')
  146. : l10n.get('expenseApply'),
  147. showBack: true,
  148. onBack: () => _doPop(),
  149. ),
  150. );
  151. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  152. final bottomInset = MediaQuery.of(context).padding.bottom;
  153. Widget pageContent = PopScope(
  154. canPop: false,
  155. onPopInvokedWithResult: (didPop, _) {
  156. if (didPop) return;
  157. _doPop();
  158. },
  159. child: Column(
  160. children: [
  161. Expanded(
  162. child: Align(
  163. alignment: Alignment.topCenter,
  164. child: ConstrainedBox(
  165. constraints: BoxConstraints(maxWidth: r.formMaxWidth),
  166. child: SingleChildScrollView(
  167. controller: _scrollCtrl,
  168. padding: const EdgeInsets.all(16),
  169. child: Column(
  170. crossAxisAlignment: CrossAxisAlignment.start,
  171. children: [
  172. _buildImportLink(),
  173. const SizedBox(height: 16),
  174. _buildBasicInfoSection(controller, state),
  175. const SizedBox(height: 16),
  176. _buildDetailSection(controller, state),
  177. const SizedBox(height: 16),
  178. _buildAttachmentSection(controller, state),
  179. ],
  180. ),
  181. ),
  182. ),
  183. ),
  184. ),
  185. ColoredBox(
  186. color: colors.bgCard,
  187. child: Column(
  188. mainAxisSize: MainAxisSize.min,
  189. children: [
  190. _buildBottomButtons(controller, state),
  191. if (bottomInset > 0) SizedBox(height: bottomInset),
  192. ],
  193. ),
  194. ),
  195. ],
  196. ),
  197. );
  198. return FutureBuilder<bool>(
  199. future: _draftFuture,
  200. builder: (ctx, snapshot) {
  201. final hasDraft = snapshot.hasData && snapshot.data == true;
  202. if (hasDraft && !_draftHandled) {
  203. _draftHandled = true;
  204. WidgetsBinding.instance.addPostFrameCallback((_) {
  205. if (mounted) _showDraftDialog();
  206. });
  207. }
  208. return pageContent;
  209. },
  210. );
  211. }
  212. void _showDraftDialog() {
  213. final l10n = AppLocalizations.of(context);
  214. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  215. showDialog(
  216. context: context,
  217. barrierDismissible: false,
  218. builder: (ctx) => TDAlertDialog(
  219. title: l10n.get('draftFound'),
  220. content: l10n.get('draftRestorePrompt'),
  221. leftBtn: TDDialogButtonOptions(
  222. title: l10n.get('discard'),
  223. titleColor: colors.textSecondary,
  224. action: () {
  225. Navigator.pop(ctx);
  226. ExpenseCreateController.deleteDraft();
  227. },
  228. ),
  229. rightBtn: TDDialogButtonOptions(
  230. title: l10n.get('restore'),
  231. titleColor: colors.primary,
  232. action: () async {
  233. Navigator.pop(ctx);
  234. final draft = await ExpenseCreateController.loadDraft();
  235. if (draft != null && mounted) {
  236. final api = ref.read(expenseApiProvider);
  237. ref.read(expenseCreateProvider(widget.editId).notifier)
  238. .restoreFromDraft(draft, api);
  239. _purposeController.text = draft.purpose;
  240. _remarkController.text = draft.remark;
  241. if (draft.attachments.isNotEmpty) {
  242. await _attachmentController.restoreFromPaths(draft.attachments);
  243. }
  244. }
  245. },
  246. ),
  247. ),
  248. );
  249. }
  250. // ═══ API 数据 → 弹窗类型转换 ═══
  251. List<CostCategory> get _dialogCategories => _costTypes
  252. .map((c) => CostCategory(code: c.typeNo, nameKey: c.typeName, acctSubjectId: c.accNo, acctSubjectName: c.accName))
  253. .toList();
  254. List<Project> get _dialogProjects => _projects
  255. .map((p) => Project(id: int.tryParse(p.objNo) ?? 0, name: p.name))
  256. .toList();
  257. List<CostDept> get _dialogCostDepts => _departments
  258. .map((d) => CostDept(id: d.dep, name: d.name))
  259. .toList();
  260. String _currencyLabel(String code) {
  261. final match = _currencies.where((c) => c.curId == code);
  262. return match.isNotEmpty ? '${match.first.curId}/${match.first.name}' : code;
  263. }
  264. List<CustomerVendor> get _dialogCustomers => _customers
  265. .map((c) => CustomerVendor(id: c.cusNo, name: c.name))
  266. .toList();
  267. List<EmployeeItem> get _dialogEmployees => _employees;
  268. void _autoSelectDept() {
  269. if (_selectedDeptId.isNotEmpty) return;
  270. final dep = HostAppChannel.dep;
  271. if (dep.isEmpty) return;
  272. final match = _departments.where((d) => d.dep == dep);
  273. if (match.isNotEmpty) {
  274. _selectedDeptId = match.first.dep;
  275. _selectedDeptName = match.first.name;
  276. }
  277. }
  278. void _showDeptPicker() {
  279. if (_departments.isEmpty) {
  280. TDToast.showText(AppLocalizations.of(context).get('noData'), context: context);
  281. return;
  282. }
  283. final l10n = AppLocalizations.of(context);
  284. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  285. final labels = _departments.map((d) => d.name).toList();
  286. TDPicker.showMultiPicker(
  287. context,
  288. title: l10n.get('expenseDept'),
  289. backgroundColor: colors.bgCard,
  290. data: [labels],
  291. onConfirm: (selected) {
  292. if (selected.isNotEmpty && selected[0] is int) {
  293. final idx = selected[0] as int;
  294. if (idx >= 0 && idx < labels.length) {
  295. Navigator.of(context).pop();
  296. setState(() {
  297. _selectedDeptId = _departments[idx].dep;
  298. _selectedDeptName = _departments[idx].name;
  299. });
  300. }
  301. }
  302. },
  303. );
  304. }
  305. Map<String, dynamic> _buildSubmitData(ExpenseCreateState state) {
  306. final expense = state.expense;
  307. return {
  308. 'HeadData': {
  309. 'BX_DD': _today(),
  310. 'DEP': _selectedDeptId,
  311. 'USR_NO': HostAppChannel.usr,
  312. 'PAY_ID': expense.paymentMethod,
  313. 'PRT_SW': 'N',
  314. 'USR': HostAppChannel.usr,
  315. 'REM': expense.remark,
  316. 'CUR_ID': expense.currencyCode,
  317. 'EXC_RTO': 1,
  318. 'REASON': expense.purpose,
  319. },
  320. 'BodyData1': expense.details.asMap().entries.map((e) {
  321. final i = e.key;
  322. final d = e.value;
  323. return {
  324. 'ITM': i + 1,
  325. 'BX_DD': _today(),
  326. 'ACC_NO': d.acctSubjectId,
  327. 'AMT': d.amount,
  328. 'AMTN': d.totalAmount,
  329. 'REM': d.remark,
  330. 'CUST': d.customerVendorId,
  331. 'OBJ_NO': d.projectId.isNotEmpty ? d.projectId : '',
  332. 'TAX': d.taxAmount,
  333. 'TAX_RTO': d.taxRate,
  334. 'DEP': d.costDeptId,
  335. 'SQ_MAN': d.sqMan.isNotEmpty ? d.sqMan : HostAppChannel.usr,
  336. };
  337. }).toList(),
  338. };
  339. }
  340. Widget _buildImportLink() {
  341. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  342. final l10n = AppLocalizations.of(context);
  343. return GestureDetector(
  344. onTap: () async {
  345. final result = await GoRouter.of(context).push<List<ImportableItem>>('/expense/import-apply');
  346. if (result == null || result.isEmpty || !mounted) return;
  347. // 将选中的导入数据转换为明细
  348. final controller = ref.read(expenseCreateProvider(widget.editId).notifier);
  349. final now = DateTime.now();
  350. for (final item in result) {
  351. controller.addDetail(ExpenseDetailModel(
  352. id: '${now.millisecondsSinceEpoch}_${item.itm}',
  353. expenseId: '',
  354. expenseApplyId: '', expenseApplyNo: item.aeNo,
  355. expenseApplyDate: item.aeDd.isNotEmpty ? DateTime.tryParse(item.aeDd) : null,
  356. expenseCategory: item.typeNo, purpose: item.rem,
  357. projectId: item.objNo, projectName: '',
  358. costDeptId: item.dep, costDeptName: '',
  359. acctSubjectId: item.accNo, acctSubjectName: item.accName,
  360. amount: item.amtnYj, taxRate: 0.13, taxAmount: item.amtnYj - item.amtnYj / 1.13,
  361. totalAmount: item.amtnYj, currencyCode: '', exchangeRate: 1.0,
  362. baseAmount: item.amtnYj, approvedAmount: 0,
  363. customerVendorId: '', customerVendorName: '',
  364. offsetAmount: 0, bankName: '', bankAccountName: '', bankAccount: '',
  365. remark: item.rem, sortOrder: item.itm,
  366. attachments: const [],
  367. sqMan: item.sqMan, sqManName: '',
  368. aeNo: item.aeNo, aeDd: item.aeDd,
  369. createTime: now, updateTime: now,
  370. ));
  371. }
  372. controller.recalculateAmount();
  373. TDToast.showSuccess(l10n.get('importSuccess'), context: context);
  374. },
  375. child: Container(
  376. height: 44,
  377. decoration: BoxDecoration(
  378. color: colors.primaryLight,
  379. borderRadius: BorderRadius.circular(8),
  380. ),
  381. child: Row(
  382. mainAxisAlignment: MainAxisAlignment.center,
  383. children: [
  384. Icon(Icons.download, size: 14, color: colors.primary),
  385. const SizedBox(width: 8),
  386. Text(
  387. l10n.get('importApprovedPreApp'),
  388. style: TextStyle(
  389. fontSize: AppFontSizes.body,
  390. color: colors.primary,
  391. ),
  392. ),
  393. ],
  394. ),
  395. ),
  396. );
  397. }
  398. Widget _buildBasicInfoSection(
  399. ExpenseCreateController controller,
  400. ExpenseCreateState state,
  401. ) {
  402. final l10n = AppLocalizations.of(context);
  403. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  404. final expense = state.expense;
  405. return FormSection(
  406. title: l10n.get('basicInfo'),
  407. leadingIcon: Icons.info_outline,
  408. children: [
  409. FormFieldRow(
  410. label: l10n.get('date'),
  411. value: _today(),
  412. readOnly: true,
  413. showArrow: false,
  414. ),
  415. const SizedBox(height: 16),
  416. FormFieldRow(
  417. label: l10n.get('expensePersonnel'),
  418. value: HostAppChannel.usr.isNotEmpty && HostAppChannel.usrName.isNotEmpty
  419. ? '${HostAppChannel.usr}/${HostAppChannel.usrName}'
  420. : '--',
  421. readOnly: true,
  422. showArrow: false,
  423. ),
  424. const SizedBox(height: 16),
  425. FormFieldRow(
  426. label: l10n.get('expenseDept'),
  427. value: _selectedDeptName.isNotEmpty
  428. ? '$_selectedDeptId/$_selectedDeptName'
  429. : null,
  430. hint: l10n.get('pleaseSelect'),
  431. onTap: _refDataLoading ? null : () => _showDeptPicker(),
  432. ),
  433. const SizedBox(height: 16),
  434. _label(l10n.get('expenseReason'), required: true),
  435. const SizedBox(height: 8),
  436. TDTextarea(
  437. controller: _purposeController,
  438. focusNode: _purposeFocus,
  439. hintText: l10n.get('enterExpenseReason'),
  440. maxLines: 4,
  441. minLines: 1,
  442. maxLength: 500,
  443. indicator: true,
  444. padding: EdgeInsets.zero,
  445. bordered: true,
  446. backgroundColor: colors.bgPage,
  447. onChanged: (_) => controller.updatePurpose(_purposeController.text),
  448. ),
  449. const SizedBox(height: 16),
  450. FormFieldRow(
  451. label: l10n.get('paymentMethod'),
  452. value: expense.paymentMethod,
  453. hint: l10n.get('pleaseEnter'),
  454. onTap: () => _showTextInput(
  455. l10n.get('paymentMethod'),
  456. (v) => controller.updatePaymentMethod(v),
  457. initialText: expense.paymentMethod,
  458. ),
  459. onClear: () => controller.updatePaymentMethod(''),
  460. ),
  461. const SizedBox(height: 16),
  462. FormFieldRow(
  463. label: l10n.get('currency'),
  464. value: expense.currencyCode.isNotEmpty
  465. ? _currencyLabel(expense.currencyCode)
  466. : null,
  467. hint: l10n.get('selectCurrency'),
  468. onTap: () => _showCurrencyPicker(controller, expense.currencyCode),
  469. onClear: () => controller.updateCurrencyCode(''),
  470. ),
  471. const SizedBox(height: 16),
  472. _label(l10n.get('remark')),
  473. const SizedBox(height: 8),
  474. TDTextarea(
  475. controller: _remarkController,
  476. focusNode: _remarkFocus,
  477. hintText: l10n.get('enterRemark'),
  478. maxLines: 3,
  479. minLines: 1,
  480. maxLength: 500,
  481. indicator: true,
  482. padding: EdgeInsets.zero,
  483. bordered: true,
  484. backgroundColor: colors.bgPage,
  485. onChanged: (_) => controller.updateRemark(_remarkController.text),
  486. ),
  487. ],
  488. );
  489. }
  490. Widget _buildDetailSection(
  491. ExpenseCreateController controller,
  492. ExpenseCreateState state,
  493. ) {
  494. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  495. final l10n = AppLocalizations.of(context);
  496. final totalApproved = state.expense.details.fold<double>(
  497. 0,
  498. (sum, d) => sum + d.approvedAmount,
  499. );
  500. return FormSection(
  501. title: l10n.get('expenseDetails'),
  502. leadingIcon: Icons.receipt_long_outlined,
  503. showAction: !_refDataLoading,
  504. actionText: l10n.get('add'),
  505. onActionTap: () => _showAddDetailDialog(controller),
  506. children: [
  507. if (state.expense.details.isEmpty)
  508. Padding(
  509. padding: const EdgeInsets.symmetric(vertical: 8),
  510. child: Text(
  511. l10n.get('noDetailHint'),
  512. style: TextStyle(fontSize: AppFontSizes.subtitle, color: colors.textPlaceholder),
  513. ),
  514. )
  515. else
  516. ...state.expense.details.asMap().entries.map((entry) {
  517. final d = entry.value;
  518. return GestureDetector(
  519. onTap: () => _showAddDetailDialog(controller, editIndex: entry.key),
  520. child: Container(
  521. margin: const EdgeInsets.symmetric(vertical: 6),
  522. padding: const EdgeInsets.all(12),
  523. decoration: BoxDecoration(color: colors.bgPage, borderRadius: BorderRadius.circular(8)),
  524. child: Row(
  525. children: [
  526. Expanded(
  527. child: Column(
  528. crossAxisAlignment: CrossAxisAlignment.start,
  529. children: [
  530. Row(children: [
  531. Expanded(child: Text(d.expenseCategory.isNotEmpty ? d.expenseCategory : d.purpose, style: TextStyle(fontSize: AppFontSizes.body, fontWeight: FontWeight.w500, color: colors.textPrimary))),
  532. Text('¥${d.totalAmount.toStringAsFixed(2)}', style: TextStyle(fontSize: AppFontSizes.body, fontWeight: FontWeight.w600, color: colors.amountPrimary)),
  533. ]),
  534. const SizedBox(height: 4),
  535. if (d.purpose.isNotEmpty)
  536. Text(d.purpose, maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textPrimary)),
  537. const SizedBox(height: 2),
  538. Text('¥${d.amount.toStringAsFixed(2)} ${l10n.get('tax')}¥${d.taxAmount.toStringAsFixed(2)} | ${d.acctSubjectName}', style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textSecondary)),
  539. if (d.projectName.isNotEmpty || d.costDeptName.isNotEmpty)
  540. Text('${d.projectName}${d.costDeptName.isNotEmpty ? " | ${d.costDeptName}" : ""}', style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textSecondary)),
  541. if (d.customerVendorName.isNotEmpty)
  542. Text('${l10n.get('customerVendor')}: ${d.customerVendorName}', style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textSecondary)),
  543. if (d.bankName.isNotEmpty || d.bankAccount.isNotEmpty)
  544. Text('${d.bankName}${d.bankAccount.isNotEmpty ? " | ${d.bankAccount}" : ""}', style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textSecondary)),
  545. if (d.sqManName.isNotEmpty)
  546. Text('${l10n.get('applicant')}: ${d.sqManName.isNotEmpty ? d.sqManName : d.sqMan}', style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textSecondary)),
  547. ],
  548. ),
  549. ),
  550. const SizedBox(width: 8),
  551. GestureDetector(
  552. onTap: () { controller.removeDetail(entry.key); controller.recalculateAmount(); },
  553. child: Container(width: 24, height: 24, decoration: BoxDecoration(color: colors.primaryLight, shape: BoxShape.circle), child: Icon(Icons.close, size: 14, color: colors.primary700)),
  554. ),
  555. ],
  556. ),
  557. ),
  558. );
  559. }),
  560. const SizedBox(height: 8),
  561. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  562. Text(l10n.get('totalExpense'), style: TextStyle(fontSize: AppFontSizes.body, fontWeight: FontWeight.w600, color: colors.textPrimary)),
  563. Text('¥${state.expense.totalAmount.toStringAsFixed(2)}', style: TextStyle(fontSize: AppFontSizes.subtitle, fontWeight: FontWeight.w700, color: colors.amountPrimary)),
  564. ]),
  565. const SizedBox(height: 4),
  566. Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
  567. Text(l10n.get('approvedTotal'), style: TextStyle(fontSize: AppFontSizes.body, fontWeight: FontWeight.w600, color: colors.textPrimary)),
  568. Text('¥${totalApproved.toStringAsFixed(2)}', style: TextStyle(fontSize: AppFontSizes.subtitle, fontWeight: FontWeight.w700, color: colors.amountPrimary)),
  569. ]),
  570. ],
  571. );
  572. }
  573. Widget _buildAttachmentSection(ExpenseCreateController controller, ExpenseCreateState state) {
  574. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  575. final l10n = AppLocalizations.of(context);
  576. return FormSection(
  577. title: l10n.get('attachmentUpload'),
  578. leadingIcon: Icons.attach_file_outlined,
  579. children: [
  580. Text(l10n.get('maxAttachment'), style: TextStyle(fontSize: AppFontSizes.caption, color: colors.textPlaceholder)),
  581. const SizedBox(height: 8),
  582. AttachmentPicker(
  583. controller: _attachmentController,
  584. maxImageSizeMB: 10,
  585. maxFileSizeMB: 20,
  586. allowedExtensions: const ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt'],
  587. onFileRejected: (file, reason) {
  588. if (context.mounted) TDToast.showText(reason, context: context);
  589. },
  590. ),
  591. ],
  592. );
  593. }
  594. Widget _buildBottomButtons(ExpenseCreateController controller, ExpenseCreateState state) {
  595. final l10n = AppLocalizations.of(context);
  596. return ActionBar(
  597. showLeft: false,
  598. centerLabel: l10n.get('saveDraft'),
  599. rightLabel: l10n.get('submit'),
  600. centerTextOnly: true,
  601. onCenterTap: () async {
  602. if (state.isSubmitting) return;
  603. controller.updateAttachments(_attachmentController.toPathList());
  604. controller.updateDept(_selectedDeptId, _selectedDeptName);
  605. final ok = await controller.saveDraft();
  606. if (mounted) {
  607. if (ok) {
  608. _forcePop();
  609. } else {
  610. TDToast.showFail(l10n.get('saveFailed'), context: context);
  611. }
  612. }
  613. },
  614. onRightTap: () async {
  615. if (state.isSubmitting) return;
  616. final err = _validate(l10n, state);
  617. if (err.isNotEmpty) {
  618. TDToast.showText(err.first, context: context);
  619. return;
  620. }
  621. SubmittingDialog.show(context);
  622. try {
  623. final data = _buildSubmitData(state);
  624. await ref.read(expenseApiProvider).submit(data);
  625. await ExpenseCreateController.deleteDraft();
  626. if (mounted) {
  627. SubmittingDialog.hide(context);
  628. TDToast.showSuccess(l10n.get('submittedAwaitingApproval'), context: context);
  629. GoRouter.of(context).go('/expense/list');
  630. }
  631. } catch (_) {
  632. if (mounted) {
  633. SubmittingDialog.hide(context);
  634. TDToast.showFail(l10n.get('submitFailedRetry'), context: context);
  635. }
  636. }
  637. },
  638. );
  639. }
  640. Future<void> _showAddDetailDialog(ExpenseCreateController controller, {int? editIndex}) async {
  641. final l10n = AppLocalizations.of(context);
  642. if (_costTypes.isEmpty) {
  643. TDToast.showText(l10n.get('noCostTypeData'), context: context);
  644. return;
  645. }
  646. final state = controller.currentState;
  647. ExpenseDetailInputData? initialData;
  648. if (editIndex != null) {
  649. final d = state.expense.details[editIndex];
  650. initialData = ExpenseDetailInputData(
  651. category: d.expenseCategory,
  652. categoryName: '',
  653. acctSubjectId: d.acctSubjectId,
  654. acctSubjectName: d.acctSubjectName,
  655. purpose: d.purpose,
  656. amount: d.amount,
  657. taxRate: d.taxRate,
  658. projectId: d.projectId,
  659. projectName: d.projectName,
  660. costDeptId: d.costDeptId,
  661. costDeptName: d.costDeptName,
  662. customerVendorId: d.customerVendorId,
  663. customerVendorName: d.customerVendorName,
  664. offsetAmount: d.offsetAmount,
  665. bankName: d.bankName,
  666. bankAccountName: d.bankAccountName,
  667. bankAccount: d.bankAccount,
  668. remark: d.remark,
  669. attachmentPaths: d.attachments,
  670. sqMan: d.sqMan,
  671. sqManName: d.sqManName,
  672. aeNo: d.aeNo,
  673. aeDd: d.aeDd,
  674. );
  675. }
  676. final result = await ExpenseDetailDialog.show(
  677. context,
  678. categories: _dialogCategories,
  679. projects: _dialogProjects,
  680. costDepts: _dialogCostDepts,
  681. customers: _dialogCustomers,
  682. employees: _dialogEmployees,
  683. l10n: l10n,
  684. initialData: initialData,
  685. );
  686. if (result != null && mounted) {
  687. final now = DateTime.now();
  688. final detail = ExpenseDetailModel(
  689. id: editIndex != null ? state.expense.details[editIndex].id : now.millisecondsSinceEpoch.toString(),
  690. expenseId: '',
  691. expenseCategory: result.category,
  692. purpose: result.purpose,
  693. amount: result.amount,
  694. taxRate: result.taxRate,
  695. taxAmount: result.amount - result.amount / (1 + result.taxRate),
  696. totalAmount: result.amount,
  697. projectId: result.projectId,
  698. projectName: result.projectName,
  699. costDeptId: result.costDeptId,
  700. costDeptName: result.costDeptName,
  701. acctSubjectId: result.acctSubjectId,
  702. acctSubjectName: result.acctSubjectName,
  703. customerVendorName: result.customerVendorName,
  704. offsetAmount: result.offsetAmount,
  705. bankName: result.bankName,
  706. bankAccountName: result.bankAccountName,
  707. bankAccount: result.bankAccount,
  708. remark: result.remark,
  709. attachments: result.attachmentPaths,
  710. createTime: now,
  711. updateTime: now,
  712. );
  713. if (editIndex != null) {
  714. controller.updateDetail(editIndex, detail);
  715. } else {
  716. controller.addDetail(detail);
  717. }
  718. controller.recalculateAmount();
  719. }
  720. }
  721. void _showCurrencyPicker(ExpenseCreateController controller, String cur) {
  722. if (_currencies.isEmpty) {
  723. TDToast.showText(AppLocalizations.of(context).get('noData'), context: context);
  724. return;
  725. }
  726. final l10n = AppLocalizations.of(context);
  727. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  728. final codes = _currencies.map((c) => c.curId).toList();
  729. final labels = _currencies.map((c) => '${c.curId} ${c.name}').toList();
  730. TDPicker.showMultiPicker(context, title: l10n.get('selectCurrency'), backgroundColor: colors.bgCard, data: [labels], onConfirm: (s) {
  731. if (s.isNotEmpty && s[0] is int) { final i = s[0] as int; if (i >= 0 && i < codes.length) { Navigator.of(context).pop(); controller.updateCurrencyCode(codes[i]); } }
  732. });
  733. }
  734. void _showTextInput(
  735. String title,
  736. Function(String) onConfirm, {
  737. String initialText = '',
  738. }) {
  739. FocusScope.of(context).unfocus();
  740. final l10n = AppLocalizations.of(context);
  741. final c = TextEditingController(text: initialText);
  742. showGeneralDialog(
  743. context: context,
  744. pageBuilder: (ctx, animation, secondaryAnimation) => TDInputDialog(
  745. textEditingController: c,
  746. title: title,
  747. hintText: l10n.get('pleaseEnter'),
  748. leftBtn: TDDialogButtonOptions(
  749. title: l10n.get('cancel'),
  750. action: () => Navigator.pop(ctx),
  751. ),
  752. rightBtn: TDDialogButtonOptions(
  753. title: l10n.get('confirm'),
  754. action: () {
  755. onConfirm(c.text);
  756. Navigator.pop(ctx);
  757. },
  758. ),
  759. ),
  760. );
  761. }
  762. Widget _label(String t, {bool required = false}) {
  763. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  764. return Text.rich(
  765. TextSpan(
  766. children: [
  767. TextSpan(text: t, style: TextStyle(fontSize: AppFontSizes.subtitle, color: colors.textSecondary)),
  768. if (required)
  769. TextSpan(text: ' *', style: TextStyle(fontSize: AppFontSizes.subtitle, color: colors.danger)),
  770. ],
  771. ),
  772. );
  773. }
  774. List<String> _validate(AppLocalizations l10n, ExpenseCreateState state) {
  775. final e = <String>[];
  776. if (_purposeController.text.trim().isEmpty) {
  777. e.add(l10n.get('enterExpenseReason'));
  778. }
  779. if (state.expense.details.isEmpty) {
  780. e.add(l10n.get('addAtLeastOneDetail'));
  781. }
  782. return e;
  783. }
  784. bool _hasUnsaved(ExpenseCreateState state) =>
  785. _purposeController.text.isNotEmpty ||
  786. state.expense.paymentMethod.isNotEmpty ||
  787. state.expense.currencyCode.isNotEmpty ||
  788. _remarkController.text.isNotEmpty ||
  789. state.expense.details.isNotEmpty ||
  790. _attachmentController.files.isNotEmpty ||
  791. _selectedDeptId.isNotEmpty;
  792. void _doPop() {
  793. final l10n = AppLocalizations.of(context);
  794. final state = ref.read(expenseCreateProvider(widget.editId));
  795. if (_hasUnsaved(state)) {
  796. _showConfirmDialog(
  797. l10n.get('confirmExit'),
  798. l10n.get('unsavedContentWarning'),
  799. l10n.get('continueEditing'),
  800. l10n.get('discardAndExit'),
  801. () async {
  802. try {
  803. await ExpenseCreateController.deleteDraft();
  804. } catch (_) {}
  805. if (!mounted) return;
  806. setState(() {
  807. _selectedDeptId = '';
  808. _selectedDeptName = '';
  809. });
  810. ref.read(expenseCreateProvider(widget.editId).notifier).reset();
  811. _forcePop();
  812. },
  813. );
  814. } else {
  815. _forcePop();
  816. }
  817. }
  818. void _forcePop() {
  819. _isPoppingToNative = true;
  820. SystemNavigator.pop();
  821. }
  822. void _showConfirmDialog(
  823. String title, String content, String leftText, String rightText, VoidCallback onConfirm,
  824. ) {
  825. FocusScope.of(context).unfocus();
  826. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  827. showDialog(
  828. context: context,
  829. useRootNavigator: true,
  830. builder: (ctx) => TDAlertDialog(
  831. title: title,
  832. content: content,
  833. buttonStyle: TDDialogButtonStyle.text,
  834. leftBtn: TDDialogButtonOptions(
  835. title: leftText, titleColor: colors.primary, action: () => Navigator.pop(ctx),
  836. ),
  837. rightBtn: TDDialogButtonOptions(
  838. title: rightText, titleColor: colors.danger, action: () { Navigator.pop(ctx); onConfirm(); },
  839. ),
  840. ),
  841. );
  842. }
  843. String _today() { final n = DateTime.now(); return '${n.year}-${n.month.toString().padLeft(2, '0')}-${n.day.toString().padLeft(2, '0')}'; }
  844. }