@ -1,6 +1,8 @@
import ' dart:async ' ;
import ' dart:async ' ;
import ' dart:developer ' ;
import ' dart:developer ' ;
import ' package:flutter/cupertino.dart ' ;
import ' package:mc_common_app/generated/locale_keys.g.dart ' ;
import ' package:mc_common_app/generated/locale_keys.g.dart ' ;
import ' package:mc_common_app/models/appointments_models/appointment_basic_detail_model.dart ' ;
import ' package:mc_common_app/models/general_models/generic_resp_model.dart ' ;
import ' package:mc_common_app/models/general_models/generic_resp_model.dart ' ;
import ' package:mc_common_app/utils/navigator.dart ' ;
import ' package:mc_common_app/utils/navigator.dart ' ;
import ' package:mc_common_app/view_models/service_view_model.dart ' ;
import ' package:mc_common_app/view_models/service_view_model.dart ' ;
@ -29,7 +31,7 @@ import 'package:provider/provider.dart';
class CreateServicesPage3 extends StatefulWidget {
class CreateServicesPage3 extends StatefulWidget {
final CreateBranchModel ? branchModel ;
final CreateBranchModel ? branchModel ;
const CreateServicesPage3 ( this . branchModel , { Key ? key } ) : super ( key: key ) ;
const CreateServicesPage3 ( this . branchModel , { super . key } ) ;
@ override
@ override
State < CreateServicesPage3 > createState ( ) = > _CreateServicesPage3State ( ) ;
State < CreateServicesPage3 > createState ( ) = > _CreateServicesPage3State ( ) ;
@ -38,6 +40,7 @@ class CreateServicesPage3 extends StatefulWidget {
class _CreateServicesPage3State extends State < CreateServicesPage3 > {
class _CreateServicesPage3State extends State < CreateServicesPage3 > {
bool isAppointmentAvailable = false ;
bool isAppointmentAvailable = false ;
bool isHomeAppointmentAvailable = false ;
bool isHomeAppointmentAvailable = false ;
ServiceStatusEnum serviceStatusEnum = ServiceStatusEnum . pending ;
int serviceRage = 0 ;
int serviceRage = 0 ;
String chargersPerKm = " " ;
String chargersPerKm = " " ;
int ? categoryId = - 1 ;
int ? categoryId = - 1 ;
@ -45,18 +48,23 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
DropValue ? category ;
DropValue ? category ;
DropValue ? service ;
DropValue ? service ;
bool isEditDisabled = false ;
bool isServiceActive = false ;
@ override
@ override
void initState ( ) {
void initState ( ) {
super . initState ( ) ;
super . initState ( ) ;
scheduleMicrotask ( ( ) async {
scheduleMicrotask ( ( ) async {
ServiceVM serviceVM = context . read < ServiceVM > ( ) ;
ServiceVM serviceVM = context . read < ServiceVM > ( ) ;
log ( " widget.branchModel!.categoryId: ${ widget . branchModel ! . categoryId } " ) ;
if ( widget . branchModel ! . categoryId ! = null ) {
if ( widget . branchModel ! . categoryId ! = null ) {
await serviceVM . fetchServicesByCategoryId ( int . parse ( widget . branchModel ! . categoryId ? ? " 0 " ) ) ;
await serviceVM . fetchServicesByCategoryId ( int . parse ( widget . branchModel ! . categoryId ? ? " 0 " ) ) ;
category = DropValue ( int . parse ( widget . branchModel ! . categoryId ? ? " 0 " ) , widget . branchModel ! . categoryName ? ? " " , " " ) ;
category = DropValue ( int . parse ( widget . branchModel ! . categoryId ? ? " 0 " ) , widget . branchModel ! . categoryName ? ? " " , " " ) ;
service = serviceVM . servicesDropList . firstWhere ( ( element ) = > element . id = = widget . branchModel ! . serviceProviderService ! . serviceId ) ;
service = serviceVM . servicesDropList . firstWhere ( ( element ) = > element . id = = widget . branchModel ! . serviceProviderService ! . serviceId ) ;
isAppointmentAvailable = widget . branchModel ? . serviceProviderService ? . isAllowAppointment ? ? false ;
isAppointmentAvailable = widget . branchModel ? . serviceProviderService ? . isAllowAppointment ? ? false ;
serviceRage = widget . branchModel ? . serviceProviderService ? . customerLocationRange ? ? 0 ;
serviceRage = widget . branchModel ? . serviceProviderService ? . customerLocationRange ? ? 0 ;
serviceStatusEnum = widget . branchModel ? . serviceProviderService ? . serviceStatusEnum ? ? ServiceStatusEnum . pending ;
isServiceActive = widget . branchModel ? . serviceProviderService ? . serviceStatusEnum ! = ServiceStatusEnum . deactivated ;
isEditDisabled = serviceStatusEnum = = ServiceStatusEnum . blocked ;
if ( serviceRage > 0 ) {
if ( serviceRage > 0 ) {
isHomeAppointmentAvailable = true ;
isHomeAppointmentAvailable = true ;
}
}
@ -71,14 +79,13 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
@ override
@ override
Widget build ( BuildContext context ) {
Widget build ( BuildContext context ) {
log ( " categoryId: ${ categoryId } " ) ;
return Scaffold (
return Scaffold (
appBar: CustomAppBar ( title: LocaleKeys . editServices . tr ( ) ) ,
appBar: CustomAppBar ( title: LocaleKeys . editServices . tr ( ) ) ,
body: SizedBox (
body: SizedBox (
width: double . infinity ,
width: double . infinity ,
height: double . infinity ,
height: double . infinity ,
child: Consumer < ServiceVM > (
child: Consumer < ServiceVM > (
builder: ( context , model , _ ) {
builder: ( context , serviceVM , _ ) {
return Column (
return Column (
children: [
children: [
Expanded (
Expanded (
@ -96,30 +103,67 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
serviceId = - 1 ;
serviceId = - 1 ;
isAppointmentAvailable = false ;
isAppointmentAvailable = false ;
isHomeAppointmentAvailable = false ;
isHomeAppointmentAvailable = false ;
model . fetchServicesByCategoryId ( value . id ) ;
serviceVM . fetchServicesByCategoryId ( value . id ) ;
} ,
} ,
dropdownValue: category ,
dropdownValue: category ,
list: model . categoryDropList ,
list: serviceVM . categoryDropList ,
hint: category ! = null ? category ! . value : LocaleKeys . selectServiceCategory . tr ( ) ,
hint: category ! = null ? category ! . value : LocaleKeys . selectServiceCategory . tr ( ) ,
) ,
) ,
if ( widget . branchModel ! . isForEdit ) . . . [
22. height ,
Padding (
padding: const EdgeInsets . symmetric ( horizontal: 18 ) ,
child: Row (
children: [
LocaleKeys . active . tr ( ) . toText ( fontSize: 16 ) ,
8. width ,
Container (
width: 50 ,
height: 30 ,
decoration: BoxDecoration (
color: isServiceActive ? MyColors . darkPrimaryColor : MyColors . white ,
borderRadius: BorderRadius . circular ( 25.0 ) ,
border: Border . all ( color: MyColors . lightTextColor , width: 1 ) ,
) ,
child: Transform . scale (
scale: 0.8 ,
child: CupertinoSwitch (
activeColor: MyColors . darkPrimaryColor ,
trackColor: MyColors . white ,
thumbColor: MyColors . greyACColor ,
value: isServiceActive ,
onChanged: ( value ) async {
if ( isEditDisabled ) return ;
isServiceActive = await updateServiceStatus ( context , value ) ;
setState ( ( ) { } ) ;
} ,
) ,
) ,
) ,
] ,
) ,
) ,
] else . . . [
12. height ,
12. height ,
( model . state = = ViewState . idle )
] ,
? categoryId ! = - 1 & & model . servicesDropList . isNotEmpty
( serviceVM . state = = ViewState . idle )
? categoryId ! = - 1 & & serviceVM . servicesDropList . isNotEmpty
? DropdownField (
? DropdownField (
( DropValue value ) {
( DropValue value ) {
service = value ;
service = value ;
serviceId = value . id ;
serviceId = value . id ;
isAppointmentAvailable = false ;
isAppointmentAvailable = false ;
isHomeAppointmentAvailable = false ;
isHomeAppointmentAvailable = false ;
model . setState ( ViewState . idle ) ;
serviceVM . setState ( ViewState . idle ) ;
} ,
} ,
dropdownValue: service ,
dropdownValue: service ,
list: model . servicesDropList ,
list: serviceVM . servicesDropList ,
hint: LocaleKeys . defineServices . tr ( ) ,
hint: LocaleKeys . defineServices . tr ( ) ,
)
)
: categoryId = = - 1
: categoryId = = - 1
? const SizedBox ( )
? const SizedBox ( )
: model . state = = ViewState . idle
: serviceVM . state = = ViewState . idle
? EmptyWidget ( text: LocaleKeys . noServicesAvailable . tr ( ) )
? EmptyWidget ( text: LocaleKeys . noServicesAvailable . tr ( ) )
: const SizedBox ( )
: const SizedBox ( )
: const CircularProgressIndicator ( ) ,
: const CircularProgressIndicator ( ) ,
@ -129,22 +173,31 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
children: [
children: [
20. height ,
20. height ,
CheckBoxWithTitleDescription (
CheckBoxWithTitleDescription (
isDisabled: isEditDisabled ,
isSelected: isAppointmentAvailable ,
isSelected: isAppointmentAvailable ,
title: LocaleKeys . availableforAppointment . tr ( ) ,
title: LocaleKeys . availableforAppointment . tr ( ) ,
description: LocaleKeys . bookAppointmentForServices . tr ( ) ,
description: LocaleKeys . bookAppointmentForServices . tr ( ) ,
onSelection: ( bool v ) {
onSelection: ( bool v ) {
isAppointmentAvailable = v ;
isAppointmentAvailable = v ;
model . setState ( ViewState . idle ) ;
if ( ! isAppointmentAvailable ) {
isHomeAppointmentAvailable = false ;
}
serviceVM . setState ( ViewState . idle ) ;
} ,
} ,
) ,
) ,
20. height ,
20. height ,
if ( isAppointmentAvailable )
CheckBoxWithTitleDescription (
CheckBoxWithTitleDescription (
isDisabled: isEditDisabled ,
isSelected: isHomeAppointmentAvailable ,
isSelected: isHomeAppointmentAvailable ,
title: LocaleKeys . allowingHomeService . tr ( ) ,
title: LocaleKeys . allowingHomeService . tr ( ) ,
description: LocaleKeys . bookAppointmentAtLocation . tr ( ) ,
description: LocaleKeys . bookAppointmentAtLocation . tr ( ) ,
onSelection: ( bool v ) {
onSelection: ( bool v ) {
isHomeAppointmentAvailable = v ;
isHomeAppointmentAvailable = v ;
model . setState ( ViewState . idle ) ;
if ( isHomeAppointmentAvailable & & ! isAppointmentAvailable ) {
isAppointmentAvailable = true ;
}
serviceVM . setState ( ViewState . idle ) ;
} ,
} ,
) ,
) ,
20. height ,
20. height ,
@ -203,21 +256,25 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
) ,
) ,
) ,
) ,
if ( serviceId ! = - 1 )
if ( serviceId ! = - 1 )
if ( isEditDisabled ) . . . [
Utils . buildStatusContainer ( LocaleKeys . blockedByAdmin . tr ( ) ) ,
] else . . . [
ShowFillButton (
ShowFillButton (
title: LocaleKeys . save . tr ( ) ,
title: LocaleKeys . save . tr ( ) ,
maxWidth: double . infinity ,
maxWidth: double . infinity ,
margin: const EdgeInsets . all ( 20 ) ,
margin: const EdgeInsets . all ( 20 ) ,
onPressed: ( ) {
onPressed: ( ) {
if ( widget . branchModel ! . serviceProviderService ! = null ) {
if ( widget . branchModel ! . serviceProviderService ! = null ) {
updateService ( context , model ) ;
updateService ( context , serviceVM ) ;
} else {
} else {
if ( model . services ! = null ) {
if ( serviceVM . services ! = null ) {
createService ( context , model ) ;
createService ( context , serviceVM ) ;
}
}
}
}
} ,
} ,
) ,
) ,
] ,
] ,
] ,
) ;
) ;
} ,
} ,
) ,
) ,
@ -225,9 +282,15 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
) ;
) ;
}
}
createService ( BuildContext context , ServiceVM model ) async {
createService ( BuildContext context , ServiceVM serviceVM ) async {
if ( isHomeAppointmentAvailable ) {
if ( serviceRage = = 0 | | chargersPerKm . isEmpty | | double . parse ( chargersPerKm ) < 1 ) {
Utils . showToast ( LocaleKeys . chargesAndServiceRangeGreaterThanZero . tr ( ) ) ;
return ;
}
}
List < Map < String , dynamic > > map = [ ] ;
List < Map < String , dynamic > > map = [ ] ;
model . services ! . data ? . forEach ( ( element ) {
serviceVM . services ! . data ? . forEach ( ( element ) {
if ( serviceId = = element . id ) {
if ( serviceId = = element . id ) {
element . isSelected = true ;
element . isSelected = true ;
} else {
} else {
@ -235,12 +298,12 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
}
}
} ) ;
} ) ;
for ( int i = 0 ; i < model . services ! . data ! . length ; i + + ) {
for ( int i = 0 ; i < serviceVM . services ! . data ! . length ; i + + ) {
if ( model . services ! . data ! [ i ] . isSelected ? ? false ) {
if ( serviceVM . services ! . data ! [ i ] . isSelected ? ? false ) {
var postParams = {
var postParams = {
/ / " id " : services ! . data ! [ i ] . id ,
/ / " id " : services ! . data ! [ i ] . id ,
" providerBranchID " : widget . branchModel ! . branchId ,
" providerBranchID " : widget . branchModel ! . branchId ,
" serviceID " : model . services ! . data ! [ i ] . id ,
" serviceID " : serviceVM . services ! . data ! [ i ] . id ,
" isAllowAppointment " : isAppointmentAvailable ,
" isAllowAppointment " : isAppointmentAvailable ,
" isActive " : true ,
" isActive " : true ,
" customerLocationRange " : serviceRage ,
" customerLocationRange " : serviceRage ,
@ -251,8 +314,8 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
}
}
/ / print ( map ) ;
/ / print ( map ) ;
Utils . showLoading ( context ) ;
Utils . showLoading ( context ) ;
GenericRespModel mResponse = await model . createService ( map ) ;
GenericRespModel mResponse = await serviceVM . createService ( map ) ;
await model . getBranchAndServices ( ) ;
await serviceVM . getBranchAndServices ( ) ;
Utils . hideLoading ( context ) ;
Utils . hideLoading ( context ) ;
Utils . showToast ( mResponse . message ? ? " " ) ;
Utils . showToast ( mResponse . message ? ? " " ) ;
if ( mResponse . messageStatus = = 1 ) {
if ( mResponse . messageStatus = = 1 ) {
@ -263,6 +326,13 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
updateService ( BuildContext context , ServiceVM model ) async {
updateService ( BuildContext context , ServiceVM model ) async {
List < Map < String , dynamic > > map = [ ] ;
List < Map < String , dynamic > > map = [ ] ;
if ( isHomeAppointmentAvailable ) {
if ( serviceRage = = 0 | | chargersPerKm . isEmpty | | double . parse ( chargersPerKm ) < 1 ) {
Utils . showToast ( LocaleKeys . chargesAndServiceRangeGreaterThanZero . tr ( ) ) ;
return ;
}
}
if ( isHomeAppointmentAvailable ) {
if ( isHomeAppointmentAvailable ) {
map = [
map = [
{
{
@ -282,7 +352,7 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
}
}
] ;
] ;
}
}
try {
/ / print ( map ) ;
/ / print ( map ) ;
Utils . showLoading ( context ) ;
Utils . showLoading ( context ) ;
GenericRespModel mResponse = await model . updateServices ( map ) ;
GenericRespModel mResponse = await model . updateServices ( map ) ;
@ -291,7 +361,49 @@ class _CreateServicesPage3State extends State<CreateServicesPage3> {
Utils . showToast ( mResponse . message ? ? " " ) ;
Utils . showToast ( mResponse . message ? ? " " ) ;
if ( mResponse . messageStatus = = 1 ) {
if ( mResponse . messageStatus = = 1 ) {
context . read < ServiceVM > ( ) . filterUserBranchCategories ( ) ;
context . read < ServiceVM > ( ) . filterUserBranchCategories ( ) ;
context . read < ServiceVM > ( ) . isNeedRefreshServicesList = true ;
pop ( context ) ;
pop ( context ) ;
}
}
} catch ( e ) {
Utils . hideLoading ( context ) ;
log ( e . toString ( ) ) ;
Utils . showToast ( e . toString ( ) ? ? " " ) ;
}
}
Future < bool > updateServiceStatus ( BuildContext context , bool value ) async {
try {
final serviceVM = context . read < ServiceVM > ( ) ;
List < AppointmentBasicDetailsModel > list = await serviceVM . getAppointmentsByServiceID (
context: context ,
branchId: int . parse ( widget . branchModel ! . branchId ) ,
serviceId: widget . branchModel ! . serviceProviderService ! . serviceProviderServiceId ! ,
) ;
List < int > providerServiceIds = [ ] ;
providerServiceIds . add ( widget . branchModel ! . serviceProviderService ! . serviceProviderServiceId ! ) ;
if ( list . isEmpty ) {
bool status = await serviceVM . updateServiceStatus (
context: context ,
serviceStatusEnum: ServiceStatusEnum . deactivated ,
branchId: int . parse ( widget . branchModel ! . branchId ) ,
providerServiceIds: providerServiceIds ,
) ;
return ! status ;
} else {
serviceVM . buildDealNotCompletedBottomSheetOptions (
mainContext: context ,
appointments: list ,
branchName: widget . branchModel ! . branchName ,
) ;
}
return value ;
} catch ( e ) {
Utils . hideLoading ( context ) ;
log ( e . toString ( ) ) ;
Utils . showToast ( e . toString ( ) ? ? " " ) ;
return value ;
}
}
}
}
}