You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/pages/user/gas_refill/request_gas_refill.dart

670 lines
32 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
import 'package:test_sa/controllers/providers/api/gas_refill_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/controllers/validator/validator.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/timer_model.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/app_text_form_field.dart';
import 'package:test_sa/views/widgets/buttons/app_button.dart';
import 'package:test_sa/views/widgets/gas_refill/building_type_menu.dart';
import 'package:test_sa/views/widgets/gas_refill/department_type_menu.dart';
import 'package:test_sa/views/widgets/gas_refill/floor_type_menu.dart';
import 'package:test_sa/views/widgets/gas_refill/gas_refill_create_details_item.dart';
import 'package:test_sa/views/widgets/hospitals/hospital_auto_complete_field_new.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/status/gas_refill/gas_cylinder_size.dart';
import 'package:test_sa/views/widgets/status/gas_refill/gas_cylinder_type.dart';
import 'package:test_sa/views/widgets/status/gas_refill/gas_status.dart';
import 'package:test_sa/views/widgets/status/gas_refill/gas_type.dart';
import 'package:test_sa/views/widgets/titles/app_sub_title.dart';
import '../../../../controllers/providers/api/hospitals_provider.dart';
import '../../../../extensions/text_extensions.dart';
import '../../../../models/new_models/gas_refill_model.dart';
import '../../../../new_views/common_widgets/app_text_form_field.dart';
import '../../../../new_views/common_widgets/default_app_bar.dart';
import '../../../../new_views/common_widgets/single_item_drop_down_menu.dart';
import '../../../../providers/gas_request_providers/gas_status_provider.dart';
import '../../../widgets/e_signature/e_signature.dart';
import '../../../widgets/timer/app_timer.dart';
class RequestGasRefill extends StatefulWidget {
static const String id = "/request-gas-refill";
final GasRefillModel gasRefillModel;
const RequestGasRefill({this.gasRefillModel, Key key}) : super(key: key);
@override
State<RequestGasRefill> createState() => _RequestGasRefillState();
}
class _RequestGasRefillState extends State<RequestGasRefill> {
bool _isLoading = false;
bool _validate = false;
UserProvider _userProvider;
SettingProvider _settingProvider;
GasRefillProvider _gasRefillProvider;
GasRefillDetails _currentDetails = GasRefillDetails();
final TextEditingController _deliveredQuantityController = TextEditingController();
final TextEditingController _commentController = TextEditingController();
final TextEditingController _workingHoursController = TextEditingController();
GasRefillModel _formModel = GasRefillModel(gazRefillDetails: []);
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey _DetailsKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool _firstTime = true;
@override
void initState() {
super.initState();
if (widget.gasRefillModel != null) {
_formModel = widget.gasRefillModel;
}
}
@override
void setState(VoidCallback fn) {
if (mounted) super.setState(() {});
}
_onSubmit(BuildContext context) async {
if (_formModel.gazRefillDetails.isEmpty) {
if (!(await _addNewModel(context))) return;
}
//_isLoading = true;
setState(() {});
// if (widget.gasRefillModel != null) {
// if (!(await _formModel.validate(context))) {
// _isLoading = false;
// setState(() {});
// return;
// }
// }
//if(!(_formKey.currentState.validate())) return;
// if (_gasRefillProvider.department?.name == null) {
// ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Select department")));
// _isLoading = false;
// setState(() {});
// return;
// }
int status = widget.gasRefillModel == null
? null /*await _gasRefillProvider.createModel(
user: _userProvider.user,
model: _formModel,
)*/
: await _gasRefillProvider.updateModel(
user: _userProvider.user,
host: _settingProvider.host,
oldModel: widget.gasRefillModel,
newModel: _formModel,
);
//_isLoading = false;
setState(() {});
if (status >= 200 && status < 300) {
Fluttertoast.showToast(
msg: context.translation.successfulRequestMessage,
);
Navigator.of(context).pop(_formModel);
setState(() {});
} else {
String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: context.translation);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(errorMessage),
));
}
}
Future<bool> _addNewModel(BuildContext context) async {
_validate = true;
if (!_formKey.currentState.validate()) {
setState(() {});
return false;
}
_formKey.currentState.save();
if (!(await _currentDetails.validate(context))) {
setState(() {});
return false;
}
_formModel.gazRefillDetails.insert(0, _currentDetails);
_validate = false;
Scrollable.ensureVisible(_DetailsKey.currentContext);
_deliveredQuantityController.clear();
_workingHoursController.clear();
_commentController.clear();
_currentDetails = GasRefillDetails();
setState(() {});
return true;
}
@override
void dispose() {
_deliveredQuantityController.dispose();
_commentController.dispose();
_workingHoursController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_userProvider = Provider.of<UserProvider>(context);
_settingProvider = Provider.of<SettingProvider>(context);
if (_gasRefillProvider == null) {
_gasRefillProvider = Provider.of<GasRefillProvider>(context, listen: false);
//_gasRefillProvider.reset();
}
if (_firstTime) {
String _clientName;
if (widget.gasRefillModel != null) {
_formModel.status = widget.gasRefillModel?.status ?? Lookup(value: 0);
_gasRefillProvider.expectedDateTime = DateTime.tryParse(_formModel.expectedDate??"");
_gasRefillProvider.timer = TimerModel(startAt: DateTime.tryParse(widget.gasRefillModel?.startDate??""), endAt: DateTime.tryParse(widget.gasRefillModel?.endDate??""));
_clientName = _formModel.site.custName;
} else {
_gasRefillProvider.timer = null;
_clientName = _userProvider.user?.clientName;
}
HospitalsProvider().getHospitalsListByVal(searchVal: _clientName).then((value) {
_gasRefillProvider.hospital = value?.firstWhere((element) => element.name == _clientName, orElse: () => null);
_gasRefillProvider.building = _gasRefillProvider.hospital?.buildings?.firstWhere((element) => element.name == widget.gasRefillModel?.building?.name, orElse: () => null);
_gasRefillProvider.floor = _gasRefillProvider.building?.floors?.firstWhere((element) => element.name == widget.gasRefillModel?.floor?.name, orElse: () => null);
_gasRefillProvider.department = _gasRefillProvider.floor?.departments?.firstWhere((element) => element.name == widget.gasRefillModel?.department?.departmentName, orElse: () => null);
_firstTime = false;
setState(() {});
});
}
return Scaffold(
appBar: DefaultAppBar(title: context.translation.updateRequest),
key: _scaffoldKey,
body: Form(
key: _formKey,
child: SafeArea(
child: LoadingManager(
isLoading: _isLoading,
isFailedLoading: false,
stateCode: 200,
onRefresh: () async {},
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
shadows: const [BoxShadow(color: Color(0x07000000), blurRadius: 14, offset: Offset(0, 0), spreadRadius: 0)],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(context.translation.gasRefill, style: AppTextStyles.heading5.copyWith(color: const Color(0xFF3B3D4A))),
8.height,
Text(
'Gas Request:',
style: AppTextStyles.bodyText.copyWith(color: Color(0xFF757575)),
),
Text(
'Cylinder Size: ${widget.gasRefillModel.gazRefillDetails[0].cylinderSize.value}',
style: AppTextStyles.bodyText.copyWith(color: Color(0xFF757575)),
),
Text(
'Request Quantity: ${widget.gasRefillModel.gazRefillDetails[0].requestedQty}',
style: AppTextStyles.bodyText.copyWith(color: Color(0xFF757575)),
),
Text(
'Site: ${widget.gasRefillModel.site.name}',
style: AppTextStyles.bodyText.copyWith(color: Color(0xFF757575)),
),
],
).paddingAll(16),
),
12.height,
AppTextFormField(
labelText: context.translation.workingHours,
onSaved:(value){
_formModel?.workingHours = double.tryParse(value);
},
textInputType: TextInputType.number,
controller: _workingHoursController,
validator: (value) => Validator.isNumeric(value) ? null : "allow numbers only",
),
12.height,
SingleItemDropDownMenu<Lookup,GasStatusProvider >(
context: context,
title: context.translation.reportStatus,
initialValue: _formModel.status,
onSelect: (value) {
_formModel.status=value;
},
),
12.height,
AppTextFormField(
labelText: context.translation.deliveredQuantity,
onSaved:(value){
_currentDetails?.deliverdQty = double.tryParse(value);
},
textInputType: TextInputType.number,
controller: _deliveredQuantityController,
validator: (value) => Validator.isNumeric(value) ? null : "allow numbers only",
),
12.height,
/// TBD
AppTextFormField(
labelText: context.translation.comments,
textInputType: TextInputType.multiline,
alignLabelWithHint: true,
controller: _commentController,
onSaved: (value){
},
),
],
).paddingAll(16),
AButton(
text: widget.gasRefillModel == null ? context.translation.submit : context.translation.update,
onPressed:()async{_onSubmit.call(context);},
).paddingAll(16),
],
),
)
// SingleChildScrollView(
// padding: EdgeInsets.all(12 * AppStyle.getScaleFactor(context)),
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Center(
// child: Padding(
// padding: const EdgeInsets.all(8.0),
// child: Text(
// "${widget.gasRefillModel == null ? "Request" : "Update"} Gas Refill",
// style: Theme.of(context).textTheme.headline5.copyWith(color: Theme.of(context).primaryColor, fontSize: 28, fontWeight: FontWeight.bold),
// ),
// ),
// ),
// // const SizedBox(height: 4,),
// // ASubTitle(context.translation.title),
// // if(_validate && _formModel.title == null)
// // ASubTitle(context.translation.requiredWord,color: Colors.red,),
// // SizedBox(height: 4,),
// // ATextFormField(
// // initialValue: _formModel?.title,
// // textAlign: TextAlign.center,
// // style: Theme.of(context).textTheme.subtitle1,
// // textInputType: TextInputType.text,
// // onSaved: (value){
// // _formModel.title = value;
// // },
// // ),
// // const SizedBox(height: 8,),
// ASubTitle(context.translation.status),
// if (_validate && _formModel.status == null)
// ASubTitle(
// context.translation.requiredWord,
// color: Colors.red,
// ),
// const SizedBox(
// height: 4,
// ),
// GasStatusMenu(
// initialValue: _formModel.status ?? Lookup(value: 0),
// enabled: widget.gasRefillModel != null,
// onSelect: (status) {
// _formModel.status = status;
// },
// ),
// const SizedBox(
// height: 8,
// ),
// Divider(
// color: Theme.of(context).colorScheme.primary,
// ),
// const SizedBox(
// height: 4,
// ),
// HospitalAutoCompleteField(
// enabled: false,
// initialValue: _gasRefillProvider.hospital?.name,
// // onSave: (value){
// // _search.hospital = value;
// // },
// onSearch: (value) {
// _gasRefillProvider.hospital = value;
// _gasRefillProvider.building = null;
// _gasRefillProvider.floor = null;
// _gasRefillProvider.department = null;
// setState(() {});
// },
// ),
// const SizedBox(
// height: 8,
// ),
// BuildingTypeMenu(
// initialValue: _gasRefillProvider?.building,
// building: _gasRefillProvider?.hospital?.buildings,
// loading: _firstTime,
// enabled: widget.gasRefillModel == null,
// onSelect: (status) {
// _gasRefillProvider.building = status;
// setState(() {});
// },
// ),
// const SizedBox(height: 8),
// FloorTypeMenu(
// initialValue: _gasRefillProvider?.floor,
// floors: _gasRefillProvider?.building?.floors,
// enabled: widget.gasRefillModel == null,
// loading: _firstTime,
// onSelect: (status) {
// _gasRefillProvider.floor = status;
// setState(() {});
// },
// ),
// const SizedBox(height: 8),
// DepartmentTypeMenu(
// initialValue: _gasRefillProvider?.department,
// departments: _gasRefillProvider?.floor?.departments,
// loading: _firstTime,
// enabled: widget.gasRefillModel == null,
// onSelect: (status) {
// _gasRefillProvider.department = status;
// setState(() {});
// },
// ),
// const SizedBox(height: 8),
// if (widget.gasRefillModel != null) ASubTitle(context.translation.workingHours),
// if (widget.gasRefillModel != null) const SizedBox(height: 8),
// if (widget.gasRefillModel != null)
// Row(
// children: [
// Expanded(
// child: AppTimer(
// timer: _gasRefillProvider.timer,
// onChange: (timer) async {
// _gasRefillProvider.timer = timer;
// _formModel.workingHours = num.tryParse((((timer?.durationInSecond ?? 0) / 60) / 60)?.toStringAsFixed(2) ?? "0");
// _formModel.startDate = timer.startAt.toString();
// _formModel.endDate = timer.endAt.toString();
// return true;
// },
// ),
// ),
// ],
// ),
// //const SizedBox(height: 8),
// // if (_userProvider.user?.type == UsersTypes.normal_user || widget.gasRefillModel != null)
// // Column(
// // crossAxisAlignment: CrossAxisAlignment.stretch,
// // children: [
// // const ASubTitle("Expected Date"),
// // SizedBox(
// // height: 8 * AppStyle.getScaleFactor(context),
// // ),
// // ADateTimePicker(
// // date: _gasRefillProvider.expectedDateTime,
// // from: DateTime.now().subtract(const Duration(days: 365)),
// // to: DateTime.now().add(const Duration(days: 365)),
// // onDateTimePicker: (date) {
// // _gasRefillProvider.expectedDateTime = date;
// // _formModel.expectedDate = _gasRefillProvider.expectedDateTime;
// // setState(() {});
// // },
// // ),
// // ],
// // ),
// // if (_userProvider.user?.type == UsersTypes.engineer)
// // Column(
// // children: [
// // Row(
// // children: [
// // Expanded(
// // child: Column(
// // crossAxisAlignment: CrossAxisAlignment.stretch,
// // children: [
// // const ASubTitle("Start of Work"),
// // SizedBox(
// // height: 8 * AppStyle.getScaleFactor(context),
// // ),
// // ADateTimePicker(
// // date: _gasRefillProvider.startDate,
// // from: DateTime.now().subtract(const Duration(days: 365)),
// // to: DateTime.now().add(const Duration(days: 365)),
// // onDateTimePicker: (date) {
// // _gasRefillProvider.startDate = date;
// // setState(() {});
// // },
// // ),
// // ],
// // ),
// // ),
// // const SizedBox(width: 8),
// // Expanded(
// // child: Column(
// // crossAxisAlignment: CrossAxisAlignment.stretch,
// // children: [
// // const ASubTitle("End of Work"),
// // SizedBox(
// // height: 8 * AppStyle.getScaleFactor(context),
// // ),
// // ADateTimePicker(
// // date: _gasRefillProvider.endDate,
// // from: DateTime.now().subtract(const Duration(days: 365)),
// // to: DateTime.now().add(const Duration(days: 365)),
// // onDateTimePicker: (date) {
// // _gasRefillProvider.endDate = date;
// // setState(() {});
// // },
// // ),
// // ],
// // ),
// // ),
// // ],
// // ),
// // const SizedBox(height: 8),
// // ASubTitle(context.translation.workingHours),
// // const SizedBox(height: 4),
// // ATextFormField(
// // initialValue: null,
// // textAlign: TextAlign.center,
// // hintText: _gasRefillProvider.startDate == null
// // ? "0"
// // : ((_gasRefillProvider.endDate?.difference(_gasRefillProvider.startDate)?.inMinutes ?? 0) / 60)?.toStringAsFixed(2)?.toString() ?? "0",
// // enable: false,
// // style: Theme.of(context).textTheme.subtitle1,
// // validator: (value) => Validator.isNumeric(value) ? null : context.translation.requiredWord,
// // textInputType: TextInputType.number,
// // onSaved: (value) {
// // // _serviceReport.workHours = value;
// // },
// // ),
// // ],
// // ),
// if (widget.gasRefillModel == null)
// Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// const SizedBox(height: 8),
// Divider(color: Theme.of(context).colorScheme.primary),
// const SizedBox(height: 4),
// const ASubTitle("Gas Type"),
// if (_validate && _currentDetails.gasType == null) ASubTitle(context.translation.requiredWord, color: Colors.red),
// const SizedBox(height: 4),
// GasTypeMenu(
// initialValue: _currentDetails.gasType,
// onSelect: (status) {
// _currentDetails.gasType = status;
// },
// ),
// const SizedBox(height: 8),
// const ASubTitle("Cylinder Size"),
// if (_validate && _currentDetails.cylinderSize == null)
// ASubTitle(
// context.translation.requiredWord,
// color: Colors.red,
// ),
// const SizedBox(
// height: 4,
// ),
// GasCylinderSizeMenu(
// initialValue: _currentDetails.cylinderSize,
// onSelect: (status) {
// _currentDetails.cylinderSize = status;
// },
// ),
// const SizedBox(
// height: 8,
// ),
// const ASubTitle("Cylinder Type"),
// if (_validate && _currentDetails.cylinderSize == null)
// ASubTitle(
// context.translation.requiredWord,
// color: Colors.red,
// ),
// const SizedBox(
// height: 4,
// ),
// GasCylinderTypesMenu(
// initialValue: _currentDetails.cylinderType,
// onSelect: (status) {
// _currentDetails.cylinderType = status;
// },
// ),
// const SizedBox(
// height: 8,
// ),
// ASubTitle(context.translation.requestedQuantity),
// if (_validate && _currentDetails?.requestedQty == null)
// ASubTitle(
// context.translation.requiredWord,
// color: Colors.red,
// ),
// SizedBox(
// height: 4,
// ),
// ATextFormField(
// initialValue: (_currentDetails?.requestedQty ?? "").toString(),
// textAlign: TextAlign.center,
// controller: _requestedQuantityController,
// style: Theme.of(context).textTheme.subtitle1,
// validator: (value) => Validator.isNumeric(value) ? null : "allow numbers only",
// textInputType: TextInputType.number,
// onChange: (value) {
// _currentDetails?.requestedQty = double.tryParse(value);
// },
// ),
// if (widget.gasRefillModel != null) const SizedBox(height: 16),
// if (widget.gasRefillModel != null) ASubTitle(context.translation.deliveredQuantity),
// if (widget.gasRefillModel != null && _validate && _currentDetails?.deliverdQty == null)
// ASubTitle(
// context.translation.requiredWord,
// color: Colors.red,
// ),
// if (widget.gasRefillModel != null) const SizedBox(height: 4),
// if (widget.gasRefillModel != null)
// ATextFormField(
// initialValue: (_currentDetails?.deliverdQty ?? "").toString(),
// textAlign: TextAlign.center,
// controller: _deliveredQuantityController,
// style: Theme.of(context).textTheme.subtitle1,
// validator: (value) => Validator.isNumeric(value) ? null : "allow numbers only",
// textInputType: TextInputType.number,
// onChange: (value) {
// _currentDetails?.deliverdQty = double.tryParse(value);
// },
// ),
// const SizedBox(height: 16),
// AButton(
// text: context.translation.add,
// onPressed: ()async{await _addNewModel.call(context);},
// ),
// ],
// ),
// const SizedBox(height: 16),
// if (_formModel.gazRefillDetails.isNotEmpty) const ASubTitle("Gas Requests"),
// ListView.builder(
// key: _DetailsKey,
// shrinkWrap: true,
// physics: const ClampingScrollPhysics(),
// itemCount: _formModel.gazRefillDetails.length,
// itemBuilder: (context, index) {
// final model = _formModel.gazRefillDetails[index];
// return GasRefillCreateDetailsItem(
// isUpdate: widget.gasRefillModel != null,
// model: model,
// onPressed: () {
// if (widget.gasRefillModel != null) {
// model.selectedForEditing = !(model.selectedForEditing ?? false);
// }
// if (widget.gasRefillModel == null) {
// _formModel.gazRefillDetails.remove(model);
// }
// setState(() {});
// },
// );
// }),
// if (widget.gasRefillModel != null) const SizedBox(height: 16),
// if (widget.gasRefillModel != null) const ASubTitle("Nurse Signature"),
// if (widget.gasRefillModel != null)
// ESignature(
// oldSignature: _formModel.nurseSignature,
// newSignature: _formModel.localNurseSignature,
// onChange: (signature) {
// if (signature == null || signature.isEmpty) {
// return;
// }
// _formModel.localNurseSignature = signature;
// _formModel.nurseSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
// },
// ),
// if (widget.gasRefillModel != null)
// const SizedBox(
// height: 8,
// ),
// if (widget.gasRefillModel != null) const ASubTitle("Engineer Signature"),
// if (widget.gasRefillModel != null)
// ESignature(
// oldSignature: _formModel.engSignature,
// newSignature: _formModel.localEngineerSignature,
// onChange: (signature) {
// if (signature == null || signature.isEmpty) {
// return;
// }
// _formModel.localEngineerSignature = signature;
// _formModel.engSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
// },
// ),
// const SizedBox(height: 16),
// AButton(
// text: widget.gasRefillModel == null ? context.translation.submit : context.translation.update,
// onPressed:()async{_onSubmit.call(context);},
// ),
// const SizedBox(height: 100)
// ],
// ),
// ),
),
),
),
);
}
}