overtime_apply_detail_dialog.dart 46 KB

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