import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/hmg_services/hmg_services_view_model.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/req_models/search_e_referral_req_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/e_referral/widget/search_e_referral_form.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';
import 'e_referral_form_manager.dart';
import 'e_referral_search_result.dart';

class SearchEReferralPage extends StatefulWidget {

  const SearchEReferralPage({
    super.key,
  });

  @override
  State<SearchEReferralPage> createState() => _SearchEReferralPageState();
}

class _SearchEReferralPageState extends State<SearchEReferralPage> {
  final ReferralFormManager _formManager = ReferralFormManager();
  late HmgServicesViewModel hmgServicesVM;
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  void _handleSearch() {
    _formManager.validateSearchForm();
    if (!_formManager.isSearchFormInValid) {
      _searchReferral();
    }
  }

  void _searchReferral() {
    SearchEReferralRequestModel searchEReferralReq;

    if (_formManager.searchCriteria == 0) {
      searchEReferralReq = SearchEReferralRequestModel(
        identificationNo: _formManager.searchValue,
        patientMobileNumber: _formManager.formData.countryEnum.countryCode + _formManager.searchPhone!,
        referralNumber: 0,
      );
    } else {

      searchEReferralReq = SearchEReferralRequestModel(referralNumber: int.parse(_formManager.searchValue!), patientMobileNumber: _formManager.formData.patientPhone, identificationNo: '');
    }

    hmgServicesVM = context.read<HmgServicesViewModel>();
    LoaderBottomSheet.showLoader();
    hmgServicesVM.searchEReferral(
      requestModel: searchEReferralReq,
      onSuccess: (response) async {
        LoaderBottomSheet.hideLoader();
        await Navigator.of(context).push(
          CustomPageRoute(
            page: SearchResultPage(),
            fullScreenDialog: true,
            direction: AxisDirection.down,
          ),
        );
      },

    );
  }

  void _loadData() {
    WidgetsBinding.instance.addPostFrameCallback((_) {});
  }

  @override
  Widget build(BuildContext context) {
    return CollapsingListView(
      title: LocaleKeys.searchEReferral.tr(context: context),
      isClose: true,
      bottomChild: Container(
        color: Colors.white,
        padding: EdgeInsets.all(ResponsiveExtension(20).h),
        child: CustomButton(
          text: LocaleKeys.search.tr(),
          icon: AppAssets.search_icon,
          iconColor: Colors.white,
          onPressed: () => {
            _handleSearch()
          },
        ),
      ),
      child: ChangeNotifierProvider.value(
          value: _formManager,
          child: Column(
            children: [
              _buildHeader(),
              SearchEReferralFormForm(),

            ],
          )),
    );
  }

  Widget _buildHeader() {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [SizedBox(height: 8), LocaleKeys.enterRequiredInfoToSearch.tr(context: context).toText12()],
      ),
    );
  }

}
