65 lines
2.0 KiB
Dart
65 lines
2.0 KiB
Dart
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 'companies_management_controller.dart';
|
|
|
|
class AddCompanyController extends GetxController {
|
|
final nameController = TextEditingController();
|
|
final tinController = TextEditingController();
|
|
final crnController = TextEditingController();
|
|
|
|
var isSubmitting = false.obs;
|
|
|
|
@override
|
|
void onClose() {
|
|
nameController.dispose();
|
|
tinController.dispose();
|
|
crnController.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
Future<void> submit() async {
|
|
final name = nameController.text.trim();
|
|
final tin = tinController.text.trim();
|
|
|
|
if (name.isEmpty || tin.isEmpty) {
|
|
AppSnackbar.showError('خطأ', 'الرجاء إدخال اسم الشركة والرقم الضريبي');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
isSubmitting.value = true;
|
|
final dio = DioClient().client;
|
|
final response = await dio.post('companies/create', data: {
|
|
'name': name,
|
|
'tax_identification_number': tin,
|
|
'commercial_registration_number': crnController.text.trim(),
|
|
});
|
|
|
|
if (response.data['success'] == true) {
|
|
AppSnackbar.showSuccess('نجاح', 'تمت إضافة الشركة بنجاح');
|
|
|
|
// Refresh list if controller exists
|
|
if (Get.isRegistered<CompaniesManagementController>()) {
|
|
Get.find<CompaniesManagementController>().fetchCompanies();
|
|
}
|
|
|
|
Get.back();
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 403) {
|
|
AppSnackbar.showError(
|
|
'خطأ', 'لقد وصلت للحد الأقصى المسموح به للشركات في باقتك');
|
|
} else {
|
|
AppSnackbar.showError('خطأ', 'تعذر إضافة الشركة');
|
|
}
|
|
} catch (e) {
|
|
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع');
|
|
} finally {
|
|
isSubmitting.value = false;
|
|
}
|
|
}
|
|
}
|