overtime_apply_detail_dialog.dart 48 KB

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