import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/organization/org_personnel/controller.dart';
import 'package:flyinsono/pages/organization/org_personnel/view.dart';
import 'package:flyinsono/pages/organization/org_personnel/widgets/org_member.dart';
import 'package:get/get.dart';

/// 组织架构单行选择
class OrgStructureRow extends StatelessWidget {
  OrgStructureRow({
    Key? key,
    required this.title,
    this.icon,
    this.onClick,
  }) : super(key: key);

  /// 组织架构名
  final FWidget title;

  /// 组织架构icon
  final FWidget? icon;

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

  @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,
                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),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

///组织架构视图内容
class SelectOrgStructure extends FStatefulWidget {
  SelectOrgStructure({
    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<SelectOrgStructure> createState() => _SelectOrgStructureState();
}

class _SelectOrgStructureState extends FState<SelectOrgStructure> {
  @override
  void initState() {
    super.initState();
  }

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

  @override
  FWidget build(BuildContext context) {
    final orgPesonnelController = Get.put(OrgPesonnelController());

    return FScaffold(
      appBar: FAppBar.simple(
        title: i18nBook.organization.orgStructure.t,
        onBackClicked: () {
          router.back();
        },
      ),
      body: FMaterial(
        color: Colors.white,
        child: FSafeArea(
          child: FColumn(
            children: [
              _buildOrgStructureTitle(),
              FExpanded(
                child: FListView.builder(
                  shrinkWrap: true,
                  itemCount: widget.list.length,
                  itemBuilder: (context, index) {
                    return OrgStructureRow(
                      title: FText(widget.list.toList()[index].name),
                      onClick: () async {
                        orgPesonnelController.orgStructureFiltering(
                          widget.list.toList()[index],
                        );
                        Get.to(
                          () => SelectOrgMember(
                            name:
                                '${widget.name} > ${widget.list.toList()[index].name}',
                          ),
                          preventDuplicates: false,
                          transition: Transition.rightToLeftWithFade,
                        );
                      },
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///组织机构标题
  FWidget _buildOrgStructureTitle() {
    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('');
  }
}
