import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/allergy/get_allergies_list_vida_plus.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/allergies/reactions_selection.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/rounded_container_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../../../../../../core/model/SOAP/allergy/get_patient_allergies_list_vida_plus.dart';
import '../../../../../../core/model/patient/patiant_info_model.dart';
import '../../../../../../widgets/transitions/fade_page.dart';
import '../../objective/widget/EmptyExamination.dart';

class MasterKeyCheckboxSearchAllergiesWidget extends StatefulWidget {
  final SOAPViewModel model;
  final String? buttonName;
  final String? hintSearchText;
  final PatientAllergiesVidaPlus? myAllergiesList;
  final PatiantInformtion patientInfo;

  MasterKeyCheckboxSearchAllergiesWidget({
    Key? key,
    required this.model,
    this.myAllergiesList,
    required this.patientInfo,
    this.buttonName,
    this.hintSearchText,
  }) : super(key: key);

  @override
  _MasterKeyCheckboxSearchAllergiesWidgetState createState() =>
      _MasterKeyCheckboxSearchAllergiesWidgetState();
}

class _MasterKeyCheckboxSearchAllergiesWidgetState
    extends State<MasterKeyCheckboxSearchAllergiesWidget> {
  // List<MasterKeyModel> items = [];
  bool loading = false;
  TextEditingController filteredSearchController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            margin: EdgeInsets.all(20),
            height: Utils.getTextFieldHeight(),
            child: Center(
              child: Container(
                decoration:
                    BoxDecoration(borderRadius: BorderRadius.circular(12)),
                child: Column(
                  children: [
                    AppTextFieldCustom(
                      hintText: TranslationBase.of(context).selectAllergy,
                      isTextFieldHasSuffix: true,
                      hasBorder: true,
                      controller: filteredSearchController,
                      onChanged: (value) {
                        // filterSearchResults(value);
                      },
                      onFieldSubmitted: () {
                      },
                      onEditingComplete: (){
                        loading = true;
                        if(filteredSearchController.text.isNotEmpty) {
                          widget.model
                              .searchAllergies(filteredSearchController.text);
                        }
                      },
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.search,
                          color: Color(0xff2B353E),
                          size: 30,
                        ),
                        onPressed: () {
                          loading = true;
                          widget.model
                              .searchAllergies(filteredSearchController.text);
                        },
                      ),
                    ),
                    // DividerWithSpacesAround(),
                    SizedBox(
                      height: 10,
                    ),
                    Expanded(
                      child: RoundedContainer(
                          width: MediaQuery.of(context).size.width,
                          height: MediaQuery.of(context).size.height * 0.60,
                          child: widget.model.searchAllergiesVidaPlus.isEmpty
                                  ? Center(
                                      child: EmptyWidget(
                                          TranslationBase.of(context)
                                              .noDataAvailable))
                                  : ListView.builder(
                                      itemCount: widget
                                          .model.searchAllergiesVidaPlus.length,
                                      itemBuilder: (context, index) {
                                        loading = false;
                                        return ListTile(
                                          title: AppText(widget
                                              .model
                                              .searchAllergiesVidaPlus[index]
                                              .allergyName!),
                                          trailing: TextButton.icon(
                                            icon: SvgPicture.asset(
                                              "assets/images/svgs/add-square.svg",
                                              color: Color(0xffD02127),
                                            ),
                                            style: ButtonStyle(

                                              // iconColor:
                                              //     WidgetStateProperty.all<
                                              //         Color>()),
                                            ),
                                            onPressed: () {
                                              openReaction(
                                                  widget.model,
                                                  widget.model
                                                          .searchAllergiesVidaPlus[
                                                      index]);
                                            },
                                            label: AppText(
                                              TranslationBase.of(context).add,
                                              fontSize: 12,
                                              color: Color(0xffD02127),
                                            ),
                                          ),
                                        );
                                      },
                                    )
                              ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  openReaction(model, AllergiesListVidaPlus mySelectedAllergy) {
    Navigator.of(context).pop();
    Navigator.push(
        context,
        FadePage(
            page: ReactionsSelectionAllergiesWidget(
                model: model,
                mySelectedAllergy: mySelectedAllergy,
                patientInfo: widget.patientInfo)));
  }
}
