import 'package:doctor_app_flutter/core/enum/patient_type.dart';
import 'package:doctor_app_flutter/core/model/patient/patient_model.dart';
import 'package:doctor_app_flutter/core/model/patient_muse/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/routes.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patient_search_header.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_card/PatientCard.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app_text_field_custom_serach.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class PatientsSearchResultScreen extends StatefulWidget {
  final PatientType? selectedPatientType;
  final PatientSearchRequestModel? patientSearchRequestModel;
  final bool isSearchWithKeyInfo;
  final bool isSearch;
  final bool isInpatient;
  final bool isSearchAndOut;
  final String? searchKey;

  PatientsSearchResultScreen(
      {this.selectedPatientType, this.patientSearchRequestModel, this.isSearchWithKeyInfo = true, this.isSearch = false, this.isInpatient = false, this.searchKey, this.isSearchAndOut = false});

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

class _PatientsSearchResultScreenState extends State<PatientsSearchResultScreen> {
  int? clinicId;
  AuthenticationViewModel authenticationViewModel = AuthenticationViewModel();

  String patientType = '';
  String? patientTypeTitle;
  var selectedFilter = 1;
  String arrivalType = '';
  late ProjectViewModel projectsProvider;
  var isView;
  final _controller = TextEditingController();

  late PatientModel patient;

  @override
  Widget build(BuildContext context) {
    authenticationViewModel = Provider.of(context);
    return BaseView<PatientSearchViewModel>(
      onModelReady: (model) async {
        if (!widget.isSearchWithKeyInfo && widget.selectedPatientType == PatientType.OutPatient) {
          await model.getOutPatient(widget.patientSearchRequestModel!);
        } else {
          await model.getPatientFileInformation(widget.patientSearchRequestModel!);
        }
      },
      builder: (_, model, w) => AppScaffold(
          appBarTitle: "Search Patient",
          isShowAppBar: true,
          appBar: PatientSearchHeader(
            title: "Search for ${widget.searchKey}",
          ),
          baseViewModel: model,
          body: Column(
            children: [
              SizedBox(height: 18.5),
              AppTextFieldCustomSearch(
                searchController: _controller,
                onChangeFun: (value) {
                  model.searchData(value);
                },
                marginTop: 5,
                suffixIcon: IconButton(
                  icon: Icon(
                    DoctorApp.filter_1,
                    color: Colors.black,
                  ),
                  iconSize: 20, onPressed: () {},
                  // padding: EdgeInsets.only(bottom: 30),
                ),
                onFieldSubmitted: () {},
              ),
              SizedBox(
                height: 10.0,
              ),
              Expanded(
                child: Container(
                    child: model.filterData.isEmpty
                        ? Center(
                            child: ErrorMessage(
                              error: TranslationBase.of(context).youDontHaveAnyPatient,
                            ),
                          )
                        : ListView.builder(
                            scrollDirection: Axis.vertical,
                            shrinkWrap: true,
                            itemCount: model.filterData.length,
                            padding: EdgeInsets.zero,
                            itemBuilder: (BuildContext ctxt, int index) {
                              return Padding(
                                padding: EdgeInsets.all(8.0),
                                child: PatientCard(
                                  patientInfo: model.filterData[index],
                                  patientType: patientType,
                                  arrivalType: arrivalType,
                                  isFromSearch: widget.isSearchAndOut,
                                  isInpatient: widget.isInpatient,
                                  onTap: () {
                                    // TODO change the parameter to daynamic
                                    Navigator.of(context).pushNamed(PATIENTS_PROFILE, arguments: {
                                      "patient": model.filterData[index],
                                      "patientType": "1",
                                      "from": widget.patientSearchRequestModel!.from,
                                      "to": widget.patientSearchRequestModel!.from,
                                      "isSearch": widget.isSearch,
                                      "isInpatient": widget.isInpatient,
                                      "arrivalType": "7",
                                      "isSearchAndOut": widget.isSearchAndOut,
                                    });
                                  },
                                  // isFromSearch: widget.isSearch,
                                ),
                              );
                            })),
              ),
            ],
          )),
    );
  }
}
