import 'package:flutter/material.dart'; import './leave_model.dart'; import './leave_api.dart'; import 'package:intl/intl.dart'; class LeaveForm extends StatefulWidget { final String userId; const LeaveForm({required this.userId, super.key}); @override State createState() => _LeaveFormState(); } class _LeaveFormState extends State { final _formKey = GlobalKey(); String _selectedType = '事假'; String _agentId = ''; String _note = ''; DateTime _start = DateTime.now(); DateTime _end = DateTime.now().add(const Duration(hours: 8)); final List _types = ['事假', '病假', '特休', '婚假', '喪假']; void _submit() async { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); final newLeave = Leave( billNo: '', // API 端生成 personId: widget.userId, agentId: _agentId, leaveType: _selectedType, startTime: _start, endTime: _end, days: 1.0, // 簡化處理,實際可依 start/end 計算 hours: 8.0, leaveNote: _note, ); final success = await LeaveApiService().createLeave(newLeave); if (success && mounted) { Navigator.pop(context, true); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('新增請假申請')), body: Form( key: _formKey, child: ListView( padding: const EdgeInsets.all(16), children: [ DropdownButtonFormField( value: _selectedType, decoration: const InputDecoration(labelText: '請假類別'), items: _types.map((t) => DropdownMenuItem(value: t, child: Text(t))).toList(), onChanged: (v) => setState(() => _selectedType = v!), ), const SizedBox(height: 16), TextFormField( decoration: const InputDecoration(labelText: '代理人工號'), validator: (v) => v!.isEmpty ? '必填' : null, onSaved: (v) => _agentId = v!, ), const SizedBox(height: 16), ListTile( title: const Text('開始時間'), subtitle: Text(DateFormat('yyyy/MM/dd HH:mm').format(_start)), trailing: const Icon(Icons.calendar_today), onTap: () async { // 這裡簡化,實務上可串接 showDatePicker + showTimePicker }, ), const SizedBox(height: 16), TextFormField( decoration: const InputDecoration(labelText: '事由說明'), maxLines: 3, onSaved: (v) => _note = v!, ), const SizedBox(height: 30), ElevatedButton( onPressed: _submit, style: ElevatedButton.styleFrom(minimumSize: const Size(double.infinity, 50)), child: const Text('提交申請'), ), ], ), ), ); } }