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/consultation/controller.dart';
import 'package:get/get.dart';

class ReferMobileSelectPopup extends FStatefulWidget {
  ReferMobileSelectPopup({
    Key? key,
    required this.source,
    this.onChangeIndex,
    this.value,
    this.isCustomMade = false,
  }) : super(key: key);

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

  ///选中值
  final String? value;

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

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

  @override
  FState<ReferMobileSelectPopup> createState() =>
      _ReferMobileSelectPopupState();
}

class _ReferMobileSelectPopupState extends FState<ReferMobileSelectPopup> {
  int _selectIndex = 0;

  final fontStyle = TextStyle(
    fontSize: 14,
  );
  final controller = Get.find<ConsultationController>();
  final ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    super.initState();
    final index = widget.source.indexWhere(
      (x) => x.code == 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 _buildImage(String? isImage) {
    var child;
    isImage!.isEmpty
        ? child = FImage.asset(
            'assets/images/avatar_man.png',
            width: 50,
            height: 50,
            fit: BoxFit.cover,
          )
        : child = FImage.network(
            isImage,
            width: 50,
            height: 50,
            fit: BoxFit.cover,
          );
    return FContainer(
      margin: EdgeInsets.only(
        right: 15,
      ),
      child: FClipRRect(
        borderRadius: BorderRadius.circular(
          25,
        ),
        child: child,
      ),
    );
  }

  String _buildRoleName(String roleCode) {
    final index = controller.state.userRoleList.indexWhere(
      (x) => x.code == roleCode,
    );
    if (index == -1) {
      return '';
    }
    return controller.state.userRoleList[index].name;
  }

  ///编辑所选的行
  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.source[index].name,
                            ),
                          ],
                        )
                      ],
                    )
                  : FRow(
                      children: [
                        _buildImage(widget.source[index].headImageUrl),
                        FColumn(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            FText(
                              widget.source[index].name,
                              style: fontStyle,
                            ),
                            if (widget.source[index].roleCodes!.isNotEmpty)
                              FText(
                                _buildRoleName(
                                  widget.source[index].roleCodes![0],
                                ),
                                style: fontStyle,
                              ),
                            if (widget.source[index].fieldList!.isNotEmpty)
                              FText(
                                i18nBook.user.skilledAreaWithDevider.t +
                                    widget.source[index].fieldList!.join("，"),
                                style: fontStyle,
                              ),
                          ],
                        )
                      ],
                    ),
              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,
        ),
      ],
    );
  }
}
