import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/patients/PatientCard.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/errors/error_message.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:doctor_app_flutter/widgets/shared/text_fields/text_fields_utils.dart';
import 'package:flutter/material.dart';

import '../../routes.dart';

class PatientInPatientScreen extends StatefulWidget {
  @override
  _PatientInPatientScreenState createState() => _PatientInPatientScreenState();
}

class _PatientInPatientScreenState extends State<PatientInPatientScreen> {
  PatientSearchRequestModel requestModel = PatientSearchRequestModel();
  int _activeTab = 0;

  TextEditingController _searchController = TextEditingController();

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return BaseView<PatientSearchViewModel>(
      onModelReady: (model) => model.getInPatientList(requestModel),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: EdgeInsets.only(left: 0, right: 5, bottom: 5, top: 5),
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              child: Container(
                padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                margin: EdgeInsets.only(top: 50),
                child: Row(children: [
                  IconButton(
                    icon: Icon(Icons.arrow_back_ios),
                    color: Colors.black, //Colors.black,
                    onPressed: () => Navigator.pop(context),
                  ),
                  Expanded(
                    child: AppText(
                      TranslationBase.of(context).inPatient,
                      fontSize: SizeConfig.textMultiplier * 2.8,
                      fontWeight: FontWeight.bold,
                      color: Color(0xFF2B353E),
                      fontFamily: 'Poppins',
                    ),
                  ),
                ]),
              ),
            ),
            tabsBar(context, screenSize, model),
            Container(
              margin: EdgeInsets.all(16.0),
              child: AppTextFieldCustom(
                  hintText: TranslationBase.of(context)
                      .searchPatientName,
                  isTextFieldHasSuffix: true,
                  suffixIcon: IconButton(
                    icon: Icon(
                      Icons.search,
                      color: Colors.black,
                    ),
                    onPressed: () {},
                  ),
                  controller: _searchController,
                  onChanged: (value) {
                    model.filterSearchResults(value);
                  }),
            ),
            model.filteredInPatientItems.length > 0
                ? Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 16.0),
                      child: SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // const SizedBox(
                            //   height: 16,
                            // ),
                            ...model.filteredInPatientItems.map((item) {
                              return PatientCard(
                                patientInfo: item,
                                patientType: "1",
                                arrivalType: "1",
                                isInpatient: true,
                                onTap: () {
                                  FocusScopeNode currentFocus = FocusScope.of(context);
                                  if (!currentFocus.hasPrimaryFocus) {
                                    currentFocus.unfocus();
                                  }

                                  Navigator.of(context)
                                      .pushNamed(PATIENTS_PROFILE, arguments: {
                                    "patient": item,
                                    "patientType": "1",
                                    "from": "0",
                                    "to": "0",
                                    "isSearch": false,
                                    "isInpatient": true,
                                    "arrivalType": "1",
                                  });
                                },
                              );
                            }).toList(),
                          ],
                        ),
                      ),
                    ),
                  )
                : Expanded(
                  child: SingleChildScrollView(
                    child: Container(
                        child: ErrorMessage(
                            error: TranslationBase.of(context).noDataAvailable)),
                  ),
                ),
          ],
        ),
      ),
    );
  }

  Widget tabsBar(
      BuildContext context, Size screenSize, PatientSearchViewModel model) {
    List<String> _tabs = [
      TranslationBase.of(context).inPatientAll.toUpperCase(),
      TranslationBase.of(context).inPatient.toUpperCase(),
    ];

    return Container(
      height: screenSize.height * 0.070,
      decoration: TextFieldsUtils.containerBorderDecoration(
          Color(0Xffffffff), Color(0xFFCCCCCC),
          borderRadius: 4, borderWidth: 0),
      child: Row(
        mainAxisSize: MainAxisSize.max,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: _tabs.map((item) {
          bool _isActive = _tabs[_activeTab] == item ? true : false;

          return Expanded(
            child: InkWell(
              onTap: () async {
                setState(() {
                  _activeTab = _tabs.indexOf(item);
                });
                FocusScopeNode currentFocus = FocusScope.of(context);
                if (!currentFocus.hasPrimaryFocus) {
                  currentFocus.unfocus();
                }
                GifLoaderDialogUtils.showMyDialog(
                    context);
               await model.getInPatientList(requestModel,
                    isMyInpatient: _activeTab == 1, isFirstTime: false);
                GifLoaderDialogUtils.hideDialog(
                    context);
              },
              child: Center(
                child: Container(
                  height: screenSize.height * 0.070,
                  decoration: TextFieldsUtils.containerBorderDecoration(
                      _isActive
                          ? Color(0xFFD02127 /*B8382B*/)
                          : Color(0xFFEAEAEA),
                      _isActive ? Color(0xFFD02127) : Color(0xFFEAEAEA),
                      borderRadius: 4,
                      borderWidth: 0),
                  child: Center(
                    child: AppText(
                      item,
                      fontSize: SizeConfig.textMultiplier * 1.8,
                      color: _isActive ? Colors.white : Color(0xFF2B353E),
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
