Added Service

This commit is contained in:
2026-02-21 21:57:31 +08:00
parent 6238c701c0
commit 46a84b4d95
8 changed files with 171 additions and 41 deletions
+88 -35
View File
@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/office.dart';
import '../../providers/profile_provider.dart';
import '../../providers/tickets_provider.dart';
import '../../providers/services_provider.dart';
import '../../widgets/mono_text.dart';
import '../../widgets/responsive_body.dart';
import '../../theme/app_surfaces.dart';
@@ -163,45 +164,97 @@ class _OfficesScreenState extends ConsumerState<OfficesScreen> {
Office? office,
}) async {
final nameController = TextEditingController(text: office?.name ?? '');
String? selectedServiceId = office?.serviceId;
await showDialog<void>(
context: context,
builder: (dialogContext) {
return AlertDialog(
shape: AppSurfaces.of(context).dialogShape,
title: Text(office == null ? 'Create Office' : 'Edit Office'),
content: TextField(
controller: nameController,
decoration: const InputDecoration(labelText: 'Office name'),
),
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () async {
final name = nameController.text.trim();
if (name.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Name is required.')),
);
return;
}
final controller = ref.read(officesControllerProvider);
if (office == null) {
await controller.createOffice(name: name);
} else {
await controller.updateOffice(id: office.id, name: name);
}
ref.invalidate(officesProvider);
if (context.mounted) {
Navigator.of(dialogContext).pop();
}
},
child: Text(office == null ? 'Create' : 'Save'),
),
],
final servicesAsync = ref.watch(servicesOnceProvider);
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
shape: AppSurfaces.of(context).dialogShape,
title: Text(office == null ? 'Create Office' : 'Edit Office'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: nameController,
decoration: const InputDecoration(
labelText: 'Office name',
),
),
const SizedBox(height: 12),
servicesAsync.when(
data: (services) {
return DropdownButtonFormField<String?>(
initialValue: selectedServiceId,
decoration: const InputDecoration(
labelText: 'Service',
),
items: [
const DropdownMenuItem<String?>(
value: null,
child: Text('None'),
),
...services.map(
(s) => DropdownMenuItem<String?>(
value: s.id,
child: Text(s.name),
),
),
],
onChanged: (v) =>
setState(() => selectedServiceId = v),
);
},
loading: () => const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: LinearProgressIndicator(),
),
error: (e, _) => Text('Failed to load services: $e'),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () async {
final name = nameController.text.trim();
if (name.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Name is required.')),
);
return;
}
final controller = ref.read(officesControllerProvider);
if (office == null) {
await controller.createOffice(
name: name,
serviceId: selectedServiceId,
);
} else {
await controller.updateOffice(
id: office.id,
name: name,
serviceId: selectedServiceId,
);
}
ref.invalidate(officesProvider);
if (context.mounted) {
Navigator.of(dialogContext).pop();
}
},
child: Text(office == null ? 'Create' : 'Save'),
),
],
);
},
);
},
);