overtime_apply_detail_dialog.dart 48 KB

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