import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/organization/org_personnel/view.dart';

/// 不同区域
enum OrgType {
  companyPersonnel,
  position,
  professional,
}

///组织信息卡片内容布局
class OrgCard extends FStatefulWidget implements FInteractiveContainer {
  OrgCard({
    Key? key,
    required this.name,
    required this.icon,
    required this.mList,
    this.pageName = 'OrgCard',
    this.addCard,
    this.deleteCard,
    this.editCard,
    this.clickCard,
    this.type,
  }) : super(key: key);
  final List<PublicChoice> mList;
  final String name;
  final FWidget icon;
  final OrgType? type;
  @override
  final String pageName;

  /// 新增
  final void Function()? addCard;

  /// 删除
  ///
  /// [code] 删除的组织code
  final void Function(String code)? deleteCard;

  /// 编辑
  ///
  /// [obj] 编辑的组织对象
  final void Function(dynamic obj)? editCard;

  /// 点击
  ///
  /// [obj] 点击的组织对象
  final void Function(dynamic obj)? clickCard;

  @override
  FState<OrgCard> createState() => _BuildOrgState();
}

class _BuildOrgState extends FState<OrgCard> {
  /// 是否选中的状态
  final activeState = (isActive) {
    return isActive != null && isActive;
  };

  /// 是否是全部或者未分配
  final isAllOrNotAlloc = (type) {
    return type != '0' && type != '2';
  };
  final scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    scrollController.dispose();
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      children: [
        FContainer(
          color: Color.fromRGBO(233, 237, 240, 1),
          child: _buildTitleBar(),
        ),
        _buildBodyContent(),
      ],
    );
  }

  ///新增组织按钮
  FWidget _buildAddButton() {
    if (widget.addCard != null) {
      return FContainer(
        decoration: BoxDecoration(
          border: Border.all(
            color: Color.fromRGBO(
              155,
              155,
              155,
              1,
            ),
            width: 1,
          ),
          borderRadius: BorderRadius.all(
            Radius.circular(4.0),
          ),
        ),
        child: FInk(
          child: FInkWell(
            child: FIcon(
              Icons.add,
              color: Color.fromRGBO(
                78,
                75,
                74,
                1,
              ),
              size: 18,
            ),
            onTap: () => widget.addCard?.call(),
          ),
        ),
      );
    } else {
      return FInk(
        child: FInkWell(
          child: FContainer(),
        ),
      );
    }
  }

  ///组织信息卡片
  FWidget _buildTitleBar() {
    return FFlex(
      direction: Axis.horizontal,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        FExpanded(
          flex: 2,
          child: FColumn(
            children: [
              FContainer(
                height: 45,
                child: FRow(
                  children: [
                    FSizedBox(
                      width: 15,
                    ),
                    widget.icon,
                    FSizedBox(
                      width: 15,
                    ),
                    FText(
                      widget.name,
                      style: TextStyle(fontSize: 18),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
        FExpanded(
          flex: 1,
          child: FContainer(
            child: FFlex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                FMaterial(
                  child: _buildAddButton(),
                ),
                FSizedBox(
                  width: 15,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  ///组织信息卡片内容视图
  FWidget _buildBodyContent() {
    return FScrollbar(
      isAlwaysShown: true,
      controller: scrollController,
      child: FContainer(
        height: 40.0 * widget.mList.length < 160.0
            ? 40.0 * widget.mList.length
            : 160,
        child: FListView.builder(
          itemCount: widget.mList.length,
          controller: scrollController,
          itemBuilder: (context, index) {
            final child = _buildOrgCardItemView(index);
            return FMaterial(
              child: FInk(
                child: FInkWell(
                  mouseCursor: SystemMouseCursors.click,
                  onTap: () {
                    setState(
                      () {
                        widget.mList.forEach(
                          (element) {
                            element.isActive = false;
                          },
                        );
                        widget.mList[index].isActive = true;
                        widget.clickCard!(
                          widget.mList[index],
                        );
                      },
                    );
                  },
                  onHover: (value) {
                    setState(
                      () {
                        if (value) {
                          widget.mList[index].isHover = true;
                        } else {
                          widget.mList[index].isHover = false;
                        }
                      },
                    );
                  },
                  child: child,
                ),
                color: Colors.white,
              ),
            );
          },
        ),
      ),
    );
  }

  ///组织信息单个卡片视图
  FWidget _buildOrgCardItemView(int index) {
    final isHoverAndAlloc = widget.mList[index].isHover &&
        isAllOrNotAlloc(
          widget.mList[index].type,
        );
    final FWidget opRow;
    if (isHoverAndAlloc) {
      var des = widget.mList[index];
      var code = widget.mList[index].code;
      opRow = FRow(
        children: [
          FIconButton(
            businessParent: widget,
            name: "$des",
            onPressed: () => widget.editCard!(
              widget.mList[index],
            ),
            icon: FIcon(
              Icons.edit,
              size: 18.0,
            ),
          ),
          FIconButton(
            businessParent: widget,
            name: "deleteCard:$code",
            onPressed: () => widget.deleteCard!(
              widget.mList[index].code,
            ),
            icon: FIcon(
              Icons.delete_outline,
              size: 18.0,
            ),
          ),
        ],
      );
    } else {
      opRow = const FSizedBox();
    }
    return FContainer(
      padding: EdgeInsets.only(
        left: 55,
        right: 10,
      ),
      height: 40,
      color: activeState(widget.mList[index].isActive)
          ? Theme.of(context).primaryColor
          : widget.mList[index].isHover
              ? Theme.of(context).primaryColorLight
              : Color.fromRGBO(0, 0, 0, 0),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          FExpanded(
            child: FText(
              widget.mList[index].name,
              style: activeState(
                widget.mList[index].isActive,
              )
                  ? TextStyle(
                      fontSize: 16,
                      overflow: TextOverflow.ellipsis,
                    ).copyWith(
                      color: Colors.white,
                    )
                  : TextStyle(
                      fontSize: 16,
                      overflow: TextOverflow.ellipsis,
                    ),
            ),
          ),
          opRow,
        ],
      ),
    );
  }
}
