111 lines
2.9 KiB
Dart
111 lines
2.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:sefer_driver/constant/box_name.dart';
|
|
import 'package:sefer_driver/constant/colors.dart';
|
|
import 'package:sefer_driver/constant/links.dart';
|
|
import 'package:sefer_driver/controller/functions/crud.dart';
|
|
import 'package:sefer_driver/main.dart';
|
|
import 'package:sefer_driver/views/widgets/error_snakbar.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class DriverCarController extends GetxController {
|
|
bool isLoading = false;
|
|
List cars = [];
|
|
// int? carId;
|
|
fetchCatrsForDrivers() async {
|
|
isLoading = true;
|
|
update();
|
|
var res = await CRUD().get(link: AppLink.getNewCarsDrivers, payload: {
|
|
"driverID": box.read(BoxName.driverID).toString(),
|
|
});
|
|
if (res != 'failure') {
|
|
var d = jsonDecode(res)['message'];
|
|
cars = d;
|
|
// carId = cars.isEmpty ? 1 : cars.length + 1;
|
|
}
|
|
isLoading = false;
|
|
update();
|
|
}
|
|
|
|
addCarsForDrivers(
|
|
String vin,
|
|
String carPlate,
|
|
String make,
|
|
String model,
|
|
String year,
|
|
String expirationDate,
|
|
String color,
|
|
String colorHex,
|
|
String address,
|
|
String owner,
|
|
String registrationDate,
|
|
String displacement,
|
|
String fuel) async {
|
|
var res = await CRUD().post(
|
|
link: AppLink.addRegisrationCar,
|
|
payload: {
|
|
"driverID": box.read(BoxName.driverID).toString(),
|
|
"vin": vin ?? 'unknown',
|
|
"car_plate": carPlate.toString(),
|
|
"make": make ?? 'unknown',
|
|
"model": model ?? 'unknown',
|
|
"year": year ?? 'unknown',
|
|
"expiration_date": expirationDate ?? 'unknown',
|
|
"color": color ?? 'unknown',
|
|
"owner": owner ?? 'unknown',
|
|
"color_hex": colorHex ?? '#000000',
|
|
"address": address ?? 'unknown',
|
|
"displacement": displacement ?? 'unknown',
|
|
"fuel": fuel ?? 'unknown',
|
|
"registration_date": registrationDate ?? 'unknown',
|
|
},
|
|
);
|
|
if (res != 'failure') {
|
|
mySnackbarSuccess('');
|
|
|
|
fetchCatrsForDrivers();
|
|
} else {
|
|
mySnackeBarError('');
|
|
}
|
|
}
|
|
|
|
// update carRegistration only and insert on it without tow column
|
|
Future<void> updateCarRegistration(String id, String driverID) async {
|
|
final body = {
|
|
'id': id,
|
|
'driverID': driverID,
|
|
};
|
|
// remove default before update
|
|
var response = await CRUD().post(
|
|
link: AppLink.makeDefaultCar,
|
|
payload: body,
|
|
);
|
|
|
|
if (response != 'failure') {
|
|
mySnackbarSuccess('Updated'.tr);
|
|
} else {
|
|
mySnackeBarError('Not updated'.tr);
|
|
}
|
|
}
|
|
|
|
//todo need review
|
|
removeCar(String carId) async {
|
|
isLoading = true;
|
|
update();
|
|
var res = await CRUD().post(link: AppLink.deleteNewCarsDrivers, payload: {
|
|
"id": carId.toString(),
|
|
});
|
|
if (res != 'failure') {
|
|
mySnackbarSuccess('deleted'.tr);
|
|
}
|
|
isLoading = false;
|
|
update();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
fetchCatrsForDrivers();
|
|
super.onInit();
|
|
}
|
|
}
|