import 'package:car_provider_app/generated/locale_keys.g.dart'; import 'package:car_provider_app/view_models/service_view_model.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:car_provider_app/view_models/branch_view_model.dart'; import 'package:car_provider_app/views/location/pick_location_page.dart'; import 'package:mc_common_app/extensions/int_extensions.dart'; import 'package:mc_common_app/extensions/string_extensions.dart'; import 'package:mc_common_app/models/general_models/m_response.dart'; import 'package:mc_common_app/models/provider_branches_models/branch_detail_model.dart'; import 'package:mc_common_app/theme/colors.dart'; import 'package:mc_common_app/utils/enums.dart'; import 'package:mc_common_app/utils/navigator.dart'; import 'package:mc_common_app/utils/utils.dart'; import 'package:mc_common_app/widgets/button/show_fill_button.dart'; import 'package:mc_common_app/widgets/common_widgets/app_bar.dart'; import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart'; import 'package:mc_common_app/widgets/txt_field.dart'; import 'package:provider/provider.dart'; class DefineBranchPage extends StatelessWidget { BranchDetailModel? branchData; DefineBranchPage(this.branchData); @override Widget build(BuildContext context) { ServiceVM branchVM = context.read(); branchVM.getAllCountriesList(branchData, EasyLocalization.of(context)?.currentLocale?.countryCode ?? "SA"); return Scaffold( appBar: CustomAppBar( title: LocaleKeys.defineBranches.tr(), ), body: Consumer(builder: (context, model, _) { return SizedBox( width: double.infinity, height: double.infinity, child: Column( children: [ Expanded( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ // LocaleKeys.chooseCountry.tr().toText(fontSize: 14, color: MyColors.lightTextColor), // 8.height, model.country != null ? DropdownField( (DropValue value) { model.countryCode = value.subValue; model.countryId = value.id; model.getAllCities(branchData, EasyLocalization.of(context)?.currentLocale?.countryCode ?? "SA"); }, list: model.countryDropList, dropdownValue: model.countryValue, hint: "${LocaleKeys.chooseCountry.tr()}*", ) : const CircularProgressIndicator(), 12.height, if (model.countryId != -1) model.cities != null ? model.citiesDropList.isEmpty ? Text(LocaleKeys.no_city_available.tr()) : DropdownField( (DropValue value) { // countryCode = value.subValue; model.setOnlyState(ViewState.busy); model.cityId = value.id; model.setState(ViewState.idle); }, list: model.citiesDropList, dropdownValue: model.cityValue, hint: "${LocaleKeys.chooseCity.tr()}*", ) : const CircularProgressIndicator(), 12.height, if (model.cityId != -1) Column( children: [ TxtField( hint: "${LocaleKeys.branchName.tr()}*", value: model.branchName, onChanged: (v) { model.branchName = v; }, ), 12.height, TxtField( hint: "${LocaleKeys.branchDescription.tr()}*", value: model.branchDescription, onChanged: (v) { model.branchDescription = v; }, ), 12.height, TxtField( hint: LocaleKeys.address.tr(), isNeedClickAll: false, maxLines: 5, value: model.address, onChanged: (v) { model.address = v; }, ), 12.height, InkWell( onTap: () { navigateTo( context, PickLocationPage( onPickAddress: (double latitude, double longitude, String address) { model.latitude = latitude; model.longitude = longitude; model.address = address; model.setState(ViewState.idle); }, ), ); }, child: Container( width: double.infinity, height: 45, decoration: BoxDecoration( color: Colors.transparent, border: Border.all(color: MyColors.darkPrimaryColor, width: 2), borderRadius: const BorderRadius.all(Radius.circular(8)), ), child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ const Icon( Icons.place_outlined, size: 18, color: MyColors.darkPrimaryColor, ), 8.width, Text( LocaleKeys.pickAddress.tr(), style: const TextStyle( color: MyColors.darkPrimaryColor, ), ), const Icon( Icons.place_outlined, size: 18, color: Colors.transparent, ), ], ), ), ), ], ), ], ), ), ), ), SizedBox( width: double.infinity, child: Padding( padding: const EdgeInsets.only(left: 20, right: 20, bottom: 20), child: ShowFillButton( title: branchData != null ? (branchData!.id == null ? LocaleKeys.createBranch.tr() : LocaleKeys.updateBranch.tr()) : LocaleKeys.createBranch.tr(), onPressed: () async { if (branchData == null) { Utils.showLoading(context); MResponse res = await model.createBranch(model.branchName, model.branchDescription, model.cityId.toString(), model.address, model.latitude.toString(), model.longitude.toString()); Utils.hideLoading(context); if (res.messageStatus == 1) { Utils.showToast(LocaleKeys.branch_created.tr()); pop(context); model.getBranchAndServices(); } else { Utils.showToast(res.message ?? ""); } } else { Utils.showLoading(context); MResponse res = await model.updateBranch( branchData!.id ?? 0, model.branchName, model.branchDescription, model.cityId.toString(), model.address, model.latitude.toString(), model.longitude.toString()); Utils.hideLoading(context); if (res.messageStatus == 1) { Utils.showToast(LocaleKeys.branch_updated.tr()); pop(context); pop(context); model.getBranchAndServices(); } else { Utils.showToast(res.message ?? ""); } } }, ), ), ), ], ), ); }), ); } }