import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/organization/org_personnel/controller.dart';
import 'package:flyinsono/pages/organization/org_personnel/widgets/select_org_member_info.dart';
import 'package:get/get.dart';

List<String> deleteList = [];

///组织成员结构
class OrgMember extends StatefulWidget implements FInteractiveContainer {
  const OrgMember({
    Key? key,
    this.pageName = 'SelectOrgMember',
    required this.title,
    required this.positationalText,
    this.image,
    this.isImage = false,
    this.onClick,
    this.hintText = '',
    this.imageUrl = '',
    this.listObject,
  }) : super(key: key);

  @override
  final String pageName;

  //标题
  final FWidget title;

  //个人描述
  final FWidget positationalText;

  //图片
  final FWidget? image;

  //是否为图片
  final bool? isImage;

  //提示信息
  final String? hintText;

  //点击事件
  final Function? onClick;

  //图片地址
  final String? imageUrl;

  //列表对象
  final dynamic listObject;

  @override
  State<OrgMember> createState() => _OrgMemberState();
}

///选择组织成员
class SelectOrgMember extends FStatefulWidget implements FInteractiveContainer {
  SelectOrgMember({
    Key? key,
    this.pageName = 'SelectOrgMember',
    this.name = '',
    this.code = '',
  }) : super(key: key);

  @override
  final String pageName;

  ///成员代码
  final String? code;

  ///成员名字
  final String? name;

  set value(String value) {}

  @override
  FState<SelectOrgMember> createState() => _SelectOrgMemberState();
}

class _OrgMemberState extends State<OrgMember> {
  final orgPesonnelController = Get.find<OrgPesonnelController>();

  @override
  FWidget build(BuildContext context) {
    return FMaterial(
      color: Colors.white,
      child: FColumn(
        children: [
          FSlidable(
            endActionPane: ActionPane(
              motion: ScrollMotion(),
              children: [
                SlidableAction(
                  onPressed: (BuildContext context) {
                    Get.to(
                      () => SelectOrgMemberInfo(
                        userItem: widget.listObject,
                      ),
                      preventDuplicates: false,
                      transition: Transition.rightToLeftWithFade,
                    );
                  },
                  backgroundColor: Theme.of(context).primaryColor,
                  foregroundColor: Colors.white,
                  icon: Icons.edit,
                  label: i18nBook.common.edit.t,
                ),
                SlidableAction(
                  onPressed: (BuildContext context) {
                    orgPesonnelController.deleteUserAlert([
                      widget.listObject.userCode.toString(),
                    ], widget);
                  },
                  backgroundColor: Theme.of(context).colorScheme.error,
                  foregroundColor: Colors.white,
                  icon: Icons.delete,
                  label: i18nBook.common.remove.t,
                ),
              ],
            ),
            child: FInkWell(
              onTap: () {},
              child: FContainer(
                margin: EdgeInsets.symmetric(
                  horizontal: 20,
                ),
                padding: EdgeInsets.symmetric(
                  horizontal: 13,
                  vertical: 10,
                ),
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Color.fromRGBO(228, 228, 228, 1),
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: FContainer(
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      FRow(
                        children: [
                          widget.image!,
                          FSizedBox(
                            width: 12,
                          ),
                          FColumn(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              widget.title,
                              FSizedBox(
                                height: 3,
                              ),
                              widget.positationalText,
                            ],
                          ),
                        ],
                      ),
                      FObx(
                        () {
                          if (orgPesonnelController.state.batchOperation)
                            return FContainer(
                              child: FCheckbox(
                                value: widget.listObject.userCode,
                                onChange: (value, isCheck) {
                                  if (isCheck!) {
                                    deleteList.add(
                                      widget.listObject.userCode.toString(),
                                    );
                                  } else {
                                    deleteList.remove(
                                      widget.listObject.userCode.toString(),
                                    );
                                  }
                                  orgPesonnelController.state.selectedIdxs =
                                      deleteList;
                                },
                              ),
                            );
                          else
                            return FContainer();
                        },
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
          FSizedBox(
            height: 15,
          )
        ],
      ),
    );
  }
}

class _SelectOrgMemberState extends FState<SelectOrgMember> {
  final orgPesonnelController = Get.find<OrgPesonnelController>();
  final ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    super.initState();
  }

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

  @override
  FWidget build(BuildContext context) {
    orgPesonnelController.state.batchOperation = false;
    return FScaffold(
      appBar: _buildOrgMemberAppBar(),
      body: FMaterial(
        color: Colors.white,
        child: FSafeArea(
          child: FColumn(
            children: [
              _buildOrgMemberName(),
              FExpanded(
                child: FObx(() => FAlwaysScrollListView(
                      scrollController: _scrollController,
                      child: QuickFWidget(ScrollConfiguration(
                        behavior: ScrollConfiguration.of(context)
                            .copyWith(scrollbars: false),
                        child: FListView.builder(
                          controller: _scrollController,
                          shrinkWrap: true,
                          itemCount: orgPesonnelController
                              .state.tableDataSource.length,
                          itemBuilder: (context, index) {
                            return OrgMember(
                              image: FCircleAvatar(
                                child: _buildOrgMemberImage(
                                    orgPesonnelController.state.tableDataSource
                                        .toList()[index]
                                        .headImageUrl),
                              ),
                              title: FText(
                                orgPesonnelController.state.tableDataSource
                                        .toList()[index]
                                        .displayName ??
                                    '',
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 15,
                                ),
                              ),
                              listObject: orgPesonnelController
                                  .state.tableDataSource
                                  .toList()[index],
                              positationalText: FText(
                                orgPesonnelController.state.tableDataSource
                                        .toList()[index]
                                        .positionName ??
                                    '',
                                style: TextStyle(
                                  color: Color.fromRGBO(143, 141, 140, 1),
                                  fontSize: 14,
                                ),
                              ),
                              onClick: () {},
                            );
                          },
                        ),
                      )),
                    )),
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: FObx(
        () {
          if (orgPesonnelController.state.batchOperation) {
            return FBottomSubmitButton(
              businessParent: widget,
              name: "remove",
              title: i18nBook.common.remove.t,
              onPressed: () {
                deleteList = [];
                orgPesonnelController.deleteUserListAlert(
                    orgPesonnelController.state.selectedIdxs, widget);
              },
            );
          } else {
            return FText('');
          }
        },
      ),
    );
  }

  ///组织成员名称视图
  FWidget _buildOrgMemberName() {
    return widget.name!.isNotEmpty
        ? FMaterial(
            color: Colors.white,
            child: FColumn(
              children: [
                FContainer(
                  padding: EdgeInsets.symmetric(horizontal: 20),
                  height: 50,
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  alignment: Alignment.centerLeft,
                  child: FText(
                    widget.name!,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                FContainer(
                  margin: EdgeInsets.symmetric(horizontal: 20),
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        color: Color.fromRGBO(245, 245, 245, 1),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          )
        : FText('');
  }

  ///组织成员头像
  FWidget _buildOrgMemberImage(String? isImage) {
    var child;
    isImage!.isEmpty
        ? child = QuickFWidget(FlutterLogo())
        : child = FImage.network(isImage);
    return child;
  }

  ///组织成员视图AppBar
  AppBar _buildOrgMemberAppBar() {
    return FAppBar(
      title: FText(i18nBook.organization.orgStructure.t),
      actions: [
        FGestureDetector(
          businessParent: widget,
          name: "batchOperation",
          onTap: () {
            orgPesonnelController.state.batchOperation =
                !orgPesonnelController.state.batchOperation;
            deleteList = [];
          },
          child: FObx(
            () {
              var child;
              orgPesonnelController.state.batchOperation
                  ? child = FRow(
                      children: [
                        FCenter(
                          child: FText(i18nBook.common.cancel.t),
                        ),
                        const FSizedBox(width: 5),
                      ],
                    )
                  : child = FCenter(
                      child: FText(i18nBook.user.batchOperation.t),
                    );
              return child;
            },
          ),
        ),
        FSizedBox(
          width: 10,
        )
      ],
      centerTitle: true,
      elevation: 0,
      leading: FIconButton(
        businessParent: widget,
        icon: FIcon(Icons.arrow_back_ios_new),
        name: "back",
        onPressed: () {
          deleteList = [];
          Get.back();
        },
      ),
    );
  }
}
