overtime_apply_detail_dialog.dart 48 KB

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