overtime_list_page.dart 14 KB

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