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/organization/org_personnel/controller.dart';
import 'package:flyinsono/pages/organization/org_personnel/view.dart';
import 'package:flyinsono/pages/user/user_device/widgets/share_user_terminal.dart';
import 'package:get/get.dart';
import 'package:flyinsono/architecture/router/index.dart';

///手机端分享设备
class OrgStructureRow extends StatelessWidget {
  OrgStructureRow({
    Key? key,
    required this.title,
    this.icon,
    this.isImage = false,
    this.onClick,
    this.hintText = '',
    this.imageUrl = '',
  }) : super(key: key);

  ///标题
  final FWidget title;

  ///图标
  final FWidget? icon;

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

  ///提示文字
  final String? hintText;

  ///点击事件
  final Function? onClick;

  ///图像地址
  final String? imageUrl;

  @override
  FWidget build(BuildContext context) {
    return FMaterial(
      color: Colors.white,
      child: FInk(
        child: FInkWell(
          onTap: () => onClick!(),
          child: FColumn(
            children: [
              FContainer(
                padding: EdgeInsets.symmetric(horizontal: 20),
                height: 50,
                // alignment: Alignment.center,
                child: FRow(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    FContainer(
                      child: FRow(
                        children: [
                          FRow(
                            children: [
                              icon ?? FText(''),
                            ],
                          ),
                          title,
                        ],
                      ),
                    ),
                    FContainer(
                      child: FRow(
                        children: [
                          FIcon(
                            Icons.chevron_right,
                            color: Color.fromRGBO(164, 164, 164, 1),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              FContainer(
                margin: EdgeInsets.symmetric(horizontal: 20),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color.fromRGBO(245, 245, 245, 1),
                    ),
                  ),
                  // color: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

///选择用户结构
class SelectUserStructure extends FStatefulWidget {
  SelectUserStructure(
      {Key? key, required this.list, this.name = '', this.code = ''})
      : super(key: key);

  ///用户Code
  final String? code;

  ///选择类集合
  final List<PublicChoice> list;

  ///名称
  final String? name;

  @override
  FState<SelectUserStructure> createState() => _SelectUserStructureState();
}

class _SelectUserStructureState extends FState<SelectUserStructure> {
  final ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    super.initState();
  }

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

  @override
  FWidget build(BuildContext context) {
    final controller = Get.put(OrgPesonnelController());
    return FScaffold(
      appBar: FAppBar.simple(
        title: i18nBook.user.shareDevice.t,
        onBackClicked: () {
          router.back();
        },
      ),
      body: FMaterial(
        color: Colors.white,
        child: FSafeArea(
          child: FColumn(
            children: [
              _titleName(),
              FExpanded(
                child: QuickFWidget(
                  ScrollConfiguration(
                      behavior: ScrollConfiguration.of(context)
                          .copyWith(scrollbars: false),
                      child: FAlwaysScrollListView(
                        scrollController: _scrollController,
                        child: QuickFWidget(ScrollConfiguration(
                          behavior: ScrollConfiguration.of(context)
                              .copyWith(scrollbars: false),
                          child: FListView.builder(
                            controller: _scrollController,
                            shrinkWrap: true,
                            itemCount: widget.list.length,
                            itemBuilder: (context, index) {
                              if (widget.list.toList()[index].name !=
                                  i18nBook.common.distributed.t) {
                                return OrgStructureRow(
                                  title:
                                      FText(widget.list.toList()[index].name),
                                  onClick: () {
                                    controller.orgStructureFiltering(
                                      widget.list.toList()[index],
                                    );
                                    Future.delayed(Duration(milliseconds: 500),
                                        () {
                                      Get.to(
                                        () => SelectUser(
                                          name:
                                              '${widget.name} > ${widget.list.toList()[index].name}',
                                        ),
                                        preventDuplicates: false,
                                        transition:
                                            Transition.rightToLeftWithFade,
                                      );
                                    });
                                  },
                                );
                              } else {
                                return SizedBox();
                              }
                            },
                          ),
                        )),
                      )),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///标题名称
  FWidget _titleName() {
    return widget.name!.isNotEmpty
        ? FMaterial(
            color: Colors.white,
            child: FColumn(
              children: [
                FContainer(
                  // color: Colors.white,
                  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('');
  }
}

///手机端分享设备
class ShareTerminalMobile extends StatelessWidget implements FWidget {
  ShareTerminalMobile({Key? key}) : super(key: key);
  final controller = Get.put(OrgPesonnelController());

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      body: FContainer(
        child: FObx(
          () => SelectUserStructure(
            name: controller.state.organizationName,
            list: controller.state.companyPersonnelList,
          ),
        ),
      ),
    );
  }
}
