import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-admission-request-viewmodel.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.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/date-utils.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/profile/patient-page-header-widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_buttons_widget.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/dialogs/dailog-list-select.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

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

class AdmissionRequestDetailScreen extends StatefulWidget {
  @override
  _AdmissionRequestDetailScreenState createState() =>
      _AdmissionRequestDetailScreenState();
}

class _AdmissionRequestDetailScreenState
    extends State<AdmissionRequestDetailScreen> {
  DateTime selectedDate;
  dynamic _selectedSpeciality;
  dynamic _selectedDoctor;

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    final screenSize = MediaQuery.of(context).size;
    ProjectViewModel projectViewModel = Provider.of(context);

    return BaseView<AdmissionRequestViewModel>(
      onModelReady: (model) => model.getSpecialityList(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).admissionRequest,
        body: model.doctorsList != null
            ? Column(
                children: [
                  Expanded(
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          PatientPageHeaderWidget(patient),
                          Container(
                            margin: EdgeInsets.symmetric(
                                vertical: 16, horizontal: 16),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  height: 16,
                                ),
                                AppText(
                                  TranslationBase.of(context).patientDetails,
                                  fontWeight: FontWeight.bold,
                                  fontSize: SizeConfig.textMultiplier * 2.5,
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                    decoration:
                                        Helpers.containerBorderDecoration(
                                            Color(0xFFEEEEEE),
                                            Color(0xFFCCCCCC),
                                            borderWidth: 0.0),
                                    height: screenSize.height * 0.070,
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              "Pre Admission Number :01",
                                              null,
                                              false),
                                      enabled: false,
                                      // controller: _remarksController,
                                      keyboardType: TextInputType.text,
                                    )),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                  height: screenSize.height * 0.070,
                                  child: InkWell(
                                    onTap: () => _selectDate(context, model),
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              TranslationBase.of(context).date,
                                              selectedDate != null
                                                  ? "${DateUtils.convertStringToDateFormat(selectedDate.toString(), "yyyy-MM-dd")}"
                                                  : null,
                                              true,
                                              suffixIcon: Icon(
                                                Icons.calendar_today,
                                                color: Colors.black,
                                              )),
                                      enabled: false,
                                    ),
                                  ),
                                ),
                                SizedBox(
                                  height: 20,
                                ),
                                AppText(
                                  TranslationBase.of(context)
                                      .specialityAndDoctorDetail,
                                  fontWeight: FontWeight.bold,
                                  fontSize: SizeConfig.textMultiplier * 2.5,
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                  height: screenSize.height * 0.070,
                                  child: InkWell(
                                    onTap: () {
                                      ListSelectDialog dialog =
                                          ListSelectDialog(
                                              list: model.speciality,
                                              attributeName:
                                                  projectViewModel.isArabic
                                                      ? 'nameAr'
                                                      : 'nameEn',
                                              attributeValueId: 'id',
                                              okText:
                                                  TranslationBase.of(context)
                                                      .ok,
                                              okFunction: (selectedValue) {
                                                setState(() {
                                                  _selectedSpeciality =
                                                      selectedValue;
                                                });
                                              });
                                      showDialog(
                                        barrierDismissible: false,
                                        context: context,
                                        builder: (BuildContext context) {
                                          return dialog;
                                        },
                                      );
                                    },
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              TranslationBase.of(context)
                                                  .speciality,
                                              _selectedSpeciality != null
                                                  ? projectViewModel.isArabic
                                                      ? _selectedSpeciality[
                                                          'nameAr']
                                                      : _selectedSpeciality[
                                                          'nameEn']
                                                  : null,
                                              true),
                                      enabled: false,
                                    ),
                                  ),
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                  height: screenSize.height * 0.070,
                                  child: InkWell(
                                    onTap: model.doctorsList != null &&
                                            model.doctorsList.length > 0
                                        ? () {
                                            ListSelectDialog dialog =
                                                ListSelectDialog(
                                              list: model.doctorsList,
                                              attributeName: 'DoctorName',
                                              attributeValueId: 'DoctorID',
                                              usingSearch: true,
                                              hintSearchText:
                                                  TranslationBase.of(context)
                                                      .doctorSearch,
                                              okText:
                                                  TranslationBase.of(context)
                                                      .ok,
                                              okFunction: (selectedValue) {
                                                setState(() {
                                                  _selectedDoctor =
                                                      selectedValue;
                                                });
                                              },
                                            );
                                            showDialog(
                                              barrierDismissible: false,
                                              context: context,
                                              builder: (BuildContext context) {
                                                return dialog;
                                              },
                                            );
                                          }
                                        : null,
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              TranslationBase.of(context)
                                                  .doctor,
                                              _selectedDoctor != null
                                                  ? _selectedDoctor[
                                                      'DoctorName']
                                                  : null,
                                              true),
                                      enabled: false,
                                    ),
                                  ),
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                  height: screenSize.height * 0.070,
                                  decoration: Helpers.containerBorderDecoration(
                                      Color(0xFFEEEEEE), Color(0xFFCCCCCC),
                                      borderWidth: 0.0),
                                  child: InkWell(
                                    onTap: () => null,
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              TranslationBase.of(context)
                                                  .referringDate,
                                              null,
                                              true,
                                              suffixIcon: Icon(
                                                Icons.calendar_today,
                                                color: Color(0xFFCCCCCC),
                                              )),
                                      enabled: false,
                                    ),
                                  ),
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                                Container(
                                    decoration:
                                        Helpers.containerBorderDecoration(
                                            Color(0xFFEEEEEE),
                                            Color(0xFFCCCCCC),
                                            borderWidth: 0.0),
                                    height: screenSize.height * 0.070,
                                    child: TextField(
                                      decoration:
                                          Helpers.textFieldSelectorDecoration(
                                              TranslationBase.of(context)
                                                  .referringDoctor,
                                              null,
                                              true,
                                              dropDownColor: Color(0xFFCCCCCC)),
                                      enabled: false,
                                      // controller: _remarksController,
                                      keyboardType: TextInputType.text,
                                    )),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    child: AppButton(
                      title: TranslationBase.of(context).next,
                      color: HexColor("#B8382B"),
                      onPressed: () {
                        Navigator.of(context).pushNamed(
                            PATIENT_ADMISSION_REQUEST_2,
                            arguments: {'patient': patient});
                      },
                    ),
                  ),
                ],
              )
            : Container(),
      ),
    );
  }

  _selectDate(BuildContext context, AdmissionRequestViewModel model) async {
    selectedDate = DateTime.now();
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime.now().add(Duration(hours: 2)),
      lastDate: DateTime(2040),
      initialEntryMode: DatePickerEntryMode.calendar,
    );
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }
}
