overtime_list_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 'package:easy_refresh/easy_refresh.dart';
  6. import '../../shared/widgets/nav_bar_config.dart';
  7. import '../../core/theme/app_colors_extension.dart';
  8. import '../../core/utils/date_utils.dart' as du;
  9. import '../../core/auth/role_provider.dart';
  10. import '../../shared/widgets/list_card.dart';
  11. import '../../shared/widgets/status_tag.dart';
  12. import '../../shared/widgets/empty_state.dart';
  13. import '../../shared/widgets/skeleton_list_card.dart';
  14. import '../../shared/widgets/list_filter_panel.dart';
  15. import '../../shared/widgets/list_footer.dart';
  16. import '../../core/i18n/app_localizations.dart';
  17. import 'overtime_list_controller.dart';
  18. import 'overtime_model.dart';
  19. final _scopeProvider = StateProvider<String>((ref) => 'my');
  20. class OvertimeListPage extends ConsumerStatefulWidget {
  21. const OvertimeListPage({super.key});
  22. @override
  23. ConsumerState<OvertimeListPage> createState() => _OvertimeListPageState();
  24. }
  25. class _OvertimeListPageState extends ConsumerState<OvertimeListPage>
  26. with TickerProviderStateMixin {
  27. List<String> _getTabLabels(AppLocalizations l10n) => [
  28. l10n.get('all'),
  29. l10n.get('draft'),
  30. l10n.get('pending'),
  31. l10n.get('approved'),
  32. l10n.get('rejected'),
  33. l10n.get('withdrawn'),
  34. ];
  35. static const _tabKeys = [
  36. '',
  37. 'draft',
  38. 'pending',
  39. 'approved',
  40. 'rejected',
  41. 'withdrawn',
  42. ];
  43. late final TabController _tabCtrl;
  44. @override
  45. void initState() {
  46. super.initState();
  47. _tabCtrl = TabController(length: _tabKeys.length, vsync: this);
  48. _tabCtrl.addListener(() {
  49. if (!_tabCtrl.indexIsChanging) {
  50. ref.read(overtimeStatusFilterProvider.notifier).state =
  51. _tabKeys[_tabCtrl.index];
  52. }
  53. });
  54. }
  55. @override
  56. void dispose() {
  57. _tabCtrl.dispose();
  58. super.dispose();
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. final status = ref.watch(overtimeStatusFilterProvider);
  63. final dateStart = ref.watch(overtimeDateStartProvider);
  64. final dateEnd = ref.watch(overtimeDateEndProvider);
  65. final otTypeFilter = ref.watch(overtimeTypeFilterProvider);
  66. final l10n = AppLocalizations.of(context);
  67. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  68. final isManager = ref.watch(isManagerProvider);
  69. // Sync TabController with external filter changes
  70. final targetIdx = _tabKeys.indexOf(status);
  71. if (targetIdx >= 0 &&
  72. _tabCtrl.index != targetIdx &&
  73. !_tabCtrl.indexIsChanging) {
  74. WidgetsBinding.instance.addPostFrameCallback((_) {
  75. if (mounted) _tabCtrl.animateTo(targetIdx);
  76. });
  77. }
  78. final filterGroups = [
  79. FilterGroup(
  80. title: l10n.get('filterDateRange'),
  81. type: FilterGroupType.dateRange,
  82. sections: [
  83. FilterSection(
  84. label: l10n.get('filterDateRange'),
  85. type: FilterSectionType.dateRange,
  86. startDate: dateStart,
  87. endDate: dateEnd,
  88. onStartChanged: (v) =>
  89. ref.read(overtimeDateStartProvider.notifier).state = v,
  90. onEndChanged: (v) =>
  91. ref.read(overtimeDateEndProvider.notifier).state = v,
  92. ),
  93. ],
  94. ),
  95. FilterGroup(
  96. title: l10n.get('other'),
  97. type: FilterGroupType.other,
  98. sections: [
  99. FilterSection(
  100. label: l10n.get('overtimeType'),
  101. type: FilterSectionType.singleSelect,
  102. options: [
  103. FilterOption(
  104. value: 'workday',
  105. label: l10n.get('workdayOvertime'),
  106. ),
  107. FilterOption(
  108. value: 'weekend',
  109. label: l10n.get('weekendOvertime'),
  110. ),
  111. FilterOption(
  112. value: 'holiday',
  113. label: l10n.get('holidayOvertime'),
  114. ),
  115. ],
  116. selectedValue: otTypeFilter,
  117. onChanged: (v) =>
  118. ref.read(overtimeTypeFilterProvider.notifier).state = v,
  119. ),
  120. ],
  121. ),
  122. ];
  123. final hasFilter = ListFilterPanel.hasActiveFilter(filterGroups);
  124. final filterVersion = Object.hash(dateStart, dateEnd, otTypeFilter);
  125. final now = DateTime.now();
  126. void onFilterReset() {
  127. ref.read(overtimeDateStartProvider.notifier).state = null;
  128. ref.read(overtimeDateEndProvider.notifier).state = null;
  129. ref.read(overtimeTypeFilterProvider.notifier).state = null;
  130. }
  131. setNavBarTitle(context, ref, NavBarConfig(
  132. title: l10n.get('overtimeList'),
  133. showBack: true,
  134. showRight: true,
  135. rightWidget: GestureDetector(
  136. onTap: () => ListFilterPanel.show(
  137. context,
  138. groups: filterGroups,
  139. onReset: onFilterReset,
  140. onConfirm: () {},
  141. defaultStartDate: DateTime(now.year, now.month, 1),
  142. defaultEndDate: DateTime(now.year, now.month, now.day),
  143. ),
  144. child: Stack(
  145. clipBehavior: Clip.none,
  146. children: [
  147. Icon(
  148. TDIcons.filter,
  149. size: 22,
  150. color: hasFilter ? colors.primary : colors.textPrimary,
  151. ),
  152. if (hasFilter)
  153. Positioned(
  154. right: -2,
  155. top: -2,
  156. child: Container(
  157. width: 6,
  158. height: 6,
  159. decoration: BoxDecoration(
  160. color: colors.danger,
  161. shape: BoxShape.circle,
  162. ),
  163. ),
  164. ),
  165. ],
  166. ),
  167. ),
  168. hasFilter: hasFilter,
  169. filterVersion: filterVersion,
  170. onBack: () => context.pop(),
  171. ));
  172. return Column(
  173. children: [
  174. if (isManager)
  175. Container(
  176. color: colors.bgCard,
  177. padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
  178. child: _buildScopeChip(colors),
  179. ),
  180. Container(
  181. color: colors.bgCard,
  182. padding: EdgeInsets.zero,
  183. child: TDSearchBar(
  184. placeHolder: l10n.get('searchOvertime'),
  185. needCancel: true,
  186. style: TDSearchStyle.round,
  187. ),
  188. ),
  189. Container(
  190. color: colors.bgCard,
  191. padding: const EdgeInsets.symmetric(horizontal: 8),
  192. child: TDTabBar(
  193. tabs: _getTabLabels(l10n).map((l) => TDTab(text: l)).toList(),
  194. controller: _tabCtrl,
  195. isScrollable: true,
  196. labelColor: colors.primary,
  197. unselectedLabelColor: colors.textSecondary,
  198. outlineType: TDTabBarOutlineType.filled,
  199. showIndicator: true,
  200. indicatorColor: colors.primary,
  201. indicatorHeight: 3,
  202. dividerHeight: 0,
  203. labelPadding: const EdgeInsets.symmetric(horizontal: 12),
  204. onTap: (index) {
  205. ref.read(overtimeStatusFilterProvider.notifier).state =
  206. _tabKeys[index];
  207. },
  208. ),
  209. ),
  210. Expanded(
  211. child: Container(
  212. color: colors.bgPage,
  213. child: TabBarView(
  214. controller: _tabCtrl,
  215. children: List.generate(_tabKeys.length, (tabIdx) {
  216. return _buildTabContent(tabIdx);
  217. }),
  218. ),
  219. ),
  220. ),
  221. ],
  222. );
  223. }
  224. Widget _buildScopeChip(AppColorsExtension colors) {
  225. final scope = ref.watch(_scopeProvider);
  226. final l10n = AppLocalizations.of(context);
  227. return Row(
  228. children: [
  229. GestureDetector(
  230. onTap: () => ref.read(_scopeProvider.notifier).state = 'my',
  231. child: Container(
  232. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
  233. decoration: BoxDecoration(
  234. color: scope == 'my' ? colors.primary : colors.bgPage,
  235. borderRadius: BorderRadius.circular(16),
  236. border: scope == 'my' ? null : Border.all(color: colors.border),
  237. ),
  238. child: Text(
  239. l10n.get('scopeMyApplications'),
  240. style: TextStyle(
  241. fontSize: 13,
  242. color: scope == 'my' ? colors.bgCard : colors.textSecondary,
  243. ),
  244. ),
  245. ),
  246. ),
  247. const SizedBox(width: 8),
  248. GestureDetector(
  249. onTap: () => ref.read(_scopeProvider.notifier).state = 'sub',
  250. child: Container(
  251. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
  252. decoration: BoxDecoration(
  253. color: scope == 'sub' ? colors.primary : colors.bgPage,
  254. borderRadius: BorderRadius.circular(16),
  255. border: scope == 'sub' ? null : Border.all(color: colors.border),
  256. ),
  257. child: Text(
  258. l10n.get('scopeSubordinates'),
  259. style: TextStyle(
  260. fontSize: 13,
  261. color: scope == 'sub' ? colors.bgCard : colors.textSecondary,
  262. ),
  263. ),
  264. ),
  265. ),
  266. ],
  267. );
  268. }
  269. Widget _buildTabContent(int tabIdx) {
  270. return _OvertimeTabContent(statusKey: _tabKeys[tabIdx]);
  271. }
  272. }
  273. class _OvertimeTabContent extends ConsumerWidget {
  274. final String statusKey;
  275. const _OvertimeTabContent({required this.statusKey});
  276. @override
  277. Widget build(BuildContext context, WidgetRef ref) {
  278. final itemsAsync = ref.watch(overtimeListProvider(statusKey));
  279. final scope = ref.watch(_scopeProvider);
  280. if (itemsAsync.isLoading && !itemsAsync.hasValue) {
  281. return const SkeletonLoadingList();
  282. }
  283. return EasyRefresh(
  284. header: TDRefreshHeader(),
  285. onRefresh: () async {
  286. ref.read(overtimeRefreshProvider.notifier).state++;
  287. },
  288. child: _buildContent(itemsAsync, context, ref, scope),
  289. );
  290. }
  291. Widget _buildContent(
  292. AsyncValue<List<OvertimeModel>> itemsAsync,
  293. BuildContext context,
  294. WidgetRef ref,
  295. String scope,
  296. ) {
  297. final l10n = AppLocalizations.of(context);
  298. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  299. final isSub = scope == 'sub';
  300. if (itemsAsync.isReloading) {
  301. final oldItems = itemsAsync.valueOrNull ?? [];
  302. if (oldItems.isEmpty) {
  303. return ListView(
  304. children: [
  305. const SizedBox(height: 120),
  306. EmptyState(message: l10n.get('noOvertimes')),
  307. ],
  308. );
  309. }
  310. return ListView.builder(
  311. padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
  312. itemCount: oldItems.length,
  313. itemBuilder: (_, i) {
  314. final desc = isSub
  315. ? '${oldItems[i].otType} · ${oldItems[i].compensationType}\n申请人: ${oldItems[i].applicantName} · ${oldItems[i].deptName}'
  316. : '${oldItems[i].otType} · ${oldItems[i].compensationType}';
  317. final card = ListCard(
  318. cardNo: oldItems[i].applicationNo,
  319. description: desc,
  320. amount:
  321. '${oldItems[i].otHours.toStringAsFixed(1)}${l10n.get('hours')}',
  322. amountColor: colors.textPrimary,
  323. date: du.DateUtils.formatDate(oldItems[i].otDate),
  324. statusTag: StatusTag.fromStatus(oldItems[i].status, l10n),
  325. onTap: () => context.push('/overtime/detail/${oldItems[i].id}'),
  326. );
  327. if (isSub && oldItems[i].status == 'pending') {
  328. return Padding(
  329. padding: const EdgeInsets.only(bottom: 16),
  330. child: _buildSwipeApprove(card, oldItems[i].id),
  331. );
  332. }
  333. return Padding(
  334. padding: const EdgeInsets.only(bottom: 16),
  335. child: card,
  336. );
  337. },
  338. );
  339. }
  340. if (itemsAsync.hasError) {
  341. return ListView(
  342. children: [
  343. const SizedBox(height: 120),
  344. EmptyState(message: l10n.get('loadFailed')),
  345. ],
  346. );
  347. }
  348. final items = itemsAsync.requireValue;
  349. if (items.isEmpty) {
  350. return ListView(
  351. children: [
  352. const SizedBox(height: 120),
  353. EmptyState(message: l10n.get('noOvertimes')),
  354. ],
  355. );
  356. }
  357. return ListView.builder(
  358. padding: const EdgeInsets.fromLTRB(16, 16, 16, 24),
  359. itemCount: items.length + 1,
  360. itemBuilder: (_, i) {
  361. if (i == items.length) return ListFooter(itemCount: items.length);
  362. final desc = isSub
  363. ? '${items[i].otType} · ${items[i].compensationType}\n申请人: ${items[i].applicantName} · ${items[i].deptName}'
  364. : '${items[i].otType} · ${items[i].compensationType}';
  365. final card = ListCard(
  366. cardNo: items[i].applicationNo,
  367. description: desc,
  368. amount: '${items[i].otHours.toStringAsFixed(1)}${l10n.get('hours')}',
  369. amountColor: colors.textPrimary,
  370. date: du.DateUtils.formatDate(items[i].otDate),
  371. statusTag: StatusTag.fromStatus(items[i].status, l10n),
  372. onTap: () => context.push('/overtime/detail/${items[i].id}'),
  373. );
  374. if (isSub && items[i].status == 'pending') {
  375. return Padding(
  376. padding: const EdgeInsets.only(bottom: 16),
  377. child: _buildSwipeApprove(card, items[i].id),
  378. );
  379. }
  380. return Padding(padding: const EdgeInsets.only(bottom: 16), child: card);
  381. },
  382. );
  383. }
  384. Widget _buildSwipeApprove(Widget card, String itemId) {
  385. return Builder(
  386. builder: (ctx) {
  387. final screenWidth = MediaQuery.of(ctx).size.width;
  388. return TDSwipeCell(
  389. groupTag: 'overtime_approve',
  390. right: TDSwipeCellPanel(
  391. extentRatio: 100 / screenWidth,
  392. children: [
  393. TDSwipeCellAction(
  394. label: '',
  395. backgroundColor: Colors.transparent,
  396. builder: (_) => Container(
  397. margin: const EdgeInsets.symmetric(
  398. horizontal: 4,
  399. vertical: 8,
  400. ),
  401. decoration: BoxDecoration(
  402. color: Colors.green,
  403. borderRadius: BorderRadius.circular(8),
  404. ),
  405. alignment: Alignment.center,
  406. padding: const EdgeInsets.symmetric(horizontal: 12),
  407. child: const Text(
  408. '一键同意',
  409. style: TextStyle(
  410. color: Colors.white,
  411. fontSize: 14,
  412. fontWeight: FontWeight.w600,
  413. ),
  414. ),
  415. ),
  416. onPressed: (_) async {
  417. final confirmed = await showDialog<bool>(
  418. context: ctx,
  419. builder: (dCtx) => TDAlertDialog(
  420. title: '确认审批',
  421. content: '确认同意该加班申请?',
  422. leftBtn: TDDialogButtonOptions(
  423. title: '取消',
  424. action: () => Navigator.of(dCtx).pop(false),
  425. ),
  426. rightBtn: TDDialogButtonOptions(
  427. title: '确认',
  428. action: () => Navigator.of(dCtx).pop(true),
  429. ),
  430. ),
  431. );
  432. if (confirmed == true) {
  433. // TODO: 接入实际审批 API
  434. if (ctx.mounted) {
  435. TDToast.showSuccess('已审批通过', context: ctx);
  436. }
  437. }
  438. },
  439. ),
  440. ],
  441. ),
  442. cell: card,
  443. );
  444. },
  445. );
  446. }
  447. }