import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:tdesign_flutter/tdesign_flutter.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 = ['客户接待', '商务出行', '内部公务', '其他']; static const _driverTypes = ['自驾', '配驾']; @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: TDNavBar( title: widget.editId != null ? '编辑用车' : '用车申请', titleColor: Colors.white, backgroundColor: const Color(0xFF00ABF3), centerTitle: false, ), body: Column( children: [ Expanded( child: Align(alignment: Alignment.topCenter, child: ConstrainedBox( constraints: BoxConstraints(maxWidth: r.formMaxWidth), child: SingleChildScrollView( padding: const EdgeInsets.symmetric(vertical: 8), child: Column(children: [ FormSection( title: '基本信息', children: [ FormFieldRow(label: '车辆类型', value: state.vehicle.vehicleType, onTap: () => _showPicker(_vehicleTypes, ctrl.updateVehicleType, title: '选择车辆类型')), FormFieldRow(label: '用车目的', value: state.vehicle.purpose, hint: '请选择', onTap: () => _showPicker(_purposes, ctrl.updatePurpose, title: '选择用车目的')), 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: () => _showPicker(_driverTypes, ctrl.updateDriver, title: '选择驾驶员')), FormFieldRow(label: '预估里程', value: state.vehicle.estimatedMileage > 0 ? '${state.vehicle.estimatedMileage}公里' : null, hint: '请输入', onTap: () => _showMileageInput('预估里程', ctrl.updateEstimatedMileage, state.vehicle.estimatedMileage)), FormFieldRow(label: '用车事由', value: state.vehicle.reason, hint: '请输入事由', onTap: () => _showInput('用车事由', ctrl.updateReason)), ], ), FormSection( title: '时间', children: [ 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)), ], ), ]), ), ), ), ), _buildBottomButtons(ctrl, state), ], ), ); } void _showInput(String title, void Function(String) onSave) { final ctrl = TextEditingController(); showDialog( context: context, builder: (_) => TDAlertDialog( title: title, contentWidget: TextField(controller: ctrl, decoration: InputDecoration(hintText: '请输入$title', border: const OutlineInputBorder())), leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)), rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(ctrl.text); Navigator.pop(context); }), ), ); } void _showNumberInput(String title, void Function(int) onSave, int current) { final ctrl = TextEditingController(text: '$current'); showDialog( context: context, builder: (_) => TDAlertDialog( title: title, contentWidget: TextField(controller: ctrl, keyboardType: TextInputType.number, decoration: const InputDecoration(border: OutlineInputBorder())), leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)), rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(int.tryParse(ctrl.text) ?? 1); Navigator.pop(context); }), ), ); } void _showMileageInput(String title, void Function(double) onSave, double current) { final ctrl = TextEditingController(text: current > 0 ? '$current' : ''); showDialog( context: context, builder: (_) => TDAlertDialog( title: title, contentWidget: TextField(controller: ctrl, keyboardType: TextInputType.number, decoration: const InputDecoration(border: OutlineInputBorder())), leftBtn: TDDialogButtonOptions(title: '取消', action: () => Navigator.pop(context)), rightBtn: TDDialogButtonOptions(title: '确定', theme: TDButtonTheme.primary, action: () { onSave(double.tryParse(ctrl.text) ?? 0); Navigator.pop(context); }), ), ); } void _showPicker(List options, void Function(String) onPick, {String title = '请选择'}) { TDPicker.showMultiPicker( context, title: title, data: [options], onConfirm: (selected) { onPick(selected.first); }, ); } void _pickDateTime(void Function(DateTime) onPicked, DateTime initial) { TDPicker.showDatePicker( context, title: '选择日期时间', useYear: true, useMonth: true, useDay: true, useHour: true, useMinute: true, initialDate: [initial.year, initial.month, initial.day, initial.hour, initial.minute], onConfirm: (selected) { onPicked(DateTime(selected['year']!, selected['month']!, selected['day']!, selected['hour']!, selected['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: TDButton( text: '存草稿', type: TDButtonType.outline, isBlock: true, disabled: state.isSubmitting, onTap: state.isSubmitting ? null : () async { await ctrl.saveDraft(); if (context.mounted) context.pop(); }, )), const SizedBox(width: 12), Expanded(flex: 2, child: TDButton( text: '提交审批', type: TDButtonType.fill, theme: TDButtonTheme.primary, isBlock: true, disabled: state.isSubmitting, onTap: state.isSubmitting ? null : () async { final ok = await ctrl.submit(); if (context.mounted && ok) context.pop(); }, )), ], ), ); } }