import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/CityItem.dart';
import 'package:iketfaa_delivery/App/Common/Models/Main/address.dart';
import 'package:iketfaa_delivery/App/Common/Services/AuthenticationService/Core/manager.dart';
import 'package:iketfaa_delivery/App/Common/Services/PermissionService/permission_handler_service.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/CategoryItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/Main/SpecialityItem.dart';
import 'package:iketfaa_delivery/App/Freelance/Models/RequestsParameters/request_service_dto.dart';
import 'package:iketfaa_delivery/App/Freelance/Modules/FreelanceHome/controller/freelance_home_controller.dart';
import 'package:multi_image_picker2/multi_image_picker2.dart';
import 'package:path_provider/path_provider.dart';
class BrowseServicesController extends GetxController {
final AuthenticationManager authManager = Get.find();
final FreelanceHomeController freelanceHomeController = Get.find();
TextEditingController descriptionTF = TextEditingController(),
budgetTF = TextEditingController();
final List<String> schedules = [
'As soon as possible',
'Schedule an appointment'
],
prefferedLocations = ['Default', 'Specific location'];
RxList<CategoryItem> categoriesItems = <CategoryItem>[].obs;
RxList<SpecialityItem> specialitiesItems = <SpecialityItem>[].obs;
RxList<CityItem> citiesItems = <CityItem>[].obs;
RxList<int> cityIds = <int>[].obs, cityTemp = <int>[].obs;
List<File> serviceImages = <File>[].obs;
List<Asset> serviceAssets = <Asset>[].obs;
Rx<SpecialityItem> specialitySelected = SpecialityItem().obs;
Rx<Address> destinationAddress = Address().obs;
RxString? selectedSchedule;
RxString scheduleDateString = ''.obs;
RxInt genderID = 0.obs,
currentSchedule = (0).obs,
prefferedLocation = (0).obs;
int noOfImagescanAdd = 5;
Rx<DateTime> scheduleDate = DateTime.now().obs;
late Rx<DateTime> minimumDate = DateTime.now().obs;
@override
Future<void> onReady() async {
super.onReady();
if (citiesItems.isEmpty)
citiesItems.value = await authManager.api.getAllCities();
}
@override
Future<void> onInit() async {
super.onInit();
minimumDate = DateTime.parse(
'${minimumDate.value.year}-${minimumDate.value.month < 9 ? '0' + minimumDate.value.month.toString() : minimumDate.value.month}-${minimumDate.value.day < 9 ? '0' + minimumDate.value.day.toString() : minimumDate.value.day} 00:00:00')
.obs;
}
Future<void> loadPostsAssets(BuildContext context) async {
bool permission = await PermissionHandlerPermissionService()
.handlePhotosPermission(context);
if (permission) {
try {
await MultiImagePicker.pickImages(
maxImages: noOfImagescanAdd,
enableCamera: true,
selectedAssets: serviceAssets,
cupertinoOptions: const CupertinoOptions(
takePhotoIcon: 'chat',
),
materialOptions: const MaterialOptions(
actionBarColor: '#339A58',
statusBarColor: '#339A58',
allViewTitle: 'All Photos',
useDetailsView: false,
selectCircleStrokeColor: '#339A58',
),
).then((value) {
value.forEach((assetFile) async {
var bytes = await assetFile.getByteData();
String dir = (await getApplicationDocumentsDirectory()).path;
await authManager.commonTools
.writeToFile(bytes, '$dir/${assetFile.name}.jpg');
File tempFile = File('$dir/${assetFile.name}.jpg');
serviceImages.add(tempFile);
noOfImagescanAdd -= 1;
});
});
} on Error {}
}
}
Future<void> getAllCategories(int roleId) async {
categoriesItems.value =
await authManager.freelanceAPI.getAllCategories(roleId);
}
Future<void> getAllSepcialities(int categoryId) async {
specialitiesItems.value =
await authManager.freelanceAPI.getAllSepcialities(categoryId);
}
Future<void> sendRequest() async {
authManager.commonTools.showLoading();
int citiesLength = cityIds.toString().length;
String citiesValues = cityIds.toString().substring(1, (citiesLength - 1));
citiesValues = citiesValues.replaceAll(' ', '');
RequestServiceDTO requestServiceDTO = RequestServiceDTO(
delivertype: currentSchedule.value == 0 ? 4 : 5,
details: descriptionTF.text,
genderId: genderID.value,
addressMap: destinationAddress.value,
addressId: destinationAddress.value.id != '-1' &&
destinationAddress.value.id != '-2'
? destinationAddress.value.id
: null,
specialityId: specialitySelected.value.id,
budget: double.parse(budgetTF.text),
cityId: prefferedLocation.value == 0 ? null : citiesValues,
prefferedLocation: prefferedLocation.value + 1,
SchedulerOrderDate:
currentSchedule.value == 0 ? null : scheduleDateString.value);
print(requestServiceDTO.toJson());
await authManager.freelanceAPI
.requestService(requestServiceDTO, serviceImages);
}
void clearAllData() {
descriptionTF.clear();
budgetTF.clear();
cityIds = <int>[].obs;
serviceImages.clear();
serviceAssets.clear();
specialitySelected.value = SpecialityItem();
destinationAddress.value = Address();
selectedSchedule!.value = scheduleDateString.value = '';
genderID.value = currentSchedule.value = prefferedLocation.value = 0;
noOfImagescanAdd = 5;
scheduleDate = DateTime.now().obs;
}
}