overtime_apply_detail_dialog.dart 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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 '../../../shared/widgets/location_picker.dart';
  11. import '../../expense_apply/expense_apply_api.dart';
  12. import '../overtime_apply_api.dart';
  13. /// 加班明细输入数据。
  14. class OvertimeDetailData {
  15. final String? jbNo;
  16. final int? itm;
  17. final String salNo;
  18. final String salName;
  19. final String dep;
  20. final String depName;
  21. final String jbType;
  22. final String jbDate;
  23. final String startTime;
  24. final String endTime;
  25. final double jbHours;
  26. final double jbDays;
  27. final String attPeriod;
  28. final String reason;
  29. final String compensationType;
  30. final double compensationCount;
  31. final String adr;
  32. final String rem;
  33. const OvertimeDetailData({
  34. this.jbNo,
  35. this.itm,
  36. this.salNo = '',
  37. this.salName = '',
  38. this.dep = '',
  39. this.depName = '',
  40. this.jbType = 'WORKING_DAY',
  41. this.jbDate = '',
  42. this.startTime = '',
  43. this.endTime = '',
  44. this.jbHours = 0.0,
  45. this.jbDays = 0.0,
  46. this.attPeriod = '',
  47. this.reason = '',
  48. this.compensationType = '',
  49. this.compensationCount = 0.0,
  50. this.adr = '',
  51. this.rem = '',
  52. });
  53. }
  54. /// 添加/编辑加班明细弹窗。
  55. class OvertimeApplyDetailDialog extends StatefulWidget {
  56. final OvertimeApplyApi api;
  57. final AppLocalizations l10n;
  58. final OvertimeDetailData? initialData;
  59. const OvertimeApplyDetailDialog({
  60. super.key,
  61. required this.api,
  62. required this.l10n,
  63. this.initialData,
  64. });
  65. /// 显示弹窗,返回 [OvertimeDetailData] 或 `null`(取消时)。
  66. static Future<OvertimeDetailData?> show(
  67. BuildContext context, {
  68. required OvertimeApplyApi api,
  69. required AppLocalizations l10n,
  70. OvertimeDetailData? initialData,
  71. }) {
  72. FocusScope.of(context).unfocus();
  73. return Navigator.push<OvertimeDetailData>(
  74. context,
  75. TDSlidePopupRoute<OvertimeDetailData>(
  76. slideTransitionFrom: SlideTransitionFrom.bottom,
  77. isDismissible: true,
  78. builder: (_) => OvertimeApplyDetailDialog(
  79. api: api,
  80. l10n: l10n,
  81. initialData: initialData,
  82. ),
  83. ),
  84. );
  85. }
  86. @override
  87. State<OvertimeApplyDetailDialog> createState() =>
  88. _OvertimeApplyDetailDialogState();
  89. }
  90. class _OvertimeApplyDetailDialogState extends State<OvertimeApplyDetailDialog> {
  91. // 员工
  92. EmployeeItem? _selEmployee;
  93. // 部门
  94. String _dep = '';
  95. String _depName = '';
  96. // 加班类型
  97. String _jbType = 'WORKING_DAY';
  98. // 加班日期
  99. String _jbDate = '';
  100. // 开始/结束时间
  101. String _startTime = '';
  102. String _endTime = '';
  103. // 加班时长、天数
  104. final _hoursCtrl = TextEditingController();
  105. final _hoursFocus = FocusNode();
  106. final _daysCtrl = TextEditingController();
  107. final _daysFocus = FocusNode();
  108. // 考勤周期
  109. String _attPeriod = '';
  110. // 事由
  111. final _reasonCtrl = TextEditingController();
  112. final _reasonFocus = FocusNode();
  113. // 补偿类型
  114. String _compensationType = '';
  115. // 折算补偿次数
  116. final _compCountCtrl = TextEditingController();
  117. final _compCountFocus = FocusNode();
  118. // 地点
  119. final _adrCtrl = TextEditingController();
  120. final _adrFocus = FocusNode();
  121. // 备注
  122. final _remarkCtrl = TextEditingController();
  123. final _remarkFocus = FocusNode();
  124. // 滚动
  125. final _scrollCtrl = ScrollController();
  126. // 加班类型选项
  127. static const _jbTypeOptions = [
  128. 'WORKING_DAY',
  129. 'REST_DAY',
  130. 'PUBLIC_HOLIDAY',
  131. 'SPECIAL_HOLIDAY',
  132. 'OTHER',
  133. ];
  134. // 补偿类型选项
  135. static const _compensationTypeOptions = [
  136. 'OVERTIME_PAY',
  137. 'COMPENSATORY_LEAVE',
  138. 'NO_COMPENSATION',
  139. 'OTHER',
  140. ];
  141. AppLocalizations get _l10n => widget.l10n;
  142. bool get _isEdit => widget.initialData != null;
  143. String _todayStr() {
  144. final now = DateTime.now();
  145. return '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
  146. }
  147. @override
  148. void initState() {
  149. super.initState();
  150. final d = widget.initialData;
  151. _selEmployee = (d != null && d.salNo.isNotEmpty)
  152. ? EmployeeItem(salNo: d.salNo, name: d.salName)
  153. : null;
  154. _dep = d?.dep ?? '';
  155. _depName = d?.depName ?? '';
  156. _jbType = d?.jbType ?? 'WORKING_DAY';
  157. _jbDate = d?.jbDate ?? '';
  158. if (!_isEdit && _jbDate.isEmpty) {
  159. final now = DateTime.now();
  160. _jbDate =
  161. '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
  162. }
  163. _startTime = d?.startTime ?? '';
  164. _endTime = d?.endTime ?? '';
  165. // 新增模式:开始/结束时间给默认值
  166. if (!_isEdit) {
  167. final dateStr = _jbDate.isNotEmpty ? _jbDate : _todayStr();
  168. final weekday =
  169. DateTime.tryParse(dateStr)?.weekday ?? DateTime.now().weekday;
  170. if (_startTime.isEmpty) {
  171. final h = (weekday <= DateTime.friday) ? 18 : 9;
  172. _startTime = '$dateStr ${h.toString().padLeft(2, '0')}:00';
  173. }
  174. if (_endTime.isEmpty) {
  175. final h = (weekday <= DateTime.friday) ? 19 : 18;
  176. _endTime = '$dateStr ${h.toString().padLeft(2, '0')}:00';
  177. }
  178. _attPeriod = dateStr.substring(0, 7);
  179. _autoCalcHours();
  180. }
  181. if (d != null) {
  182. _hoursCtrl.text = d.jbHours > 0 ? d.jbHours.toString() : '';
  183. _daysCtrl.text = d.jbDays > 0 ? d.jbDays.toString() : '';
  184. _attPeriod = d.attPeriod;
  185. }
  186. if (d != null) {
  187. _reasonCtrl.text = d.reason;
  188. }
  189. _compensationType = d?.compensationType ?? '';
  190. if (d != null) {
  191. _compCountCtrl.text = d.compensationCount > 0
  192. ? d.compensationCount.toString()
  193. : '';
  194. }
  195. if (d != null) {
  196. _adrCtrl.text = d.adr;
  197. _remarkCtrl.text = d.rem;
  198. }
  199. _reasonFocus.addListener(() => _ensureVisible(_reasonFocus));
  200. _hoursFocus.addListener(() {
  201. _ensureVisible(_hoursFocus);
  202. if (!_hoursFocus.hasFocus) _roundToHalf(_hoursCtrl);
  203. });
  204. _daysFocus.addListener(() {
  205. _ensureVisible(_daysFocus);
  206. if (!_daysFocus.hasFocus) _roundToHalf(_daysCtrl);
  207. });
  208. _compCountFocus.addListener(() => _ensureVisible(_compCountFocus));
  209. _adrFocus.addListener(() => _ensureVisible(_adrFocus));
  210. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  211. // 非编辑模式自动带出当前用户
  212. if (!_isEdit) {
  213. WidgetsBinding.instance.addPostFrameCallback((_) => _doAutoFill());
  214. }
  215. }
  216. void _ensureVisible(FocusNode node) {
  217. if (!node.hasFocus) return;
  218. _doEnsureVisible(node, 0, -1);
  219. }
  220. void _doEnsureVisible(FocusNode node, int attempt, double lastInsets) {
  221. if (attempt >= 15) return;
  222. WidgetsBinding.instance.addPostFrameCallback((_) {
  223. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  224. final insets = MediaQuery.of(context).viewInsets.bottom;
  225. if (insets != lastInsets) {
  226. _doEnsureVisible(node, attempt + 1, insets);
  227. return;
  228. }
  229. final ctx = node.context;
  230. if (ctx == null) return;
  231. Future.delayed(const Duration(milliseconds: 500), () {
  232. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  233. Scrollable.ensureVisible(
  234. ctx,
  235. alignment: 0.5,
  236. duration: const Duration(milliseconds: 300),
  237. );
  238. });
  239. });
  240. }
  241. Future<void> _doAutoFill() async {
  242. await HostAppChannel.ensureConfig();
  243. final usr = HostAppChannel.usr;
  244. if (usr.isEmpty) return;
  245. try {
  246. final employees = await widget.api.getEmployees(salNo: usr);
  247. if (employees.isNotEmpty && mounted) {
  248. setState(() {
  249. _selEmployee = employees.first;
  250. });
  251. // 自动带出部门
  252. final depCode = HostAppChannel.dep;
  253. if (depCode.isNotEmpty) {
  254. final depts = await widget.api.getDepartments();
  255. final matched = depts.where((d) => d.dep == depCode);
  256. if (matched.isNotEmpty && mounted) {
  257. setState(() {
  258. _dep = matched.first.dep;
  259. _depName = matched.first.name;
  260. });
  261. }
  262. }
  263. }
  264. } catch (_) {
  265. // 静默失败
  266. }
  267. }
  268. @override
  269. void dispose() {
  270. _hoursCtrl.dispose();
  271. _hoursFocus.dispose();
  272. _daysCtrl.dispose();
  273. _daysFocus.dispose();
  274. _reasonCtrl.dispose();
  275. _reasonFocus.dispose();
  276. _compCountCtrl.dispose();
  277. _compCountFocus.dispose();
  278. _adrCtrl.dispose();
  279. _adrFocus.dispose();
  280. _remarkCtrl.dispose();
  281. _remarkFocus.dispose();
  282. _scrollCtrl.dispose();
  283. super.dispose();
  284. }
  285. void _confirm() {
  286. if (_selEmployee == null) {
  287. TDToast.showText(
  288. '${_l10n.get('pleaseSelect')}${_l10n.get('employee')}',
  289. context: context,
  290. );
  291. return;
  292. }
  293. if (_jbDate.isEmpty) {
  294. TDToast.showText(
  295. '${_l10n.get('pleaseSelect')}${_l10n.get('date')}',
  296. context: context,
  297. );
  298. return;
  299. }
  300. if (_startTime.isEmpty) {
  301. TDToast.showText(
  302. '${_l10n.get('pleaseSelect')}${_l10n.get('startTime')}',
  303. context: context,
  304. );
  305. return;
  306. }
  307. if (_endTime.isEmpty) {
  308. TDToast.showText(
  309. '${_l10n.get('pleaseSelect')}${_l10n.get('endTime')}',
  310. context: context,
  311. );
  312. return;
  313. }
  314. final st = DateTime.tryParse(_startTime);
  315. final et = DateTime.tryParse(_endTime);
  316. if (st == null ||
  317. et == null ||
  318. et.isBefore(st) ||
  319. et.isAtSameMomentAs(st)) {
  320. TDToast.showText(_l10n.get('endTimeMustLater'), context: context);
  321. return;
  322. }
  323. final hours = double.tryParse(_hoursCtrl.text) ?? 0;
  324. if (hours <= 0) {
  325. TDToast.showText(_l10n.get('overtimeHoursPositive'), context: context);
  326. return;
  327. }
  328. Navigator.pop(
  329. context,
  330. OvertimeDetailData(
  331. jbNo: widget.initialData?.jbNo,
  332. itm: widget.initialData?.itm,
  333. salNo: _selEmployee!.salNo,
  334. salName: _selEmployee!.name,
  335. dep: _dep,
  336. depName: _depName,
  337. jbType: _jbType,
  338. jbDate: _jbDate,
  339. startTime: _startTime,
  340. endTime: _endTime,
  341. jbHours: hours,
  342. jbDays: 0,
  343. attPeriod: _attPeriod,
  344. reason: _reasonCtrl.text.trim(),
  345. compensationType: _compensationType,
  346. compensationCount: 0,
  347. adr: _adrCtrl.text,
  348. rem: _remarkCtrl.text,
  349. ),
  350. );
  351. }
  352. @override
  353. Widget build(BuildContext context) {
  354. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  355. return AnimatedPadding(
  356. padding: EdgeInsets.only(
  357. bottom: MediaQuery.of(context).viewInsets.bottom,
  358. ),
  359. duration: const Duration(milliseconds: 200),
  360. child: SafeArea(
  361. child: ConstrainedBox(
  362. constraints: BoxConstraints(
  363. maxHeight: MediaQuery.of(context).size.height * 0.85,
  364. ),
  365. child: Container(
  366. decoration: BoxDecoration(
  367. color: colors.bgPage,
  368. borderRadius: const BorderRadius.vertical(
  369. top: Radius.circular(16),
  370. ),
  371. ),
  372. child: Column(
  373. mainAxisSize: MainAxisSize.min,
  374. crossAxisAlignment: CrossAxisAlignment.stretch,
  375. children: [
  376. _buildHeader(colors),
  377. Flexible(
  378. child: GestureDetector(
  379. onTap: () => FocusScope.of(context).unfocus(),
  380. behavior: HitTestBehavior.translucent,
  381. child: SingleChildScrollView(
  382. controller: _scrollCtrl,
  383. keyboardDismissBehavior:
  384. ScrollViewKeyboardDismissBehavior.manual,
  385. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  386. child: Column(
  387. mainAxisSize: MainAxisSize.min,
  388. crossAxisAlignment: CrossAxisAlignment.stretch,
  389. children: [
  390. // ── 必填 ──
  391. _buildEmployeePicker(colors),
  392. const SizedBox(height: 12),
  393. _buildJbTypeSelector(colors),
  394. const SizedBox(height: 12),
  395. _buildJbDatePicker(colors),
  396. const SizedBox(height: 12),
  397. _buildStartTimePicker(colors),
  398. if (_startTime.isNotEmpty &&
  399. _endTime.isNotEmpty &&
  400. () {
  401. final st = DateTime.tryParse(_startTime);
  402. final et = DateTime.tryParse(_endTime);
  403. return st != null &&
  404. et != null &&
  405. !et.isAfter(st);
  406. }())
  407. Padding(
  408. padding: const EdgeInsets.only(top: 8),
  409. child: _buildTimeErrorHint(),
  410. ),
  411. const SizedBox(height: 12),
  412. _buildEndTimePicker(colors),
  413. const SizedBox(height: 12),
  414. _buildHoursInput(colors),
  415. // ── 非必填 ──
  416. const SizedBox(height: 12),
  417. _buildDeptPicker(colors),
  418. const SizedBox(height: 12),
  419. _buildAttPeriodInput(colors),
  420. const SizedBox(height: 12),
  421. _buildReasonInput(colors),
  422. const SizedBox(height: 12),
  423. _buildCompensationTypeSelector(colors),
  424. // TODO: 加班天数、折算补偿次数暂时隐藏
  425. // const SizedBox(height: 12),
  426. // _buildDaysInput(colors),
  427. // if (_compensationType != 'NO_COMPENSATION') ...[
  428. // const SizedBox(height: 12),
  429. // _buildCompCountInput(colors),
  430. // ],
  431. const SizedBox(height: 12),
  432. _buildAdrInput(colors),
  433. const SizedBox(height: 12),
  434. _buildRemarkInput(colors),
  435. ],
  436. ),
  437. ),
  438. ),
  439. ),
  440. Container(
  441. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  442. decoration: BoxDecoration(
  443. color: colors.bgCard,
  444. border: Border(
  445. top: BorderSide(color: colors.border, width: 0.5),
  446. ),
  447. ),
  448. child: _buildActions(),
  449. ),
  450. ],
  451. ),
  452. ),
  453. ),
  454. ),
  455. );
  456. }
  457. // ── 标题栏 ──
  458. Widget _buildHeader(AppColorsExtension colors) {
  459. return Column(
  460. mainAxisSize: MainAxisSize.min,
  461. children: [
  462. Center(
  463. child: Container(
  464. margin: const EdgeInsets.only(top: 8, bottom: 4),
  465. width: 36,
  466. height: 4,
  467. decoration: BoxDecoration(
  468. color: colors.border,
  469. borderRadius: BorderRadius.circular(2),
  470. ),
  471. ),
  472. ),
  473. Padding(
  474. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  475. child: Row(
  476. children: [
  477. const SizedBox(width: 24),
  478. Expanded(
  479. child: Center(
  480. child: Text(
  481. _isEdit
  482. ? _l10n.get('editOvertimeDetail')
  483. : _l10n.get('addOvertimeDetail'),
  484. style: TextStyle(
  485. fontSize: AppFontSizes.title,
  486. fontWeight: FontWeight.w600,
  487. color: colors.textPrimary,
  488. ),
  489. ),
  490. ),
  491. ),
  492. GestureDetector(
  493. onTap: () => Navigator.pop(context),
  494. child: Padding(
  495. padding: const EdgeInsets.all(4),
  496. child: Icon(
  497. Icons.close,
  498. size: 20,
  499. color: colors.textSecondary,
  500. ),
  501. ),
  502. ),
  503. ],
  504. ),
  505. ),
  506. ],
  507. );
  508. }
  509. // ── 通用 Picker 卡片 ──
  510. Widget _pickerCard({
  511. required String label,
  512. required bool required,
  513. required String currentLabel,
  514. required bool hasValue,
  515. required VoidCallback onTap,
  516. VoidCallback? onClear,
  517. }) {
  518. final tdTheme = TDTheme.of(context);
  519. return GestureDetector(
  520. onTap: () {
  521. FocusManager.instance.primaryFocus?.unfocus();
  522. onTap();
  523. },
  524. child: Container(
  525. padding: const EdgeInsets.only(
  526. left: 16,
  527. right: 10,
  528. top: 12,
  529. bottom: 12,
  530. ),
  531. decoration: BoxDecoration(
  532. color: tdTheme.bgColorContainer,
  533. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  534. border: Border.all(color: tdTheme.componentStrokeColor),
  535. ),
  536. child: Row(
  537. children: [
  538. TDText(
  539. label,
  540. maxLines: 1,
  541. overflow: TextOverflow.visible,
  542. font: tdTheme.fontBodyLarge,
  543. fontWeight: FontWeight.w400,
  544. style: const TextStyle(letterSpacing: 0),
  545. ),
  546. if (required)
  547. Padding(
  548. padding: const EdgeInsets.only(left: 4),
  549. child: TDText(
  550. '*',
  551. font: tdTheme.fontBodyLarge,
  552. fontWeight: FontWeight.w400,
  553. style: TextStyle(color: tdTheme.errorColor6),
  554. ),
  555. ),
  556. const SizedBox(width: 12),
  557. Expanded(
  558. child: Row(
  559. mainAxisAlignment: MainAxisAlignment.end,
  560. mainAxisSize: MainAxisSize.max,
  561. children: [
  562. Flexible(
  563. child: TDText(
  564. currentLabel,
  565. maxLines: 1,
  566. overflow: TextOverflow.ellipsis,
  567. font: tdTheme.fontBodyLarge,
  568. fontWeight: FontWeight.w400,
  569. textColor: hasValue
  570. ? tdTheme.textColorPrimary
  571. : tdTheme.textColorPlaceholder,
  572. textAlign: TextAlign.end,
  573. ),
  574. ),
  575. const SizedBox(width: 4),
  576. SizedBox(
  577. width: 18,
  578. height: 18,
  579. child: onClear != null
  580. ? GestureDetector(
  581. onTap: onClear,
  582. child: Icon(
  583. Icons.close,
  584. size: 18,
  585. color: tdTheme.textColorPlaceholder,
  586. ),
  587. )
  588. : Icon(
  589. Icons.chevron_right,
  590. size: 18,
  591. color: tdTheme.textColorPlaceholder,
  592. ),
  593. ),
  594. ],
  595. ),
  596. ),
  597. ],
  598. ),
  599. ),
  600. );
  601. }
  602. // ── 0. 员工 ──
  603. Widget _buildEmployeePicker(AppColorsExtension colors) {
  604. return _pickerCard(
  605. label: _l10n.get('employee'),
  606. required: true,
  607. hasValue: _selEmployee != null,
  608. currentLabel: _selEmployee != null
  609. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  610. : _l10n.get('pleaseSelect'),
  611. onTap: () async {
  612. final result = await showSearchablePicker<EmployeeItem>(
  613. context,
  614. title: '${_l10n.get('select')}${_l10n.get('employee')}',
  615. searchHint: _l10n.get('search'),
  616. loader: (keyword, page) =>
  617. widget.api.getEmployees(keyword: keyword, page: page, size: 20),
  618. labelBuilder: (e) =>
  619. e.name.isEmpty ? e.salNo : '${e.salNo} ${e.name}',
  620. onRefresh: () => widget.api.clearRefCache(),
  621. );
  622. if (result != null && mounted) {
  623. setState(() => _selEmployee = result);
  624. }
  625. },
  626. onClear: _selEmployee != null
  627. ? () => setState(() => _selEmployee = null)
  628. : null,
  629. );
  630. }
  631. // ── 1. 部门 ──
  632. Widget _buildDeptPicker(AppColorsExtension colors) {
  633. return _pickerCard(
  634. label: _l10n.get('dep'),
  635. required: false,
  636. hasValue: _dep.isNotEmpty,
  637. currentLabel: _dep.isNotEmpty
  638. ? (_depName.isNotEmpty ? '$_dep/$_depName' : _dep)
  639. : _l10n.get('pleaseSelect'),
  640. onTap: () async {
  641. final result = await showSearchablePicker<DepartmentItem>(
  642. context,
  643. title: '${_l10n.get('select')}${_l10n.get('dep')}',
  644. searchHint: _l10n.get('search'),
  645. loader: (keyword, page) =>
  646. widget.api.getDepartments(keyword: keyword, page: page, size: 20),
  647. labelBuilder: (d) => d.name.isEmpty ? d.dep : '${d.dep} ${d.name}',
  648. onRefresh: () => widget.api.clearRefCache(),
  649. );
  650. if (result != null && mounted) {
  651. setState(() {
  652. _dep = result.dep;
  653. _depName = result.name;
  654. });
  655. }
  656. },
  657. onClear: _dep.isNotEmpty
  658. ? () => setState(() {
  659. _dep = '';
  660. _depName = '';
  661. })
  662. : null,
  663. );
  664. }
  665. // ── 2. 加班类型 ──
  666. Widget _buildJbTypeSelector(AppColorsExtension colors) {
  667. final selLabel = _jbTypeLabel(_jbType);
  668. return _pickerCard(
  669. label: _l10n.get('jbType'),
  670. required: true,
  671. hasValue: true,
  672. currentLabel: selLabel,
  673. onTap: () => _showJbTypePicker(),
  674. );
  675. }
  676. void _showJbTypePicker() {
  677. FocusManager.instance.primaryFocus?.unfocus();
  678. final labels = _jbTypeOptions.map((t) => _jbTypeLabel(t)).toList();
  679. TDPicker.showMultiPicker(
  680. context,
  681. title: _l10n.get('selectJbType'),
  682. data: [labels],
  683. onConfirm: (selected) {
  684. if (selected.isNotEmpty) {
  685. final idx = selected.first is int ? selected.first as int : 0;
  686. if (idx >= 0 && idx < _jbTypeOptions.length) {
  687. setState(() => _jbType = _jbTypeOptions[idx]);
  688. }
  689. }
  690. Navigator.of(context).pop();
  691. },
  692. );
  693. }
  694. String _jbTypeLabel(String type) {
  695. switch (type) {
  696. case 'WORKING_DAY':
  697. return _l10n.get('workingDay');
  698. case 'REST_DAY':
  699. return _l10n.get('restDay');
  700. case 'PUBLIC_HOLIDAY':
  701. return _l10n.get('publicHoliday');
  702. case 'SPECIAL_HOLIDAY':
  703. return _l10n.get('specialHoliday');
  704. case 'OTHER':
  705. return _l10n.get('other');
  706. default:
  707. return type;
  708. }
  709. }
  710. String? _dayOfWeekLabel(String dateTimeStr) {
  711. final dt = DateTime.tryParse(dateTimeStr);
  712. if (dt == null) return null;
  713. switch (dt.weekday) {
  714. case 1:
  715. return _l10n.get('monday');
  716. case 2:
  717. return _l10n.get('tuesday');
  718. case 3:
  719. return _l10n.get('wednesday');
  720. case 4:
  721. return _l10n.get('thursday');
  722. case 5:
  723. return _l10n.get('friday');
  724. case 6:
  725. return _l10n.get('saturday');
  726. case 7:
  727. return _l10n.get('sunday');
  728. default:
  729. return null;
  730. }
  731. }
  732. // ── 4. 加班日期 ──
  733. Widget _buildJbDatePicker(AppColorsExtension colors) {
  734. return _datePickerCard(
  735. label: _l10n.get('jbDate'),
  736. value: _jbDate,
  737. hint: _l10n.get('pleaseSelect'),
  738. colors: colors,
  739. required: true,
  740. showDayOfWeek: true,
  741. onPick: (d) {
  742. setState(() {
  743. _jbDate = d;
  744. if (_startTime.isNotEmpty) {
  745. _startTime = '$d ${_fmtTime(_startTime)}';
  746. }
  747. if (_endTime.isNotEmpty) {
  748. _endTime = '$d ${_fmtTime(_endTime)}';
  749. }
  750. _autoCalcHours();
  751. });
  752. },
  753. );
  754. }
  755. // ── 5. 开始时间 ──
  756. Widget _buildStartTimePicker(AppColorsExtension colors) {
  757. return _datePickerCard(
  758. label: _l10n.get('startTime'),
  759. value: _startTime.isNotEmpty ? _fmtTime(_startTime) : '',
  760. hint: _l10n.get('pleaseSelect'),
  761. colors: colors,
  762. required: true,
  763. isTimeOnly: true,
  764. onPick: (d) {
  765. final dateStr = _jbDate.isNotEmpty ? _jbDate : _todayStr();
  766. setState(() {
  767. _startTime = '$dateStr $d';
  768. if (dateStr.length >= 7) _attPeriod = dateStr.substring(0, 7);
  769. _autoCalcHours();
  770. });
  771. },
  772. onClear: _startTime.isNotEmpty
  773. ? () {
  774. setState(() {
  775. _startTime = '';
  776. _autoCalcHours();
  777. });
  778. }
  779. : null,
  780. );
  781. }
  782. // ── 6. 结束时间 ──
  783. Widget _buildEndTimePicker(AppColorsExtension colors) {
  784. return _datePickerCard(
  785. label: _l10n.get('endTime'),
  786. value: _endTime.isNotEmpty ? _fmtTime(_endTime) : '',
  787. hint: _l10n.get('pleaseSelect'),
  788. colors: colors,
  789. required: true,
  790. isTimeOnly: true,
  791. onPick: (d) {
  792. final dateStr = _jbDate.isNotEmpty ? _jbDate : _todayStr();
  793. setState(() {
  794. _endTime = '$dateStr $d';
  795. _autoCalcHours();
  796. });
  797. },
  798. onClear: _endTime.isNotEmpty
  799. ? () {
  800. setState(() {
  801. _endTime = '';
  802. _autoCalcHours();
  803. });
  804. }
  805. : null,
  806. );
  807. }
  808. Widget _buildTimeErrorHint() {
  809. final tdTheme = TDTheme.of(context);
  810. return Row(
  811. mainAxisAlignment: MainAxisAlignment.end,
  812. children: [
  813. Icon(
  814. Icons.warning_amber_rounded,
  815. size: 14,
  816. color: tdTheme.warningColor5,
  817. ),
  818. const SizedBox(width: 4),
  819. TDText(
  820. _l10n.get('endTimeMustLater'),
  821. font: tdTheme.fontBodySmall,
  822. textColor: tdTheme.warningColor5,
  823. ),
  824. ],
  825. );
  826. }
  827. String _fmtTime(String dt) {
  828. if (dt.length >= 16) return dt.substring(11, 16);
  829. return dt;
  830. }
  831. void _autoCalcHours() {
  832. if (_startTime.isEmpty || _endTime.isEmpty) return;
  833. final st = DateTime.tryParse(_startTime);
  834. DateTime? et = DateTime.tryParse(_endTime);
  835. if (st == null || et == null) return;
  836. // 结束时间 23:59 视为当天结束(24:00),时长补 1 分钟。
  837. // 数据库不支持 "24:00" 格式,同一天约束下以 23:59 表达"加班到晚上12点整"。
  838. if (et.hour == 23 && et.minute == 59) {
  839. et = et.add(const Duration(minutes: 1));
  840. }
  841. double totalHours = et.difference(st).inMinutes / 60.0;
  842. // 跨午夜:结束时间早于开始时间 → 加 24 小时
  843. if (totalHours < 0) totalHours += 24;
  844. if (totalHours > 0) {
  845. _hoursCtrl.text = ((totalHours * 2).floor() / 2.0).toStringAsFixed(1);
  846. }
  847. _autoCalcDays();
  848. }
  849. void _autoCalcDays() {
  850. final hours = double.tryParse(_hoursCtrl.text) ?? 0;
  851. if (hours <= 0) return;
  852. // 8小时工作制:天数 = 小时 ÷ 8
  853. final diffDays = hours / 8.0;
  854. _daysCtrl.text = ((diffDays * 2).floor() / 2.0).toStringAsFixed(1);
  855. }
  856. /// 手动输入时失焦自动按 0.5 向上取整
  857. void _roundToHalf(TextEditingController controller) {
  858. final text = controller.text.trim();
  859. if (text.isEmpty) return;
  860. final value = double.tryParse(text);
  861. if (value == null || value <= 0) return;
  862. final rounded = (value * 2).floor() / 2.0;
  863. final formatted = rounded.toStringAsFixed(1);
  864. if (controller.text != formatted) {
  865. controller.text = formatted;
  866. }
  867. }
  868. // ── 6. 加班时长 ──
  869. Widget _buildHoursInput(AppColorsExtension colors) {
  870. return _inputCard(
  871. label: _l10n.get('overtimeHours'),
  872. required: true,
  873. controller: _hoursCtrl,
  874. hintText: '>0',
  875. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  876. inputFormatters: [
  877. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?[05]?$')),
  878. ],
  879. focusNode: _hoursFocus,
  880. );
  881. }
  882. // ── 7. 加班天数 ──
  883. Widget _buildDaysInput(AppColorsExtension colors) {
  884. return _inputCard(
  885. label: _l10n.get('overtimeDays'),
  886. required: false,
  887. controller: _daysCtrl,
  888. hintText: '0',
  889. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  890. inputFormatters: [
  891. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?[05]?$')),
  892. ],
  893. focusNode: _daysFocus,
  894. );
  895. }
  896. // ── 8. 考勤周期 ──
  897. Widget _buildAttPeriodInput(AppColorsExtension colors) {
  898. return _datePickerCard(
  899. label: _l10n.get('attPeriod'),
  900. value: _attPeriod,
  901. hint: _l10n.get('autoFromDate'),
  902. colors: colors,
  903. onPick: (d) => setState(() => _attPeriod = d),
  904. onClear: _attPeriod.isNotEmpty
  905. ? () => setState(() => _attPeriod = '')
  906. : null,
  907. );
  908. }
  909. // ── 10. 事由 ──
  910. Widget _buildReasonInput(AppColorsExtension colors) {
  911. final tdTheme = TDTheme.of(context);
  912. return TDTextarea(
  913. controller: _reasonCtrl,
  914. focusNode: _reasonFocus,
  915. label: _l10n.get('overtimeDetailReason'),
  916. hintText: _l10n.get('enterReason'),
  917. maxLines: 3,
  918. minLines: 1,
  919. maxLength: 1000,
  920. indicator: true,
  921. decoration: BoxDecoration(
  922. color: tdTheme.bgColorContainer,
  923. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  924. border: Border.all(color: tdTheme.componentStrokeColor),
  925. ),
  926. onChanged: (_) => setState(() {}),
  927. );
  928. }
  929. // ── 10. 补偿类型 ──
  930. Widget _buildCompensationTypeSelector(AppColorsExtension colors) {
  931. final hasValue = _compensationType.isNotEmpty;
  932. final selLabel = hasValue
  933. ? _compensationTypeLabel(_compensationType)
  934. : _l10n.get('pleaseSelect');
  935. return _pickerCard(
  936. label: _l10n.get('compensationType'),
  937. required: false,
  938. hasValue: hasValue,
  939. currentLabel: selLabel,
  940. onTap: () => _showCompensationTypePicker(),
  941. );
  942. }
  943. void _showCompensationTypePicker() {
  944. FocusManager.instance.primaryFocus?.unfocus();
  945. final labels = _compensationTypeOptions
  946. .map((t) => _compensationTypeLabel(t))
  947. .toList();
  948. TDPicker.showMultiPicker(
  949. context,
  950. title: _l10n.get('selectCompensationType'),
  951. data: [labels],
  952. onConfirm: (selected) {
  953. if (selected.isNotEmpty) {
  954. final idx = selected.first is int ? selected.first as int : 0;
  955. if (idx >= 0 && idx < _compensationTypeOptions.length) {
  956. final newType = _compensationTypeOptions[idx];
  957. setState(() {
  958. _compensationType = newType;
  959. // 无补偿 → 清空;其他 → 默认取 JB_DAYS
  960. if (newType == 'NO_COMPENSATION') {
  961. _compCountCtrl.clear();
  962. } else if (_compCountCtrl.text.isEmpty &&
  963. _daysCtrl.text.isNotEmpty) {
  964. _compCountCtrl.text = _daysCtrl.text;
  965. }
  966. });
  967. }
  968. }
  969. Navigator.of(context).pop();
  970. },
  971. );
  972. }
  973. String _compensationTypeLabel(String type) {
  974. switch (type) {
  975. case 'OVERTIME_PAY':
  976. return _l10n.get('overtimePay');
  977. case 'COMPENSATORY_LEAVE':
  978. return _l10n.get('compensatoryLeave');
  979. case 'NO_COMPENSATION':
  980. return _l10n.get('noCompensation');
  981. case 'OTHER':
  982. return _l10n.get('other');
  983. default:
  984. return type;
  985. }
  986. }
  987. // ── 11. 折算补偿次数 ──
  988. Widget _buildCompCountInput(AppColorsExtension colors) {
  989. // 无补偿时显示空
  990. if (_compensationType == 'NO_COMPENSATION') {
  991. final tdTheme = TDTheme.of(context);
  992. return GestureDetector(
  993. onTap: () {},
  994. child: Container(
  995. padding: const EdgeInsets.only(
  996. left: 16,
  997. right: 10,
  998. top: 12,
  999. bottom: 12,
  1000. ),
  1001. decoration: BoxDecoration(
  1002. color: tdTheme.bgColorContainer,
  1003. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1004. border: Border.all(color: tdTheme.componentStrokeColor),
  1005. ),
  1006. child: Row(
  1007. children: [
  1008. TDText(
  1009. _l10n.get('compensationCount'),
  1010. maxLines: 1,
  1011. overflow: TextOverflow.visible,
  1012. font: tdTheme.fontBodyLarge,
  1013. fontWeight: FontWeight.w400,
  1014. style: const TextStyle(letterSpacing: 0),
  1015. ),
  1016. const SizedBox(width: 12),
  1017. Expanded(
  1018. child: TDText(
  1019. '',
  1020. maxLines: 1,
  1021. textAlign: TextAlign.end,
  1022. font: tdTheme.fontBodyLarge,
  1023. fontWeight: FontWeight.w400,
  1024. textColor: tdTheme.textColorPlaceholder,
  1025. ),
  1026. ),
  1027. ],
  1028. ),
  1029. ),
  1030. );
  1031. }
  1032. return _inputCard(
  1033. label: _l10n.get('compensationCount'),
  1034. required: false,
  1035. controller: _compCountCtrl,
  1036. hintText: '0',
  1037. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  1038. inputFormatters: [
  1039. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,1}$')),
  1040. ],
  1041. focusNode: _compCountFocus,
  1042. );
  1043. }
  1044. // ── 12. 地点 ──
  1045. Widget _buildAdrInput(AppColorsExtension colors) {
  1046. final tdTheme = TDTheme.of(context);
  1047. return GestureDetector(
  1048. onTap: () async {
  1049. FocusScope.of(context).unfocus();
  1050. final result = await LocationPicker.show(context);
  1051. if (result != null && mounted) {
  1052. setState(() => _adrCtrl.text = result.address);
  1053. }
  1054. },
  1055. child: Container(
  1056. padding: const EdgeInsets.only(
  1057. left: 16,
  1058. right: 10,
  1059. top: 12,
  1060. bottom: 12,
  1061. ),
  1062. decoration: BoxDecoration(
  1063. color: tdTheme.bgColorContainer,
  1064. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1065. border: Border.all(color: tdTheme.componentStrokeColor),
  1066. ),
  1067. child: Row(
  1068. children: [
  1069. TDText(
  1070. _l10n.get('adr'),
  1071. maxLines: 1,
  1072. overflow: TextOverflow.visible,
  1073. font: tdTheme.fontBodyLarge,
  1074. fontWeight: FontWeight.w400,
  1075. style: const TextStyle(letterSpacing: 0),
  1076. ),
  1077. const SizedBox(width: 12),
  1078. Expanded(
  1079. child: Row(
  1080. mainAxisAlignment: MainAxisAlignment.end,
  1081. children: [
  1082. Flexible(
  1083. child: TDText(
  1084. _adrCtrl.text.isNotEmpty
  1085. ? _adrCtrl.text
  1086. : _l10n.get('enterAdr'),
  1087. maxLines: 1,
  1088. overflow: TextOverflow.ellipsis,
  1089. font: tdTheme.fontBodyLarge,
  1090. fontWeight: FontWeight.w400,
  1091. textColor: _adrCtrl.text.isNotEmpty
  1092. ? tdTheme.textColorPrimary
  1093. : tdTheme.textColorPlaceholder,
  1094. textAlign: TextAlign.end,
  1095. ),
  1096. ),
  1097. const SizedBox(width: 4),
  1098. SizedBox(
  1099. width: 18,
  1100. height: 18,
  1101. child: _adrCtrl.text.isNotEmpty
  1102. ? GestureDetector(
  1103. onTap: () {
  1104. _adrCtrl.clear();
  1105. setState(() {});
  1106. },
  1107. child: Icon(
  1108. Icons.close,
  1109. size: 18,
  1110. color: tdTheme.textColorPlaceholder,
  1111. ),
  1112. )
  1113. : Icon(
  1114. Icons.location_on_outlined,
  1115. size: 20,
  1116. color: tdTheme.textColorPlaceholder,
  1117. ),
  1118. ),
  1119. ],
  1120. ),
  1121. ),
  1122. ],
  1123. ),
  1124. ),
  1125. );
  1126. }
  1127. // ── 13. 备注 ──
  1128. Widget _buildRemarkInput(AppColorsExtension colors) {
  1129. final tdTheme = TDTheme.of(context);
  1130. return TDTextarea(
  1131. controller: _remarkCtrl,
  1132. focusNode: _remarkFocus,
  1133. label: _l10n.get('remark'),
  1134. hintText: _l10n.get('enterRemark'),
  1135. maxLines: 3,
  1136. minLines: 1,
  1137. maxLength: 1000,
  1138. indicator: true,
  1139. decoration: BoxDecoration(
  1140. color: tdTheme.bgColorContainer,
  1141. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1142. border: Border.all(color: tdTheme.componentStrokeColor),
  1143. ),
  1144. onChanged: (_) => setState(() {}),
  1145. );
  1146. }
  1147. // ── 操作按钮 ──
  1148. Widget _buildActions() {
  1149. return Row(
  1150. children: [
  1151. Expanded(
  1152. child: TDButton(
  1153. text: _l10n.get('cancel'),
  1154. size: TDButtonSize.large,
  1155. type: TDButtonType.outline,
  1156. shape: TDButtonShape.rectangle,
  1157. theme: TDButtonTheme.defaultTheme,
  1158. onTap: () => Navigator.pop(context),
  1159. ),
  1160. ),
  1161. const SizedBox(width: 12),
  1162. Expanded(
  1163. child: TDButton(
  1164. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  1165. size: TDButtonSize.large,
  1166. type: TDButtonType.fill,
  1167. shape: TDButtonShape.rectangle,
  1168. theme: TDButtonTheme.primary,
  1169. onTap: _confirm,
  1170. ),
  1171. ),
  1172. ],
  1173. );
  1174. }
  1175. // ── 日期选择器卡片 ──
  1176. Widget _datePickerCard({
  1177. required String label,
  1178. required String value,
  1179. required String hint,
  1180. required AppColorsExtension colors,
  1181. required ValueChanged<String> onPick,
  1182. VoidCallback? onClear,
  1183. bool required = false,
  1184. bool showDayOfWeek = false,
  1185. bool isTimeOnly = false,
  1186. }) {
  1187. final tdTheme = TDTheme.of(context);
  1188. final hasValue = value.isNotEmpty;
  1189. final jbDateFull = _jbDate.isNotEmpty ? _jbDate : _todayStr();
  1190. final dayOfWeek = showDayOfWeek && jbDateFull.isNotEmpty
  1191. ? _dayOfWeekLabel(jbDateFull)
  1192. : null;
  1193. return GestureDetector(
  1194. onTap: () => _pickDateTime(
  1195. label,
  1196. onPick,
  1197. currentValue: isTimeOnly && _startTime.isNotEmpty ? _startTime : value,
  1198. isTimeOnly: isTimeOnly,
  1199. ),
  1200. child: Container(
  1201. padding: const EdgeInsets.only(
  1202. left: 16,
  1203. right: 10,
  1204. top: 12,
  1205. bottom: 12,
  1206. ),
  1207. decoration: BoxDecoration(
  1208. color: tdTheme.bgColorContainer,
  1209. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1210. border: Border.all(color: tdTheme.componentStrokeColor),
  1211. ),
  1212. child: Row(
  1213. children: [
  1214. TDText(
  1215. label,
  1216. maxLines: 1,
  1217. overflow: TextOverflow.visible,
  1218. font: tdTheme.fontBodyLarge,
  1219. fontWeight: FontWeight.w400,
  1220. style: const TextStyle(letterSpacing: 0),
  1221. ),
  1222. if (required)
  1223. Padding(
  1224. padding: const EdgeInsets.only(left: 4),
  1225. child: TDText(
  1226. '*',
  1227. font: tdTheme.fontBodyLarge,
  1228. fontWeight: FontWeight.w400,
  1229. style: TextStyle(color: tdTheme.errorColor6),
  1230. ),
  1231. ),
  1232. const SizedBox(width: 12),
  1233. Expanded(
  1234. child: Row(
  1235. mainAxisAlignment: MainAxisAlignment.end,
  1236. mainAxisSize: MainAxisSize.max,
  1237. children: [
  1238. Flexible(
  1239. child: TDText(
  1240. value.isEmpty ? hint : value,
  1241. maxLines: 1,
  1242. overflow: TextOverflow.ellipsis,
  1243. font: tdTheme.fontBodyLarge,
  1244. fontWeight: FontWeight.w400,
  1245. textColor: value.isEmpty
  1246. ? tdTheme.textColorPlaceholder
  1247. : tdTheme.textColorPrimary,
  1248. textAlign: TextAlign.end,
  1249. ),
  1250. ),
  1251. if (dayOfWeek != null) ...[
  1252. const SizedBox(width: 6),
  1253. Container(
  1254. padding: const EdgeInsets.symmetric(
  1255. horizontal: 6,
  1256. vertical: 2,
  1257. ),
  1258. decoration: BoxDecoration(
  1259. color: tdTheme.brandColor1,
  1260. borderRadius: BorderRadius.circular(4),
  1261. ),
  1262. child: TDText(
  1263. dayOfWeek,
  1264. font: TDTheme.of(context).fontBodySmall,
  1265. fontWeight: FontWeight.w500,
  1266. textColor: tdTheme.brandColor7,
  1267. ),
  1268. ),
  1269. ],
  1270. const SizedBox(width: 4),
  1271. SizedBox(
  1272. width: 18,
  1273. height: 18,
  1274. child: hasValue
  1275. ? GestureDetector(
  1276. onTap: onClear,
  1277. child: Icon(
  1278. Icons.close,
  1279. size: 18,
  1280. color: tdTheme.textColorPlaceholder,
  1281. ),
  1282. )
  1283. : Icon(
  1284. Icons.chevron_right,
  1285. size: 18,
  1286. color: tdTheme.textColorPlaceholder,
  1287. ),
  1288. ),
  1289. ],
  1290. ),
  1291. ),
  1292. ],
  1293. ),
  1294. ),
  1295. );
  1296. }
  1297. void _pickDateTime(
  1298. String label,
  1299. ValueChanged<String> onPick, {
  1300. String currentValue = '',
  1301. bool isTimeOnly = false,
  1302. }) {
  1303. final l10n = _l10n;
  1304. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1305. final now = DateTime.now();
  1306. final isDateOnly =
  1307. label == _l10n.get('applyDate') ||
  1308. label == _l10n.get('jbDate') ||
  1309. label == _l10n.get('attPeriod');
  1310. final isAttPeriod = label == _l10n.get('attPeriod');
  1311. final isStartTime = label == _l10n.get('startTime');
  1312. final isEndTime = label == _l10n.get('endTime');
  1313. // 根据当前值定位初始日期,无值时默认当月/当天
  1314. int initYear = now.year;
  1315. int initMonth = now.month;
  1316. int initDay = now.day;
  1317. int initHour = now.hour;
  1318. int initMinute = now.minute;
  1319. if (currentValue.isNotEmpty) {
  1320. if (isAttPeriod) {
  1321. final parts = currentValue.split('-');
  1322. if (parts.length == 2) {
  1323. initYear = int.tryParse(parts[0]) ?? now.year;
  1324. initMonth = int.tryParse(parts[1]) ?? now.month;
  1325. initDay = 1;
  1326. }
  1327. } else {
  1328. final dt = DateTime.tryParse(currentValue);
  1329. if (dt != null) {
  1330. initYear = dt.year;
  1331. initMonth = dt.month;
  1332. initDay = dt.day;
  1333. initHour = dt.hour;
  1334. initMinute = dt.minute;
  1335. }
  1336. }
  1337. } else if (isStartTime) {
  1338. final date = _jbDate.isNotEmpty ? DateTime.tryParse(_jbDate) : null;
  1339. final weekday = date?.weekday ?? now.weekday;
  1340. initHour = (weekday <= DateTime.friday) ? 18 : 9;
  1341. initMinute = 0;
  1342. } else if (isEndTime) {
  1343. final date = _jbDate.isNotEmpty ? DateTime.tryParse(_jbDate) : null;
  1344. final weekday = date?.weekday ?? now.weekday;
  1345. initHour = (weekday <= DateTime.friday) ? 19 : 18;
  1346. initMinute = 0;
  1347. }
  1348. final useTime = isTimeOnly || !isDateOnly;
  1349. FocusManager.instance.primaryFocus?.unfocus();
  1350. TDPicker.showDatePicker(
  1351. context,
  1352. title: isTimeOnly ? l10n.get('selectTime') : l10n.get('selectDate'),
  1353. backgroundColor: colors.bgCard,
  1354. useYear: !isTimeOnly,
  1355. useMonth: !isTimeOnly,
  1356. useDay: !isTimeOnly && !isAttPeriod,
  1357. useHour: useTime,
  1358. useMinute: useTime,
  1359. useSecond: false,
  1360. useWeekDay: false,
  1361. dateStart: useTime ? const [2020, 1, 1, 0, 0] : const [2020, 1, 1],
  1362. dateEnd: useTime
  1363. ? [now.year + 1, 12, 31, 23, 59]
  1364. : [now.year + 1, 12, 31],
  1365. initialDate: useTime
  1366. ? [initYear, initMonth, initDay, initHour, initMinute]
  1367. : [initYear, initMonth, initDay],
  1368. onConfirm: (selected) {
  1369. final hour = selected['hour']?.toString().padLeft(2, '0') ?? '00';
  1370. final minute = selected['minute']?.toString().padLeft(2, '0') ?? '00';
  1371. if (isTimeOnly) {
  1372. onPick('$hour:$minute');
  1373. } else if (isAttPeriod) {
  1374. final year = selected['year'];
  1375. final month = selected['month'].toString().padLeft(2, '0');
  1376. onPick('$year-$month');
  1377. } else if (isDateOnly) {
  1378. final year = selected['year'];
  1379. final month = selected['month'].toString().padLeft(2, '0');
  1380. final day = selected['day'].toString().padLeft(2, '0');
  1381. onPick('$year-$month-$day');
  1382. } else {
  1383. final year = selected['year'];
  1384. final month = selected['month'].toString().padLeft(2, '0');
  1385. final day = selected['day'].toString().padLeft(2, '0');
  1386. onPick('$year-$month-$day $hour:$minute');
  1387. }
  1388. Navigator.of(context).pop();
  1389. },
  1390. );
  1391. }
  1392. // ── 通用输入卡片 ──
  1393. Widget _inputCard({
  1394. required String label,
  1395. required bool required,
  1396. required TextEditingController controller,
  1397. required String hintText,
  1398. TextInputType? keyboardType,
  1399. List<TextInputFormatter>? inputFormatters,
  1400. FocusNode? focusNode,
  1401. }) {
  1402. final tdTheme = TDTheme.of(context);
  1403. final hasValue = controller.text.isNotEmpty;
  1404. return Container(
  1405. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  1406. decoration: BoxDecoration(
  1407. color: tdTheme.bgColorContainer,
  1408. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1409. border: Border.all(color: tdTheme.componentStrokeColor),
  1410. ),
  1411. child: Row(
  1412. children: [
  1413. TDText(
  1414. label,
  1415. maxLines: 1,
  1416. overflow: TextOverflow.visible,
  1417. font: tdTheme.fontBodyLarge,
  1418. fontWeight: FontWeight.w400,
  1419. style: const TextStyle(letterSpacing: 0),
  1420. ),
  1421. if (required)
  1422. Padding(
  1423. padding: const EdgeInsets.only(left: 4),
  1424. child: TDText(
  1425. '*',
  1426. font: tdTheme.fontBodyLarge,
  1427. fontWeight: FontWeight.w400,
  1428. style: TextStyle(color: tdTheme.errorColor6),
  1429. ),
  1430. ),
  1431. const SizedBox(width: 12),
  1432. Expanded(
  1433. child: Row(
  1434. mainAxisAlignment: MainAxisAlignment.end,
  1435. mainAxisSize: MainAxisSize.max,
  1436. children: [
  1437. Flexible(
  1438. child: TextField(
  1439. controller: controller,
  1440. focusNode: focusNode,
  1441. textAlign: TextAlign.end,
  1442. keyboardType: keyboardType,
  1443. inputFormatters: inputFormatters,
  1444. style: TextStyle(
  1445. fontSize: 16,
  1446. color: tdTheme.textColorPrimary,
  1447. ),
  1448. decoration: InputDecoration(
  1449. hintText: hintText,
  1450. hintStyle: TextStyle(
  1451. fontSize: 16,
  1452. color: tdTheme.textColorPlaceholder,
  1453. ),
  1454. border: InputBorder.none,
  1455. isDense: true,
  1456. contentPadding: EdgeInsets.zero,
  1457. ),
  1458. onChanged: (_) => setState(() {}),
  1459. ),
  1460. ),
  1461. const SizedBox(width: 4),
  1462. SizedBox(
  1463. width: 18,
  1464. height: 18,
  1465. child: hasValue
  1466. ? GestureDetector(
  1467. onTap: () {
  1468. controller.clear();
  1469. setState(() {});
  1470. },
  1471. child: Icon(
  1472. Icons.close,
  1473. size: 18,
  1474. color: tdTheme.textColorPlaceholder,
  1475. ),
  1476. )
  1477. : null,
  1478. ),
  1479. ],
  1480. ),
  1481. ),
  1482. ],
  1483. ),
  1484. );
  1485. }
  1486. }