vehicle_apply_detail_page.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import '../../core/i18n/app_localizations.dart';
  6. import '../../core/navigation/host_app_channel.dart';
  7. import '../../core/theme/app_colors.dart';
  8. import '../../core/theme/app_colors_extension.dart';
  9. import '../../core/utils/date_utils.dart' as du;
  10. import '../../core/utils/amount_utils.dart';
  11. import '../../shared/widgets/app_skeletons.dart';
  12. import '../../shared/widgets/bill_status_bar.dart';
  13. import '../../shared/widgets/loading_dialog.dart';
  14. import '../../shared/widgets/trip_route_card.dart';
  15. import '../../shared/widgets/form_field_row.dart';
  16. import '../../shared/widgets/form_section.dart';
  17. import 'vehicle_apply_api.dart';
  18. import 'vehicle_apply_list_controller.dart';
  19. import 'vehicle_apply_model.dart';
  20. import 'widgets/return_car_dialog.dart';
  21. class VehicleApplyDetailPage extends ConsumerStatefulWidget {
  22. final String billNo;
  23. const VehicleApplyDetailPage({super.key, required this.billNo});
  24. @override
  25. ConsumerState<VehicleApplyDetailPage> createState() =>
  26. _VehicleApplyDetailPageState();
  27. }
  28. class _VehicleApplyDetailPageState
  29. extends ConsumerState<VehicleApplyDetailPage> {
  30. bool _loading = true;
  31. String? _error;
  32. VehicleApplyModel? _data;
  33. BillStatusBar? _billStatusBar;
  34. @override
  35. void initState() {
  36. super.initState();
  37. _loadData();
  38. }
  39. Future<void> _loadData() async {
  40. setState(() {
  41. _loading = true;
  42. _error = null;
  43. });
  44. try {
  45. // 先用 mock 数据匹配 billNo
  46. final match = mockVehicles.where((e) => e.ycNo == widget.billNo);
  47. if (match.isNotEmpty) {
  48. if (mounted) {
  49. setState(() {
  50. _data = match.first;
  51. _loading = false;
  52. });
  53. }
  54. } else {
  55. final api = ref.read(vehicleApplyApiProvider);
  56. final detail = await api.fetchDetail(widget.billNo);
  57. if (!mounted) return;
  58. // 获取单据状态 + 审核配置 + 还车权限(非致命),与详情合并到一个 setState 避免中间闪烁
  59. Map<String, dynamic>? status;
  60. Map<String, dynamic>? auditConfig;
  61. Map<String, dynamic>? rights;
  62. try {
  63. final results = await Future.wait([
  64. api.getBillStatus(widget.billNo),
  65. api.getBillAuditConfig('YC'),
  66. api.getUserRights('RHB'),
  67. ]);
  68. final Map<String, dynamic> s = results[0];
  69. final Map<String, dynamic> c = results[1];
  70. final Map<String, dynamic> r = results[2];
  71. status = s;
  72. auditConfig = c;
  73. rights = r;
  74. } catch (_) {}
  75. final hasAuditFlow = auditConfig?['hasAuditFlow'] == true;
  76. final autoSubmit = auditConfig?['autoSubmit'] == true;
  77. final canManualSubmit = hasAuditFlow && !autoSubmit;
  78. final canReturn =
  79. rights?['canAdd'] == true || rights?['canModify'] == true;
  80. if (mounted) {
  81. setState(() {
  82. _data = detail;
  83. _billStatusBar = (status != null)
  84. ? BillStatusBar(
  85. billStatus: status,
  86. canManualSubmit: canManualSubmit,
  87. returnedText: AppLocalizations.of(
  88. context,
  89. ).get('vehicleReturned'),
  90. onEdit: () {
  91. GoRouter.of(context)
  92. .push('/vehicle-apply/edit/${widget.billNo}')
  93. .then((result) {
  94. if (result == true && mounted) _loadData();
  95. });
  96. },
  97. onSubmit: () async {
  98. final l10n = AppLocalizations.of(context);
  99. final dd = _data?.ycDd != null
  100. ? du.DateUtils.formatDate(_data!.ycDd!)
  101. : '';
  102. LoadingDialog.show(context, text: l10n.get('submitting'));
  103. try {
  104. await api.shSubmit(
  105. bilId: 'YC',
  106. bilNo: widget.billNo,
  107. bilDd: dd,
  108. );
  109. } finally {
  110. if (mounted) LoadingDialog.hide(context);
  111. }
  112. if (mounted) _loadData();
  113. },
  114. onCancelSubmit: () async {
  115. final l10n = AppLocalizations.of(context);
  116. final dd = _data?.ycDd != null
  117. ? du.DateUtils.formatDate(_data!.ycDd!)
  118. : '';
  119. LoadingDialog.show(context, text: l10n.get('submitting'));
  120. try {
  121. await api.shSubmit(
  122. bilId: 'YC',
  123. bilNo: widget.billNo,
  124. bilDd: dd,
  125. isCancel: true,
  126. );
  127. } finally {
  128. if (mounted) LoadingDialog.hide(context);
  129. }
  130. if (mounted) _loadData();
  131. },
  132. onTapStatusTag: () => _showAuditTrail('YC'),
  133. onReturn: canReturn
  134. ? () {
  135. ReturnCarDialog.show(
  136. context,
  137. billNo: widget.billNo,
  138. odometerBegin: _data?.odometerBegin ?? 0,
  139. recordDd: _data?.recordDd,
  140. onSubmit: (returnData) async {
  141. final api = ref.read(vehicleApplyApiProvider);
  142. returnData['usrCheck'] = HostAppChannel.usr;
  143. try {
  144. await api.submitReturn(
  145. widget.billNo,
  146. returnData,
  147. );
  148. if (mounted) _loadData();
  149. return true;
  150. } catch (_) {
  151. return false;
  152. }
  153. },
  154. );
  155. }
  156. : null,
  157. onEditReturn: () {
  158. ReturnCarDialog.show(
  159. context,
  160. billNo: widget.billNo,
  161. odometerBegin: _data?.odometerBegin ?? 0,
  162. recordDd: _data?.recordDd,
  163. onSubmit: (returnData) async {
  164. final api = ref.read(vehicleApplyApiProvider);
  165. returnData['usrCheck'] = HostAppChannel.usr;
  166. try {
  167. await api.submitReturn(
  168. widget.billNo,
  169. returnData,
  170. );
  171. if (mounted) _loadData();
  172. return true;
  173. } catch (_) {
  174. return false;
  175. }
  176. },
  177. );
  178. },
  179. )
  180. : null;
  181. _loading = false;
  182. });
  183. }
  184. }
  185. } catch (e) {
  186. if (mounted) {
  187. setState(() {
  188. _error = e.toString();
  189. _loading = false;
  190. });
  191. }
  192. }
  193. }
  194. @override
  195. Widget build(BuildContext context) {
  196. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  197. final l10n = AppLocalizations.of(context);
  198. if (_loading) return const SkeletonDetailPage(sectionRows: [5, 6, 4, 1]);
  199. if (_error != null) {
  200. return Center(
  201. child: Column(
  202. mainAxisSize: MainAxisSize.min,
  203. children: [
  204. Icon(Icons.error_outline, size: 48, color: colors.danger),
  205. const SizedBox(height: 16),
  206. Padding(
  207. padding: const EdgeInsets.symmetric(horizontal: 32),
  208. child: Text(
  209. _error!,
  210. textAlign: TextAlign.center,
  211. style: TextStyle(
  212. fontSize: AppFontSizes.body,
  213. color: colors.textSecondary,
  214. ),
  215. ),
  216. ),
  217. const SizedBox(height: 16),
  218. TDButton(
  219. text: l10n.get('retry'),
  220. size: TDButtonSize.medium,
  221. onTap: _loadData,
  222. ),
  223. ],
  224. ),
  225. );
  226. }
  227. final app = _data!;
  228. return Column(
  229. children: [
  230. Expanded(
  231. child: SingleChildScrollView(
  232. physics: const AlwaysScrollableScrollPhysics(),
  233. padding: const EdgeInsets.all(16),
  234. child: Column(
  235. children: [
  236. _buildBasicInfoSection(app, l10n, colors),
  237. const SizedBox(height: 16),
  238. _buildVehicleInfoSection(app, l10n, colors),
  239. const SizedBox(height: 16),
  240. _buildTripInfoSection(app, l10n, colors),
  241. const SizedBox(height: 16),
  242. _buildPassengerInfoSection(app, l10n, colors),
  243. if (app.isReturn) ...[
  244. const SizedBox(height: 16),
  245. _buildReturnInfoSection(app, l10n, colors),
  246. ],
  247. const SizedBox(height: 24),
  248. _buildPageFooter(colors),
  249. ],
  250. ),
  251. ),
  252. ),
  253. _billStatusBar?.buildActions(context) ?? const SizedBox.shrink(),
  254. ],
  255. );
  256. }
  257. // ═══ 基本信息 ═══
  258. Widget _buildBasicInfoSection(
  259. VehicleApplyModel app,
  260. AppLocalizations l10n,
  261. AppColorsExtension colors,
  262. ) {
  263. return FormSection(
  264. title: l10n.get('basicInfo'),
  265. leadingIcon: Icons.info_outline,
  266. trailing:
  267. _billStatusBar?.buildStatusTag(context) ?? const SizedBox.shrink(),
  268. children: [
  269. FormFieldRow(
  270. label: l10n.get('vehicleApplyNo'),
  271. value: app.ycNo,
  272. readOnly: true,
  273. showArrow: false,
  274. ),
  275. const SizedBox(height: 16),
  276. FormFieldRow(
  277. label: l10n.get('date'),
  278. value: app.ycDd != null ? du.DateUtils.formatDate(app.ycDd!) : '-',
  279. readOnly: true,
  280. showArrow: false,
  281. ),
  282. const SizedBox(height: 16),
  283. FormFieldRow(
  284. label: l10n.get('applicant'),
  285. value: app.applicantName.isNotEmpty ? app.applicantName : app.salNo,
  286. readOnly: true,
  287. showArrow: false,
  288. ),
  289. const SizedBox(height: 16),
  290. FormFieldRow(
  291. label: l10n.get('dep'),
  292. value: app.dep.isNotEmpty
  293. ? '${app.dep}${app.deptName.isNotEmpty ? '/${app.deptName}' : ''}'
  294. : '-',
  295. readOnly: true,
  296. showArrow: false,
  297. ),
  298. const SizedBox(height: 16),
  299. FormFieldRow(
  300. label: l10n.get('vehicleReason'),
  301. value: app.reason.isNotEmpty ? app.reason : '-',
  302. readOnly: true,
  303. showArrow: false,
  304. bold: true,
  305. showMoreOnOverflow: true,
  306. ),
  307. ],
  308. );
  309. }
  310. // ═══ 车辆信息 ═══
  311. Widget _buildVehicleInfoSection(
  312. VehicleApplyModel app,
  313. AppLocalizations l10n,
  314. AppColorsExtension colors,
  315. ) {
  316. return FormSection(
  317. title: l10n.get('vehicleInfo'),
  318. leadingIcon: Icons.directions_car_outlined,
  319. children: [
  320. FormFieldRow(
  321. label: l10n.get('licensePlate'),
  322. value: app.licensePlate.isNotEmpty ? app.licensePlate : '-',
  323. readOnly: true,
  324. showArrow: false,
  325. bold: true,
  326. valueColor: colors.platePrimary,
  327. ),
  328. const SizedBox(height: 16),
  329. FormFieldRow(
  330. label: l10n.get('vehicleType'),
  331. value: app.vehicleType.isNotEmpty
  332. ? _vehicleTypeLabel(app.vehicleType, l10n)
  333. : '-',
  334. readOnly: true,
  335. showArrow: false,
  336. ),
  337. const SizedBox(height: 16),
  338. FormFieldRow(
  339. label: l10n.get('brand'),
  340. value: app.brand.isNotEmpty ? app.brand : '-',
  341. readOnly: true,
  342. showArrow: false,
  343. ),
  344. const SizedBox(height: 16),
  345. FormFieldRow(
  346. label: l10n.get('seats'),
  347. value: app.seats > 0 ? '${app.seats}' : '-',
  348. readOnly: true,
  349. showArrow: false,
  350. ),
  351. const SizedBox(height: 16),
  352. FormFieldRow(
  353. label: l10n.get('odometerBegin'),
  354. value: app.odometerBegin > 0 ? '${app.odometerBegin} km' : '-',
  355. readOnly: true,
  356. showArrow: false,
  357. ),
  358. const SizedBox(height: 16),
  359. FormFieldRow(
  360. label: l10n.get('driverName'),
  361. value: app.driverName.isNotEmpty ? app.driverName : '-',
  362. readOnly: true,
  363. showArrow: false,
  364. ),
  365. ],
  366. );
  367. }
  368. // ═══ 行程信息 ═══
  369. Widget _buildTripInfoSection(
  370. VehicleApplyModel app,
  371. AppLocalizations l10n,
  372. AppColorsExtension colors,
  373. ) {
  374. return FormSection(
  375. title: l10n.get('tripInfo'),
  376. leadingIcon: Icons.route_outlined,
  377. children: [
  378. FormFieldRow(
  379. label: l10n.get('departTime'),
  380. value: app.startTime != null
  381. ? du.DateUtils.formatDateTime(app.startTime!)
  382. : '-',
  383. readOnly: true,
  384. showArrow: false,
  385. ),
  386. const SizedBox(height: 16),
  387. FormFieldRow(
  388. label: l10n.get('returnTime'),
  389. value: app.endTime != null
  390. ? du.DateUtils.formatDateTime(app.endTime!)
  391. : '-',
  392. readOnly: true,
  393. showArrow: false,
  394. ),
  395. const SizedBox(height: 16),
  396. TripRouteCard(
  397. originLabel: app.originAdr.isNotEmpty ? app.originAdr : '-',
  398. destLabel: app.destAdr.isNotEmpty ? app.destAdr : '-',
  399. originLat: app.originLatitude,
  400. originLng: app.originLongitude,
  401. destLat: app.destLatitude,
  402. destLng: app.destLongitude,
  403. ),
  404. ],
  405. );
  406. }
  407. // ═══ 同行信息 ═══
  408. Widget _buildPassengerInfoSection(
  409. VehicleApplyModel app,
  410. AppLocalizations l10n,
  411. AppColorsExtension colors,
  412. ) {
  413. final names = app.passengerName.isNotEmpty
  414. ? app.passengerName
  415. .split(';')
  416. .where((n) => n.trim().isNotEmpty)
  417. .toList()
  418. : <String>[];
  419. return FormSection(
  420. title: l10n.get('companionInfo'),
  421. leadingIcon: Icons.people_outline,
  422. children: [
  423. if (names.isEmpty)
  424. Padding(
  425. padding: const EdgeInsets.symmetric(vertical: 8),
  426. child: Text(
  427. '-',
  428. style: TextStyle(
  429. fontSize: AppFontSizes.subtitle,
  430. color: colors.textPlaceholder,
  431. ),
  432. ),
  433. )
  434. else
  435. Wrap(
  436. spacing: 10,
  437. runSpacing: 10,
  438. children: names.map((name) {
  439. return Container(
  440. padding: const EdgeInsets.symmetric(
  441. horizontal: 14,
  442. vertical: 10,
  443. ),
  444. decoration: BoxDecoration(
  445. color: colors.bgCard,
  446. borderRadius: BorderRadius.circular(20),
  447. border: Border.all(color: colors.border, width: 0.5),
  448. boxShadow: [
  449. BoxShadow(
  450. color: Colors.black.withValues(alpha: 0.04),
  451. blurRadius: 4,
  452. offset: const Offset(0, 1),
  453. ),
  454. ],
  455. ),
  456. child: Row(
  457. mainAxisSize: MainAxisSize.min,
  458. children: [
  459. Icon(Icons.person_outline, size: 16, color: colors.primary),
  460. const SizedBox(width: 8),
  461. Text(
  462. name.trim(),
  463. style: TextStyle(
  464. fontSize: AppFontSizes.body,
  465. color: colors.textPrimary,
  466. ),
  467. ),
  468. ],
  469. ),
  470. );
  471. }).toList(),
  472. ),
  473. const SizedBox(height: 16),
  474. Row(
  475. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  476. children: [
  477. Text(
  478. l10n.get('passengerCount'),
  479. style: TextStyle(
  480. fontSize: AppFontSizes.body,
  481. fontWeight: FontWeight.w600,
  482. color: colors.textPrimary,
  483. ),
  484. ),
  485. Text(
  486. '${app.passengerCount}',
  487. style: TextStyle(
  488. fontSize: AppFontSizes.subtitle,
  489. fontWeight: FontWeight.w700,
  490. color: colors.textPrimary,
  491. ),
  492. ),
  493. ],
  494. ),
  495. ],
  496. );
  497. }
  498. // ═══ 审批轨迹 ═══
  499. Future<void> _showAuditTrail(String billId) async {
  500. await HostAppChannel.showAuditTrail(billId, widget.billNo);
  501. }
  502. // ═══ 还车信息 ═══
  503. Widget _buildReturnInfoSection(
  504. VehicleApplyModel app,
  505. AppLocalizations l10n,
  506. AppColorsExtension colors,
  507. ) {
  508. return FormSection(
  509. title: l10n.get('returnCarInfo'),
  510. leadingIcon: Icons.assignment_return_outlined,
  511. children: [
  512. FormFieldRow(
  513. label: l10n.get('actualReturnTime'),
  514. value: app.returnTime != null
  515. ? du.DateUtils.formatDateTimeSec(app.returnTime!)
  516. : '-',
  517. readOnly: true,
  518. showArrow: false,
  519. ),
  520. const SizedBox(height: 16),
  521. FormFieldRow(
  522. label: l10n.get('odometerEnd'),
  523. value: app.odometerEnd > 0 ? '${app.odometerEnd} km' : '-',
  524. readOnly: true,
  525. showArrow: false,
  526. ),
  527. const SizedBox(height: 16),
  528. FormFieldRow(
  529. label: l10n.get('amtn'),
  530. value: app.amtn > 0 ? formatAmount(app.amtn) : '-',
  531. readOnly: true,
  532. showArrow: false,
  533. valueColor: colors.amountPrimary,
  534. ),
  535. const SizedBox(height: 16),
  536. FormFieldRow(
  537. label: l10n.get('remark'),
  538. value: app.remark.isNotEmpty ? app.remark : '-',
  539. readOnly: true,
  540. showArrow: false,
  541. showMoreOnOverflow: true,
  542. ),
  543. const SizedBox(height: 16),
  544. FormFieldRow(
  545. label: l10n.get('usrCheck'),
  546. value: app.usrCheck.isNotEmpty ? app.usrCheck : '-',
  547. readOnly: true,
  548. showArrow: false,
  549. ),
  550. const SizedBox(height: 16),
  551. FormFieldRow(
  552. label: l10n.get('checkDd'),
  553. value: app.checkDd != null
  554. ? du.DateUtils.formatDateTimeSec(app.checkDd!)
  555. : '-',
  556. readOnly: true,
  557. showArrow: false,
  558. ),
  559. ],
  560. );
  561. }
  562. Widget _buildPageFooter(AppColorsExtension colors) {
  563. final l10n = AppLocalizations.of(context);
  564. return Center(
  565. child: Padding(
  566. padding: const EdgeInsets.only(bottom: 16),
  567. child: Row(
  568. mainAxisSize: MainAxisSize.min,
  569. children: [
  570. Icon(
  571. Icons.rocket_launch_outlined,
  572. size: 16,
  573. color: colors.textPlaceholder,
  574. ),
  575. const SizedBox(width: 6),
  576. Text(
  577. l10n.get('pageFooter'),
  578. style: TextStyle(
  579. fontSize: AppFontSizes.caption,
  580. color: colors.textPlaceholder,
  581. ),
  582. ),
  583. ],
  584. ),
  585. ),
  586. );
  587. }
  588. String _vehicleTypeLabel(String key, AppLocalizations l10n) {
  589. switch (key) {
  590. case 'sedan':
  591. return l10n.get('sedan');
  592. case 'suv':
  593. return 'SUV';
  594. case 'mpv':
  595. return l10n.get('businessVan');
  596. case 'van':
  597. return l10n.get('van');
  598. case 'truck':
  599. return l10n.get('truck');
  600. case 'pickup':
  601. return l10n.get('pickup');
  602. case 'minibus':
  603. return l10n.get('minibus');
  604. case 'bus':
  605. return l10n.get('bus');
  606. default:
  607. return key;
  608. }
  609. }
  610. }