import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/create_check/state.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/controller.dart';
import 'package:fis_theme/theme.dart';

/// 选择框
class CheckListPageSelect extends FisView<CheckListPageCreationController> {
  const CheckListPageSelect({
    Key? key,
    required this.configuration,
    required this.buildKey,
  }) : super(key: key);
  final Map<String, dynamic> configuration;
  final String buildKey;

  static const specialSelect = 'GestationalAgeOrigin';
  static const specialSelectItem = 'LMP';

  @override
  FWidget build(BuildContext context) {
    final state = controller.state;

    List<SelectModel> selectList = [];
    // if (configuration['name'] == specialSelect) {
    // state.contents[buildKey + '-' + configuration['name']] =
    //       specialSelectItem;
    // }
    configuration["value"]["Source"].forEach(
      (element) {
        selectList.add(
          SelectModel(
            code: element,
            name: controller.getValues(element),
          ),
        );
      },
    );

    final select = FSelect<SelectModel, String>(
      source: selectList,
      fontFamily: FTheme.ins.localeSetting.fontFamily,
      hintText: i18nBook.remedical.select.t,
      value: state.contents[buildKey + '-' + configuration['name']],
      height: 36,
      width: 400,
      optionLabelExtractor: (data) => data.name,
      optionValueExtractor: (data) => data.code,
      onSelectChanged: (value, index) {
        state.contents[buildKey + '-' + configuration['name']] = value;
        state.selectString = configuration['name'] + '.' + value;
        // 这只是一个刷新页面的操作
        state.selectCheckList = state.selectCheckList.toList();

        /// 统一的清空处理
        state.contents.removeWhere(
          ((key, value) =>
              key.contains(buildKey) &&
              key != buildKey + '-' + configuration['name']),
        );
        state.weekAndDayContents.clear();
      },
    );

    return FExpanded(child: select);
  }
}
