import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/views/mobile/confirm_application_page/controller.dart';
import 'package:flyinsono/views/mobile/confirm_application_page/widgets/select_sheet.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class FAddParticipant extends GetView<ConfirmApplicationPageController>
    implements FPage {
  FAddParticipant({
    Key? key,
    this.pageName = 'AddParticipant',
    required this.hasAssistantPhysicianCode,
    this.isConsultation = false,
    this.confirmCallback,
  }) : super(key: key);

  ///父级节点
  final String pageName;
  final bool hasAssistantPhysicianCode;
  final bool? isConsultation;
  final ValueCallback? confirmCallback;
  late List<String> _selectParticipantsList =
      controller.state.selectParticipantsList;
  late List<String> _selectParticipantsNameList =
      controller.state.selectParticipantsNameList;
  late List<String> _nameList = controller.state.selectParticipantsNameList;

  late BuildContext _context;
  @override
  FWidget build(BuildContext context) {
    _context = context;
    if (isConsultation!)
      return FSimpleDialog(
        title: FText(
          i18nBook.realTimeConsultation.addParticipants.t,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        isDefault: false,
        bottomButton: FContainer(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(vertical: 15),
          child: FFlex(
            direction: Axis.horizontal,
            mainAxisSize: MainAxisSize.min,
            children: [
              FElevatedButton(
                name: "submit",
                businessParent: this,
                onPressed: () {
                  if (confirmCallback != null) {
                    confirmCallback!
                        .call(controller.state.selectParticipantsList);
                  }
                },
                style: ButtonStyle(
                  padding: MaterialStateProperty.all(
                    EdgeInsets.symmetric(
                      vertical: 10,
                      horizontal: 25,
                    ),
                  ),
                ),
                child: FText(i18nBook.common.confirm4Btn.t),
              ),
              FSizedBox(
                width: 20,
              ),
              FOutlinedButton(
                name: "back",
                businessParent: this,
                onPressed: () {
                  Get.back();
                },
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(
                    Colors.white,
                  ),
                  padding: MaterialStateProperty.all(
                    EdgeInsets.symmetric(vertical: 10, horizontal: 25),
                  ),
                ),
                child: FText(
                  i18nBook.common.cancel4Btn.t,
                  style: TextStyle(
                    color: Color.fromRGBO(61, 146, 252, 1),
                  ),
                ),
              ),
            ],
          ),
        ),
        isNeedClose: false,
        isFConfirmAlert: true,
        okString: i18nBook.common.confirm.t,
        cancelString: i18nBook.common.cancel.t,
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 10),
            width: 300,
            child: _buildMainStructure(),
          ),
        ],
      );

    return FScaffold(
      backgroundColor: Colors.grey[100],
      appBar: FAppBar(
        backgroundColor: Colors.white,
        title: FText(
          i18nBook.realTimeConsultation.addParticipants.t,
          style: TextStyle(
            fontSize: 16,
            color: Colors.black,
            fontWeight: FontWeight.bold,
          ),
        ),
        elevation: 0,
        centerTitle: true,
        leading: _buildLeading(),
        actions: [
          FGestureDetector(
            name: "confirm",
            businessParent: this,
            onTap: () {
              if (controller.state.selectParticipantsList.length == 0) {
                PromptBox.toast(
                    i18nBook.realTimeConsultation.chooseParticipants.t);
                return;
              }
              router.back();
            },
            child: FCenter(
              child: FRow(
                children: [
                  FText(
                    i18nBook.common.confirm.t,
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  ),
                  FSizedBox(
                    width: 20,
                  )
                ],
              ),
            ),
          ),
        ],
      ),
      body: FSafeArea(
        child: FColumn(children: [
          FExpanded(
            child: FSingleChildScrollView(
              child: FContainer(
                margin: EdgeInsets.only(
                  top: 15,
                ),
                child: _buildMainStructure(),
              ),
            ),
          ),
        ]),
      ),
    );
  }

  //返回键
  FWidget _buildLeading() {
    return FIconButton(
      name: "back",
      businessParent: this,
      icon: FIcon(
        Icons.arrow_back_ios_new,
        size: 20,
        color: Colors.black,
      ),
      onPressed: () {
        router.back();
      },
    );
  }

  /// 主体结构
  FWidget _buildMainStructure() {
    return FColumn(
      children: [
        _buildConfirmApplicationClickItem(
          i18nBook.remedical.currentOrganization.t,
          FObx(
            () {
              return _buildItemValue(
                controller.state.currectOrganization,
                [],
                controller.state.organizationList,
                (value) => controller.changeOrgCode(value),
                (valueString) {},
                [],
                [],
                false,
                false,
              );
            },
          ),
        ),
        _buildConfirmApplicationClickItem(
          i18nBook.realTimeConsultation.participants.t,
          FObx(
            () => _buildItemValue(
              controller.state.participantsListString,
              [],
              controller.state.allSelectableParticipants,
              (value) {
                _selectParticipantsList = value;
                controller.state.selectParticipantsList =
                    _selectParticipantsList.toSet().toList();
              },
              (valueList) {
                _selectParticipantsNameList = valueList;
                _nameList = _selectParticipantsNameList.toSet().toList();
                controller.state.selectParticipantsNameList = _nameList;
                controller.state.participantsListString = _nameList.join(',');
              },
              controller.state.selectParticipantsList,
              controller.state.selectParticipantsNameList,
            ),
          ),
        ),
      ],
    );
  }

  /// 单行值和多选组件
  ///
  /// [value] 当前当行的值或者多行的字符串
  /// [valueList] 多行的值 用来反显
  /// [sourceList] 当前数据源
  /// [valueCallback] 值回调
  /// [isMultiSelect] 是否多选
  /// [maxConsultationMembers] 参与人数
  FWidget _buildItemValue(
    String? value,
    List<int> valueList,
    List<FSelectSheetModel> sourceList,
    ValueCallback valueCallback,
    ValueCallback onChangeString, [
    List<String>? specialValueList,
    List<String>? specialValueNameList,
    bool? isMultiSelect = true,
    bool? isSpecial = true,
  ]) {
    return FRow(
      children: [
        FExpanded(
          child: FSelectSheet<FSelectSheetModel, String>(
            hintText: i18nBook.common.Select.t,
            source: sourceList,
            value: value,
            height: isConsultation! ? MediaQuery.of(_context).size.width : null,
            valueList: valueList,
            isMultiSelect: isMultiSelect,
            isSpecial: isSpecial,
            maxMembers: hasAssistantPhysicianCode ? 4 : 3,
            specialValueList: specialValueList,
            specialValueNameList: specialValueNameList,
            optionLabelExtractor: (data) => data.title,
            optionValueExtractor: (data) => data.value,
            onChange: (value) => valueCallback(value),
            onChangeString: (value) => onChangeString(value),
          ),
        ),
      ],
    );
  }

  /// 点击的item
  FWidget _buildConfirmApplicationClickItem(
    String title,
    FWidget referralItem, [
    Function? openPicker,
    bool? isRequired = false,
    bool? isNeedOffsetRight = false,
  ]) {
    return FInkWell(
      onTap: () => openPicker?.call() ?? () {},
      child: _buildConfirmApplicationItem(
        title,
        referralItem,
        isRequired,
        isNeedOffsetRight,
      ),
    );
  }

  /// 普通展示数据的item
  FWidget _buildConfirmApplicationItem(
    String title,
    FWidget referralItem, [
    bool? isRequired,
    bool? isNeedOffsetRight = false,
  ]) {
    return _buildCommonItem(
      [
        FContainer(
          width: 120,
          child: FRow(
            children: [
              FText(title),
              isRequired ?? false
                  ? FText(
                      '*',
                      style: TextStyle(color: Colors.red),
                    )
                  : FSizedBox(),
            ],
          ),
        ),
        FExpanded(
          child: FRow(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FExpanded(child: referralItem),
              if (isNeedOffsetRight!)
                FSizedBox(
                  width: 24,
                ),
            ],
          ),
        )
      ],
    );
  }

  /// 当前最常用的item
  FWidget _buildCommonItem(List<FWidget> items) {
    return FContainer(
      margin: EdgeInsets.symmetric(
        horizontal: 15,
      ),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color.fromRGBO(211, 211, 211, 1),
          ),
        ),
      ),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: items.toList(),
      ),
    );
  }
}
