import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/er/TriageInformationRequest.dart';
import 'package:diplomaticquarterapp/core/model/er/TriageQuestionsModel.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/er/EdOnlineViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/Blood/new_text_Field.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:provider/provider.dart';

class EdOnlineNotesPage extends StatefulWidget {
  final List<TriageQuestionsModel> selectedQuestions;
  final Function changePageViewIndex;
  TriageInformationRequest triageInformationRequest;
   EdOnlineNotesPage(
      {Key key, this.selectedQuestions, this.changePageViewIndex,this.triageInformationRequest})
  ;

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

class _EdOnlineNotesPageState extends State<EdOnlineNotesPage> {
  TextEditingController _chiefComplaintsTextController =
      TextEditingController();
  TextEditingController _noteTextController = TextEditingController();
  DateTime selectedTime;
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<EdOnlineViewModel>(
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                Form(
                  key: _formKey,
                  child: NewTextFields(
                    controller: _chiefComplaintsTextController,
                    maxLines: 15,
                    minLines: 5,
                    hintText: TranslationBase.of(context).chiefComplaints,
                    validator: (value) {
                      if (value.isEmpty) return TranslationBase.of(context).errorChiefComplaints;
                      return null;
                    },
                  ),
                ),
                SizedBox(
                  height: 8,
                ),
                InkWell(
                  onTap: () {
                    DatePicker.showDateTimePicker(
                      context,
                      showTitleActions: true,
                      minTime: DateTime.now(),
                      maxTime: DateTime.now().add(Duration(hours: 24)),
                      onConfirm: (date) {
                        setState(() {
                          selectedTime = date;
                        });
                      },
                      currentTime: DateTime.now(),
                      locale: projectViewModel.localeType,
                    );
                  },
                  child: Container(
                    padding: EdgeInsets.all(12),
                    width: double.infinity,
                    // height: 65,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12),
                        color: Colors.white),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Texts(selectedTime == null ?TranslationBase.of(context).errorExpectedArrivalTime:TranslationBase.of(context).expectedArrivalTime),
                        Texts(getDate(context)),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 8,
                ),
                NewTextFields(
                  controller: _noteTextController,
                  maxLines: 15,
                  minLines: 5,
                  hintText: TranslationBase.of(context).notes,
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          height: 56,
          margin: EdgeInsets.only(bottom: 5),
          width: double.infinity,
          child: Row(
            children: [
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(left: 5,right: 5),
                  child: SecondaryButton(
                    textColor: Colors.white,
                    color: Theme.of(context).primaryColor,
                    label: TranslationBase.of(context).back.toUpperCase(),
                    onTap: () => widget.changePageViewIndex(2),
                  ),
                ),
              ),
              SizedBox(width: 10,),
              Expanded(
                child: Container(
                  margin: EdgeInsets.only(left: 5,right: 5),
                  child: SecondaryButton(
                      textColor: Colors.white,
                      color: Theme.of(context).primaryColor,
                      label: TranslationBase.of(context).save.toUpperCase(),
                      disabled: selectedTime == null,
                      onTap: () async {
                        if (_formKey.currentState.validate()) {
                          GifLoaderDialogUtils.showMyDialog(context);
                          model.saveQuestionsInformation(
                              chiefComplaint:
                                  _chiefComplaintsTextController.text.toString(),
                              notes: _noteTextController.text.toString(),
                              selectedQuestions: widget.selectedQuestions,
                              projectId: widget.triageInformationRequest.projectID,selectedTime: selectedTime).then((value) {
                            GifLoaderDialogUtils.hideDialog(context);
                            if(model.state == ViewState.ErrorLocal)
                              AppToast.showErrorToast(message: model.error);
                            else
                              {
                                widget.changePageViewIndex(4);
                              }

                          }).catchError((onError){
                            GifLoaderDialogUtils.hideDialog(context);
                            AppToast.showErrorToast(message: onError.toString());
                          });
                        }
                      }),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  getDate(BuildContext context) {
    String message = "";
    if (selectedTime != null) {
      message = "${selectedTime.hour}:${selectedTime.minute}";
    }
    return message;
  }
}
