overtime_apply_detail_dialog.dart 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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/navigation/host_app_channel.dart';
  7. import '../../../core/theme/app_colors.dart';
  8. import '../../../core/theme/app_colors_extension.dart';
  9. import '../../../shared/widgets/searchable_picker_sheet.dart';
  10. import '../../expense_apply/expense_apply_api.dart';
  11. import '../overtime_apply_api.dart';
  12. /// 加班明细输入数据。
  13. class OvertimeDetailData {
  14. final String? jbNo;
  15. final int? itm;
  16. final String salNo;
  17. final String salName;
  18. final String dep;
  19. final String jbType;
  20. final String jbDate;
  21. final String startTime;
  22. final String endTime;
  23. final double jbHours;
  24. final double jbDays;
  25. final String attPeriod;
  26. final String reason;
  27. final String compensationType;
  28. final double compensationCount;
  29. final String adr;
  30. final String rem;
  31. const OvertimeDetailData({
  32. this.jbNo,
  33. this.itm,
  34. this.salNo = '',
  35. this.salName = '',
  36. this.dep = '',
  37. this.jbType = 'WORKING_DAY',
  38. this.jbDate = '',
  39. this.startTime = '',
  40. this.endTime = '',
  41. this.jbHours = 0.0,
  42. this.jbDays = 0.0,
  43. this.attPeriod = '',
  44. this.reason = '',
  45. this.compensationType = 'OVERTIME_PAY',
  46. this.compensationCount = 0.0,
  47. this.adr = '',
  48. this.rem = '',
  49. });
  50. }
  51. /// 添加/编辑加班明细弹窗。
  52. class OvertimeApplyDetailDialog extends StatefulWidget {
  53. final OvertimeApplyApi api;
  54. final AppLocalizations l10n;
  55. final OvertimeDetailData? initialData;
  56. const OvertimeApplyDetailDialog({
  57. super.key,
  58. required this.api,
  59. required this.l10n,
  60. this.initialData,
  61. });
  62. /// 显示弹窗,返回 [OvertimeDetailData] 或 `null`(取消时)。
  63. static Future<OvertimeDetailData?> show(
  64. BuildContext context, {
  65. required OvertimeApplyApi api,
  66. required AppLocalizations l10n,
  67. OvertimeDetailData? initialData,
  68. }) {
  69. FocusScope.of(context).unfocus();
  70. return Navigator.push<OvertimeDetailData>(
  71. context,
  72. TDSlidePopupRoute<OvertimeDetailData>(
  73. slideTransitionFrom: SlideTransitionFrom.bottom,
  74. isDismissible: true,
  75. builder: (_) => OvertimeApplyDetailDialog(
  76. api: api,
  77. l10n: l10n,
  78. initialData: initialData,
  79. ),
  80. ),
  81. );
  82. }
  83. @override
  84. State<OvertimeApplyDetailDialog> createState() =>
  85. _OvertimeApplyDetailDialogState();
  86. }
  87. class _OvertimeApplyDetailDialogState extends State<OvertimeApplyDetailDialog> {
  88. // 员工
  89. EmployeeItem? _selEmployee;
  90. // 部门
  91. String _dep = '';
  92. String _depName = '';
  93. // 加班类型
  94. String _jbType = 'WORKING_DAY';
  95. // 加班日期
  96. String _jbDate = '';
  97. // 开始/结束时间
  98. String _startTime = '';
  99. String _endTime = '';
  100. // 加班时长、天数
  101. final _hoursCtrl = TextEditingController();
  102. final _hoursFocus = FocusNode();
  103. final _daysCtrl = TextEditingController();
  104. final _daysFocus = FocusNode();
  105. // 考勤周期
  106. String _attPeriod = '';
  107. // 事由
  108. final _reasonCtrl = TextEditingController();
  109. final _reasonFocus = FocusNode();
  110. // 补偿类型
  111. String _compensationType = 'OVERTIME_PAY';
  112. // 折算补偿次数
  113. final _compCountCtrl = TextEditingController();
  114. final _compCountFocus = FocusNode();
  115. // 地点
  116. final _adrCtrl = TextEditingController();
  117. final _adrFocus = FocusNode();
  118. // 备注
  119. final _remarkCtrl = TextEditingController();
  120. final _remarkFocus = FocusNode();
  121. // 滚动
  122. final _scrollCtrl = ScrollController();
  123. // 加班类型选项
  124. static const _jbTypeOptions = [
  125. 'WORKING_DAY',
  126. 'REST_DAY',
  127. 'PUBLIC_HOLIDAY',
  128. 'SPECIAL_HOLIDAY',
  129. 'OTHER',
  130. ];
  131. // 补偿类型选项
  132. static const _compensationTypeOptions = [
  133. 'OVERTIME_PAY',
  134. 'COMPENSATORY_LEAVE',
  135. 'NO_COMPENSATION',
  136. 'OTHER',
  137. ];
  138. AppLocalizations get _l10n => widget.l10n;
  139. bool get _isEdit => widget.initialData != null;
  140. @override
  141. void initState() {
  142. super.initState();
  143. final d = widget.initialData;
  144. _selEmployee = (d != null && d.salNo.isNotEmpty)
  145. ? EmployeeItem(salNo: d.salNo, name: d.salName)
  146. : null;
  147. _dep = d?.dep ?? '';
  148. _depName = d?.dep ?? '';
  149. _jbType = d?.jbType ?? 'WORKING_DAY';
  150. _jbDate = d?.jbDate ?? '';
  151. if (!_isEdit && _jbDate.isEmpty) {
  152. final now = DateTime.now();
  153. _jbDate = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
  154. _attPeriod = _jbDate.substring(0, 7);
  155. }
  156. _startTime = d?.startTime ?? '';
  157. _endTime = d?.endTime ?? '';
  158. if (d != null) {
  159. _hoursCtrl.text = d.jbHours > 0 ? d.jbHours.toString() : '';
  160. _daysCtrl.text = d.jbDays > 0 ? d.jbDays.toString() : '';
  161. }
  162. _attPeriod = d?.attPeriod ?? '';
  163. if (d != null) {
  164. _reasonCtrl.text = d.reason;
  165. }
  166. _compensationType = d?.compensationType ?? 'OVERTIME_PAY';
  167. if (d != null) {
  168. _compCountCtrl.text = d.compensationCount > 0
  169. ? d.compensationCount.toString()
  170. : '';
  171. }
  172. if (d != null) {
  173. _adrCtrl.text = d.adr;
  174. _remarkCtrl.text = d.rem;
  175. }
  176. _reasonFocus.addListener(() => _ensureVisible(_reasonFocus));
  177. _hoursFocus.addListener(() => _ensureVisible(_hoursFocus));
  178. _daysFocus.addListener(() => _ensureVisible(_daysFocus));
  179. _compCountFocus.addListener(() => _ensureVisible(_compCountFocus));
  180. _adrFocus.addListener(() => _ensureVisible(_adrFocus));
  181. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  182. // 非编辑模式自动带出当前用户
  183. if (!_isEdit) {
  184. WidgetsBinding.instance.addPostFrameCallback((_) => _doAutoFill());
  185. }
  186. }
  187. void _ensureVisible(FocusNode node) {
  188. if (!node.hasFocus) return;
  189. _doEnsureVisible(node, 0, -1);
  190. }
  191. void _doEnsureVisible(FocusNode node, int attempt, double lastInsets) {
  192. if (attempt >= 15) return;
  193. WidgetsBinding.instance.addPostFrameCallback((_) {
  194. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  195. final insets = MediaQuery.of(context).viewInsets.bottom;
  196. if (insets != lastInsets) {
  197. _doEnsureVisible(node, attempt + 1, insets);
  198. return;
  199. }
  200. final ctx = node.context;
  201. if (ctx == null) return;
  202. Future.delayed(const Duration(milliseconds: 500), () {
  203. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  204. Scrollable.ensureVisible(
  205. ctx,
  206. alignment: 0.5,
  207. duration: const Duration(milliseconds: 300),
  208. );
  209. });
  210. });
  211. }
  212. Future<void> _doAutoFill() async {
  213. await HostAppChannel.ensureConfig();
  214. final usr = HostAppChannel.usr;
  215. if (usr.isEmpty) return;
  216. try {
  217. final employees = await widget.api.getEmployees(salNo: usr);
  218. if (employees.isNotEmpty && mounted) {
  219. setState(() {
  220. _selEmployee = employees.first;
  221. });
  222. // 自动带出部门
  223. final depCode = HostAppChannel.dep;
  224. if (depCode.isNotEmpty) {
  225. final depts = await widget.api.getDepartments();
  226. final matched = depts.where((d) => d.dep == depCode);
  227. if (matched.isNotEmpty && mounted) {
  228. setState(() {
  229. _dep = matched.first.dep;
  230. _depName = matched.first.name;
  231. });
  232. }
  233. }
  234. }
  235. } catch (_) {
  236. // 静默失败
  237. }
  238. }
  239. @override
  240. void dispose() {
  241. _hoursCtrl.dispose();
  242. _hoursFocus.dispose();
  243. _daysCtrl.dispose();
  244. _daysFocus.dispose();
  245. _reasonCtrl.dispose();
  246. _reasonFocus.dispose();
  247. _compCountCtrl.dispose();
  248. _compCountFocus.dispose();
  249. _adrCtrl.dispose();
  250. _adrFocus.dispose();
  251. _remarkCtrl.dispose();
  252. _remarkFocus.dispose();
  253. _scrollCtrl.dispose();
  254. super.dispose();
  255. }
  256. void _confirm() {
  257. if (_selEmployee == null) {
  258. TDToast.showText(
  259. '${_l10n.get('pleaseSelect')}${_l10n.get('employee')}',
  260. context: context,
  261. );
  262. return;
  263. }
  264. if (_jbDate.isEmpty) {
  265. TDToast.showText(
  266. '${_l10n.get('pleaseSelect')}${_l10n.get('date')}',
  267. context: context,
  268. );
  269. return;
  270. }
  271. if (_startTime.isEmpty) {
  272. TDToast.showText(
  273. '${_l10n.get('pleaseSelect')}${_l10n.get('startTime')}',
  274. context: context,
  275. );
  276. return;
  277. }
  278. if (_endTime.isEmpty) {
  279. TDToast.showText(
  280. '${_l10n.get('pleaseSelect')}${_l10n.get('endTime')}',
  281. context: context,
  282. );
  283. return;
  284. }
  285. if (_startTime.compareTo(_endTime) >= 0) {
  286. TDToast.showText(_l10n.get('endTimeMustLater'), context: context);
  287. return;
  288. }
  289. final hours = double.tryParse(_hoursCtrl.text) ?? 0;
  290. if (hours <= 0) {
  291. TDToast.showText(_l10n.get('overtimeHoursPositive'), context: context);
  292. return;
  293. }
  294. Navigator.pop(
  295. context,
  296. OvertimeDetailData(
  297. jbNo: widget.initialData?.jbNo,
  298. itm: widget.initialData?.itm,
  299. salNo: _selEmployee!.salNo,
  300. salName: _selEmployee!.name,
  301. dep: _dep,
  302. jbType: _jbType,
  303. jbDate: _jbDate,
  304. startTime: _startTime,
  305. endTime: _endTime,
  306. jbHours: hours,
  307. jbDays: double.tryParse(_daysCtrl.text) ?? 0,
  308. attPeriod: _attPeriod,
  309. reason: _reasonCtrl.text.trim(),
  310. compensationType: _compensationType,
  311. compensationCount: double.tryParse(_compCountCtrl.text) ?? 0,
  312. adr: _adrCtrl.text,
  313. rem: _remarkCtrl.text,
  314. ),
  315. );
  316. }
  317. @override
  318. Widget build(BuildContext context) {
  319. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  320. return AnimatedPadding(
  321. padding: EdgeInsets.only(
  322. bottom: MediaQuery.of(context).viewInsets.bottom,
  323. ),
  324. duration: const Duration(milliseconds: 200),
  325. child: SafeArea(
  326. child: ConstrainedBox(
  327. constraints: BoxConstraints(
  328. maxHeight: MediaQuery.of(context).size.height * 0.85,
  329. ),
  330. child: Container(
  331. decoration: BoxDecoration(
  332. color: colors.bgPage,
  333. borderRadius: const BorderRadius.vertical(
  334. top: Radius.circular(16),
  335. ),
  336. ),
  337. child: Column(
  338. mainAxisSize: MainAxisSize.min,
  339. crossAxisAlignment: CrossAxisAlignment.stretch,
  340. children: [
  341. _buildHeader(colors),
  342. Flexible(
  343. child: GestureDetector(
  344. onTap: () => FocusScope.of(context).unfocus(),
  345. behavior: HitTestBehavior.translucent,
  346. child: SingleChildScrollView(
  347. controller: _scrollCtrl,
  348. keyboardDismissBehavior:
  349. ScrollViewKeyboardDismissBehavior.manual,
  350. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  351. child: Column(
  352. mainAxisSize: MainAxisSize.min,
  353. crossAxisAlignment: CrossAxisAlignment.stretch,
  354. children: [
  355. _buildEmployeePicker(colors),
  356. const SizedBox(height: 12),
  357. _buildDeptPicker(colors),
  358. const SizedBox(height: 12),
  359. _buildJbTypeSelector(colors),
  360. const SizedBox(height: 12),
  361. _buildJbDatePicker(colors),
  362. const SizedBox(height: 12),
  363. _buildStartTimePicker(colors),
  364. const SizedBox(height: 12),
  365. _buildEndTimePicker(colors),
  366. const SizedBox(height: 12),
  367. _buildHoursInput(colors),
  368. const SizedBox(height: 12),
  369. _buildDaysInput(colors),
  370. const SizedBox(height: 12),
  371. _buildAttPeriodInput(colors),
  372. const SizedBox(height: 12),
  373. _buildReasonInput(colors),
  374. const SizedBox(height: 12),
  375. _buildCompensationTypeSelector(colors),
  376. const SizedBox(height: 12),
  377. _buildCompCountInput(colors),
  378. const SizedBox(height: 12),
  379. _buildAdrInput(colors),
  380. const SizedBox(height: 12),
  381. _buildRemarkInput(colors),
  382. ],
  383. ),
  384. ),
  385. ),
  386. ),
  387. Container(
  388. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  389. decoration: BoxDecoration(
  390. color: colors.bgCard,
  391. border: Border(
  392. top: BorderSide(color: colors.border, width: 0.5),
  393. ),
  394. ),
  395. child: _buildActions(),
  396. ),
  397. ],
  398. ),
  399. ),
  400. ),
  401. ),
  402. );
  403. }
  404. // ── 标题栏 ──
  405. Widget _buildHeader(AppColorsExtension colors) {
  406. return Column(
  407. mainAxisSize: MainAxisSize.min,
  408. children: [
  409. Center(
  410. child: Container(
  411. margin: const EdgeInsets.only(top: 8, bottom: 4),
  412. width: 36,
  413. height: 4,
  414. decoration: BoxDecoration(
  415. color: colors.border,
  416. borderRadius: BorderRadius.circular(2),
  417. ),
  418. ),
  419. ),
  420. Padding(
  421. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  422. child: Row(
  423. children: [
  424. const SizedBox(width: 24),
  425. Expanded(
  426. child: Center(
  427. child: Text(
  428. _l10n.get('addDetail'),
  429. style: TextStyle(
  430. fontSize: AppFontSizes.title,
  431. fontWeight: FontWeight.w600,
  432. color: colors.textPrimary,
  433. ),
  434. ),
  435. ),
  436. ),
  437. GestureDetector(
  438. onTap: () => Navigator.pop(context),
  439. child: Padding(
  440. padding: const EdgeInsets.all(4),
  441. child: Icon(
  442. Icons.close,
  443. size: 20,
  444. color: colors.textSecondary,
  445. ),
  446. ),
  447. ),
  448. ],
  449. ),
  450. ),
  451. ],
  452. );
  453. }
  454. // ── 通用 Picker 卡片 ──
  455. Widget _pickerCard({
  456. required String label,
  457. required bool required,
  458. required String currentLabel,
  459. required bool hasValue,
  460. required VoidCallback onTap,
  461. VoidCallback? onClear,
  462. }) {
  463. final tdTheme = TDTheme.of(context);
  464. return GestureDetector(
  465. onTap: () {
  466. FocusManager.instance.primaryFocus?.unfocus();
  467. onTap();
  468. },
  469. child: Container(
  470. padding: const EdgeInsets.only(
  471. left: 16,
  472. right: 10,
  473. top: 12,
  474. bottom: 12,
  475. ),
  476. decoration: BoxDecoration(
  477. color: tdTheme.bgColorContainer,
  478. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  479. border: Border.all(color: tdTheme.componentStrokeColor),
  480. ),
  481. child: Row(
  482. children: [
  483. TDText(
  484. label,
  485. maxLines: 1,
  486. overflow: TextOverflow.visible,
  487. font: tdTheme.fontBodyLarge,
  488. fontWeight: FontWeight.w400,
  489. style: const TextStyle(letterSpacing: 0),
  490. ),
  491. if (required)
  492. Padding(
  493. padding: const EdgeInsets.only(left: 4),
  494. child: TDText(
  495. '*',
  496. font: tdTheme.fontBodyLarge,
  497. fontWeight: FontWeight.w400,
  498. style: TextStyle(color: tdTheme.errorColor6),
  499. ),
  500. ),
  501. const SizedBox(width: 12),
  502. Expanded(
  503. child: Row(
  504. mainAxisAlignment: MainAxisAlignment.end,
  505. mainAxisSize: MainAxisSize.max,
  506. children: [
  507. Flexible(
  508. child: TDText(
  509. currentLabel,
  510. maxLines: 1,
  511. overflow: TextOverflow.ellipsis,
  512. font: tdTheme.fontBodyLarge,
  513. fontWeight: FontWeight.w400,
  514. textColor: hasValue
  515. ? tdTheme.textColorPrimary
  516. : tdTheme.textColorPlaceholder,
  517. textAlign: TextAlign.end,
  518. ),
  519. ),
  520. const SizedBox(width: 4),
  521. SizedBox(
  522. width: 18,
  523. height: 18,
  524. child: onClear != null
  525. ? GestureDetector(
  526. onTap: onClear,
  527. child: Icon(
  528. Icons.close,
  529. size: 18,
  530. color: tdTheme.textColorPlaceholder,
  531. ),
  532. )
  533. : Icon(
  534. Icons.chevron_right,
  535. size: 18,
  536. color: tdTheme.textColorPlaceholder,
  537. ),
  538. ),
  539. ],
  540. ),
  541. ),
  542. ],
  543. ),
  544. ),
  545. );
  546. }
  547. // ── 0. 员工 ──
  548. Widget _buildEmployeePicker(AppColorsExtension colors) {
  549. return _pickerCard(
  550. label: _l10n.get('employee'),
  551. required: true,
  552. hasValue: _selEmployee != null,
  553. currentLabel: _selEmployee != null
  554. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  555. : _l10n.get('pleaseSelect'),
  556. onTap: () async {
  557. final result = await showSearchablePicker<EmployeeItem>(
  558. context,
  559. title: '${_l10n.get('select')}${_l10n.get('employee')}',
  560. searchHint: _l10n.get('search'),
  561. loader: (keyword, page) =>
  562. widget.api.getEmployees(keyword: keyword, page: page, size: 20),
  563. labelBuilder: (e) =>
  564. e.name.isEmpty ? e.salNo : '${e.salNo} ${e.name}',
  565. onRefresh: () => widget.api.clearRefCache(),
  566. );
  567. if (result != null && mounted) {
  568. setState(() => _selEmployee = result);
  569. }
  570. },
  571. onClear: _selEmployee != null
  572. ? () => setState(() => _selEmployee = null)
  573. : null,
  574. );
  575. }
  576. // ── 1. 部门 ──
  577. Widget _buildDeptPicker(AppColorsExtension colors) {
  578. return _pickerCard(
  579. label: _l10n.get('dep'),
  580. required: false,
  581. hasValue: _dep.isNotEmpty,
  582. currentLabel: _dep.isNotEmpty
  583. ? (_depName.isNotEmpty ? '$_dep/$_depName' : _dep)
  584. : _l10n.get('pleaseSelect'),
  585. onTap: () async {
  586. final result = await showSearchablePicker<DepartmentItem>(
  587. context,
  588. title: '${_l10n.get('select')}${_l10n.get('dep')}',
  589. searchHint: _l10n.get('search'),
  590. loader: (keyword, page) =>
  591. widget.api.getDepartments(keyword: keyword, page: page, size: 20),
  592. labelBuilder: (d) => d.name.isEmpty ? d.dep : '${d.dep} ${d.name}',
  593. onRefresh: () => widget.api.clearRefCache(),
  594. );
  595. if (result != null && mounted) {
  596. setState(() {
  597. _dep = result.dep;
  598. _depName = result.name;
  599. });
  600. }
  601. },
  602. onClear: _dep.isNotEmpty
  603. ? () => setState(() {
  604. _dep = '';
  605. _depName = '';
  606. })
  607. : null,
  608. );
  609. }
  610. // ── 2. 加班类型 ──
  611. Widget _buildJbTypeSelector(AppColorsExtension colors) {
  612. final selLabel = _jbTypeLabel(_jbType);
  613. return _pickerCard(
  614. label: _l10n.get('jbType'),
  615. required: true,
  616. hasValue: true,
  617. currentLabel: selLabel,
  618. onTap: () => _showJbTypePicker(),
  619. );
  620. }
  621. void _showJbTypePicker() {
  622. FocusManager.instance.primaryFocus?.unfocus();
  623. final labels = _jbTypeOptions.map((t) => _jbTypeLabel(t)).toList();
  624. TDPicker.showMultiPicker(
  625. context,
  626. title: _l10n.get('selectJbType'),
  627. data: [labels],
  628. onConfirm: (selected) {
  629. if (selected.isNotEmpty) {
  630. final idx = selected.first is int ? selected.first as int : 0;
  631. if (idx >= 0 && idx < _jbTypeOptions.length) {
  632. setState(() => _jbType = _jbTypeOptions[idx]);
  633. }
  634. }
  635. Navigator.of(context).pop();
  636. },
  637. );
  638. }
  639. String _jbTypeLabel(String type) {
  640. switch (type) {
  641. case 'WORKING_DAY':
  642. return _l10n.get('workingDay');
  643. case 'REST_DAY':
  644. return _l10n.get('restDay');
  645. case 'PUBLIC_HOLIDAY':
  646. return _l10n.get('publicHoliday');
  647. case 'SPECIAL_HOLIDAY':
  648. return _l10n.get('specialHoliday');
  649. case 'OTHER':
  650. return _l10n.get('other');
  651. default:
  652. return type;
  653. }
  654. }
  655. // ── 4. 申请日期 ──
  656. Widget _buildJbDatePicker(AppColorsExtension colors) {
  657. return _datePickerCard(
  658. label: _l10n.get('applyDate'),
  659. value: _jbDate,
  660. hint: _l10n.get('pleaseSelect'),
  661. colors: colors,
  662. required: true,
  663. onPick: (d) => setState(() => _jbDate = d),
  664. onClear: _jbDate.isNotEmpty ? () => setState(() => _jbDate = '') : null,
  665. );
  666. }
  667. // ── 4. 开始时间 ──
  668. Widget _buildStartTimePicker(AppColorsExtension colors) {
  669. return _datePickerCard(
  670. label: _l10n.get('startTime'),
  671. value: _startTime,
  672. hint: _l10n.get('pleaseSelect'),
  673. colors: colors,
  674. required: true,
  675. onPick: (d) {
  676. setState(() {
  677. _startTime = d;
  678. if (d.length >= 7) _attPeriod = d.substring(0, 7);
  679. _autoCalcHours();
  680. });
  681. },
  682. onClear: _startTime.isNotEmpty
  683. ? () {
  684. setState(() {
  685. _startTime = '';
  686. _autoCalcHours();
  687. });
  688. }
  689. : null,
  690. );
  691. }
  692. // ── 5. 结束时间 ──
  693. Widget _buildEndTimePicker(AppColorsExtension colors) {
  694. return _datePickerCard(
  695. label: _l10n.get('endTime'),
  696. value: _endTime,
  697. hint: _l10n.get('pleaseSelect'),
  698. colors: colors,
  699. required: true,
  700. onPick: (d) {
  701. setState(() {
  702. _endTime = d;
  703. _autoCalcHours();
  704. });
  705. },
  706. onClear: _endTime.isNotEmpty
  707. ? () {
  708. setState(() {
  709. _endTime = '';
  710. _autoCalcHours();
  711. });
  712. }
  713. : null,
  714. );
  715. }
  716. void _autoCalcHours() {
  717. if (_startTime.isEmpty || _endTime.isEmpty) return;
  718. try {
  719. final stParts = _startTime.split(' ');
  720. final etParts = _endTime.split(' ');
  721. if (stParts.length < 2 || etParts.length < 2) return;
  722. final stTime = stParts[1];
  723. final etTime = etParts[1];
  724. final st = _parseTime(stTime);
  725. final et = _parseTime(etTime);
  726. if (st == null || et == null) return;
  727. double hours = et - st;
  728. if (hours < 0) hours += 24.0;
  729. if (hours > 0) {
  730. // 按0.5半小时模式向上取整
  731. final rounded = (hours * 2).ceil() / 2.0;
  732. _hoursCtrl.text = rounded.toStringAsFixed(1);
  733. }
  734. _autoCalcDays();
  735. } catch (_) {}
  736. }
  737. void _autoCalcDays() {
  738. if (_startTime.isEmpty || _endTime.isEmpty) return;
  739. try {
  740. final stDateStr = _startTime.split(' ').first;
  741. final etDateStr = _endTime.split(' ').first;
  742. final stDate = DateTime.tryParse(stDateStr);
  743. final etDate = DateTime.tryParse(etDateStr);
  744. if (stDate == null || etDate == null) return;
  745. final diffDays = etDate.difference(stDate).inDays;
  746. if (diffDays < 0) return;
  747. // 按0.5半天模式向上取整
  748. final ceilDays = (diffDays * 2).ceil() / 2.0;
  749. _daysCtrl.text = ceilDays.toStringAsFixed(1);
  750. } catch (_) {}
  751. }
  752. double? _parseTime(String time) {
  753. final parts = time.split(':');
  754. if (parts.length < 2) return null;
  755. final h = double.tryParse(parts[0]);
  756. final m = double.tryParse(parts[1]);
  757. if (h == null || m == null) return null;
  758. return h + m / 60.0;
  759. }
  760. // ── 6. 加班时长 ──
  761. Widget _buildHoursInput(AppColorsExtension colors) {
  762. return _inputCard(
  763. label: _l10n.get('overtimeHours'),
  764. required: true,
  765. controller: _hoursCtrl,
  766. hintText: '>0',
  767. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  768. inputFormatters: [
  769. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,1}$')),
  770. ],
  771. focusNode: _hoursFocus,
  772. );
  773. }
  774. // ── 7. 加班天数 ──
  775. Widget _buildDaysInput(AppColorsExtension colors) {
  776. return _inputCard(
  777. label: _l10n.get('overtimeDays'),
  778. required: false,
  779. controller: _daysCtrl,
  780. hintText: '0',
  781. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  782. inputFormatters: [
  783. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  784. ],
  785. focusNode: _daysFocus,
  786. );
  787. }
  788. // ── 8. 考勤周期 ──
  789. Widget _buildAttPeriodInput(AppColorsExtension colors) {
  790. return _datePickerCard(
  791. label: _l10n.get('attPeriod'),
  792. value: _attPeriod,
  793. hint: _l10n.get('autoFromDate'),
  794. colors: colors,
  795. onPick: (d) => setState(() => _attPeriod = d),
  796. onClear: _attPeriod.isNotEmpty
  797. ? () => setState(() => _attPeriod = '')
  798. : null,
  799. );
  800. }
  801. // ── 10. 事由 ──
  802. Widget _buildReasonInput(AppColorsExtension colors) {
  803. final tdTheme = TDTheme.of(context);
  804. return TDTextarea(
  805. controller: _reasonCtrl,
  806. focusNode: _reasonFocus,
  807. label: _l10n.get('overtimeReason'),
  808. hintText: _l10n.get('enterReason'),
  809. maxLines: 3,
  810. minLines: 1,
  811. maxLength: 1000,
  812. indicator: true,
  813. decoration: BoxDecoration(
  814. color: tdTheme.bgColorContainer,
  815. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  816. border: Border.all(color: tdTheme.componentStrokeColor),
  817. ),
  818. onChanged: (_) => setState(() {}),
  819. );
  820. }
  821. // ── 10. 补偿类型 ──
  822. Widget _buildCompensationTypeSelector(AppColorsExtension colors) {
  823. final selLabel = _compensationTypeLabel(_compensationType);
  824. return _pickerCard(
  825. label: _l10n.get('compensationType'),
  826. required: true,
  827. hasValue: true,
  828. currentLabel: selLabel,
  829. onTap: () => _showCompensationTypePicker(),
  830. );
  831. }
  832. void _showCompensationTypePicker() {
  833. FocusManager.instance.primaryFocus?.unfocus();
  834. final labels = _compensationTypeOptions
  835. .map((t) => _compensationTypeLabel(t))
  836. .toList();
  837. TDPicker.showMultiPicker(
  838. context,
  839. title: _l10n.get('selectCompensationType'),
  840. data: [labels],
  841. onConfirm: (selected) {
  842. if (selected.isNotEmpty) {
  843. final idx = selected.first is int ? selected.first as int : 0;
  844. if (idx >= 0 && idx < _compensationTypeOptions.length) {
  845. setState(() => _compensationType = _compensationTypeOptions[idx]);
  846. }
  847. }
  848. Navigator.of(context).pop();
  849. },
  850. );
  851. }
  852. String _compensationTypeLabel(String type) {
  853. switch (type) {
  854. case 'OVERTIME_PAY':
  855. return _l10n.get('overtimePay');
  856. case 'COMPENSATORY_LEAVE':
  857. return _l10n.get('compensatoryLeave');
  858. case 'NO_COMPENSATION':
  859. return _l10n.get('noCompensation');
  860. case 'OTHER':
  861. return _l10n.get('other');
  862. default:
  863. return type;
  864. }
  865. }
  866. // ── 11. 折算补偿次数 ──
  867. Widget _buildCompCountInput(AppColorsExtension colors) {
  868. return _inputCard(
  869. label: _l10n.get('compensationCount'),
  870. required: false,
  871. controller: _compCountCtrl,
  872. hintText: '0',
  873. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  874. inputFormatters: [
  875. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,1}$')),
  876. ],
  877. focusNode: _compCountFocus,
  878. );
  879. }
  880. // ── 12. 地点 ──
  881. Widget _buildAdrInput(AppColorsExtension colors) {
  882. return _inputCard(
  883. label: _l10n.get('adr'),
  884. required: false,
  885. controller: _adrCtrl,
  886. hintText: _l10n.get('enterAdr'),
  887. focusNode: _adrFocus,
  888. );
  889. }
  890. // ── 13. 备注 ──
  891. Widget _buildRemarkInput(AppColorsExtension colors) {
  892. final tdTheme = TDTheme.of(context);
  893. return TDTextarea(
  894. controller: _remarkCtrl,
  895. focusNode: _remarkFocus,
  896. label: _l10n.get('remark'),
  897. hintText: _l10n.get('enterRemark'),
  898. maxLines: 3,
  899. minLines: 1,
  900. maxLength: 1000,
  901. indicator: true,
  902. decoration: BoxDecoration(
  903. color: tdTheme.bgColorContainer,
  904. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  905. border: Border.all(color: tdTheme.componentStrokeColor),
  906. ),
  907. onChanged: (_) => setState(() {}),
  908. );
  909. }
  910. // ── 操作按钮 ──
  911. Widget _buildActions() {
  912. return Row(
  913. children: [
  914. Expanded(
  915. child: TDButton(
  916. text: _l10n.get('cancel'),
  917. size: TDButtonSize.large,
  918. type: TDButtonType.outline,
  919. shape: TDButtonShape.rectangle,
  920. theme: TDButtonTheme.defaultTheme,
  921. onTap: () => Navigator.pop(context),
  922. ),
  923. ),
  924. const SizedBox(width: 12),
  925. Expanded(
  926. child: TDButton(
  927. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  928. size: TDButtonSize.large,
  929. type: TDButtonType.fill,
  930. shape: TDButtonShape.rectangle,
  931. theme: TDButtonTheme.primary,
  932. onTap: _confirm,
  933. ),
  934. ),
  935. ],
  936. );
  937. }
  938. // ── 日期选择器卡片 ──
  939. Widget _datePickerCard({
  940. required String label,
  941. required String value,
  942. required String hint,
  943. required AppColorsExtension colors,
  944. required ValueChanged<String> onPick,
  945. VoidCallback? onClear,
  946. bool required = false,
  947. }) {
  948. final tdTheme = TDTheme.of(context);
  949. final hasValue = value.isNotEmpty;
  950. return GestureDetector(
  951. onTap: () => _pickDateTime(label, onPick),
  952. child: Container(
  953. padding: const EdgeInsets.only(
  954. left: 16,
  955. right: 10,
  956. top: 12,
  957. bottom: 12,
  958. ),
  959. decoration: BoxDecoration(
  960. color: tdTheme.bgColorContainer,
  961. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  962. border: Border.all(color: tdTheme.componentStrokeColor),
  963. ),
  964. child: Row(
  965. children: [
  966. if (required)
  967. Padding(
  968. padding: const EdgeInsets.only(right: 2),
  969. child: TDText(
  970. '*',
  971. font: tdTheme.fontBodyLarge,
  972. fontWeight: FontWeight.w400,
  973. style: TextStyle(color: tdTheme.errorColor6),
  974. ),
  975. ),
  976. TDText(
  977. label,
  978. maxLines: 1,
  979. overflow: TextOverflow.visible,
  980. font: tdTheme.fontBodyLarge,
  981. fontWeight: FontWeight.w400,
  982. style: const TextStyle(letterSpacing: 0),
  983. ),
  984. const SizedBox(width: 12),
  985. Expanded(
  986. child: Row(
  987. mainAxisAlignment: MainAxisAlignment.end,
  988. mainAxisSize: MainAxisSize.max,
  989. children: [
  990. Flexible(
  991. child: TDText(
  992. value.isEmpty ? hint : value,
  993. maxLines: 1,
  994. overflow: TextOverflow.ellipsis,
  995. font: tdTheme.fontBodyLarge,
  996. fontWeight: FontWeight.w400,
  997. textColor: value.isEmpty
  998. ? tdTheme.textColorPlaceholder
  999. : tdTheme.textColorPrimary,
  1000. textAlign: TextAlign.end,
  1001. ),
  1002. ),
  1003. const SizedBox(width: 4),
  1004. SizedBox(
  1005. width: 18,
  1006. height: 18,
  1007. child: hasValue
  1008. ? GestureDetector(
  1009. onTap: onClear,
  1010. child: Icon(
  1011. Icons.close,
  1012. size: 18,
  1013. color: tdTheme.textColorPlaceholder,
  1014. ),
  1015. )
  1016. : Icon(
  1017. Icons.chevron_right,
  1018. size: 18,
  1019. color: tdTheme.textColorPlaceholder,
  1020. ),
  1021. ),
  1022. ],
  1023. ),
  1024. ),
  1025. ],
  1026. ),
  1027. ),
  1028. );
  1029. }
  1030. void _pickDateTime(String label, ValueChanged<String> onPick) {
  1031. final l10n = _l10n;
  1032. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1033. final now = DateTime.now();
  1034. final isDateOnly =
  1035. label == _l10n.get('applyDate') || label == _l10n.get('attPeriod');
  1036. final isAttPeriod = label == _l10n.get('attPeriod');
  1037. FocusManager.instance.primaryFocus?.unfocus();
  1038. TDPicker.showDatePicker(
  1039. context,
  1040. title: l10n.get('selectDate'),
  1041. backgroundColor: colors.bgCard,
  1042. useYear: true,
  1043. useMonth: true,
  1044. useDay: !isAttPeriod,
  1045. useHour: !isDateOnly,
  1046. useMinute: !isDateOnly,
  1047. useSecond: false,
  1048. useWeekDay: false,
  1049. dateStart: const [2020, 1, 1],
  1050. dateEnd: [now.year + 1, 12, 31],
  1051. initialDate: [now.year, now.month, now.day],
  1052. onConfirm: (selected) {
  1053. final year = selected['year'];
  1054. final month = selected['month'].toString().padLeft(2, '0');
  1055. final day = selected['day'].toString().padLeft(2, '0');
  1056. final hour = selected['hour']?.toString().padLeft(2, '0') ?? '00';
  1057. final minute = selected['minute']?.toString().padLeft(2, '0') ?? '00';
  1058. if (isAttPeriod) {
  1059. onPick('$year-$month');
  1060. } else if (isDateOnly) {
  1061. onPick('$year-$month-$day');
  1062. } else {
  1063. onPick('$year-$month-$day $hour:$minute');
  1064. }
  1065. Navigator.of(context).pop();
  1066. },
  1067. );
  1068. }
  1069. // ── 通用输入卡片 ──
  1070. Widget _inputCard({
  1071. required String label,
  1072. required bool required,
  1073. required TextEditingController controller,
  1074. required String hintText,
  1075. TextInputType? keyboardType,
  1076. List<TextInputFormatter>? inputFormatters,
  1077. FocusNode? focusNode,
  1078. }) {
  1079. final tdTheme = TDTheme.of(context);
  1080. final hasValue = controller.text.isNotEmpty;
  1081. return Container(
  1082. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  1083. decoration: BoxDecoration(
  1084. color: tdTheme.bgColorContainer,
  1085. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1086. border: Border.all(color: tdTheme.componentStrokeColor),
  1087. ),
  1088. child: Row(
  1089. children: [
  1090. TDText(
  1091. label,
  1092. maxLines: 1,
  1093. overflow: TextOverflow.visible,
  1094. font: tdTheme.fontBodyLarge,
  1095. fontWeight: FontWeight.w400,
  1096. style: const TextStyle(letterSpacing: 0),
  1097. ),
  1098. if (required)
  1099. Padding(
  1100. padding: const EdgeInsets.only(left: 4),
  1101. child: TDText(
  1102. '*',
  1103. font: tdTheme.fontBodyLarge,
  1104. fontWeight: FontWeight.w400,
  1105. style: TextStyle(color: tdTheme.errorColor6),
  1106. ),
  1107. ),
  1108. const SizedBox(width: 12),
  1109. Expanded(
  1110. child: Row(
  1111. mainAxisAlignment: MainAxisAlignment.end,
  1112. mainAxisSize: MainAxisSize.max,
  1113. children: [
  1114. Flexible(
  1115. child: TextField(
  1116. controller: controller,
  1117. focusNode: focusNode,
  1118. textAlign: TextAlign.end,
  1119. keyboardType: keyboardType,
  1120. inputFormatters: inputFormatters,
  1121. style: TextStyle(
  1122. fontSize: 16,
  1123. color: tdTheme.textColorPrimary,
  1124. ),
  1125. decoration: InputDecoration(
  1126. hintText: hintText,
  1127. hintStyle: TextStyle(
  1128. fontSize: 16,
  1129. color: tdTheme.textColorPlaceholder,
  1130. ),
  1131. border: InputBorder.none,
  1132. isDense: true,
  1133. contentPadding: EdgeInsets.zero,
  1134. ),
  1135. onChanged: (_) => setState(() {}),
  1136. ),
  1137. ),
  1138. const SizedBox(width: 4),
  1139. SizedBox(
  1140. width: 18,
  1141. height: 18,
  1142. child: hasValue
  1143. ? GestureDetector(
  1144. onTap: () {
  1145. controller.clear();
  1146. setState(() {});
  1147. },
  1148. child: Icon(
  1149. Icons.close,
  1150. size: 18,
  1151. color: tdTheme.textColorPlaceholder,
  1152. ),
  1153. )
  1154. : null,
  1155. ),
  1156. ],
  1157. ),
  1158. ),
  1159. ],
  1160. ),
  1161. );
  1162. }
  1163. }