import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/pages/user/user_device/widgets/single_choice_doctor.dart';
import 'package:fis_common/index.dart';

/// 手机端选择器
class SelectSheet extends FStatelessWidget {
  SelectSheet({
    Key? key,
    required this.title,
    this.icon,
    this.onClick,
    this.hintText,
    this.value = '',
    this.borderText = '',
    this.suffixIcon,
    required this.source,
    this.onChange,
  }) : super(key: key);

  /// 数据源
  final List<dynamic> source;

  ///标题
  final String title;

  ///图标
  final FWidget? icon;

  ///尾部图标
  final FWidget? suffixIcon;

  ///提示文字
  final String? hintText;

  ///点击事件
  final Function? onClick;

  ///值
  final String? value;

  ///边框文字
  final String? borderText;

  /// 下拉选中的值
  final Function(String)? onChange;

  ///区域文本内容
  FWidget _buildTextContent() {
    if (value.isNullOrEmpty)
      return FText(
        hintText!,
        style: TextStyle(
          fontSize: 14,
          color: Color.fromRGBO(155, 155, 155, 1),
        ),
      );
    else
      return FText(
        source.firstWhere((element) => element.code == value).name,
        style: TextStyle(
          fontSize: 14,
          color: Colors.black,
        ),
        textAlign: TextAlign.right,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
  }

  @override
  FWidget build(BuildContext context) {
    int index = -1;
    return FMaterial(
      color: Colors.white,
      child: FInk(
        child: FInkWell(
          onTap: () {
            showModalBottomSheet(
              isDismissible: false,
              builder: (BuildContext context) {
                return FContainer(
                  child: FColumn(
                    children: [
                      FContainer(
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              color: Color.fromRGBO(211, 211, 211, 1),
                            ),
                          ),
                        ),
                        child: FRow(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            FInkWell(
                              child: FContainer(
                                padding: EdgeInsets.all(15),
                                child: FText(
                                  i18nBook.common.confirm.t,
                                  style: TextStyle(
                                    color: FTheme.ins.colorScheme.primary,
                                  ),
                                ),
                              ),
                              onTap: () {
                                if (index < 0) {
                                  PromptBox.toast(i18nBook
                                      .organization.selectDepartmentTip.t);
                                } else {
                                  onChange?.call(source[index].code.toString());
                                  Navigator.of(context).pop();
                                }
                              },
                            ),
                            FInkWell(
                              child: FContainer(
                                padding: EdgeInsets.all(15),
                                child: FText(i18nBook.common.cancel.t),
                              ),
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                            )
                          ],
                        ),
                      ),
                      FExpanded(
                        child: SingleChoiceDoctor(
                          source: source,
                          value: value,
                          onChangeIndex: (value) => {index = value},
                        ),
                      ),
                    ],
                  ),
                );
              },
              context: context,
            );
          },
          child: FColumn(
            children: [
              FContainer(
                padding: EdgeInsets.symmetric(horizontal: 20),
                height: 50,
                child: FRow(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    FContainer(
                      child: FRow(
                        children: [
                          FRow(
                            children: [
                              icon ?? FText(''),
                            ],
                          ),
                          FText(title),
                        ],
                      ),
                    ),
                    FContainer(
                      child: FRow(
                        children: [
                          FContainer(
                            width: 200,
                            alignment: Alignment.centerRight,
                            child: _buildTextContent(),
                          ),
                          suffixIcon ??
                              FIcon(
                                Icons.chevron_right,
                                color: Color.fromRGBO(164, 164, 164, 1),
                              ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              FContainer(
                margin: EdgeInsets.symmetric(horizontal: 20),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color.fromRGBO(245, 245, 245, 1),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
