import 'dart:convert'; import 'dart:math'; import 'package:flutter/widgets.dart'; import 'package:get/get.dart'; import 'package:sefer_admin1/constant/colors.dart'; import 'package:sefer_admin1/constant/links.dart'; import 'package:sefer_admin1/controller/functions/crud.dart'; class EmployeeController extends GetxController { List employee = []; final name = TextEditingController(); final education = TextEditingController(); final site = TextEditingController(); final phone = TextEditingController(); final status = TextEditingController(); final formKey = GlobalKey(); fetchEmployee() async { var res = await CRUD().get(link: AppLink.getEmployee, payload: {}); if (res != 'failure') { employee = jsonDecode(res)['message']; update(); } else { Get.snackbar('error', '', backgroundColor: AppColor.redColor); } } late String id; String generateRandomId(int length) { const String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; Random random = Random(); return String.fromCharCodes(Iterable.generate( length, (_) => chars.codeUnitAt(random.nextInt(chars.length)), )); } addEmployee() async { // Create the payload with the employee data var res = await CRUD().post(link: AppLink.addEmployee, payload: { "id": id, "name": name.text, "education": education.text, "site": site.text, "phone": phone.text, "status": status.text, }); // Check the response from the API if (res != 'failure') { // You can handle the success case here, such as showing a success message Get.back(); Get.snackbar('Success', 'Employee added successfully', backgroundColor: AppColor.greenColor); name.clear(); education.clear(); site.clear(); phone.clear(); status.clear(); fetchEmployee(); } else { // Handle the error case by showing a snackbar with an error message Get.snackbar('Error', 'Failed to add employee', backgroundColor: AppColor.redColor); } } @override void onInit() { fetchEmployee(); super.onInit(); } }