import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:qhd_prevention/pages/my_appbar.dart'; import '../../http/ApiService.dart'; import '../../tools/tools.dart'; class MineDutyApplicationPage extends StatefulWidget { const MineDutyApplicationPage({super.key,required this.onClose}); final Function(String) onClose; // 回调函数 @override State createState() => _MineDutyApplicationPage(); } class _MineDutyApplicationPage extends State { DateTime? _startDate; DateTime? _endDate; final TextEditingController _reasonController = TextEditingController(); Future _submitApplicationLeaving() async { try { var formatter = DateFormat('yyyy-MM-dd'); // 或者使用 'dd-MM-yyyy' 取决于你的需求 String startTime= formatter.format(_startDate!); String endTime= formatter.format(_endDate!); final result = await ApiService.submitApplicationLeaving(startTime,endTime,_reasonController.text); if (result['result'] == 'success') { ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('提交成功'))); widget.onClose('关闭提交'); // 触发回调 Navigator.pop(context); // 关闭页面 } } catch (e) { print('加载出错: $e'); } } Future _selectDate(BuildContext context, bool isStartDate) async { final DateTime? picked = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(2000), lastDate: DateTime(2100), builder: (BuildContext context, Widget? child) { return Theme( data: ThemeData.light().copyWith( colorScheme: const ColorScheme.light( primary: Color(0xFF1976D2), // buttonTheme: ButtonThemeData( // textTheme: ButtonTextTheme.primary, // ), ), ), child: child!, ); }, ); if (picked != null) { setState(() { if (isStartDate) { if(picked.isBefore(DateTime.now().subtract(Duration(days: 1)))){ ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('开始日期不能早于当前日期'))); }else { _startDate = picked; // 如果结束日期早于开始日期,自动更新结束日期 if (_endDate == null || _endDate!.isBefore(picked)) { _endDate = picked; } } } else { if(picked.isBefore(DateTime.now().subtract(Duration(days: 1)))){ ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('结束日期不能早于当前日期'))); }else { // 确保结束日期不早于开始日期 if (_startDate != null && picked.isBefore(_startDate!)) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('结束日期不能早于开始日期')) ); } else { _endDate = picked; } } } }); } } void _submitApplication() { if (_startDate == null || _endDate == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('请选择离岗时间')) ); return; } if (_reasonController.text.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('请输入离岗原因')) ); return; } // 提交申请逻辑 showDialog( context: context, builder: (context) => AlertDialog( title: const Text('申请提交'), content: const Text('您确定提交离岗申请吗?'), actions: [ TextButton( onPressed: () { Navigator.pop(context); // 关闭对话框 }, child: const Text("取消"), ), TextButton( onPressed: () { Navigator.pop(context); // 关闭对话框 // Navigator.pop(context); // 返回上一页 _submitApplicationLeaving(); }, child: const Text("确定"), ), ], ), ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFFF5F7FA), appBar:MyAppbar(title: "离岗申请"), body: SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // 离岗开始时间 _buildDateField( label: "离岗开始时间", date: _startDate, onTap: () => _selectDate(context, true), ), const SizedBox(height: 16), // 离岗结束时间 _buildDateField( label: "离岗结束时间", date: _endDate, onTap: () => _selectDate(context, false), ), const SizedBox(height: 24), // 离岗原因标题 const Text( "离岗原因", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500), ), const SizedBox(height: 8), // 原因输入框 Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), spreadRadius: 1, blurRadius: 6, offset: const Offset(0, 2), ), ], ), child: TextField( controller: _reasonController, maxLines: 5, decoration: const InputDecoration( hintText: "请输入离岗原因", hintStyle: TextStyle(color: Color(0xFF9E9E9E)), border: InputBorder.none, ), ), ), const SizedBox(height: 24), // 申请人信息 // _buildInfoRow("申请人", _applicant), _buildInfoRow("申请人", SessionService.instance.username.toString()), const SizedBox(height: 40), // 提交按钮 SizedBox( width: double.infinity, child: TextButton( onPressed: _submitApplication, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF1976D2), padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: Text( "提交", style: TextStyle(fontSize: 16, color: Colors.white), ), ), ), ], ), ), ); } Widget _buildDateField({ required String label, required DateTime? date, required VoidCallback onTap, }) { return GestureDetector( onTap: onTap, child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), spreadRadius: 1, blurRadius: 6, offset: const Offset(0, 2), ), ], ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( label, style: const TextStyle(fontSize: 16), ), Text( date != null ? DateFormat('yyyy-MM-dd').format(date) : "请选择日期", style: TextStyle( fontSize: 16, color: date != null ? Colors.black : const Color(0xFF9E9E9E), ), ), ], ), ), ); } Widget _buildInfoRow(String title, String value) { return Container( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), spreadRadius: 1, blurRadius: 6, offset: const Offset(0, 2), ), ], ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( title, style: const TextStyle(fontSize: 16), ), Text( value, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500), ), ], ), ); } }