import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/create_check/controller.dart';
import 'package:flyinsono/pages/remedical/create_check/widgets/FValueController.dart';
import 'package:flyinsono/pages/remedical/create_check/widgets/check_item_date_time.dart';
import 'package:flyinsono/pages/remedical/create_check/widgets/check_item_input.dart';
import 'package:flyinsono/pages/remedical/create_check/widgets/check_item_select.dart';
import 'package:flyinsono/pages/remedical/create_check/widgets/check_item_week_and_day.dart';

/// 检查行 循环遍历出ui
class CheckItem extends FisView<CreatecheckController> {
  CheckItem({required this.selectCheckListItem});
  final String selectCheckListItem;
  final ScrollController _scrollController = ScrollController();
  bool isShow(int itemIndex) {
    final state = controller.state;
    var isShow = controller.getChectKeyList(selectCheckListItem)[itemIndex]
        ?["value"]["IsShow"];
    if (isShow == null || isShow == state.selectString) {
      return true;
    } else {
      return false;
    }
  }

  @override
  FWidget build(BuildContext context) {
    Map<
        String,
        FWidget Function(Map<String, dynamic> args, BuildContext context,
            String buildKey)> buildCheckItem = {
      'input': (args, context, buildKey) => CheckItemInput(
            configuration: args,
            buildKey: buildKey,
            inputFormatter: [],
          ),
      'select': (args, context, buildKey) => CheckItemSelect(
            configuration: args,
            buildKey: buildKey,
          ),
      'DateTime': (args, context, buildKey) => CheckItemDateTime(
            configuration: args,
            context: context,
            buildKey: buildKey,
          ),
      'WeekAndDay': (args, context, buildKey) => FObx(
            () => CheckItemWeekAndDay(
              configuration: args,
              buildKey: buildKey,
              increaseCount: (value) {
                if (args['name'] == 'LGA') {
                  controller.state.contents[buildKey + '-LGA'] = value;
                  controller.getRecordDateAsync(
                    value,
                    args,
                    buildKey,
                  );
                } else {
                  controller.getRecordDateAsync(
                    value,
                    args,
                    buildKey,
                  );
                }
              },
              controller: FValueController(
                name: buildKey,
                value: controller
                    .state.weekAndDayContents[buildKey + '-' + args['name']],
              ),
            ),
          ),
      'number': (args, context, buildKey) => CheckItemInput(
            configuration: args,
            buildKey: buildKey,
            inputFormatter: [FilteringTextInputFormatter.digitsOnly],
          ),
    };

    List selectCheckList = controller.getChectKeyList(selectCheckListItem);
    final child = FAlwaysScrollListView(
      scrollController: _scrollController,
      child: QuickFWidget(ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
        child: FListView.builder(
          shrinkWrap: true,
          itemCount: selectCheckList.length,
          controller: _scrollController,
          itemBuilder: (context, itemIndex) {
            return isShow(itemIndex)
                ? FContainer(
                    height: 50,
                    child: FRow(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        FContainer(
                          width: 150,
                          child: FText(
                            controller
                                .getValues(selectCheckList[itemIndex]?['name']),
                          ),
                        ),
                        buildCheckItem[selectCheckList[itemIndex]
                            ?['showType']]!(
                          selectCheckList[itemIndex],
                          context,
                          selectCheckListItem,
                        ),
                        FSizedBox(
                          width: 15,
                        ),
                        FExpanded(
                          child: FText(
                            selectCheckList[itemIndex]["value"]['Unit'] ?? '',
                          ),
                        ),
                      ],
                    ),
                  )
                : FContainer();
          },
        ),
      )),
    );

    return child;
  }
}
