Update: 2026-05-07 23:06:22
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
import 'tenants_management_controller.dart';
|
||||
|
||||
class AddTenantController extends GetxController {
|
||||
final nameController = TextEditingController();
|
||||
final emailController = TextEditingController();
|
||||
final managerNameController = TextEditingController();
|
||||
final managerEmailController = TextEditingController();
|
||||
final managerPasswordController = TextEditingController();
|
||||
|
||||
var isSubmitting = false.obs;
|
||||
final Dio _dio = DioClient().client;
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
nameController.dispose();
|
||||
emailController.dispose();
|
||||
managerNameController.dispose();
|
||||
managerEmailController.dispose();
|
||||
managerPasswordController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> submit() async {
|
||||
final name = nameController.text.trim();
|
||||
final email = emailController.text.trim();
|
||||
final managerName = managerNameController.text.trim();
|
||||
final managerEmail = managerEmailController.text.trim();
|
||||
final managerPassword = managerPasswordController.text;
|
||||
|
||||
if (name.isEmpty || email.isEmpty || managerName.isEmpty || managerEmail.isEmpty || managerPassword.isEmpty) {
|
||||
AppSnackbar.showWarning('تنبيه', 'الرجاء إدخال جميع البيانات المطلوبة');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
isSubmitting.value = true;
|
||||
final response = await _dio.post('tenants/create', data: {
|
||||
'name': name,
|
||||
'email': email,
|
||||
'manager_name': managerName,
|
||||
'manager_email': managerEmail,
|
||||
'manager_password': managerPassword,
|
||||
});
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
AppSnackbar.showSuccess('نجاح', 'تم إضافة المكتب المحاسبي بنجاح');
|
||||
|
||||
// Refresh the list if it exists
|
||||
if (Get.isRegistered<TenantsManagementController>()) {
|
||||
Get.find<TenantsManagementController>().fetchTenants();
|
||||
}
|
||||
|
||||
Get.back();
|
||||
} else {
|
||||
AppSnackbar.showError('خطأ', 'فشل إضافة المكتب المحاسبي');
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to create tenant', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر إضافة المكتب المحاسبي، يرجى المحاولة لاحقاً');
|
||||
} finally {
|
||||
isSubmitting.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
|
||||
class TenantsManagementController extends GetxController {
|
||||
final Dio _dio = DioClient().client;
|
||||
|
||||
var isLoading = true.obs;
|
||||
var tenants = <Map<String, dynamic>>[].obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchTenants();
|
||||
}
|
||||
|
||||
Future<void> fetchTenants() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
final response = await _dio.get('tenants');
|
||||
if (response.data['success'] == true) {
|
||||
tenants.value = List<Map<String, dynamic>>.from(response.data['data']);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to fetch tenants', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر تحميل المكاتب المحاسبية');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
130
musadaq-app/lib/features/tenants/views/add_tenant_view.dart
Normal file
130
musadaq-app/lib/features/tenants/views/add_tenant_view.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../controllers/add_tenant_controller.dart';
|
||||
|
||||
class AddTenantView extends StatelessWidget {
|
||||
const AddTenantView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final controller = Get.put(AddTenantController());
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('إضافة مكتب محاسبي', style: TextStyle(fontFamily: 'El Messiri')),
|
||||
centerTitle: true,
|
||||
backgroundColor: const Color(0xFF0F4C81),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'بيانات المكتب الأساسية',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
_buildTextField(
|
||||
controller: controller.nameController,
|
||||
label: 'اسم المكتب',
|
||||
icon: Icons.account_balance,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.emailController,
|
||||
label: 'البريد الإلكتروني للمكتب',
|
||||
icon: Icons.email,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
'بيانات مدير المكتب',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.managerNameController,
|
||||
label: 'اسم المدير',
|
||||
icon: Icons.person,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.managerEmailController,
|
||||
label: 'البريد الإلكتروني للمدير',
|
||||
icon: Icons.alternate_email,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildTextField(
|
||||
controller: controller.managerPasswordController,
|
||||
label: 'كلمة مرور المدير',
|
||||
icon: Icons.lock,
|
||||
isPassword: true,
|
||||
isDark: isDark,
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 54,
|
||||
child: Obx(
|
||||
() => ElevatedButton(
|
||||
onPressed: controller.isSubmitting.value ? null : controller.submit,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF0F4C81),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
||||
),
|
||||
child: controller.isSubmitting.value
|
||||
? const CircularProgressIndicator(color: Colors.white)
|
||||
: const Text(
|
||||
'حفظ وإضافة',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTextField({
|
||||
required TextEditingController controller,
|
||||
required String label,
|
||||
required IconData icon,
|
||||
TextInputType? keyboardType,
|
||||
bool isPassword = false,
|
||||
required bool isDark,
|
||||
}) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
keyboardType: keyboardType,
|
||||
obscureText: isPassword,
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
prefixIcon: Icon(icon, color: const Color(0xFF0F4C81)),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide(color: isDark ? Colors.white24 : Colors.grey.shade300),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide(color: isDark ? Colors.white24 : Colors.grey.shade300),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: const BorderSide(color: Color(0xFF0F4C81), width: 2),
|
||||
),
|
||||
filled: true,
|
||||
fillColor: isDark ? Colors.white.withValues(alpha: 0.05) : Colors.white,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../controllers/tenants_management_controller.dart';
|
||||
import 'add_tenant_view.dart';
|
||||
|
||||
class TenantsManagementView extends StatelessWidget {
|
||||
const TenantsManagementView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final controller = Get.put(TenantsManagementController());
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('إدارة المكاتب المحاسبية', style: TextStyle(fontFamily: 'El Messiri')),
|
||||
centerTitle: true,
|
||||
backgroundColor: const Color(0xFF0F4C81),
|
||||
foregroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
Get.to(() => const AddTenantView());
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Obx(() {
|
||||
if (controller.isLoading.value) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (controller.tenants.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.account_balance_outlined, size: 80, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 16),
|
||||
const Text('لا يوجد مكاتب محاسبية مسجلة', style: TextStyle(fontSize: 18, color: Colors.grey)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: controller.fetchTenants,
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: controller.tenants.length,
|
||||
itemBuilder: (context, index) {
|
||||
final tenant = controller.tenants[index];
|
||||
return Card(
|
||||
elevation: 2,
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: const Color(0xFF0F4C81).withValues(alpha: 0.1),
|
||||
child: const Icon(Icons.account_balance, color: Color(0xFF0F4C81)),
|
||||
),
|
||||
title: Text(
|
||||
tenant['name'] ?? 'مكتب محاسبي',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(tenant['email'] ?? ''),
|
||||
trailing: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF0F4C81).withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(
|
||||
tenant['status'] ?? 'active',
|
||||
style: const TextStyle(color: Color(0xFF0F4C81), fontSize: 12),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user