import 'dart:async';

import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/organization/org_personnel/view.dart';
import 'package:get/get.dart';

/// 手机端构建选择
class SelectBuildRow extends StatelessWidget {
  SelectBuildRow({
    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 SelectBuild extends StatefulWidget {
  SelectBuild({
    Key? key,
    required this.list,
    this.name = '',
    this.code = '',
  }) : super(key: key);

  ///user Code
  final String? code;

  ///选择列表
  final List<PublicChoice> list;

  ///名称
  final String? name;
  static Completer<List<String>?>? _completer;

  ///页面跳转
  static Future<List<String>?>? open(name, orgOptions) async {
    _completer = Completer<List<String>?>();
    Get.to(
      () => SelectBuild(
        name: name,
        list: orgOptions,
      ),
      preventDuplicates: false,
      transition: Transition.rightToLeftWithFade,
    );

    final result = await _completer!.future;
    _completer = null;
    return result;
  }

  @override
  State<SelectBuild> createState() => _SelectBuildState();
}

class _SelectBuildState extends State<SelectBuild> {
  /// 选择下钻的位置
  var areaIndex;
  final ScrollController _scrollController = ScrollController();

  /// 获取的areaCode
  var areaCode;
  @override
  void initState() {
    super.initState();
  }

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

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar.simple(
          title: widget.name,
          onBackClicked: () {
            Get.back();
          }),
      body: FMaterial(
        color: Colors.white,
        child: FSafeArea(
          child: FColumn(
            children: [
              FExpanded(
                child: FAlwaysScrollListView(
                  scrollController: _scrollController,
                  child: QuickFWidget(ScrollConfiguration(
                    behavior: ScrollConfiguration.of(context)
                        .copyWith(scrollbars: false),
                    child: FListView.builder(
                      shrinkWrap: true,
                      controller: _scrollController,
                      itemCount: widget.list.length,
                      itemBuilder: (context, index) {
                        return SelectBuildRow(
                          title: FText(widget.list.toList()[index].name),
                          onClick: () {
                            SelectBuild._completer!.complete([
                              '${widget.name}',
                              '${widget.list.toList()[index].code}'
                            ]);
                            Get.until(
                              (route) {
                                final backRoute =
                                    route.settings.name != "/SelectBuild";
                                return backRoute;
                              },
                            );
                          },
                        );
                      },
                    ),
                  )),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
