expense_detail_dialog.dart 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import '../../../core/i18n/app_localizations.dart';
  6. import '../../../core/theme/app_colors.dart';
  7. import '../../../core/theme/app_colors_extension.dart';
  8. import '../../../core/data/mock_api_data.dart';
  9. import '../../../shared/models/bill_file_rights.dart';
  10. import '../expense_api.dart';
  11. import '../../../shared/widgets/attachment_picker.dart';
  12. /// 报销明细输入数据。
  13. class ExpenseDetailInputData {
  14. final String category;
  15. final String categoryName;
  16. final String acctSubjectId;
  17. final String acctSubjectName;
  18. final String purpose;
  19. final double amount; // 含税金额
  20. final double taxRate;
  21. final String projectId;
  22. final String projectName;
  23. final String costDeptId;
  24. final String costDeptName;
  25. final String customerVendorId;
  26. final String customerVendorName;
  27. final double approvedAmount;
  28. final double offsetAmount;
  29. final String bankName;
  30. final String bankAccountName;
  31. final String bankAccount;
  32. final String remark;
  33. final List<String> attachmentPaths;
  34. final String sqMan;
  35. final String sqManName;
  36. final String aeNo;
  37. final String aeDd;
  38. const ExpenseDetailInputData({
  39. required this.category,
  40. required this.categoryName,
  41. required this.acctSubjectId,
  42. required this.acctSubjectName,
  43. required this.purpose,
  44. required this.amount,
  45. required this.taxRate,
  46. this.projectId = '',
  47. this.projectName = '',
  48. this.costDeptId = '',
  49. this.costDeptName = '',
  50. this.customerVendorId = '',
  51. this.customerVendorName = '',
  52. this.approvedAmount = 0.0,
  53. this.offsetAmount = 0.0,
  54. this.bankName = '',
  55. this.bankAccountName = '',
  56. this.bankAccount = '',
  57. this.remark = '',
  58. this.attachmentPaths = const [],
  59. this.sqMan = '',
  60. this.sqManName = '',
  61. this.aeNo = '',
  62. this.aeDd = '',
  63. });
  64. }
  65. /// 报销明细编辑弹窗。
  66. ///
  67. /// 使用 [TDSlidePopupRoute] 从底部滑出,卡片化展示表单字段。
  68. /// 参照 ExpenseApplyCreatePage 的 ExpenseDetailDialog 样式。
  69. class ExpenseDetailDialog extends StatefulWidget {
  70. final List<CostCategory> categories;
  71. final List<Project> projects;
  72. final List<CostDept> costDepts;
  73. final List<CustomerVendor> customers;
  74. final List<EmployeeItem> employees;
  75. final AppLocalizations l10n;
  76. final ExpenseDetailInputData? initialData;
  77. final Future<bool> Function()? checkAttachHealth;
  78. final bool showAttachments;
  79. final bool canEditApprovedAmount;
  80. final BillFileRights? billFileRights;
  81. const ExpenseDetailDialog({
  82. super.key,
  83. required this.categories,
  84. required this.projects,
  85. required this.costDepts,
  86. required this.customers,
  87. required this.employees,
  88. required this.l10n,
  89. this.initialData,
  90. this.checkAttachHealth,
  91. this.showAttachments = true,
  92. this.canEditApprovedAmount = false,
  93. this.billFileRights,
  94. });
  95. /// 显示弹窗,返回 [ExpenseDetailInputData] 或 `null`(取消时)。
  96. static Future<ExpenseDetailInputData?> show(
  97. BuildContext context, {
  98. required List<CostCategory> categories,
  99. required List<Project> projects,
  100. required List<CostDept> costDepts,
  101. required List<CustomerVendor> customers,
  102. required List<EmployeeItem> employees,
  103. required AppLocalizations l10n,
  104. ExpenseDetailInputData? initialData,
  105. Future<bool> Function()? checkAttachHealth,
  106. bool showAttachments = true,
  107. bool canEditApprovedAmount = false,
  108. BillFileRights? billFileRights,
  109. }) {
  110. FocusScope.of(context).unfocus();
  111. return Navigator.push<ExpenseDetailInputData>(
  112. context,
  113. TDSlidePopupRoute<ExpenseDetailInputData>(
  114. slideTransitionFrom: SlideTransitionFrom.bottom,
  115. isDismissible: true,
  116. builder: (_) => ExpenseDetailDialog(
  117. categories: categories,
  118. projects: projects,
  119. costDepts: costDepts,
  120. customers: customers,
  121. employees: employees,
  122. l10n: l10n,
  123. initialData: initialData,
  124. checkAttachHealth: checkAttachHealth,
  125. showAttachments: showAttachments,
  126. canEditApprovedAmount: canEditApprovedAmount,
  127. billFileRights: billFileRights,
  128. ),
  129. ),
  130. );
  131. }
  132. @override
  133. State<ExpenseDetailDialog> createState() => _ExpenseDetailDialogState();
  134. }
  135. class _ExpenseDetailDialogState extends State<ExpenseDetailDialog> {
  136. late String _cat;
  137. late TextEditingController _descCtrl;
  138. late TextEditingController _amountCtrl;
  139. CustomerVendor? _selCustomer;
  140. late TextEditingController _approvedAmountCtrl;
  141. late TextEditingController _remarkCtrl;
  142. late TextEditingController _bankNameCtrl;
  143. late TextEditingController _bankAccountNameCtrl;
  144. late TextEditingController _bankAccountCtrl;
  145. double _taxRate = 0;
  146. Project? _selProject;
  147. CostDept? _selDept;
  148. EmployeeItem? _selEmployee;
  149. late final AttachmentPickerController _attachmentCtrl;
  150. final ScrollController _scrollCtrl = ScrollController();
  151. final _remarkFocus = FocusNode();
  152. final _bankNameFocus = FocusNode();
  153. final _bankAccountNameFocus = FocusNode();
  154. final _bankAccountFocus = FocusNode();
  155. bool _attachAvailable = false;
  156. void _ensureVisible(FocusNode node) {
  157. if (!node.hasFocus) return;
  158. _doEnsureVisible(node, 0, -1);
  159. }
  160. void _doEnsureVisible(FocusNode node, int attempt, double lastInsets) {
  161. if (attempt >= 15) return;
  162. WidgetsBinding.instance.addPostFrameCallback((_) {
  163. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  164. final insets = MediaQuery.of(context).viewInsets.bottom;
  165. if (insets != lastInsets) {
  166. _doEnsureVisible(node, attempt + 1, insets);
  167. return;
  168. }
  169. final ctx = node.context;
  170. if (ctx == null) return;
  171. Future.delayed(const Duration(milliseconds: 500), () {
  172. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  173. Scrollable.ensureVisible(
  174. ctx,
  175. alignment: 0.5,
  176. duration: const Duration(milliseconds: 300),
  177. );
  178. });
  179. });
  180. }
  181. static const _taxOptions = [0, 6, 9, 13];
  182. static const _taxLabels = ['0%', '6%', '9%', '13%'];
  183. List<CostCategory> get _cats => widget.categories;
  184. AppLocalizations get _l10n => widget.l10n;
  185. CostCategory get _selCat => _cats.firstWhere((c) => c.code == _cat);
  186. bool get _isEdit => widget.initialData != null;
  187. @override
  188. void initState() {
  189. super.initState();
  190. final d = widget.initialData;
  191. _cat = d != null
  192. ? (_cats.any((c) => c.code == d.category)
  193. ? d.category
  194. : _cats.first.code)
  195. : _cats.isNotEmpty
  196. ? _cats.first.code
  197. : 'other';
  198. _descCtrl = TextEditingController(text: d?.purpose ?? '');
  199. _amountCtrl = TextEditingController(
  200. text: d != null && d.amount > 0 ? d.amount.toStringAsFixed(2) : '',
  201. );
  202. if (d != null && d.customerVendorName.isNotEmpty) {
  203. _selCustomer = CustomerVendor(id: d.customerVendorId, name: d.customerVendorName);
  204. }
  205. _approvedAmountCtrl = TextEditingController(
  206. text: d != null && d.approvedAmount > 0
  207. ? d.approvedAmount.toStringAsFixed(2)
  208. : '',
  209. );
  210. _remarkCtrl = TextEditingController(text: d?.remark ?? '');
  211. _bankNameCtrl = TextEditingController(text: d?.bankName ?? '');
  212. _bankAccountNameCtrl = TextEditingController(
  213. text: d?.bankAccountName ?? '',
  214. );
  215. _bankAccountCtrl = TextEditingController(text: d?.bankAccount ?? '');
  216. _taxRate = d?.taxRate ?? 0;
  217. if (d != null && d.sqMan.isNotEmpty && widget.employees.isNotEmpty) {
  218. final idx = widget.employees.indexWhere((e) => e.salNo == d.sqMan);
  219. if (idx >= 0) _selEmployee = widget.employees[idx];
  220. }
  221. if (d != null) {
  222. if (d.projectId.isNotEmpty && widget.projects.isNotEmpty) {
  223. _selProject = widget.projects.firstWhere(
  224. (p) => p.id.toString() == d.projectId,
  225. orElse: () => widget.projects.first,
  226. );
  227. }
  228. if (d.costDeptId.isNotEmpty && widget.costDepts.isNotEmpty) {
  229. _selDept = widget.costDepts.firstWhere(
  230. (dept) => dept.id == d.costDeptId,
  231. orElse: () => widget.costDepts.first,
  232. );
  233. }
  234. if (d.attachmentPaths.isNotEmpty) {
  235. // Restore attachments will be handled after build
  236. WidgetsBinding.instance.addPostFrameCallback((_) {
  237. _attachmentCtrl.restoreFromPaths(d.attachmentPaths);
  238. });
  239. }
  240. }
  241. _attachmentCtrl = AttachmentPickerController(maxCount: 9)
  242. ..addListener(() => setState(() {}));
  243. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  244. _bankNameFocus.addListener(() => _ensureVisible(_bankNameFocus));
  245. _bankAccountNameFocus.addListener(
  246. () => _ensureVisible(_bankAccountNameFocus),
  247. );
  248. _bankAccountFocus.addListener(() => _ensureVisible(_bankAccountFocus));
  249. _checkAttachHealth();
  250. }
  251. Future<void> _checkAttachHealth() async {
  252. if (widget.checkAttachHealth == null) return;
  253. if (mounted) setState(() => _attachAvailable = false);
  254. try {
  255. final ok = await widget.checkAttachHealth!();
  256. if (mounted) setState(() => _attachAvailable = ok);
  257. } catch (_) {
  258. if (mounted) setState(() => _attachAvailable = false);
  259. }
  260. }
  261. @override
  262. void dispose() {
  263. _descCtrl.dispose();
  264. _amountCtrl.dispose();
  265. _approvedAmountCtrl.dispose();
  266. _remarkCtrl.dispose();
  267. _bankNameCtrl.dispose();
  268. _bankAccountNameCtrl.dispose();
  269. _bankAccountCtrl.dispose();
  270. _attachmentCtrl.dispose();
  271. _scrollCtrl.dispose();
  272. _remarkFocus.dispose();
  273. _bankNameFocus.dispose();
  274. _bankAccountNameFocus.dispose();
  275. _bankAccountFocus.dispose();
  276. super.dispose();
  277. }
  278. void _confirm() {
  279. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  280. if (amount <= 0) {
  281. TDToast.showText(_l10n.get('amountPositive'), context: context);
  282. return;
  283. }
  284. Navigator.pop(
  285. context,
  286. ExpenseDetailInputData(
  287. category: _cat,
  288. categoryName: _l10n.get(_selCat.nameKey),
  289. acctSubjectId: _selCat.acctSubjectId,
  290. acctSubjectName: _selCat.acctSubjectName,
  291. purpose: '',
  292. amount: amount,
  293. taxRate: _taxRate,
  294. projectId: _selProject?.id.toString() ?? '',
  295. projectName: _selProject?.name ?? '',
  296. costDeptId: _selDept?.id ?? '',
  297. costDeptName: _selDept?.name ?? '',
  298. customerVendorId: _selCustomer?.id ?? '',
  299. customerVendorName: _selCustomer?.name ?? '',
  300. approvedAmount: double.tryParse(_approvedAmountCtrl.text) ?? 0,
  301. bankName: _bankNameCtrl.text.trim(),
  302. bankAccountName: _bankAccountNameCtrl.text.trim(),
  303. bankAccount: _bankAccountCtrl.text.trim(),
  304. remark: _remarkCtrl.text.trim(),
  305. attachmentPaths: _attachmentCtrl.toPathList(),
  306. sqMan: _selEmployee?.salNo ?? '',
  307. sqManName: _selEmployee?.name ?? '',
  308. aeNo: widget.initialData?.aeNo ?? '',
  309. aeDd: widget.initialData?.aeDd ?? '',
  310. ),
  311. );
  312. }
  313. double get _amountExclTax => _taxRate > 0
  314. ? (double.tryParse(_amountCtrl.text) ?? 0) / (1 + _taxRate / 100)
  315. : (double.tryParse(_amountCtrl.text) ?? 0);
  316. double get _taxAmount =>
  317. (double.tryParse(_amountCtrl.text) ?? 0) - _amountExclTax;
  318. @override
  319. Widget build(BuildContext context) {
  320. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  321. return AnimatedPadding(
  322. padding: EdgeInsets.only(
  323. bottom: MediaQuery.of(context).viewInsets.bottom,
  324. ),
  325. duration: const Duration(milliseconds: 200),
  326. child: SafeArea(
  327. child: ConstrainedBox(
  328. constraints: BoxConstraints(
  329. maxHeight: MediaQuery.of(context).size.height * 0.8,
  330. ),
  331. child: Container(
  332. decoration: BoxDecoration(
  333. color: colors.bgPage,
  334. borderRadius: const BorderRadius.vertical(
  335. top: Radius.circular(16),
  336. ),
  337. ),
  338. child: Column(
  339. mainAxisSize: MainAxisSize.min,
  340. crossAxisAlignment: CrossAxisAlignment.stretch,
  341. children: [
  342. _buildHeader(colors),
  343. Flexible(
  344. child: GestureDetector(
  345. onTap: () => FocusScope.of(context).unfocus(),
  346. behavior: HitTestBehavior.translucent,
  347. child: SingleChildScrollView(
  348. controller: _scrollCtrl,
  349. keyboardDismissBehavior:
  350. ScrollViewKeyboardDismissBehavior.manual,
  351. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  352. child: Column(
  353. mainAxisSize: MainAxisSize.min,
  354. crossAxisAlignment: CrossAxisAlignment.stretch,
  355. children: [
  356. if (_isEdit &&
  357. widget.initialData!.aeNo.isNotEmpty) ...[
  358. _buildAeInfoCard(colors),
  359. const SizedBox(height: 12),
  360. ],
  361. _buildCategoryCard(colors),
  362. const SizedBox(height: 12),
  363. _buildAcctSubjectCard(colors),
  364. const SizedBox(height: 12),
  365. _buildAmountCard(),
  366. const SizedBox(height: 12),
  367. _buildTaxRateCard(colors),
  368. if ((double.tryParse(_amountCtrl.text) ?? 0) > 0) ...[
  369. const SizedBox(height: 12),
  370. _buildCalcInfo(colors),
  371. ],
  372. const SizedBox(height: 12),
  373. _buildApprovedAmountCard(),
  374. const SizedBox(height: 12),
  375. _buildProjectCard(colors),
  376. const SizedBox(height: 12),
  377. _buildCostDeptCard(colors),
  378. const SizedBox(height: 12),
  379. _buildCustomerCard(colors),
  380. const SizedBox(height: 12),
  381. _buildEmployeeCard(colors),
  382. const SizedBox(height: 12),
  383. _buildBankInfoCard(colors),
  384. const SizedBox(height: 12),
  385. _buildRemarkInput(colors),
  386. if (widget.showAttachments) ...[
  387. const SizedBox(height: 12),
  388. _buildAttachmentCard(colors),
  389. ],
  390. ],
  391. ),
  392. ),
  393. ),
  394. ),
  395. Container(
  396. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  397. decoration: BoxDecoration(
  398. color: colors.bgCard,
  399. border: Border(
  400. top: BorderSide(color: colors.border, width: 0.5),
  401. ),
  402. ),
  403. child: _buildActions(),
  404. ),
  405. ],
  406. ),
  407. ),
  408. ),
  409. ),
  410. );
  411. }
  412. // ── 标题栏 ──
  413. Widget _buildHeader(AppColorsExtension colors) {
  414. return Column(
  415. mainAxisSize: MainAxisSize.min,
  416. children: [
  417. Center(
  418. child: Container(
  419. margin: const EdgeInsets.only(top: 8, bottom: 4),
  420. width: 36,
  421. height: 4,
  422. decoration: BoxDecoration(
  423. color: colors.border,
  424. borderRadius: BorderRadius.circular(2),
  425. ),
  426. ),
  427. ),
  428. Padding(
  429. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  430. child: Row(
  431. children: [
  432. const SizedBox(width: 28),
  433. Expanded(
  434. child: Center(
  435. child: Text(
  436. _l10n.get('addExpenseDetail'),
  437. style: TextStyle(
  438. fontSize: AppFontSizes.title,
  439. fontWeight: FontWeight.w600,
  440. color: colors.textPrimary,
  441. ),
  442. ),
  443. ),
  444. ),
  445. GestureDetector(
  446. onTap: () => Navigator.pop(context),
  447. child: Padding(
  448. padding: const EdgeInsets.all(4),
  449. child: Icon(
  450. Icons.close,
  451. size: 20,
  452. color: colors.textSecondary,
  453. ),
  454. ),
  455. ),
  456. ],
  457. ),
  458. ),
  459. ],
  460. );
  461. }
  462. // ── picker 卡片 ──
  463. Widget _pickerCard({
  464. required String label,
  465. required bool required,
  466. required String currentLabel,
  467. required List<String> labels,
  468. required ValueChanged<int> onSelected,
  469. required AppColorsExtension colors,
  470. VoidCallback? onClear,
  471. }) {
  472. final tdTheme = TDTheme.of(context);
  473. final hasValue = onClear != null;
  474. return GestureDetector(
  475. onTap: () {
  476. if (labels.isEmpty) {
  477. TDToast.showText(_l10n.get('noData'), context: context);
  478. return;
  479. }
  480. FocusManager.instance.primaryFocus?.unfocus();
  481. TDPicker.showMultiPicker(
  482. context,
  483. title: label,
  484. backgroundColor: colors.bgCard,
  485. data: [labels],
  486. onConfirm: (selected) {
  487. if (selected.isNotEmpty && selected[0] is int) {
  488. final idx = selected[0] as int;
  489. if (idx >= 0 && idx < labels.length) {
  490. Navigator.of(context).pop();
  491. onSelected(idx);
  492. }
  493. }
  494. },
  495. );
  496. },
  497. child: Container(
  498. padding: const EdgeInsets.only(
  499. left: 16,
  500. right: 10,
  501. top: 12,
  502. bottom: 12,
  503. ),
  504. decoration: BoxDecoration(
  505. color: tdTheme.bgColorContainer,
  506. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  507. border: Border.all(color: tdTheme.componentStrokeColor),
  508. ),
  509. child: Row(
  510. children: [
  511. TDText(
  512. label,
  513. maxLines: 1,
  514. overflow: TextOverflow.visible,
  515. font: tdTheme.fontBodyLarge,
  516. fontWeight: FontWeight.w400,
  517. style: const TextStyle(letterSpacing: 0),
  518. ),
  519. if (required)
  520. Padding(
  521. padding: const EdgeInsets.only(left: 4),
  522. child: TDText(
  523. '*',
  524. font: tdTheme.fontBodyLarge,
  525. fontWeight: FontWeight.w400,
  526. style: TextStyle(color: tdTheme.errorColor6),
  527. ),
  528. ),
  529. const SizedBox(width: 12),
  530. Expanded(
  531. child: Row(
  532. mainAxisAlignment: MainAxisAlignment.end,
  533. mainAxisSize: MainAxisSize.max,
  534. children: [
  535. Flexible(
  536. child: TDText(
  537. currentLabel,
  538. maxLines: 1,
  539. overflow: TextOverflow.ellipsis,
  540. font: tdTheme.fontBodyLarge,
  541. fontWeight: FontWeight.w400,
  542. textColor: tdTheme.textColorPrimary,
  543. textAlign: TextAlign.end,
  544. ),
  545. ),
  546. const SizedBox(width: 4),
  547. SizedBox(
  548. width: 18,
  549. height: 18,
  550. child: hasValue
  551. ? GestureDetector(
  552. onTap: onClear,
  553. child: Icon(
  554. Icons.close,
  555. size: 18,
  556. color: tdTheme.textColorPlaceholder,
  557. ),
  558. )
  559. : Icon(
  560. Icons.chevron_right,
  561. size: 18,
  562. color: tdTheme.textColorPlaceholder,
  563. ),
  564. ),
  565. ],
  566. ),
  567. ),
  568. ],
  569. ),
  570. ),
  571. );
  572. }
  573. // ── 费用类别 ──
  574. Widget _buildCategoryCard(AppColorsExtension colors) {
  575. return _pickerCard(
  576. label: _l10n.get('expenseCategory'),
  577. required: true,
  578. currentLabel: '${_selCat.code}/${_l10n.get(_selCat.nameKey)}',
  579. labels: _cats.map((c) => '${c.code}/${_l10n.get(c.nameKey)}').toList(),
  580. colors: colors,
  581. onSelected: (idx) => setState(() => _cat = _cats[idx].code),
  582. );
  583. }
  584. // ── 输入卡片(对齐 pickerCard 样式) ──
  585. Widget _inputCard({
  586. required String label,
  587. required bool required,
  588. required TextEditingController controller,
  589. required String hintText,
  590. required AppColorsExtension colors,
  591. TextInputType? keyboardType,
  592. List<TextInputFormatter>? inputFormatters,
  593. FocusNode? focusNode,
  594. }) {
  595. final tdTheme = TDTheme.of(context);
  596. final hasValue = controller.text.isNotEmpty;
  597. return Container(
  598. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  599. decoration: BoxDecoration(
  600. color: tdTheme.bgColorContainer,
  601. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  602. border: Border.all(color: tdTheme.componentStrokeColor),
  603. ),
  604. child: Row(
  605. children: [
  606. TDText(
  607. label,
  608. maxLines: 1,
  609. overflow: TextOverflow.visible,
  610. font: tdTheme.fontBodyLarge,
  611. fontWeight: FontWeight.w400,
  612. style: const TextStyle(letterSpacing: 0),
  613. ),
  614. if (required)
  615. Padding(
  616. padding: const EdgeInsets.only(left: 4),
  617. child: TDText(
  618. '*',
  619. font: tdTheme.fontBodyLarge,
  620. fontWeight: FontWeight.w400,
  621. style: TextStyle(color: tdTheme.errorColor6),
  622. ),
  623. ),
  624. const SizedBox(width: 12),
  625. Expanded(
  626. child: Row(
  627. mainAxisAlignment: MainAxisAlignment.end,
  628. mainAxisSize: MainAxisSize.max,
  629. children: [
  630. Flexible(
  631. child: TextField(
  632. controller: controller,
  633. focusNode: focusNode,
  634. textAlign: TextAlign.end,
  635. keyboardType: keyboardType,
  636. inputFormatters: inputFormatters,
  637. style: TextStyle(fontSize: 16, color: colors.textPrimary),
  638. decoration: InputDecoration(
  639. hintText: hintText,
  640. hintStyle: TextStyle(
  641. fontSize: 16,
  642. color: colors.textPlaceholder,
  643. ),
  644. border: InputBorder.none,
  645. isDense: true,
  646. contentPadding: EdgeInsets.zero,
  647. ),
  648. onChanged: (_) => setState(() {}),
  649. ),
  650. ),
  651. const SizedBox(width: 4),
  652. SizedBox(
  653. width: 18,
  654. height: 18,
  655. child: hasValue
  656. ? GestureDetector(
  657. onTap: () {
  658. controller.clear();
  659. setState(() {});
  660. },
  661. child: Icon(
  662. Icons.close,
  663. size: 18,
  664. color: tdTheme.textColorPlaceholder,
  665. ),
  666. )
  667. : null,
  668. ),
  669. ],
  670. ),
  671. ),
  672. ],
  673. ),
  674. );
  675. }
  676. // ── 含税金额 ──
  677. Widget _buildAmountCard() {
  678. return _inputCard(
  679. label: _l10n.get('amountInclTax'),
  680. required: true,
  681. controller: _amountCtrl,
  682. hintText: '>0',
  683. colors: Theme.of(context).extension<AppColorsExtension>()!,
  684. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  685. inputFormatters: [
  686. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  687. ],
  688. );
  689. }
  690. // ── 税率 ──
  691. Widget _buildTaxRateCard(AppColorsExtension colors) {
  692. final currentLabel = '${_taxRate.toStringAsFixed(0)}%';
  693. return _pickerCard(
  694. label: _l10n.get('taxRate'),
  695. required: false,
  696. currentLabel: currentLabel,
  697. labels: _taxLabels.toList(),
  698. colors: colors,
  699. onSelected: (idx) => setState(() {
  700. _taxRate = _taxOptions[idx].toDouble();
  701. }),
  702. );
  703. }
  704. // ── 计算信息 ──
  705. Widget _buildCalcInfo(AppColorsExtension colors) {
  706. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  707. if (amount <= 0) return const SizedBox.shrink();
  708. return Container(
  709. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  710. decoration: BoxDecoration(
  711. color: colors.primaryLight,
  712. borderRadius: BorderRadius.circular(8),
  713. ),
  714. child: Row(
  715. children: [
  716. Expanded(
  717. child: Text(
  718. '${_l10n.get('amountExcludingTax')}: ¥${_amountExclTax.toStringAsFixed(2)}',
  719. style: TextStyle(
  720. fontSize: AppFontSizes.body,
  721. color: colors.textSecondary,
  722. ),
  723. ),
  724. ),
  725. Text(
  726. '${_l10n.get('taxAmount')}: ¥${_taxAmount.toStringAsFixed(2)}',
  727. style: TextStyle(
  728. fontSize: AppFontSizes.body,
  729. color: colors.textSecondary,
  730. ),
  731. ),
  732. ],
  733. ),
  734. );
  735. }
  736. // ── 导入单据信息 ──
  737. Widget _buildAeInfoCard(AppColorsExtension colors) {
  738. final d = widget.initialData!;
  739. final tdTheme = TDTheme.of(context);
  740. final dateStr = d.aeDd.isNotEmpty
  741. ? _formatDate(d.aeDd)
  742. : d.aeDd;
  743. return Container(
  744. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  745. decoration: _cardDecoration(tdTheme),
  746. child: Column(
  747. children: [
  748. _readOnlyRow(tdTheme, _l10n.get('expenseApplyNo'), d.aeNo),
  749. const SizedBox(height: 8),
  750. _readOnlyRow(tdTheme, _l10n.get('applyDate'), dateStr),
  751. ],
  752. ),
  753. );
  754. }
  755. String _formatDate(String raw) {
  756. final dt = DateTime.tryParse(raw);
  757. if (dt == null) return raw;
  758. return '${dt.year}-${dt.month.toString().padLeft(2, '0')}-${dt.day.toString().padLeft(2, '0')}';
  759. }
  760. Widget _readOnlyRow(TDThemeData tdTheme, String label, String value) {
  761. return Row(
  762. children: [
  763. TDText(
  764. label,
  765. font: tdTheme.fontBodyLarge,
  766. fontWeight: FontWeight.w400,
  767. style: const TextStyle(letterSpacing: 0),
  768. ),
  769. const SizedBox(width: 12),
  770. Expanded(
  771. child: TDText(
  772. value,
  773. maxLines: 1,
  774. overflow: TextOverflow.ellipsis,
  775. font: tdTheme.fontBodyLarge,
  776. fontWeight: FontWeight.w400,
  777. textColor: tdTheme.textColorPrimary,
  778. textAlign: TextAlign.end,
  779. ),
  780. ),
  781. ],
  782. );
  783. }
  784. BoxDecoration _cardDecoration(TDThemeData tdTheme) => BoxDecoration(
  785. color: tdTheme.bgColorContainer,
  786. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  787. border: Border.all(color: tdTheme.componentStrokeColor),
  788. );
  789. // ── 申请人 ──
  790. Widget _buildEmployeeCard(AppColorsExtension colors) {
  791. final employees = widget.employees;
  792. return _pickerCard(
  793. label: _l10n.get('applicant'),
  794. required: false,
  795. currentLabel: _selEmployee != null
  796. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  797. : _l10n.get('pleaseSelect'),
  798. labels: employees.map((e) => '${e.salNo}/${e.name}').toList(),
  799. colors: colors,
  800. onSelected: (idx) => setState(() {
  801. _selEmployee = employees[idx];
  802. _bankNameCtrl.text = _selEmployee!.bnkNo;
  803. _bankAccountNameCtrl.text = _selEmployee!.accName;
  804. _bankAccountCtrl.text = _selEmployee!.bnkId;
  805. }),
  806. onClear: _selEmployee != null
  807. ? () => setState(() {
  808. _selEmployee = null;
  809. _bankNameCtrl.clear();
  810. _bankAccountNameCtrl.clear();
  811. _bankAccountCtrl.clear();
  812. })
  813. : null,
  814. );
  815. }
  816. // ── 客户/厂商 ──
  817. Widget _buildCustomerCard(AppColorsExtension colors) {
  818. final vendors = widget.customers;
  819. return _pickerCard(
  820. label: _l10n.get('customerVendor'),
  821. required: false,
  822. currentLabel: _selCustomer != null
  823. ? '${_selCustomer!.id}/${_selCustomer!.name}'
  824. : _l10n.get('pleaseSelect'),
  825. labels: vendors.map((v) => '${v.id}/${v.name}').toList(),
  826. colors: colors,
  827. onSelected: (idx) => setState(() => _selCustomer = vendors[idx]),
  828. onClear: _selCustomer != null
  829. ? () => setState(() => _selCustomer = null)
  830. : null,
  831. );
  832. }
  833. // ── 核准金额 ──
  834. Widget _buildApprovedAmountCard() {
  835. if (!widget.canEditApprovedAmount) {
  836. final value = _approvedAmountCtrl.text.isNotEmpty
  837. ? '¥${double.tryParse(_approvedAmountCtrl.text)?.toStringAsFixed(2) ?? _approvedAmountCtrl.text}'
  838. : '-';
  839. return _readOnlyCard(
  840. label: _l10n.get('approvedAmount'),
  841. value: value,
  842. colors: Theme.of(context).extension<AppColorsExtension>()!,
  843. );
  844. }
  845. return _inputCard(
  846. label: _l10n.get('approvedAmount'),
  847. required: false,
  848. controller: _approvedAmountCtrl,
  849. hintText: '0',
  850. colors: Theme.of(context).extension<AppColorsExtension>()!,
  851. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  852. inputFormatters: [
  853. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  854. ],
  855. );
  856. }
  857. // ── 备注 ──
  858. Widget _buildRemarkInput(AppColorsExtension colors) {
  859. final tdTheme = TDTheme.of(context);
  860. return TDTextarea(
  861. controller: _remarkCtrl,
  862. focusNode: _remarkFocus,
  863. label: _l10n.get('remark'),
  864. hintText: _l10n.get('enterRemark'),
  865. maxLines: 3,
  866. minLines: 1,
  867. maxLength: 500,
  868. indicator: true,
  869. decoration: BoxDecoration(
  870. color: tdTheme.bgColorContainer,
  871. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  872. border: Border.all(color: tdTheme.componentStrokeColor),
  873. ),
  874. onChanged: (_) => setState(() {}),
  875. );
  876. }
  877. // ── 操作按钮 ──
  878. Widget _buildAttachmentCard(AppColorsExtension colors) {
  879. final tdTheme = TDTheme.of(context);
  880. Widget? hint;
  881. if (!_attachAvailable) {
  882. hint = TDText(
  883. _l10n.get('attachServiceUnavailable'),
  884. font: tdTheme.fontBodySmall,
  885. fontWeight: FontWeight.w400,
  886. textColor: tdTheme.textColorPlaceholder,
  887. );
  888. } else if (widget.billFileRights != null &&
  889. !widget.billFileRights!.canManageAttachments) {
  890. hint = TDText(
  891. _l10n.get('noAttachmentPermission'),
  892. font: tdTheme.fontBodySmall,
  893. fontWeight: FontWeight.w400,
  894. textColor: tdTheme.textColorPlaceholder,
  895. );
  896. }
  897. return Column(
  898. crossAxisAlignment: CrossAxisAlignment.start,
  899. children: [
  900. Padding(
  901. padding: const EdgeInsets.only(left: 4),
  902. child: TDText(
  903. _l10n.get('attachmentUpload'),
  904. font: tdTheme.fontBodyLarge,
  905. fontWeight: FontWeight.w400,
  906. style: const TextStyle(letterSpacing: 0),
  907. ),
  908. ),
  909. const SizedBox(height: 4),
  910. if (hint != null) ...[
  911. Padding(padding: const EdgeInsets.only(left: 4), child: hint),
  912. ] else ...[
  913. Padding(
  914. padding: const EdgeInsets.only(left: 4, top: 4),
  915. child: TDText(
  916. _l10n.get('maxAttachment'),
  917. font: tdTheme.fontBodySmall,
  918. fontWeight: FontWeight.w400,
  919. textColor: tdTheme.textColorPlaceholder,
  920. ),
  921. ),
  922. const SizedBox(height: 4),
  923. AttachmentPicker(
  924. controller: _attachmentCtrl,
  925. maxImageSizeMB: 10,
  926. maxFileSizeMB: 20,
  927. allowedExtensions: const [
  928. 'pdf',
  929. 'doc',
  930. 'docx',
  931. 'xls',
  932. 'xlsx',
  933. 'ppt',
  934. 'pptx',
  935. 'txt',
  936. ],
  937. onFileRejected: (file, reason) {
  938. if (context.mounted) TDToast.showText(reason, context: context);
  939. },
  940. ),
  941. ],
  942. ],
  943. );
  944. }
  945. // ── 会计科目(只读,选择类别后自动带出) ──
  946. Widget _buildAcctSubjectCard(AppColorsExtension colors) {
  947. final id = _selCat.acctSubjectId.isNotEmpty
  948. ? _selCat.acctSubjectId
  949. : widget.initialData?.acctSubjectId ?? '';
  950. final name = _selCat.acctSubjectName.isNotEmpty
  951. ? _selCat.acctSubjectName
  952. : widget.initialData?.acctSubjectName ?? '';
  953. return _readOnlyCard(
  954. label: _l10n.get('acctSubject'),
  955. value: id.isNotEmpty ? '$id${name.isNotEmpty ? '/$name' : ''}' : '',
  956. colors: colors,
  957. );
  958. }
  959. Widget _readOnlyCard({
  960. required String label,
  961. required String value,
  962. required AppColorsExtension colors,
  963. }) {
  964. final tdTheme = TDTheme.of(context);
  965. return Container(
  966. padding: const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 12),
  967. decoration: BoxDecoration(
  968. color: tdTheme.bgColorContainer,
  969. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  970. border: Border.all(color: tdTheme.componentStrokeColor),
  971. ),
  972. child: Row(
  973. children: [
  974. TDText(
  975. label,
  976. maxLines: 1,
  977. overflow: TextOverflow.visible,
  978. font: tdTheme.fontBodyLarge,
  979. fontWeight: FontWeight.w400,
  980. style: const TextStyle(letterSpacing: 0),
  981. ),
  982. const SizedBox(width: 12),
  983. Expanded(
  984. child: TDText(
  985. value,
  986. maxLines: 1,
  987. overflow: TextOverflow.ellipsis,
  988. font: tdTheme.fontBodyLarge,
  989. fontWeight: FontWeight.w400,
  990. textColor: tdTheme.textColorPrimary,
  991. textAlign: TextAlign.end,
  992. ),
  993. ),
  994. ],
  995. ),
  996. );
  997. }
  998. // ── 关联项目 ──
  999. Widget _buildProjectCard(AppColorsExtension colors) {
  1000. final projects = widget.projects;
  1001. return _pickerCard(
  1002. label: _l10n.get('relatedProject'),
  1003. required: false,
  1004. currentLabel: _selProject != null
  1005. ? '${_selProject!.id}/${_selProject!.name}'
  1006. : _l10n.get('pleaseSelect'),
  1007. labels: projects.map((p) => '${p.id}/${p.name}').toList(),
  1008. colors: colors,
  1009. onSelected: (idx) => setState(() => _selProject = projects[idx]),
  1010. onClear: _selProject != null
  1011. ? () => setState(() => _selProject = null)
  1012. : null,
  1013. );
  1014. }
  1015. // ── 费用承担部门 ──
  1016. Widget _buildCostDeptCard(AppColorsExtension colors) {
  1017. final depts = widget.costDepts;
  1018. return _pickerCard(
  1019. label: _l10n.get('costDept'),
  1020. required: false,
  1021. currentLabel: _selDept != null
  1022. ? '${_selDept!.id}/${_selDept!.name}'
  1023. : _l10n.get('pleaseSelect'),
  1024. labels: depts.map((d) => '${d.id}/${d.name}').toList(),
  1025. colors: colors,
  1026. onSelected: (idx) => setState(() => _selDept = depts[idx]),
  1027. onClear: _selDept != null ? () => setState(() => _selDept = null) : null,
  1028. );
  1029. }
  1030. // ── 收款银行信息 ──
  1031. Widget _buildBankInfoCard(AppColorsExtension colors) {
  1032. return Column(
  1033. crossAxisAlignment: CrossAxisAlignment.start,
  1034. children: [
  1035. _inputCard(
  1036. label: _l10n.get('bankName'),
  1037. required: false,
  1038. controller: _bankNameCtrl,
  1039. hintText: _l10n.get('pleaseEnter'),
  1040. colors: colors,
  1041. focusNode: _bankNameFocus,
  1042. ),
  1043. const SizedBox(height: 12),
  1044. _inputCard(
  1045. label: _l10n.get('bankAccountName'),
  1046. required: false,
  1047. controller: _bankAccountNameCtrl,
  1048. hintText: _l10n.get('pleaseEnter'),
  1049. colors: colors,
  1050. focusNode: _bankAccountNameFocus,
  1051. ),
  1052. const SizedBox(height: 12),
  1053. _inputCard(
  1054. label: _l10n.get('bankAccount'),
  1055. required: false,
  1056. controller: _bankAccountCtrl,
  1057. hintText: _l10n.get('pleaseEnter'),
  1058. colors: colors,
  1059. focusNode: _bankAccountFocus,
  1060. ),
  1061. ],
  1062. );
  1063. }
  1064. Widget _buildActions() {
  1065. return Row(
  1066. children: [
  1067. Expanded(
  1068. child: TDButton(
  1069. text: _l10n.get('cancel'),
  1070. size: TDButtonSize.large,
  1071. type: TDButtonType.outline,
  1072. shape: TDButtonShape.rectangle,
  1073. theme: TDButtonTheme.defaultTheme,
  1074. onTap: () => Navigator.pop(context),
  1075. ),
  1076. ),
  1077. const SizedBox(width: 12),
  1078. Expanded(
  1079. child: TDButton(
  1080. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  1081. size: TDButtonSize.large,
  1082. type: TDButtonType.fill,
  1083. shape: TDButtonShape.rectangle,
  1084. theme: TDButtonTheme.primary,
  1085. onTap: _confirm,
  1086. ),
  1087. ),
  1088. ],
  1089. );
  1090. }
  1091. }