Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixUI/hesabix_ui/lib/pages/profile/create_ticket_page.dart

713 lines
22 KiB
Dart
Executable file

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hesabix_ui/l10n/app_localizations.dart';
import 'package:hesabix_ui/core/api_client.dart';
import 'package:hesabix_ui/services/support_service.dart';
import 'package:hesabix_ui/services/support_tickets_public_config.dart';
import 'package:hesabix_ui/models/support_models.dart';
import '../../utils/error_extractor.dart';
import '../../utils/responsive_helper.dart';
import 'package:hesabix_ui/theme/semantic_color_resolver.dart';
class CreateTicketPage extends StatefulWidget {
final bool fullPage;
const CreateTicketPage({super.key, this.fullPage = false});
@override
State<CreateTicketPage> createState() => _CreateTicketPageState();
}
class _CreateTicketPageState extends State<CreateTicketPage> {
final _formKey = GlobalKey<FormState>();
final _titleController = TextEditingController();
final _descriptionController = TextEditingController();
final SupportService _supportService = SupportService(ApiClient());
List<SupportCategory> _categories = [];
List<SupportPriority> _priorities = [];
SupportCategory? _selectedCategory;
SupportPriority? _selectedPriority;
bool _isLoading = false;
bool _isSubmitting = false;
String? _error;
bool _supportBlocked = false;
String _supportBlockedMessage = '';
@override
void initState() {
super.initState();
_loadData();
}
@override
void dispose() {
_titleController.dispose();
_descriptionController.dispose();
super.dispose();
}
void _showOverlayMessage(String message, Color backgroundColor, Duration duration) {
final overlay = Overlay.of(context);
late OverlayEntry overlayEntry;
overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: MediaQuery.of(context).padding.top + 20,
left: 16,
right: 16,
child: Material(
color: Colors.transparent,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: backgroundColor,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.2),
blurRadius: 8,
offset: const Offset(0, 4),
),
],
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
backgroundColor == SemanticColorResolver.positive(context) ? Icons.check_circle : Icons.error,
color: Colors.white,
size: 20,
),
const SizedBox(width: 8),
Expanded(
child: Text(
message,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
),
),
);
overlay.insert(overlayEntry);
// Remove overlay after duration
Future.delayed(duration, () {
overlayEntry.remove();
});
}
Future<void> _loadData() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final cfg = await SupportTicketsPublicConfig.fetch(ApiClient());
if (!mounted) return;
if (!cfg.enabledForUsers) {
setState(() {
_supportBlocked = true;
_supportBlockedMessage = cfg.disabledMessage.trim();
_isLoading = false;
});
return;
}
final categories = await _supportService.getCategories();
final priorities = await _supportService.getPriorities();
setState(() {
_categories = categories;
_priorities = priorities;
_isLoading = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_error = ErrorExtractor.forContext(e, context);
_isLoading = false;
});
}
}
Future<void> _submitTicket() async {
if (!_formKey.currentState!.validate()) return;
if (_selectedCategory == null || _selectedPriority == null) {
final t = AppLocalizations.of(context);
_showOverlayMessage(
t.pleaseSelectCategoryAndPriority,
SemanticColorResolver.negative(context),
const Duration(seconds: 3),
);
return;
}
setState(() {
_isSubmitting = true;
_error = null;
});
try {
final request = CreateTicketRequest(
title: _titleController.text.trim(),
description: _descriptionController.text.trim(),
categoryId: _selectedCategory!.id,
priorityId: _selectedPriority!.id,
);
await _supportService.createTicket(request);
if (mounted) {
final t = AppLocalizations.of(context);
_showOverlayMessage(
t.ticketCreatedSuccessfully,
SemanticColorResolver.positive(context),
const Duration(seconds: 2),
);
Navigator.pop(context, true);
}
} catch (e) {
if (!mounted) return;
setState(() {
_error = ErrorExtractor.forContext(e, context);
_isSubmitting = false;
});
}
}
Widget _buildHeader(ThemeData theme, AppLocalizations t) {
return Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
theme.colorScheme.primary.withValues(alpha: 0.1),
theme.colorScheme.primary.withValues(alpha: 0.05),
],
),
borderRadius: widget.fullPage
? null
: const BorderRadius.only(
topLeft: Radius.circular(16),
topRight: Radius.circular(16),
),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: theme.colorScheme.primary,
borderRadius: BorderRadius.circular(8),
),
child: const Icon(Icons.support_agent, color: Colors.white, size: 24),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.createNewTicket,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.primary,
),
),
const SizedBox(height: 4),
Text(
t.descriptionHint,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurface.withValues(alpha: 0.7),
),
),
],
),
),
if (!widget.fullPage)
IconButton(
onPressed: () => context.pop(),
icon: const Icon(Icons.close),
style: IconButton.styleFrom(
backgroundColor: theme.colorScheme.surface,
foregroundColor: theme.colorScheme.onSurface,
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final t = AppLocalizations.of(context);
final isMobile = ResponsiveHelper.isMobile(context);
final content = Column(
children: [
if (!widget.fullPage) _buildHeader(theme, t),
Expanded(
child: _buildBody(theme, !isMobile, t, isMobile),
),
],
);
if (widget.fullPage) {
return Scaffold(
appBar: AppBar(
title: Text(t.createNewTicket),
),
body: content,
);
}
return Dialog(
insetPadding: ResponsiveHelper.getDialogPadding(context),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
constraints: ResponsiveHelper.getDialogConstraints(context),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildHeader(theme, t),
Flexible(child: _buildBody(theme, !isMobile, t, isMobile)),
],
),
),
);
}
Widget _buildBody(ThemeData theme, bool isDesktop, AppLocalizations t, bool isMobile) {
if (_isLoading) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(
color: theme.colorScheme.primary,
),
const SizedBox(height: 16),
Text(
t.loadingData,
style: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.onSurface.withValues(alpha: 0.7),
),
),
],
),
);
}
if (_supportBlocked) {
final msg =
_supportBlockedMessage.isEmpty ? t.supportTicketsUnavailableBody : _supportBlockedMessage;
return Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: SelectableText(
msg,
textAlign: TextAlign.center,
style: theme.textTheme.bodyLarge?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
height: 1.35,
),
),
),
);
}
if (_error != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: SemanticColorResolver.negative(context).withValues(alpha: 0.1),
shape: BoxShape.circle,
),
child: Icon(
Icons.error_outline,
size: 48,
color: SemanticColorResolver.negative(context).withValues(alpha: 0.8),
),
),
SizedBox(height: 24),
Text(
t.dataLoadingError,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
_error!,
style: theme.textTheme.bodyMedium?.copyWith(
color: SemanticColorResolver.negative(context),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _loadData,
icon: const Icon(Icons.refresh),
label: Text(t.retry),
style: ElevatedButton.styleFrom(
backgroundColor: theme.colorScheme.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
),
),
],
),
),
);
}
return SingleChildScrollView(
padding: EdgeInsets.all(isDesktop ? 24.0 : 16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Form Fields
if (!isMobile) ...[
// Desktop Layout - Title full width, Category and Priority in one row
_buildTitleField(theme, t),
const SizedBox(height: 20),
Row(
children: [
// Category field
Expanded(
child: _buildCategoryField(theme, t),
),
const SizedBox(width: 20),
// Priority field
Expanded(
child: _buildPriorityField(theme, t),
),
],
),
const SizedBox(height: 20),
] else ...[
// Mobile Layout - Single Column
_buildTitleField(theme, t),
const SizedBox(height: 20),
_buildCategoryField(theme, t),
const SizedBox(height: 20),
_buildPriorityField(theme, t),
const SizedBox(height: 20),
],
// Description Field (Full Width)
_buildDescriptionField(theme, t),
const SizedBox(height: 32),
// Submit Button
_buildSubmitButton(theme, !isMobile, t),
],
),
),
);
}
Widget _buildTitleField(ThemeData theme, AppLocalizations t) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.ticketTitleLabel,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
TextFormField(
controller: _titleController,
decoration: InputDecoration(
hintText: t.ticketTitleHint,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.primary,
width: 2,
),
),
filled: true,
fillColor: theme.colorScheme.surface,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return t.ticketTitleRequired;
}
if (value.trim().length < 5) {
return t.ticketTitleMinLength;
}
return null;
},
),
],
);
}
Widget _buildCategoryField(ThemeData theme, AppLocalizations t) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.categoryLabel,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
DropdownButtonFormField<SupportCategory>(
initialValue: _selectedCategory,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.primary,
width: 2,
),
),
filled: true,
fillColor: theme.colorScheme.surface,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
),
items: _categories.map((category) {
return DropdownMenuItem(
value: category,
child: Text(category.name),
);
}).toList(),
onChanged: (category) {
setState(() {
_selectedCategory = category;
});
},
validator: (value) {
if (value == null) {
return t.categoryRequired;
}
return null;
},
),
],
);
}
Widget _buildPriorityField(ThemeData theme, AppLocalizations t) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.priorityLabel,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
DropdownButtonFormField<SupportPriority>(
initialValue: _selectedPriority,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.primary,
width: 2,
),
),
filled: true,
fillColor: theme.colorScheme.surface,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
),
items: _priorities.map((priority) {
return DropdownMenuItem(
value: priority,
child: Row(
children: [
Container(
width: 12,
height: 12,
decoration: BoxDecoration(
color: priority.color != null
? Color(int.parse(priority.color!.replaceFirst('#', '0xFF')))
: theme.colorScheme.primary,
shape: BoxShape.circle,
),
),
const SizedBox(width: 12),
Text(priority.name),
],
),
);
}).toList(),
onChanged: (priority) {
setState(() {
_selectedPriority = priority;
});
},
validator: (value) {
if (value == null) {
return t.priorityRequired;
}
return null;
},
),
],
);
}
Widget _buildDescriptionField(ThemeData theme, AppLocalizations t) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.descriptionLabel,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
TextFormField(
controller: _descriptionController,
decoration: InputDecoration(
hintText: t.descriptionHint,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.3),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
color: theme.colorScheme.primary,
width: 2,
),
),
filled: true,
fillColor: theme.colorScheme.surface,
contentPadding: const EdgeInsets.all(16),
alignLabelWithHint: true,
),
maxLines: 6,
validator: (value) {
if (value == null || value.trim().isEmpty) {
return t.descriptionRequired;
}
if (value.trim().length < 10) {
return t.descriptionMinLength;
}
return null;
},
),
],
);
}
Widget _buildSubmitButton(ThemeData theme, bool isDesktop, AppLocalizations t) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.colorScheme.surface,
border: Border(
top: BorderSide(
color: theme.colorScheme.outline.withValues(alpha: 0.2),
width: 1,
),
),
),
child: Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: _isSubmitting ? null : _submitTicket,
icon: _isSubmitting
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
)
: const Icon(Icons.send),
label: Text(_isSubmitting ? t.submittingTicket : t.submitTicket),
style: ElevatedButton.styleFrom(
backgroundColor: theme.colorScheme.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
elevation: 4,
shadowColor: theme.colorScheme.primary.withValues(alpha: 0.3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
const SizedBox(width: 12),
OutlinedButton(
onPressed: _isSubmitting ? null : () => context.pop(),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(t.cancel),
),
],
),
);
}
}