import 'package:date_format/date_format.dart';
import 'package:fis_common/helpers/color.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/liveConsultation.m.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:fis_ui/widgets/checkbox/rectangle_checkbox.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/js_platform/listener.dart';
import 'package:flyinsono/managers/interfaces/appointment_consultation.dart';
import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/add_participants.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/application_info.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/simple_Info.dart';
import 'package:get/get.dart';

class ComfirmApplicationPage extends FisView implements FPage {
  final RealTimeConsultationController controller;
  @override
  final String pageName = 'ComfirmApplicationPage';
  const ComfirmApplicationPage({Key? key, required this.controller})
      : super(key: key);
  FWidget build(BuildContext context) {
    return _DesktopLayout(
      controller: controller,
    );
  }
}

class _DesktopLayout extends FStatefulWidget {
  final FInteractiveContainer businessParent = JsListener();
  final RealTimeConsultationController controller;
  _DesktopLayout({Key? key, required this.controller}) : super(key: key);

  @override
  FState<FStatefulWidget> createState() {
    return _DesktopLayoutState(businessParent);
  }
}

class _DesktopLayoutState extends FState<_DesktopLayout> {
  final FInteractiveContainer businessParent;
  final appointmentConsultationManager = Get.find<IAppointmentConsultation>();
  late DateTime currentTime = DateTime.now();
  late DateTime delayedTime = currentTime.add(Duration(minutes: 30));

  late RealTimeConsultationController controller;
  late DateTime _datetime; //日期选择，默认是申请人申请的时间
  late TimeOfDay _startTime; //开始时间
  late TimeOfDay _endTime; //结束时间
  Map<String, bool> _reminderTimeChecked =
      new Map<String, bool>(); //提醒时间的复选框选中状态
  Map<String, bool> _reminderMethodChecked =
      new Map<String, bool>(); //提醒方式的复选框选中状态
  Map<String, String> _timeDescription = new Map<String, String>(); //提醒时间的具体描述
  Map<String, String> _reminderMethodDescription =
      new Map<String, String>(); //提醒方式的具体描述
  List<String> _reminderTimeKeys = []; //提醒时间的keys
  List<String> _reminderMethodKeys = []; //提醒方式的keys
  List<ConsultationReminderDTO>? consultationReminders = [];
  List<FSelectModel> expertList = [];
  List<FSelectModel> timeSelectModelList = [];

  ///主管专家下拉列表已选择的index
  int selectedIndex = 0;

  _DesktopLayoutState(this.businessParent);

  ///添加参与人
  FWidget addParticipantsWidget() {
    return FInkWell(
      child: FContainer(
        width: 150,
        height: 50,
        decoration: BoxDecoration(
          border: Border.all(width: 1, color: Colors.black12),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: FColorHelper.mixColor(
                Colors.black12,
                Colors.black38,
                70,
              ),
              offset: Offset(3.0, 3.0), //阴影y轴偏移量
              blurRadius: 6, //阴影模糊程度
              spreadRadius: 0, //阴影扩散程度
            ),
          ],
        ),
        child: FRow(
          children: [
            FIcon(Icons.add),
            FText(i18nBook.realTimeConsultation.addParticipants.t)
          ],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
      ),
      onTap: () {
        if (controller.state.assistantPhysicianName !=
            i18nBook.realTimeConsultation.noAssistant.t) {
          //如果设置了助理
          /*  if (controller.state.applicationInfo.participantsTotal.length >= 3) {
            PromptBox.toast(
                i18nBook.realTimeConsultation.theUpperLimitHasBeenReached.t);
          } else  */
          {
            router.dialog(
              () => AddParticipantsPage(
                confirmCallback: refreshParticipantsUI, //回调刷新参与人员UI
                controller: controller,
              ),
            );
          }
        } else {
          //如果没有设置助理
          /*        if (controller.state.applicationInfo.participantsTotal.length >= 4) {
            PromptBox.toast(
                i18nBook.realTimeConsultation.theUpperLimitHasBeenReached.t);
          } else  */
          {
            router.dialog(
              () => AddParticipantsPage(
                confirmCallback: refreshParticipantsUI, //回调刷新参与人员UI
                controller: controller,
              ),
            );
          }
        }
      },
    );
  }

  @override
  FWidget build(BuildContext context) {
    return mainContent(businessParent, controller, _datetime);
  }

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
    controller.state.assistantPhysicianName = '';
    controller.state.expertCode =
        controller.state.consultationsRecordInfos.expertUserCode ?? "";
    _startTime = TimeOfDay.fromDateTime(roundTime(currentTime)); //开始时间
    _endTime = TimeOfDay.fromDateTime(roundTime(delayedTime)); //结束时间
    _datetime = _compareDates(
        controller.state.consultationsRecordInfos.consultationTime!);
    _reminderTimeKeys.add(ConsultationReminderTimeEnum.FifteenMinutes.name);
    _reminderTimeKeys.add(ConsultationReminderTimeEnum.ThirtyMinutes.name);
    _reminderTimeKeys.add(ConsultationReminderTimeEnum.SixtyMinutes.name);
    _reminderTimeKeys.add(ConsultationReminderTimeEnum.OneDay.name);

    _reminderTimeChecked[ConsultationReminderTimeEnum.FifteenMinutes.name] =
        true;
    _reminderTimeChecked[ConsultationReminderTimeEnum.ThirtyMinutes.name] =
        false;
    _reminderTimeChecked[ConsultationReminderTimeEnum.SixtyMinutes.name] =
        false;
    _reminderTimeChecked[ConsultationReminderTimeEnum.OneDay.name] = false;

    _timeDescription[ConsultationReminderTimeEnum.FifteenMinutes.name] =
        i18nBook.realTimeConsultation.minBeforeStart15.t;
    _timeDescription[ConsultationReminderTimeEnum.ThirtyMinutes.name] =
        i18nBook.realTimeConsultation.minBeforeStart30.t;
    _timeDescription[ConsultationReminderTimeEnum.SixtyMinutes.name] =
        i18nBook.realTimeConsultation.minBeforeStart60.t;
    _timeDescription[ConsultationReminderTimeEnum.OneDay.name] =
        i18nBook.realTimeConsultation.beforeStart1day.t;

    _reminderMethodKeys.add(ConsultationReminderMode.Application.name); //应用内提醒
    _reminderMethodKeys.add(ConsultationReminderMode.SMS.name); //短信提醒

    _reminderMethodChecked[ConsultationReminderMode.Application.name] = true;
    _reminderMethodChecked[ConsultationReminderMode.SMS.name] = false;

    _reminderMethodDescription[ConsultationReminderMode.Application.name] =
        i18nBook.realTimeConsultation.inAppReminder.t;
    _reminderMethodDescription[ConsultationReminderMode.SMS.name] =
        i18nBook.realTimeConsultation.SMSReminder.t;
    controller.state.applicationInfo.participantsTotal.clear();
    controller.initReferOrganization();
    controller.state.choosedParticipantsDetails.clear();
    _setTimePickerSource();
  }

  FWidget mainContent(FInteractiveContainer businessParent,
      RealTimeConsultationController controller, DateTime _datetime) {
    return FSimpleDialog(
      title: FText(
        i18nBook.realTimeConsultation.confirmApplication.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: FFontSizes.H5,
        ),
      ),
      backgroundColor: Color.fromRGBO(230, 230, 230, 1),
      children: [
        FContainer(
          padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
          color: Colors.white,
          width: i18nBook.isCurrentRussian ? 620 : 600,
          height: 600,
          child: FColumn(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _commonWidget(i18nBook.realTimeConsultation.dateTime.t,
                  _chooseTimeWidget(), true),
              _commonWidget(i18nBook.realTimeConsultation.competentExperts.t,
                  _chooseExpert(), true),
              _commonWidget(
                  i18nBook.realTimeConsultation.assistantPhysician.t,
                  FText(controller.state.assistantPhysicianName),
                  false), //controller.state.consultationsRecordInfos.
              _commonWidget(i18nBook.realTimeConsultation.participants.t,
                  _participantsWidget(), false),
              _commonWidget(
                  i18nBook.realTimeConsultation.consultationDescription.t,
                  _descriptionWidget(),
                  false),
              _commonWidget(i18nBook.realTimeConsultation.reminderTime.t,
                  _reminderTimeWidget(), false),
              _commonWidget(i18nBook.realTimeConsultation.reminderMehod.t,
                  _remindMehodWidget(), false),
              _commonWidget(
                  i18nBook.realTimeConsultation.operator.t,
                  FText(controller
                      .state.consultationsRecordInfos.operateUserName!),
                  true),
            ],
          ),
        ),
      ],
      isDefault: true,
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onOk: () async {
        await _setConsultationReminders();
        ApplicationInfo applicationInfo = new ApplicationInfo();
        DateTime startDateTime = new DateTime(
          _datetime.year,
          _datetime.month,
          _datetime.day,
          _startTime.hour,
          _startTime.minute,
        );
        DateTime endDateTime = new DateTime(
          _datetime.year,
          _datetime.month,
          _datetime.day,
          _endTime.hour,
          _endTime.minute,
        );
        applicationInfo.startTime = startDateTime;
        applicationInfo.endTime = endDateTime;
        applicationInfo.competentExperts = controller.state.expertCode;
        applicationInfo.assistantPhysician =
            controller.state.assistantPhysicianName;
        applicationInfo.participantsTotal =
            controller.state.applicationInfo.participantsTotal;
        applicationInfo.description = controller.state.description;
        applicationInfo.consultationReminders = consultationReminders;
        applicationInfo.operator =
            controller.state.consultationsRecordInfos.operateUserName;
        if (applicationInfo.startTime!.compareTo(DateTime.now()) == -1) {
          PromptBox.toast(i18nBook.realTimeConsultation.dateErrorNotice.t);
        } else if (applicationInfo.endTime!
                .compareTo(applicationInfo.startTime!) ==
            -1) {
          PromptBox.toast(i18nBook.realTimeConsultation.timeErrorNotice.t);
        } else if (applicationInfo.endTime == applicationInfo.startTime) {
          PromptBox.toast(i18nBook.realTimeConsultation.timeErrorNoticeEqual.t);
        } else {
          controller.approveApplication(applicationInfo);
          router.back();
          appointmentConsultationManager.onDataChange.emit(this, true);
          controller.state.descriptionController.clear();
        }
      },
      onClose: () {
        router.back(result: {'isCancel': true});
      },
      onCancel: () {
        router.back(result: {'isCancel': true});
        controller.state.descriptionController.clear();
      },
    );
  }

  DateTime roundTime(DateTime currentTime) {
    int minutes = currentTime.minute;
    int hours = currentTime.hour;

    if (minutes < 15) {
      minutes = 15;
    } else if (minutes < 30) {
      minutes = 30;
    } else if (minutes < 45) {
      minutes = 45;
    } else {
      minutes = 0;
      hours = (hours + 1) % 24;
    }

    return DateTime(
        currentTime.year, currentTime.month, currentTime.day, hours, minutes);
  }

  ///刷新参与人员UI
  void refreshParticipantsUI() {
    setState(() {});
  }

  ///创建日期选择器
  FWidget _buildDatePicker() {
    return FInkWell(
      child: FContainer(
        height: 45,
        padding: EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
        child: FRow(
          children: [
            FText(
              "${formatDate(
                _datetime,
                [yyyy, '-', mm, '-', dd],
              )}",
              style: TextStyle(fontSize: 16),
            ),
            FIcon(
              Icons.date_range_rounded,
              color: Colors.grey,
            ),
          ],
        ),
        decoration: BoxDecoration(
          border: Border.all(
            width: 1,
            color: Colors.black12,
          ),
          borderRadius: BorderRadius.all(Radius.elliptical(4, 4)),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: FColorHelper.mixColor(
                Colors.black12,
                Colors.black38,
                70,
              ),
              offset: Offset(3.0, 3.0), //阴影y轴偏移量
              blurRadius: 6, //阴影模糊程度
              spreadRadius: 0, //阴影扩散程度
            ),
          ],
        ),
      ),
      onTap: _showDatePicker,
    );
  }

  ///创建时间下拉框
  ///timeSelectModelList 数据源
  ///isStartTime 是否是开始时间
  FWidget _buildTimePicker(
    List<FSelectModel> timeSelectModelList,
    bool isStartTime,
  ) {
    return FContainer(
      child: FSelect<FSelectModel, String>(
        source: timeSelectModelList,
        fontFamily: FTheme.ins.localeSetting.fontFamily,
        height: 36,
        value: isStartTime
            ? controller.transformFromTimeOfDay(_startTime)
            : controller.transformFromTimeOfDay(_endTime),
        hintText: "",
        onSelectChanged: (value, index) {
          if (isStartTime) {
            _startTime = controller.transformFromString(value!);
            List<int> endTimeAuto = controller.setEndTimeAuto(_startTime);
            _endTime = TimeOfDay(
                hour: endTimeAuto[0], minute: endTimeAuto[1]); //结束时间自动加30分钟
            setState(() {});
          } else {
            _endTime = controller.transformFromString(value!);
          }
        },
        optionLabelExtractor: (data) => data.name,
        optionValueExtractor: (data) => data.code,
        decoration: BoxDecoration(
          border: Border.all(width: 1, color: Colors.black12),
          borderRadius: BorderRadius.all(Radius.elliptical(4, 4)),
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: FColorHelper.mixColor(
                Colors.black12,
                Colors.black38,
                70,
              ),
              offset: Offset(3.0, 3.0), //阴影y轴偏移量
              blurRadius: 6, //阴影模糊程度
              spreadRadius: 0, //阴影扩散程度
            ),
          ],
        ),
        fontSize: 16,
      ),
      width: 102,
      height: 45,
    );
  }

  DateTime _compareDates(DateTime inputDate) {
    DateTime currentDate = DateTime.now();

    if (inputDate.isBefore(currentDate) ||
        inputDate.isAtSameMomentAs(currentDate)) {
      return currentDate;
    } else {
      return inputDate;
    }
  }

  ///主管专家下拉列表
  FWidget _chooseExpert() {
    FWidget expertWidget;
    var name = controller.state.expertCode ?? "";
    if (name.isEmpty) {
      if (expertList.isEmpty) {
        _getCompetentExperts();
      }
      expertWidget = _selectWidget();
    } else {
      if (expertList.isEmpty || expertList.length < 0) {
        expertWidget = FText(controller
            .state.consultationsRecordInfos.expertUserName
            .toString());
      } else {
        expertWidget = _selectWidget();
      }
    }
    if (controller.state.assistantPhysicianName == '') {
      //判断是否是第一次调用获取助理医师的方法
      _getAssistantInfo();
    }
    return expertWidget;
  }

  ///选择提醒方式的通用样式
  FWidget _chooseReminderMethod(String key) {
    return FContainer(
      width: i18nBook.isCurrentRussian ? 180 : 150,
      child: FRow(
        children: [
          FRectangleCheckbox(
            value: _reminderMethodChecked[key],
            isCheck: _reminderMethodChecked[key],
            onChange: ((dynamic value, bool? isCheck) {
              setState(() {
                if (key != ConsultationReminderMode.Application.name) {
                  _reminderMethodChecked[key] = !value!;
                }
              });
            }),
          ),
          FSizedBox(
            width: 10,
          ),
          FContainer(
            width: i18nBook.isCurrentRussian ? 150 : 120,
            child: FText(
              _reminderMethodDescription[key].toString(),
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }

  ///选择提醒时间的通用样式
  FWidget _chooseReminderTime(String key) {
    return FContainer(
      width: i18nBook.isCurrentRussian ? 180 : 150,
      child: FRow(
        children: [
          FRectangleCheckbox(
            value: _reminderTimeChecked[key],
            isCheck: _reminderTimeChecked[key],
            //onChange: _onSelectAllChanged,
            onChange: (dynamic value, bool? isCheck) {
              setState(() {
                if (key != ConsultationReminderTimeEnum.FifteenMinutes.name) {
                  //开始前15分钟和开始前60分钟默认勾选且不可更改
                  _reminderTimeChecked[key] = !value!;
                }
              });
            },
          ),
          FSizedBox(
            width: 10,
          ),
          FContainer(
            width: i18nBook.isCurrentRussian ? 150 : 120,
            child: FText(
              _timeDescription[key].toString(),
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }

  ///日期选择
  FWidget _chooseTimeWidget() {
    return FRow(
      children: [
        _buildDatePicker(),
        FSizedBox(
          width: 20,
        ),
        _buildTimePicker(timeSelectModelList, true),
        FSizedBox(
          width: 10,
        ),
        FText("—"),
        FSizedBox(
          width: 10,
        ),
        _buildTimePicker(timeSelectModelList, false),
      ],
    );
  }

  ///通用的行Widget
  FWidget _commonWidget(String leftText, FWidget rightWidget, bool isRequired) {
    if (isRequired) {
      return FFlex(
        crossAxisAlignment: CrossAxisAlignment.center,
        direction: Axis.horizontal,
        children: [
          FExpanded(
              child: FRichText(
                text: TextSpan(children: [
                  TextSpan(
                    text: leftText,
                    style: TextStyle(
                      fontFamily: FTheme.ins.localeSetting.fontFamily,
                    ),
                  ),
                  TextSpan(
                    text: "*",
                    style: TextStyle(
                      fontSize: FFontSizes.H5,
                      color: Colors.red,
                      fontFamily: FTheme.ins.localeSetting.fontFamily,
                    ),
                  ),
                ]),
              ),
              flex: 1),
          FExpanded(
            child: rightWidget,
            flex: 3,
          ),
        ],
      );
    } else {
      return FFlex(
        crossAxisAlignment: CrossAxisAlignment.start,
        direction: Axis.horizontal,
        children: [
          FExpanded(
              child: FRichText(
                text: TextSpan(children: [
                  TextSpan(
                      text: leftText,
                      style: TextStyle(
                        fontFamily: FTheme.ins.localeSetting.fontFamily,
                      )),
                ]),
              ),
              flex: 1),
          FExpanded(
            child: rightWidget,
            flex: 3,
          ),
        ],
      );
    }
  }

  ///创建参与人员列表
  List<FWidget> _createParticipantsFWidget() {
    List<FWidget> participantsListWidget = []; //参与人员列表
    participantsListWidget.clear();
    for (int i = 0;
        i < controller.state.choosedParticipantsDetails.length;
        i++) {
      participantsListWidget.add(_participantsCommonFWidget(
          controller.state.choosedParticipantsDetails[i]));
    }
    FWidget addParticipants = addParticipantsWidget();

    participantsListWidget.add(addParticipants);

    return participantsListWidget;
  }

  ///会诊描述
  FWidget _descriptionWidget() {
    return FContainer(
      height: 150,
      child: FTextField(
        maxLines: 4,
        onChanged: (value) => controller.state.description = value,
        controller: controller.state.descriptionController,
        decoration: InputDecoration(
          border: OutlineInputBorder(),
        ),
      ),
    );
  }

  ///获取助理医师的信息
  Future<void> _getAssistantInfo() async {
    await controller.getAssistantInfo(controller.state.expertCode!);
    setState(() {});
  }

  ///获取主管专家信息
  Future<void> _getCompetentExperts() async {
    expertList = await controller.getCompetentExperts();
    setState(() {});
  }

  ///参与人员通用样式
  FWidget _participantsCommonFWidget(SimpleInfoItem userInfo) {
    var avatar;
    userInfo.image!.isEmpty
        ? avatar = const AssetImage("assets/images/avatar_man.png")
        : avatar = NetworkImage(userInfo.image!);
    return FContainer(
      width: 150,
      height: 50,
      decoration: BoxDecoration(
        border: Border.all(width: 1, color: Colors.black12),
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.grey,
            blurRadius: 2.0,
            offset: Offset(2, 1),
          )
        ],
      ),
      child: FRow(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
        FCircleAvatar(
          backgroundImage: avatar,
        ),
        FContainer(
          width: 70,
          child: FText(
            userInfo.name!,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        FInkWell(
          child: FMaterialTooltip(
            child: FIcon(
              Icons.delete,
              color: FTheme.ins.colorScheme.primary,
            ),
            message: i18nBook.common.delete.t,
          ),
          onTap: () {
            int index = controller.state.applicationInfo.participantsTotal
                .indexOf(userInfo.itemCode);
            if (index >= 0) {
              setState(() {
                controller.state.applicationInfo.participantsTotal
                    .removeAt(index);
                controller.getParticipantsDetails();
              });
            }
          },
        ),
      ]),
    );
  }

  ///参与人员Widget
  FWidget _participantsWidget() {
    return FContainer(
      constraints: BoxConstraints(maxHeight: 180, minHeight: 20),
      child: FListView(
        shrinkWrap: true,
        children: [
          FColumn(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                FWrap(
                  children: _createParticipantsFWidget(),
                  runSpacing: 10,
                  runAlignment: WrapAlignment.spaceAround,
                  spacing: 20,
                  alignment: WrapAlignment.start,
                ),
              ]),
        ],
      ),
    );
  }

  ///提醒时间的widget
  FWidget _reminderTimeWidget() {
    return FColumn(
      children: [
        FRow(
          children: [
            _chooseReminderTime(_reminderTimeKeys[0]),
            FSizedBox(width: 30),
            _chooseReminderTime(_reminderTimeKeys[1]),
          ],
        ),
        FSizedBox(height: 10),
        FRow(
          children: [
            _chooseReminderTime(_reminderTimeKeys[2]),
            FSizedBox(width: 30),
            _chooseReminderTime(_reminderTimeKeys[3]),
          ],
        ),
      ],
    );
  }

  ///提醒方式的Widget
  FWidget _remindMehodWidget() {
    return FRow(
      children: [
        _chooseReminderMethod(_reminderMethodKeys[0]),
        FSizedBox(width: 30),
        if (ServerConfigManager.isSMSOn) ...[
          _chooseReminderMethod(_reminderMethodKeys[1]),
        ],
      ],
    );
  }

  ///主管专家下拉框
  FWidget _selectWidget() {
    if (expertList.length > 0) {
      controller.state.expertCode = expertList[selectedIndex].code;
    } else {
      controller.state.expertCode = null;
    }
    return FSelect<FSelectModel, String>(
        source: expertList,
        hintText: expertList.length > 0
            ? i18nBook.realTimeConsultation.chooseExpert.t
            : i18nBook.realTimeConsultation.noExpertsToChooseFrom.t,
        optionLabelExtractor: (data) => data.name,
        optionValueExtractor: (data) => data.code,
        fontFamily: FTheme.ins.localeSetting.fontFamily,
        onSelectChanged: (value, index) {
          controller.state.expertCode = value;
          selectedIndex = index!;
          _getAssistantInfo();
          controller.getAllSelectableParticipants();
        },
        value: controller.state.expertCode);
  }

  ///创建提醒方式和提醒时间的统一集合
  Future<void> _setConsultationReminders() async {
    if (_reminderMethodChecked[ConsultationReminderMode.Application.name] ==
        true) {
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.FifteenMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.Application,
            consultationReminderTime:
                ConsultationReminderTimeEnum.FifteenMinutes));
      }
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.ThirtyMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.Application,
            consultationReminderTime:
                ConsultationReminderTimeEnum.ThirtyMinutes));
      }
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.SixtyMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.Application,
            consultationReminderTime:
                ConsultationReminderTimeEnum.SixtyMinutes));
      }
      if (_reminderTimeChecked[ConsultationReminderTimeEnum.OneDay.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.Application,
            consultationReminderTime: ConsultationReminderTimeEnum.OneDay));
      }
    }
    if (_reminderMethodChecked[ConsultationReminderMode.SMS.name] == true) {
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.FifteenMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.SMS,
            consultationReminderTime:
                ConsultationReminderTimeEnum.FifteenMinutes));
      }
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.ThirtyMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.SMS,
            consultationReminderTime:
                ConsultationReminderTimeEnum.ThirtyMinutes));
      }
      if (_reminderTimeChecked[
              ConsultationReminderTimeEnum.SixtyMinutes.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.SMS,
            consultationReminderTime:
                ConsultationReminderTimeEnum.SixtyMinutes));
      }
      if (_reminderTimeChecked[ConsultationReminderTimeEnum.OneDay.name] ==
          true) {
        consultationReminders!.add(new ConsultationReminderDTO(
            consultationReminderMode: ConsultationReminderMode.Application,
            consultationReminderTime: ConsultationReminderTimeEnum.OneDay));
      }
    }
  }

  ///设置时间下拉框的数据源
  void _setTimePickerSource() {
    List<TimeOfDay> timeList = [];
    int totalMinute = 0;
    int minute = 0;
    int hour = 0;
    totalMinute = hour * 60 + minute;
    TimeOfDay time = TimeOfDay(hour: hour, minute: minute);
    for (int i = 0; i < 24 * 4; i++) {
      time = time.replacing(hour: hour, minute: minute);
      timeList.add(time);
      totalMinute += 15;
      minute = totalMinute % 60;
      hour = totalMinute ~/ 60;
      timeSelectModelList.add(
        new FSelectModel(
          name: controller.transformFromTimeOfDay(timeList[i]),
          code: controller.transformFromTimeOfDay(timeList[i]),
        ),
      );
    }
  }

  ///日期组件
  _showDatePicker() async {
    var date = await showDatePicker(
        context: this.context,
        initialDate: _datetime,
        firstDate: DateTime(1900),
        locale: i18nBook.locale,
        lastDate: DateTime(2050));
    if (date == null) return;
    setState(() {
      _datetime = date;
    });
  }
}
