import 'package:doctor_app_flutter/core/enum/patient_type.dart';
import 'package:doctor_app_flutter/core/model/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patients_screen_new.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

class PatientSearchScreenNew extends StatefulWidget {
  @override
  _PatientSearchScreenNewState createState() => _PatientSearchScreenNewState();
}

class _PatientSearchScreenNewState extends State<PatientSearchScreenNew> {
  bool showOther = false;
  bool isFormSubmitted = false;
  TextEditingController patientFileInfoController = TextEditingController();
  TextEditingController firstNameInfoController = TextEditingController();
  TextEditingController middleNameInfoController = TextEditingController();
  TextEditingController lastNameFileInfoController = TextEditingController();
  PatientType selectedPatientType = PatientType.inPatient;

  @override
  Widget build(BuildContext context) {
    return BaseView<PatientSearchViewModel>(
      onModelReady: (model) async {},
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              children: [
                BottomSheetTitle(
                    title: TranslationBase.of(context).searchPatient),
                FractionallySizedBox(
                  widthFactor: 0.9,
                  child: Container(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(
                              height: 16,
                            ),
                            AppText(
                              'Patient Type',
                              fontWeight: FontWeight.w600,
                            ),
                            Row(
                              children: [
                                Row(
                                  children: [
                                    Radio(
                                      activeColor: Color(0xFFB9382C),
                                      value: PatientType.inPatient,
                                      groupValue: selectedPatientType,
                                      onChanged: (value) {
                                        setState(() {
                                          selectedPatientType =
                                              PatientType.inPatient;
                                        });
                                      },
                                    ),
                                    Text('InPatient'),
                                  ],
                                ),
                                Row(
                                  children: [
                                    Radio(
                                      activeColor: Color(0xFFB9382C),
                                      value: PatientType.OutPatient,
                                      groupValue: selectedPatientType,
                                      onChanged: (value) {
                                        setState(() {
                                          selectedPatientType =
                                              PatientType.OutPatient;
                                        });
                                      },
                                    ),
                                    Text('OutPatient'),
                                  ],
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 10,
                            ),
                            Container(
                              margin:
                                  EdgeInsets.only(left: 0, right: 0, top: 15),
                              child: AppTextFieldCustom(
                                hintText: TranslationBase.of(context)
                                    .patpatientIDMobilenationalientID,
                                isTextFieldHasSuffix: false,
                                maxLines: 1,
                                minLines: 1,
                                hasBorder: true,
                                controller: patientFileInfoController,
                                validationError: (isFormSubmitted && (
                                            patientFileInfoController
                                                .text.isEmpty &&
                                        firstNameInfoController
                                            .text.isEmpty &&
                                        middleNameInfoController
                                            .text.isEmpty &&
                                        lastNameFileInfoController
                                            .text.isEmpty))
                                    ? TranslationBase.of(context).emptyMessage
                                    : null,
                              ),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                InkWell(
                                  child: this.showOther == false
                                      ? AppText(
                                          TranslationBase.of(context)
                                              .searchWithOther,
                                          color: Colors.red,
                                          fontWeight: FontWeight.bold,
                                        )
                                      : AppText(
                                          TranslationBase.of(context)
                                              .hideOtherCriteria,
                                          color: Colors.red,
                                          fontWeight: FontWeight.bold),
                                  onTap: () {
                                    setState(() {
                                      this.showOther = !this.showOther;
                                    });
                                  },
                                )
                              ],
                            ),
                            SizedBox(
                              height: 30,
                            ),
                            if (showOther)
                              Column(
                                children: [
                                  AppTextFieldCustom(
                                    hintText:
                                        TranslationBase.of(context).firstName,
                                    controller: firstNameInfoController,
                                    maxLines: 1,
                                    minLines: 1,
                                    hasBorder: true,
                                    onChanged: (_) {},
                                    // validationError:illnessController.text.isEmpty && illnessControllerError !=''?illnessControllerError:null ,
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  AppTextFieldCustom(
                                    hintText:
                                        TranslationBase.of(context).middleName,
                                    controller: middleNameInfoController,
                                    maxLines: 1,
                                    minLines: 1,
                                    onChanged: (_) {},
                                    hasBorder: true,
                                    // validationError:illnessController.text.isEmpty && illnessControllerError !=''?illnessControllerError:null ,
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  AppTextFieldCustom(
                                    hintText:
                                        TranslationBase.of(context).lastName,
                                    controller: lastNameFileInfoController,
                                    maxLines: 1,
                                    minLines: 1,
                                    onChanged: (_) {},
                                    hasBorder: true,
                                    // validationError:illnessController.text.isEmpty && illnessControllerError !=''?illnessControllerError:null ,
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                ],
                              ),
                            SizedBox(
                              height: MediaQuery.of(context).size.height * 0.12,
                            ),
                          ])),
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(
              Radius.circular(0.0),
            ),
            border: Border.all(color: HexColor('#707070'), width: 0),
          ),
          height: MediaQuery.of(context).size.height * 0.1,
          width: double.infinity,
          child: Column(
            children: [
              SizedBox(
                height: 10,
              ),
              Container(
                child: FractionallySizedBox(
                  widthFactor: .80,
                  child: Center(
                    child: AppButton(
                      fontWeight: FontWeight.w700,
                      title: TranslationBase.of(context).search,
                      onPressed: () {
                        onSubmitSearch();
                      },
                      color: Colors.red[800],
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 5,
              ),
            ],
          ),
        ),
      ),
    );
  }

  onSubmitSearch() {
    GifLoaderDialogUtils.showMyDialog(context);
    setState(() {
      isFormSubmitted = true;
    });
    PatientSearchRequestModel patientSearchRequestModel =
        PatientSearchRequestModel();
    if (showOther) {
      patientSearchRequestModel.firstName = firstNameInfoController.text.trim().isEmpty?"0":firstNameInfoController.text.trim();
      patientSearchRequestModel.middleName = middleNameInfoController.text.trim().isEmpty?"0":middleNameInfoController.text.trim();
      patientSearchRequestModel.lastName = lastNameFileInfoController.text.isEmpty?"0":lastNameFileInfoController.text.trim();
    }

    if (patientFileInfoController.text.isNotEmpty) {
      if (patientFileInfoController.text.length == 10 &&
          (patientFileInfoController.text[0] == '2' ||
              patientFileInfoController.text[0] == '1')) {
        patientSearchRequestModel.identificationNo =
            patientFileInfoController.text;
        patientSearchRequestModel.searchType = 2;
        patientSearchRequestModel.patientID = 0;
      } else if ((patientFileInfoController.text.length == 10 ||
              patientFileInfoController.text.length == 9) &&
          ((patientFileInfoController.text[0] == '0' &&
                  patientFileInfoController.text[1] == '5') ||
              patientFileInfoController.text[0] == '5')) {
        patientSearchRequestModel.mobileNo = patientFileInfoController.text;
        patientSearchRequestModel.searchType = 0;
      } else {
        patientSearchRequestModel.patientID =
            int.parse(patientFileInfoController.text);
        patientSearchRequestModel.searchType = 1;
      }
    }

    GifLoaderDialogUtils.hideDialog(context);

    if (patientFileInfoController.text.isNotEmpty ||
        firstNameInfoController.text.isNotEmpty ||
        middleNameInfoController.text.isNotEmpty ||
        lastNameFileInfoController.text.isNotEmpty) {
      setState(() {
        isFormSubmitted = false;
      });
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (BuildContext context) => PatientsScreenNew(
            selectedPatientType: selectedPatientType,
            patientSearchRequestModel: patientSearchRequestModel,
            isSearchWithKeyInfo:
                patientFileInfoController.text.isNotEmpty ? true : false,
            isSearch: true,
            searchKey: patientFileInfoController.text,
            isInpatient: selectedPatientType == PatientType.OutPatient && patientFileInfoController.text.isEmpty?false: true,
          ),
        ),
      );
    }
  }
}
