import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';

class RecordReferMobileSelectPopup<T, TValue> extends FStatefulWidget {
  RecordReferMobileSelectPopup({
    Key? key,
    required this.source,
    this.onChangeIndex,
    this.value,
    this.isCustomMade = false,
    required this.optionValueExtractor,
    required this.optionLabelExtractor,
    this.specialWidget,
  }) : super(key: key);

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

  ///选中值
  final TValue? value;

  /// 下拉选中的值
  final ValueCallback? onChangeIndex;

  /// 是否定制下拉UI
  final bool? isCustomMade;

  /// 选项值提取器
  ///
  /// [data] 选项数据源
  final TValue Function(T data) optionValueExtractor;

  /// 选项文本提取器
  ///
  /// [data] 选项数据源
  final String Function(T data) optionLabelExtractor;

  final FWidget? Function(T data)? specialWidget;

  @override
  FState<RecordReferMobileSelectPopup> createState() =>
      _RecordReferMobileSelectPopupState<T, TValue>();
}

class _RecordReferMobileSelectPopupState<T, TValue>
    extends FState<RecordReferMobileSelectPopup<T, TValue>> {
  int _selectIndex = 0;

  final fontStyle = TextStyle(
    fontSize: 14,
  );
  // final controller = Get.find<ConsultationRecordViewController>();
  final ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    super.initState();
    print(widget.optionValueExtractor(widget.source[0]));
    final index = widget.source.indexWhere(
      (x) => widget.optionValueExtractor(x) == widget.value,
    );
    _selectIndex = index;
    widget.onChangeIndex!(index);
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 500,
      child: FAlwaysScrollListView(
        scrollController: _scrollController,
        child: QuickFWidget(ScrollConfiguration(
          behavior: ScrollConfiguration.of(context).copyWith(scrollbars: false),
          child: FListView.builder(
            controller: _scrollController,
            shrinkWrap: true,
            itemCount: widget.source.length,
            itemBuilder: (BuildContext context, int index) {
              return FMaterial(
                color: Colors.white,
                child: FInkWell(
                  onTap: () {
                    setState(() {
                      _selectIndex = index;
                      widget.onChangeIndex!(index);
                    });
                  },
                  child: FContainer(
                    padding: EdgeInsets.symmetric(
                      vertical: 15,
                    ),
                    margin: EdgeInsets.symmetric(
                      horizontal: 15,
                    ),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          color: Color.fromRGBO(231, 231, 231, 1),
                          width: 0.5,
                        ),
                      ),
                    ),
                    child: _buildChoiceRow(index),
                  ),
                ),
              );
            },
          ),
        )),
      ),
    );
  }

  ///编辑所选的行
  FWidget _buildChoiceRow(int index) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FExpanded(
          child: FRow(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              !widget.isCustomMade!
                  ? FRow(
                      children: [
                        FColumn(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            FText(
                              widget.optionLabelExtractor(widget.source[index]),
                            ),
                          ],
                        )
                      ],
                    )
                  : widget.specialWidget!(widget.source[index]) ?? FSizedBox(),
              FContainer(
                width: 25,
                height: 25,
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Color.fromRGBO(141, 141, 141, 1),
                    width: 0.5,
                  ),
                  borderRadius: BorderRadius.circular(25),
                ),
                child: _selectIndex == index
                    ? FContainer(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25),
                          color: Theme.of(context).primaryColor,
                        ),
                        child: FCenter(
                          child: FContainer(
                            child: FIcon(
                              Icons.check,
                              size: 20,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      )
                    : FContainer(),
              ),
            ],
          ),
        ),
        FSizedBox(
          width: 15,
        ),
      ],
    );
  }
}
