overtime_apply_detail_dialog.dart 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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. if (_startTime.compareTo(_endTime) >= 0) {
  315. TDToast.showText(_l10n.get('endTimeMustLater'), context: context);
  316. return;
  317. }
  318. final hours = double.tryParse(_hoursCtrl.text) ?? 0;
  319. if (hours <= 0) {
  320. TDToast.showText(_l10n.get('overtimeHoursPositive'), context: context);
  321. return;
  322. }
  323. Navigator.pop(
  324. context,
  325. OvertimeDetailData(
  326. jbNo: widget.initialData?.jbNo,
  327. itm: widget.initialData?.itm,
  328. salNo: _selEmployee!.salNo,
  329. salName: _selEmployee!.name,
  330. dep: _dep,
  331. depName: _depName,
  332. jbType: _jbType,
  333. jbDate: _jbDate,
  334. startTime: _startTime,
  335. endTime: _endTime,
  336. jbHours: hours,
  337. jbDays: double.tryParse(_daysCtrl.text) ?? 0,
  338. attPeriod: _attPeriod,
  339. reason: _reasonCtrl.text.trim(),
  340. compensationType: _compensationType,
  341. compensationCount: double.tryParse(_compCountCtrl.text) ?? 0,
  342. adr: _adrCtrl.text,
  343. rem: _remarkCtrl.text,
  344. ),
  345. );
  346. }
  347. @override
  348. Widget build(BuildContext context) {
  349. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  350. return AnimatedPadding(
  351. padding: EdgeInsets.only(
  352. bottom: MediaQuery.of(context).viewInsets.bottom,
  353. ),
  354. duration: const Duration(milliseconds: 200),
  355. child: SafeArea(
  356. child: ConstrainedBox(
  357. constraints: BoxConstraints(
  358. maxHeight: MediaQuery.of(context).size.height * 0.85,
  359. ),
  360. child: Container(
  361. decoration: BoxDecoration(
  362. color: colors.bgPage,
  363. borderRadius: const BorderRadius.vertical(
  364. top: Radius.circular(16),
  365. ),
  366. ),
  367. child: Column(
  368. mainAxisSize: MainAxisSize.min,
  369. crossAxisAlignment: CrossAxisAlignment.stretch,
  370. children: [
  371. _buildHeader(colors),
  372. Flexible(
  373. child: GestureDetector(
  374. onTap: () => FocusScope.of(context).unfocus(),
  375. behavior: HitTestBehavior.translucent,
  376. child: SingleChildScrollView(
  377. controller: _scrollCtrl,
  378. keyboardDismissBehavior:
  379. ScrollViewKeyboardDismissBehavior.manual,
  380. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  381. child: Column(
  382. mainAxisSize: MainAxisSize.min,
  383. crossAxisAlignment: CrossAxisAlignment.stretch,
  384. children: [
  385. // ── 必填 ──
  386. _buildEmployeePicker(colors),
  387. const SizedBox(height: 12),
  388. _buildJbTypeSelector(colors),
  389. const SizedBox(height: 12),
  390. _buildJbDatePicker(colors),
  391. const SizedBox(height: 12),
  392. _buildStartTimePicker(colors),
  393. const SizedBox(height: 12),
  394. _buildEndTimePicker(colors),
  395. const SizedBox(height: 12),
  396. _buildHoursInput(colors),
  397. // ── 非必填 ──
  398. const SizedBox(height: 12),
  399. _buildDeptPicker(colors),
  400. const SizedBox(height: 12),
  401. _buildDaysInput(colors),
  402. const SizedBox(height: 12),
  403. _buildAttPeriodInput(colors),
  404. const SizedBox(height: 12),
  405. _buildReasonInput(colors),
  406. const SizedBox(height: 12),
  407. _buildCompensationTypeSelector(colors),
  408. const SizedBox(height: 12),
  409. _buildCompCountInput(colors),
  410. const SizedBox(height: 12),
  411. _buildAdrInput(colors),
  412. const SizedBox(height: 12),
  413. _buildRemarkInput(colors),
  414. ],
  415. ),
  416. ),
  417. ),
  418. ),
  419. Container(
  420. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  421. decoration: BoxDecoration(
  422. color: colors.bgCard,
  423. border: Border(
  424. top: BorderSide(color: colors.border, width: 0.5),
  425. ),
  426. ),
  427. child: _buildActions(),
  428. ),
  429. ],
  430. ),
  431. ),
  432. ),
  433. ),
  434. );
  435. }
  436. // ── 标题栏 ──
  437. Widget _buildHeader(AppColorsExtension colors) {
  438. return Column(
  439. mainAxisSize: MainAxisSize.min,
  440. children: [
  441. Center(
  442. child: Container(
  443. margin: const EdgeInsets.only(top: 8, bottom: 4),
  444. width: 36,
  445. height: 4,
  446. decoration: BoxDecoration(
  447. color: colors.border,
  448. borderRadius: BorderRadius.circular(2),
  449. ),
  450. ),
  451. ),
  452. Padding(
  453. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  454. child: Row(
  455. children: [
  456. const SizedBox(width: 24),
  457. Expanded(
  458. child: Center(
  459. child: Text(
  460. _isEdit
  461. ? _l10n.get('editOvertimeDetail')
  462. : _l10n.get('addOvertimeDetail'),
  463. style: TextStyle(
  464. fontSize: AppFontSizes.title,
  465. fontWeight: FontWeight.w600,
  466. color: colors.textPrimary,
  467. ),
  468. ),
  469. ),
  470. ),
  471. GestureDetector(
  472. onTap: () => Navigator.pop(context),
  473. child: Padding(
  474. padding: const EdgeInsets.all(4),
  475. child: Icon(
  476. Icons.close,
  477. size: 20,
  478. color: colors.textSecondary,
  479. ),
  480. ),
  481. ),
  482. ],
  483. ),
  484. ),
  485. ],
  486. );
  487. }
  488. // ── 通用 Picker 卡片 ──
  489. Widget _pickerCard({
  490. required String label,
  491. required bool required,
  492. required String currentLabel,
  493. required bool hasValue,
  494. required VoidCallback onTap,
  495. VoidCallback? onClear,
  496. }) {
  497. final tdTheme = TDTheme.of(context);
  498. return GestureDetector(
  499. onTap: () {
  500. FocusManager.instance.primaryFocus?.unfocus();
  501. onTap();
  502. },
  503. child: Container(
  504. padding: const EdgeInsets.only(
  505. left: 16,
  506. right: 10,
  507. top: 12,
  508. bottom: 12,
  509. ),
  510. decoration: BoxDecoration(
  511. color: tdTheme.bgColorContainer,
  512. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  513. border: Border.all(color: tdTheme.componentStrokeColor),
  514. ),
  515. child: Row(
  516. children: [
  517. TDText(
  518. label,
  519. maxLines: 1,
  520. overflow: TextOverflow.visible,
  521. font: tdTheme.fontBodyLarge,
  522. fontWeight: FontWeight.w400,
  523. style: const TextStyle(letterSpacing: 0),
  524. ),
  525. if (required)
  526. Padding(
  527. padding: const EdgeInsets.only(left: 4),
  528. child: TDText(
  529. '*',
  530. font: tdTheme.fontBodyLarge,
  531. fontWeight: FontWeight.w400,
  532. style: TextStyle(color: tdTheme.errorColor6),
  533. ),
  534. ),
  535. const SizedBox(width: 12),
  536. Expanded(
  537. child: Row(
  538. mainAxisAlignment: MainAxisAlignment.end,
  539. mainAxisSize: MainAxisSize.max,
  540. children: [
  541. Flexible(
  542. child: TDText(
  543. currentLabel,
  544. maxLines: 1,
  545. overflow: TextOverflow.ellipsis,
  546. font: tdTheme.fontBodyLarge,
  547. fontWeight: FontWeight.w400,
  548. textColor: hasValue
  549. ? tdTheme.textColorPrimary
  550. : tdTheme.textColorPlaceholder,
  551. textAlign: TextAlign.end,
  552. ),
  553. ),
  554. const SizedBox(width: 4),
  555. SizedBox(
  556. width: 18,
  557. height: 18,
  558. child: onClear != null
  559. ? GestureDetector(
  560. onTap: onClear,
  561. child: Icon(
  562. Icons.close,
  563. size: 18,
  564. color: tdTheme.textColorPlaceholder,
  565. ),
  566. )
  567. : Icon(
  568. Icons.chevron_right,
  569. size: 18,
  570. color: tdTheme.textColorPlaceholder,
  571. ),
  572. ),
  573. ],
  574. ),
  575. ),
  576. ],
  577. ),
  578. ),
  579. );
  580. }
  581. // ── 0. 员工 ──
  582. Widget _buildEmployeePicker(AppColorsExtension colors) {
  583. return _pickerCard(
  584. label: _l10n.get('employee'),
  585. required: true,
  586. hasValue: _selEmployee != null,
  587. currentLabel: _selEmployee != null
  588. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  589. : _l10n.get('pleaseSelect'),
  590. onTap: () async {
  591. final result = await showSearchablePicker<EmployeeItem>(
  592. context,
  593. title: '${_l10n.get('select')}${_l10n.get('employee')}',
  594. searchHint: _l10n.get('search'),
  595. loader: (keyword, page) =>
  596. widget.api.getEmployees(keyword: keyword, page: page, size: 20),
  597. labelBuilder: (e) =>
  598. e.name.isEmpty ? e.salNo : '${e.salNo} ${e.name}',
  599. onRefresh: () => widget.api.clearRefCache(),
  600. );
  601. if (result != null && mounted) {
  602. setState(() => _selEmployee = result);
  603. }
  604. },
  605. onClear: _selEmployee != null
  606. ? () => setState(() => _selEmployee = null)
  607. : null,
  608. );
  609. }
  610. // ── 1. 部门 ──
  611. Widget _buildDeptPicker(AppColorsExtension colors) {
  612. return _pickerCard(
  613. label: _l10n.get('dep'),
  614. required: false,
  615. hasValue: _dep.isNotEmpty,
  616. currentLabel: _dep.isNotEmpty
  617. ? (_depName.isNotEmpty ? '$_dep/$_depName' : _dep)
  618. : _l10n.get('pleaseSelect'),
  619. onTap: () async {
  620. final result = await showSearchablePicker<DepartmentItem>(
  621. context,
  622. title: '${_l10n.get('select')}${_l10n.get('dep')}',
  623. searchHint: _l10n.get('search'),
  624. loader: (keyword, page) =>
  625. widget.api.getDepartments(keyword: keyword, page: page, size: 20),
  626. labelBuilder: (d) => d.name.isEmpty ? d.dep : '${d.dep} ${d.name}',
  627. onRefresh: () => widget.api.clearRefCache(),
  628. );
  629. if (result != null && mounted) {
  630. setState(() {
  631. _dep = result.dep;
  632. _depName = result.name;
  633. });
  634. }
  635. },
  636. onClear: _dep.isNotEmpty
  637. ? () => setState(() {
  638. _dep = '';
  639. _depName = '';
  640. })
  641. : null,
  642. );
  643. }
  644. // ── 2. 加班类型 ──
  645. Widget _buildJbTypeSelector(AppColorsExtension colors) {
  646. final selLabel = _jbTypeLabel(_jbType);
  647. return _pickerCard(
  648. label: _l10n.get('jbType'),
  649. required: true,
  650. hasValue: true,
  651. currentLabel: selLabel,
  652. onTap: () => _showJbTypePicker(),
  653. );
  654. }
  655. void _showJbTypePicker() {
  656. FocusManager.instance.primaryFocus?.unfocus();
  657. final labels = _jbTypeOptions.map((t) => _jbTypeLabel(t)).toList();
  658. TDPicker.showMultiPicker(
  659. context,
  660. title: _l10n.get('selectJbType'),
  661. data: [labels],
  662. onConfirm: (selected) {
  663. if (selected.isNotEmpty) {
  664. final idx = selected.first is int ? selected.first as int : 0;
  665. if (idx >= 0 && idx < _jbTypeOptions.length) {
  666. setState(() => _jbType = _jbTypeOptions[idx]);
  667. }
  668. }
  669. Navigator.of(context).pop();
  670. },
  671. );
  672. }
  673. String _jbTypeLabel(String type) {
  674. switch (type) {
  675. case 'WORKING_DAY':
  676. return _l10n.get('workingDay');
  677. case 'REST_DAY':
  678. return _l10n.get('restDay');
  679. case 'PUBLIC_HOLIDAY':
  680. return _l10n.get('publicHoliday');
  681. case 'SPECIAL_HOLIDAY':
  682. return _l10n.get('specialHoliday');
  683. case 'OTHER':
  684. return _l10n.get('other');
  685. default:
  686. return type;
  687. }
  688. }
  689. String? _dayOfWeekLabel(String dateTimeStr) {
  690. final dt = DateTime.tryParse(dateTimeStr);
  691. if (dt == null) return null;
  692. switch (dt.weekday) {
  693. case 1:
  694. return _l10n.get('monday');
  695. case 2:
  696. return _l10n.get('tuesday');
  697. case 3:
  698. return _l10n.get('wednesday');
  699. case 4:
  700. return _l10n.get('thursday');
  701. case 5:
  702. return _l10n.get('friday');
  703. case 6:
  704. return _l10n.get('saturday');
  705. case 7:
  706. return _l10n.get('sunday');
  707. default:
  708. return null;
  709. }
  710. }
  711. // ── 4. 申请日期 ──
  712. Widget _buildJbDatePicker(AppColorsExtension colors) {
  713. return _datePickerCard(
  714. label: _l10n.get('applyDate'),
  715. value: _jbDate,
  716. hint: _l10n.get('pleaseSelect'),
  717. colors: colors,
  718. required: true,
  719. onPick: (d) => setState(() => _jbDate = d),
  720. onClear: _jbDate.isNotEmpty ? () => setState(() => _jbDate = '') : null,
  721. );
  722. }
  723. // ── 4. 开始时间 ──
  724. Widget _buildStartTimePicker(AppColorsExtension colors) {
  725. return _datePickerCard(
  726. label: _l10n.get('startTime'),
  727. value: _startTime,
  728. hint: _l10n.get('pleaseSelect'),
  729. colors: colors,
  730. required: true,
  731. showDayOfWeek: true,
  732. onPick: (d) {
  733. setState(() {
  734. _startTime = d;
  735. if (d.length >= 7) _attPeriod = d.substring(0, 7);
  736. _autoCalcHours();
  737. });
  738. },
  739. onClear: _startTime.isNotEmpty
  740. ? () {
  741. setState(() {
  742. _startTime = '';
  743. _autoCalcHours();
  744. });
  745. }
  746. : null,
  747. );
  748. }
  749. // ── 5. 结束时间 ──
  750. Widget _buildEndTimePicker(AppColorsExtension colors) {
  751. return _datePickerCard(
  752. label: _l10n.get('endTime'),
  753. value: _endTime,
  754. hint: _l10n.get('pleaseSelect'),
  755. colors: colors,
  756. required: true,
  757. showDayOfWeek: true,
  758. onPick: (d) {
  759. setState(() {
  760. _endTime = d;
  761. _autoCalcHours();
  762. });
  763. },
  764. onClear: _endTime.isNotEmpty
  765. ? () {
  766. setState(() {
  767. _endTime = '';
  768. _autoCalcHours();
  769. });
  770. }
  771. : null,
  772. );
  773. }
  774. void _autoCalcHours() {
  775. if (_startTime.isEmpty || _endTime.isEmpty) return;
  776. final st = DateTime.tryParse(_startTime);
  777. final et = DateTime.tryParse(_endTime);
  778. if (st == null || et == null || et.isBefore(st)) return;
  779. // 按自然日逐天累加,每天最多 8 小时
  780. double totalHours = 0;
  781. DateTime dayStart = DateTime(st.year, st.month, st.day);
  782. while (dayStart.isBefore(et)) {
  783. final dayEnd = dayStart.add(const Duration(days: 1));
  784. final segStart = st.isAfter(dayStart) ? st : dayStart;
  785. final segEnd = et.isBefore(dayEnd) ? et : dayEnd;
  786. final segHours = segEnd.difference(segStart).inMinutes / 60.0;
  787. totalHours += segHours > 8.0 ? 8.0 : segHours;
  788. dayStart = dayEnd;
  789. }
  790. if (totalHours > 0) {
  791. _hoursCtrl.text = ((totalHours * 2).ceil() / 2.0).toStringAsFixed(1);
  792. }
  793. _autoCalcDays();
  794. }
  795. void _autoCalcDays() {
  796. final hours = double.tryParse(_hoursCtrl.text) ?? 0;
  797. if (hours <= 0) return;
  798. // 8小时工作制:天数 = 小时 ÷ 8
  799. final diffDays = hours / 8.0;
  800. _daysCtrl.text = ((diffDays * 2).ceil() / 2.0).toStringAsFixed(1);
  801. }
  802. /// 手动输入时失焦自动按 0.5 向上取整
  803. void _roundToHalf(TextEditingController controller) {
  804. final text = controller.text.trim();
  805. if (text.isEmpty) return;
  806. final value = double.tryParse(text);
  807. if (value == null || value <= 0) return;
  808. final rounded = (value * 2).ceil() / 2.0;
  809. final formatted = rounded.toStringAsFixed(1);
  810. if (controller.text != formatted) {
  811. controller.text = formatted;
  812. }
  813. }
  814. // ── 6. 加班时长 ──
  815. Widget _buildHoursInput(AppColorsExtension colors) {
  816. return _inputCard(
  817. label: _l10n.get('overtimeHours'),
  818. required: true,
  819. controller: _hoursCtrl,
  820. hintText: '>0',
  821. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  822. inputFormatters: [
  823. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?[05]?$')),
  824. ],
  825. focusNode: _hoursFocus,
  826. );
  827. }
  828. // ── 7. 加班天数 ──
  829. Widget _buildDaysInput(AppColorsExtension colors) {
  830. return _inputCard(
  831. label: _l10n.get('overtimeDays'),
  832. required: false,
  833. controller: _daysCtrl,
  834. hintText: '0',
  835. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  836. inputFormatters: [
  837. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?[05]?$')),
  838. ],
  839. focusNode: _daysFocus,
  840. );
  841. }
  842. // ── 8. 考勤周期 ──
  843. Widget _buildAttPeriodInput(AppColorsExtension colors) {
  844. return _datePickerCard(
  845. label: _l10n.get('attPeriod'),
  846. value: _attPeriod,
  847. hint: _l10n.get('autoFromDate'),
  848. colors: colors,
  849. onPick: (d) => setState(() => _attPeriod = d),
  850. onClear: _attPeriod.isNotEmpty
  851. ? () => setState(() => _attPeriod = '')
  852. : null,
  853. );
  854. }
  855. // ── 10. 事由 ──
  856. Widget _buildReasonInput(AppColorsExtension colors) {
  857. final tdTheme = TDTheme.of(context);
  858. return TDTextarea(
  859. controller: _reasonCtrl,
  860. focusNode: _reasonFocus,
  861. label: _l10n.get('overtimeDetailReason'),
  862. hintText: _l10n.get('enterReason'),
  863. maxLines: 3,
  864. minLines: 1,
  865. maxLength: 1000,
  866. indicator: true,
  867. decoration: BoxDecoration(
  868. color: tdTheme.bgColorContainer,
  869. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  870. border: Border.all(color: tdTheme.componentStrokeColor),
  871. ),
  872. onChanged: (_) => setState(() {}),
  873. );
  874. }
  875. // ── 10. 补偿类型 ──
  876. Widget _buildCompensationTypeSelector(AppColorsExtension colors) {
  877. final hasValue = _compensationType.isNotEmpty;
  878. final selLabel = hasValue
  879. ? _compensationTypeLabel(_compensationType)
  880. : _l10n.get('pleaseSelect');
  881. return _pickerCard(
  882. label: _l10n.get('compensationType'),
  883. required: false,
  884. hasValue: hasValue,
  885. currentLabel: selLabel,
  886. onTap: () => _showCompensationTypePicker(),
  887. );
  888. }
  889. void _showCompensationTypePicker() {
  890. FocusManager.instance.primaryFocus?.unfocus();
  891. final labels = _compensationTypeOptions
  892. .map((t) => _compensationTypeLabel(t))
  893. .toList();
  894. TDPicker.showMultiPicker(
  895. context,
  896. title: _l10n.get('selectCompensationType'),
  897. data: [labels],
  898. onConfirm: (selected) {
  899. if (selected.isNotEmpty) {
  900. final idx = selected.first is int ? selected.first as int : 0;
  901. if (idx >= 0 && idx < _compensationTypeOptions.length) {
  902. final newType = _compensationTypeOptions[idx];
  903. setState(() {
  904. _compensationType = newType;
  905. // 无补偿 → 清空;其他 → 默认取 JB_DAYS
  906. if (newType == 'NO_COMPENSATION') {
  907. _compCountCtrl.clear();
  908. } else if (_compCountCtrl.text.isEmpty &&
  909. _daysCtrl.text.isNotEmpty) {
  910. _compCountCtrl.text = _daysCtrl.text;
  911. }
  912. });
  913. }
  914. }
  915. Navigator.of(context).pop();
  916. },
  917. );
  918. }
  919. String _compensationTypeLabel(String type) {
  920. switch (type) {
  921. case 'OVERTIME_PAY':
  922. return _l10n.get('overtimePay');
  923. case 'COMPENSATORY_LEAVE':
  924. return _l10n.get('compensatoryLeave');
  925. case 'NO_COMPENSATION':
  926. return _l10n.get('noCompensation');
  927. case 'OTHER':
  928. return _l10n.get('other');
  929. default:
  930. return type;
  931. }
  932. }
  933. // ── 11. 折算补偿次数 ──
  934. Widget _buildCompCountInput(AppColorsExtension colors) {
  935. // 无补偿时显示空
  936. if (_compensationType == 'NO_COMPENSATION') {
  937. final tdTheme = TDTheme.of(context);
  938. return GestureDetector(
  939. onTap: () {},
  940. child: Container(
  941. padding: const EdgeInsets.only(
  942. left: 16,
  943. right: 10,
  944. top: 12,
  945. bottom: 12,
  946. ),
  947. decoration: BoxDecoration(
  948. color: tdTheme.bgColorContainer,
  949. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  950. border: Border.all(color: tdTheme.componentStrokeColor),
  951. ),
  952. child: Row(
  953. children: [
  954. TDText(
  955. _l10n.get('compensationCount'),
  956. maxLines: 1,
  957. overflow: TextOverflow.visible,
  958. font: tdTheme.fontBodyLarge,
  959. fontWeight: FontWeight.w400,
  960. style: const TextStyle(letterSpacing: 0),
  961. ),
  962. const SizedBox(width: 12),
  963. Expanded(
  964. child: TDText(
  965. '',
  966. maxLines: 1,
  967. textAlign: TextAlign.end,
  968. font: tdTheme.fontBodyLarge,
  969. fontWeight: FontWeight.w400,
  970. textColor: tdTheme.textColorPlaceholder,
  971. ),
  972. ),
  973. ],
  974. ),
  975. ),
  976. );
  977. }
  978. return _inputCard(
  979. label: _l10n.get('compensationCount'),
  980. required: false,
  981. controller: _compCountCtrl,
  982. hintText: '0',
  983. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  984. inputFormatters: [
  985. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,1}$')),
  986. ],
  987. focusNode: _compCountFocus,
  988. );
  989. }
  990. // ── 12. 地点 ──
  991. Widget _buildAdrInput(AppColorsExtension colors) {
  992. final tdTheme = TDTheme.of(context);
  993. return GestureDetector(
  994. onTap: () async {
  995. FocusScope.of(context).unfocus();
  996. final result = await LocationPicker.show(context);
  997. if (result != null && mounted) {
  998. setState(() => _adrCtrl.text = result.address);
  999. }
  1000. },
  1001. child: Container(
  1002. padding: const EdgeInsets.only(
  1003. left: 16,
  1004. right: 10,
  1005. top: 12,
  1006. bottom: 12,
  1007. ),
  1008. decoration: BoxDecoration(
  1009. color: tdTheme.bgColorContainer,
  1010. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1011. border: Border.all(color: tdTheme.componentStrokeColor),
  1012. ),
  1013. child: Row(
  1014. children: [
  1015. TDText(
  1016. _l10n.get('adr'),
  1017. maxLines: 1,
  1018. overflow: TextOverflow.visible,
  1019. font: tdTheme.fontBodyLarge,
  1020. fontWeight: FontWeight.w400,
  1021. style: const TextStyle(letterSpacing: 0),
  1022. ),
  1023. const SizedBox(width: 12),
  1024. Expanded(
  1025. child: Row(
  1026. mainAxisAlignment: MainAxisAlignment.end,
  1027. children: [
  1028. Flexible(
  1029. child: TDText(
  1030. _adrCtrl.text.isNotEmpty
  1031. ? _adrCtrl.text
  1032. : _l10n.get('enterAdr'),
  1033. maxLines: 1,
  1034. overflow: TextOverflow.ellipsis,
  1035. font: tdTheme.fontBodyLarge,
  1036. fontWeight: FontWeight.w400,
  1037. textColor: _adrCtrl.text.isNotEmpty
  1038. ? tdTheme.textColorPrimary
  1039. : tdTheme.textColorPlaceholder,
  1040. textAlign: TextAlign.end,
  1041. ),
  1042. ),
  1043. const SizedBox(width: 4),
  1044. SizedBox(
  1045. width: 18,
  1046. height: 18,
  1047. child: _adrCtrl.text.isNotEmpty
  1048. ? GestureDetector(
  1049. onTap: () {
  1050. _adrCtrl.clear();
  1051. setState(() {});
  1052. },
  1053. child: Icon(
  1054. Icons.close,
  1055. size: 18,
  1056. color: tdTheme.textColorPlaceholder,
  1057. ),
  1058. )
  1059. : Icon(
  1060. Icons.location_on_outlined,
  1061. size: 20,
  1062. color: tdTheme.textColorPlaceholder,
  1063. ),
  1064. ),
  1065. ],
  1066. ),
  1067. ),
  1068. ],
  1069. ),
  1070. ),
  1071. );
  1072. }
  1073. // ── 13. 备注 ──
  1074. Widget _buildRemarkInput(AppColorsExtension colors) {
  1075. final tdTheme = TDTheme.of(context);
  1076. return TDTextarea(
  1077. controller: _remarkCtrl,
  1078. focusNode: _remarkFocus,
  1079. label: _l10n.get('remark'),
  1080. hintText: _l10n.get('enterRemark'),
  1081. maxLines: 3,
  1082. minLines: 1,
  1083. maxLength: 1000,
  1084. indicator: true,
  1085. decoration: BoxDecoration(
  1086. color: tdTheme.bgColorContainer,
  1087. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1088. border: Border.all(color: tdTheme.componentStrokeColor),
  1089. ),
  1090. onChanged: (_) => setState(() {}),
  1091. );
  1092. }
  1093. // ── 操作按钮 ──
  1094. Widget _buildActions() {
  1095. return Row(
  1096. children: [
  1097. Expanded(
  1098. child: TDButton(
  1099. text: _l10n.get('cancel'),
  1100. size: TDButtonSize.large,
  1101. type: TDButtonType.outline,
  1102. shape: TDButtonShape.rectangle,
  1103. theme: TDButtonTheme.defaultTheme,
  1104. onTap: () => Navigator.pop(context),
  1105. ),
  1106. ),
  1107. const SizedBox(width: 12),
  1108. Expanded(
  1109. child: TDButton(
  1110. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  1111. size: TDButtonSize.large,
  1112. type: TDButtonType.fill,
  1113. shape: TDButtonShape.rectangle,
  1114. theme: TDButtonTheme.primary,
  1115. onTap: _confirm,
  1116. ),
  1117. ),
  1118. ],
  1119. );
  1120. }
  1121. // ── 日期选择器卡片 ──
  1122. Widget _datePickerCard({
  1123. required String label,
  1124. required String value,
  1125. required String hint,
  1126. required AppColorsExtension colors,
  1127. required ValueChanged<String> onPick,
  1128. VoidCallback? onClear,
  1129. bool required = false,
  1130. bool showDayOfWeek = false,
  1131. }) {
  1132. final tdTheme = TDTheme.of(context);
  1133. final hasValue = value.isNotEmpty;
  1134. final dayOfWeek = showDayOfWeek && hasValue ? _dayOfWeekLabel(value) : null;
  1135. return GestureDetector(
  1136. onTap: () => _pickDateTime(label, onPick, currentValue: value),
  1137. child: Container(
  1138. padding: const EdgeInsets.only(
  1139. left: 16,
  1140. right: 10,
  1141. top: 12,
  1142. bottom: 12,
  1143. ),
  1144. decoration: BoxDecoration(
  1145. color: tdTheme.bgColorContainer,
  1146. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1147. border: Border.all(color: tdTheme.componentStrokeColor),
  1148. ),
  1149. child: Row(
  1150. children: [
  1151. TDText(
  1152. label,
  1153. maxLines: 1,
  1154. overflow: TextOverflow.visible,
  1155. font: tdTheme.fontBodyLarge,
  1156. fontWeight: FontWeight.w400,
  1157. style: const TextStyle(letterSpacing: 0),
  1158. ),
  1159. if (required)
  1160. Padding(
  1161. padding: const EdgeInsets.only(left: 4),
  1162. child: TDText(
  1163. '*',
  1164. font: tdTheme.fontBodyLarge,
  1165. fontWeight: FontWeight.w400,
  1166. style: TextStyle(color: tdTheme.errorColor6),
  1167. ),
  1168. ),
  1169. const SizedBox(width: 12),
  1170. Expanded(
  1171. child: Row(
  1172. mainAxisAlignment: MainAxisAlignment.end,
  1173. mainAxisSize: MainAxisSize.max,
  1174. children: [
  1175. Flexible(
  1176. child: TDText(
  1177. value.isEmpty ? hint : value,
  1178. maxLines: 1,
  1179. overflow: TextOverflow.ellipsis,
  1180. font: tdTheme.fontBodyLarge,
  1181. fontWeight: FontWeight.w400,
  1182. textColor: value.isEmpty
  1183. ? tdTheme.textColorPlaceholder
  1184. : tdTheme.textColorPrimary,
  1185. textAlign: TextAlign.end,
  1186. ),
  1187. ),
  1188. if (dayOfWeek != null) ...[
  1189. const SizedBox(width: 6),
  1190. Container(
  1191. padding: const EdgeInsets.symmetric(
  1192. horizontal: 6,
  1193. vertical: 2,
  1194. ),
  1195. decoration: BoxDecoration(
  1196. color: tdTheme.brandColor1,
  1197. borderRadius: BorderRadius.circular(4),
  1198. ),
  1199. child: TDText(
  1200. dayOfWeek,
  1201. font: TDTheme.of(context).fontBodySmall,
  1202. fontWeight: FontWeight.w500,
  1203. textColor: tdTheme.brandColor7,
  1204. ),
  1205. ),
  1206. ],
  1207. const SizedBox(width: 4),
  1208. SizedBox(
  1209. width: 18,
  1210. height: 18,
  1211. child: hasValue
  1212. ? GestureDetector(
  1213. onTap: onClear,
  1214. child: Icon(
  1215. Icons.close,
  1216. size: 18,
  1217. color: tdTheme.textColorPlaceholder,
  1218. ),
  1219. )
  1220. : Icon(
  1221. Icons.chevron_right,
  1222. size: 18,
  1223. color: tdTheme.textColorPlaceholder,
  1224. ),
  1225. ),
  1226. ],
  1227. ),
  1228. ),
  1229. ],
  1230. ),
  1231. ),
  1232. );
  1233. }
  1234. void _pickDateTime(
  1235. String label,
  1236. ValueChanged<String> onPick, {
  1237. String currentValue = '',
  1238. }) {
  1239. final l10n = _l10n;
  1240. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  1241. final now = DateTime.now();
  1242. final isDateOnly =
  1243. label == _l10n.get('applyDate') || label == _l10n.get('attPeriod');
  1244. final isAttPeriod = label == _l10n.get('attPeriod');
  1245. final isStartTime = label == _l10n.get('startTime');
  1246. final isEndTime = label == _l10n.get('endTime');
  1247. // 根据当前值定位初始日期,无值时默认当月/当天
  1248. int initYear = now.year;
  1249. int initMonth = now.month;
  1250. int initDay = now.day;
  1251. int initHour = now.hour;
  1252. int initMinute = now.minute;
  1253. if (currentValue.isNotEmpty) {
  1254. if (isAttPeriod) {
  1255. // yyyy-MM
  1256. final parts = currentValue.split('-');
  1257. if (parts.length == 2) {
  1258. initYear = int.tryParse(parts[0]) ?? now.year;
  1259. initMonth = int.tryParse(parts[1]) ?? now.month;
  1260. initDay = 1;
  1261. }
  1262. } else {
  1263. // yyyy-MM-dd 或 yyyy-MM-dd HH:mm
  1264. final dt = DateTime.tryParse(currentValue);
  1265. if (dt != null) {
  1266. initYear = dt.year;
  1267. initMonth = dt.month;
  1268. initDay = dt.day;
  1269. initHour = dt.hour;
  1270. initMinute = dt.minute;
  1271. }
  1272. }
  1273. } else if (isStartTime) {
  1274. // 开始时间无当前值:工作日默认 18:00,周末默认 09:00
  1275. final date = _jbDate.isNotEmpty ? DateTime.tryParse(_jbDate) : null;
  1276. final weekday = date?.weekday ?? now.weekday;
  1277. initHour = (weekday <= DateTime.friday) ? 18 : 9;
  1278. initMinute = 0;
  1279. } else if (isEndTime) {
  1280. // 结束时间无当前值:工作日默认 19:00,周末默认 18:00
  1281. final date = _jbDate.isNotEmpty ? DateTime.tryParse(_jbDate) : null;
  1282. final weekday = date?.weekday ?? now.weekday;
  1283. initHour = (weekday <= DateTime.friday) ? 19 : 18;
  1284. initMinute = 0;
  1285. }
  1286. final useTime = !isDateOnly;
  1287. FocusManager.instance.primaryFocus?.unfocus();
  1288. TDPicker.showDatePicker(
  1289. context,
  1290. title: l10n.get('selectDate'),
  1291. backgroundColor: colors.bgCard,
  1292. useYear: true,
  1293. useMonth: true,
  1294. useDay: !isAttPeriod,
  1295. useHour: useTime,
  1296. useMinute: useTime,
  1297. useSecond: false,
  1298. useWeekDay: false,
  1299. dateStart: useTime ? const [2020, 1, 1, 0, 0] : const [2020, 1, 1],
  1300. dateEnd: useTime
  1301. ? [now.year + 1, 12, 31, 23, 59]
  1302. : [now.year + 1, 12, 31],
  1303. initialDate: useTime
  1304. ? [initYear, initMonth, initDay, initHour, initMinute]
  1305. : [initYear, initMonth, initDay],
  1306. onConfirm: (selected) {
  1307. final year = selected['year'];
  1308. final month = selected['month'].toString().padLeft(2, '0');
  1309. final day = selected['day'].toString().padLeft(2, '0');
  1310. final hour = selected['hour']?.toString().padLeft(2, '0') ?? '00';
  1311. final minute = selected['minute']?.toString().padLeft(2, '0') ?? '00';
  1312. if (isAttPeriod) {
  1313. onPick('$year-$month');
  1314. } else if (isDateOnly) {
  1315. onPick('$year-$month-$day');
  1316. } else {
  1317. onPick('$year-$month-$day $hour:$minute');
  1318. }
  1319. Navigator.of(context).pop();
  1320. },
  1321. );
  1322. }
  1323. // ── 通用输入卡片 ──
  1324. Widget _inputCard({
  1325. required String label,
  1326. required bool required,
  1327. required TextEditingController controller,
  1328. required String hintText,
  1329. TextInputType? keyboardType,
  1330. List<TextInputFormatter>? inputFormatters,
  1331. FocusNode? focusNode,
  1332. }) {
  1333. final tdTheme = TDTheme.of(context);
  1334. final hasValue = controller.text.isNotEmpty;
  1335. return Container(
  1336. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  1337. decoration: BoxDecoration(
  1338. color: tdTheme.bgColorContainer,
  1339. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1340. border: Border.all(color: tdTheme.componentStrokeColor),
  1341. ),
  1342. child: Row(
  1343. children: [
  1344. TDText(
  1345. label,
  1346. maxLines: 1,
  1347. overflow: TextOverflow.visible,
  1348. font: tdTheme.fontBodyLarge,
  1349. fontWeight: FontWeight.w400,
  1350. style: const TextStyle(letterSpacing: 0),
  1351. ),
  1352. if (required)
  1353. Padding(
  1354. padding: const EdgeInsets.only(left: 4),
  1355. child: TDText(
  1356. '*',
  1357. font: tdTheme.fontBodyLarge,
  1358. fontWeight: FontWeight.w400,
  1359. style: TextStyle(color: tdTheme.errorColor6),
  1360. ),
  1361. ),
  1362. const SizedBox(width: 12),
  1363. Expanded(
  1364. child: Row(
  1365. mainAxisAlignment: MainAxisAlignment.end,
  1366. mainAxisSize: MainAxisSize.max,
  1367. children: [
  1368. Flexible(
  1369. child: TextField(
  1370. controller: controller,
  1371. focusNode: focusNode,
  1372. textAlign: TextAlign.end,
  1373. keyboardType: keyboardType,
  1374. inputFormatters: inputFormatters,
  1375. style: TextStyle(
  1376. fontSize: 16,
  1377. color: tdTheme.textColorPrimary,
  1378. ),
  1379. decoration: InputDecoration(
  1380. hintText: hintText,
  1381. hintStyle: TextStyle(
  1382. fontSize: 16,
  1383. color: tdTheme.textColorPlaceholder,
  1384. ),
  1385. border: InputBorder.none,
  1386. isDense: true,
  1387. contentPadding: EdgeInsets.zero,
  1388. ),
  1389. onChanged: (_) => setState(() {}),
  1390. ),
  1391. ),
  1392. const SizedBox(width: 4),
  1393. SizedBox(
  1394. width: 18,
  1395. height: 18,
  1396. child: hasValue
  1397. ? GestureDetector(
  1398. onTap: () {
  1399. controller.clear();
  1400. setState(() {});
  1401. },
  1402. child: Icon(
  1403. Icons.close,
  1404. size: 18,
  1405. color: tdTheme.textColorPlaceholder,
  1406. ),
  1407. )
  1408. : null,
  1409. ),
  1410. ],
  1411. ),
  1412. ),
  1413. ],
  1414. ),
  1415. );
  1416. }
  1417. }