import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../core/utils/date_utils.dart' as du; import '../../core/utils/responsive.dart'; import '../../shared/widgets/form_section.dart'; import '../../shared/widgets/form_field_row.dart'; import 'vehicle_apply_controller.dart'; class VehicleApplyPage extends ConsumerStatefulWidget { final String? editId; const VehicleApplyPage({super.key, this.editId}); @override ConsumerState createState() => _VehicleApplyPageState(); } class _VehicleApplyPageState extends ConsumerState { static const _vehicleTypes = ['轿车', '商务车', '中巴', '大巴', '货车']; static const _purposes = ['客户接待', '商务出行', '货物运输', '其他']; @override Widget build(BuildContext context) { final ctrl = ref.watch(vehicleApplyProvider(widget.editId).notifier); final state = ref.watch(vehicleApplyProvider(widget.editId)); final r = ResponsiveHelper.of(context); return Scaffold( appBar: AppBar(title: Text(widget.editId != null ? '编辑用车' : '用车申请')), body: Column( children: [ Expanded( child: Center( child: ConstrainedBox( constraints: BoxConstraints(maxWidth: r.formMaxWidth), child: SingleChildScrollView( padding: const EdgeInsets.symmetric(vertical: 8), child: FormSection( title: '用车信息', children: [ FormFieldRow(label: '用车目的', value: state.vehicle.purpose, hint: '请选择', onTap: () => _showPicker(_purposes, ctrl.updatePurpose)), FormFieldRow(label: '车型', value: state.vehicle.vehicleType, onTap: () => _showPicker(_vehicleTypes, ctrl.updateVehicleType)), FormFieldRow(label: '开始时间', value: du.DateUtils.formatDateTime(state.vehicle.startTime), onTap: () => _pickDateTime(ctrl.updateStartTime, state.vehicle.startTime)), FormFieldRow(label: '结束时间', value: du.DateUtils.formatDateTime(state.vehicle.endTime), onTap: () => _pickDateTime(ctrl.updateEndTime, state.vehicle.endTime)), FormFieldRow(label: '出发地', value: state.vehicle.origin, hint: '请输入', onTap: () => _showInput('出发地', ctrl.updateOrigin)), FormFieldRow(label: '目的地', value: state.vehicle.destination, hint: '请输入', onTap: () => _showInput('目的地', ctrl.updateDestination)), FormFieldRow(label: '乘车人数', value: '${state.vehicle.passengerCount}人', onTap: () => _showNumberInput('乘车人数', ctrl.updatePassengerCount, state.vehicle.passengerCount)), FormFieldRow(label: '驾驶员', value: state.vehicle.driver, hint: '请输入', onTap: () => _showInput('驾驶员', ctrl.updateDriver)), FormFieldRow(label: '用车事由', value: state.vehicle.reason, hint: '请输入事由', onTap: () => _showInput('用车事由', ctrl.updateReason)), ], ), ), ), ), ), _buildBottomButtons(ctrl, state), ], ), ); } void _showInput(String title, void Function(String) onSave) { final ctrl = TextEditingController(); showDialog( context: context, builder: (_) => AlertDialog( title: Text(title), content: TextField(controller: ctrl, decoration: InputDecoration(hintText: '请输入$title', border: const OutlineInputBorder())), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')), TextButton(onPressed: () { onSave(ctrl.text); Navigator.pop(context); }, child: const Text('确定')), ], ), ); } void _showNumberInput(String title, void Function(int) onSave, int current) { final ctrl = TextEditingController(text: '$current'); showDialog( context: context, builder: (_) => AlertDialog( title: Text(title), content: TextField(controller: ctrl, keyboardType: TextInputType.number, decoration: const InputDecoration(border: OutlineInputBorder())), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')), TextButton(onPressed: () { onSave(int.tryParse(ctrl.text) ?? 1); Navigator.pop(context); }, child: const Text('确定')), ], ), ); } void _showPicker(List options, void Function(String) onPick) { showModalBottomSheet( context: context, builder: (_) => Column( mainAxisSize: MainAxisSize.min, children: options.map((t) => ListTile(title: Text(t), onTap: () { onPick(t); Navigator.pop(context); })).toList(), ), ); } Future _pickDateTime(void Function(DateTime) onPicked, DateTime initial) async { final date = await showDatePicker(context: context, initialDate: initial, firstDate: DateTime(2020), lastDate: DateTime(2030)); if (date == null || !context.mounted) return; final time = await showTimePicker(context: context, initialTime: TimeOfDay.fromDateTime(initial)); if (time == null) return; onPicked(DateTime(date.year, date.month, date.day, time.hour, time.minute)); } Widget _buildBottomButtons(VehicleApplyController ctrl, VehicleApplyState state) { return Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration(color: Colors.white, boxShadow: [BoxShadow(color: Colors.black.withValues(alpha: 0.05), blurRadius: 4, offset: const Offset(0, -1))]), child: Row( children: [ Expanded(child: OutlinedButton( onPressed: state.isSubmitting ? null : () async { await ctrl.saveDraft(); if (context.mounted) context.pop(); }, child: const Text('存草稿'), )), const SizedBox(width: 12), Expanded(flex: 2, child: ElevatedButton( onPressed: state.isSubmitting ? null : () async { final ok = await ctrl.submit(); if (context.mounted && ok) context.pop(); }, child: state.isSubmitting ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white)) : const Text('提交申请'), )), ], ), ); } }