import 'package:flutter/material.dart'; import '../../core/theme/app_colors.dart'; import '../models/user_model.dart'; class ApprovalActions extends StatefulWidget { final String status; final UserRole userRole; final VoidCallback onApprove; final VoidCallback onReject; final VoidCallback? onEdit; final VoidCallback? onWithdraw; final bool isSubmitting; const ApprovalActions({ super.key, required this.status, required this.userRole, required this.onApprove, required this.onReject, this.onEdit, this.onWithdraw, this.isSubmitting = false, }); @override State createState() => _ApprovalActionsState(); } class _ApprovalActionsState extends State { final _opinionCtrl = TextEditingController(); Future _showOpinionDialog(String action) async { final result = await showDialog( context: context, builder: (ctx) => AlertDialog( title: Text(action == 'approve' ? '确认通过' : '确认拒绝'), content: Column( mainAxisSize: MainAxisSize.min, children: [ Text('确定要${action == 'approve' ? '通过' : '拒绝'}该申请吗?'), const SizedBox(height: 12), TextField( controller: _opinionCtrl, maxLines: 3, decoration: InputDecoration( hintText: '审批意见(选填)', border: OutlineInputBorder( borderRadius: BorderRadius.circular(8)), ), ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('取消'), ), TextButton( onPressed: () => Navigator.pop(ctx, true), child: Text('确定', style: TextStyle( color: action == 'approve' ? AppColors.primary : AppColors.error)), ), ], ), ); if (result == true) { if (action == 'approve') { widget.onApprove(); } else { widget.onReject(); } } } @override Widget build(BuildContext context) { final isPending = widget.status == 'pending'; final isDraft = widget.status == 'draft'; final canApprove = isPending && (widget.userRole == UserRole.approver || widget.userRole == UserRole.finance || widget.userRole == UserRole.admin); 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: canApprove ? Row(children: [ Expanded( child: OutlinedButton( onPressed: widget.isSubmitting ? null : () => _showOpinionDialog('reject'), style: OutlinedButton.styleFrom( foregroundColor: AppColors.error, side: const BorderSide(color: AppColors.error), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8)), minimumSize: const Size(double.infinity, 48), ), child: const Text('拒绝'), ), ), const SizedBox(width: 12), Expanded( flex: 2, child: ElevatedButton( onPressed: widget.isSubmitting ? null : () => _showOpinionDialog('approve'), style: ElevatedButton.styleFrom( backgroundColor: AppColors.primary, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8)), minimumSize: const Size(double.infinity, 48), ), child: widget.isSubmitting ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white)) : const Text('通过'), ), ), ]) : Row(children: [ if (isDraft && widget.onEdit != null) ...[ Expanded( child: OutlinedButton( onPressed: widget.onEdit, child: const Text('编辑'), ), ), const SizedBox(width: 12), ], if (isPending && widget.onWithdraw != null) Expanded( child: OutlinedButton( onPressed: widget.onWithdraw, style: OutlinedButton.styleFrom( foregroundColor: AppColors.warning, side: const BorderSide(color: AppColors.warning), ), child: const Text('撤回'), ), ), ]), ); } @override void dispose() { _opinionCtrl.dispose(); super.dispose(); } }