import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/organization/org_info/controller.dart';
import 'package:flyinsono/pages/organization/org_info/state.dart';
import 'package:get/get.dart';

/// PC上级机构列表选择页面
class HigherOrgDialog extends StatefulWidget implements FPage {
  HigherOrgDialog({
    Key? key,
    this.pageName = 'HigherOrgDialog',
    this.higherOrgCode = '',
  }) : super(key: key);
  final String? higherOrgCode;

  @override
  final String pageName;

  @override
  State<HigherOrgDialog> createState() => _HigherOrgDialogState();
}

class _HigherOrgDialogState extends State<HigherOrgDialog> {
  final controller = Get.find<OrgInfoController>();
  final ScrollController _scrollController = ScrollController();
  late String orgCode = widget.higherOrgCode ?? '';

  late List<OrgModel> orgModelList = [];
  late String orgName = '';
  void onChangeParentOrg(int index) {
    orgCode = orgModelList[index].code;
    controller.state.parentCode = orgModelList[index].code;
    controller.state.parentOrganizationName = orgModelList[index].name;
    setState(() {});
    Get.back();
  }

  @override
  void initState() {
    controller.state.isSearchReferOrg = false;
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      title: FText(
        i18nBook.organization.selectSuperiorOrg.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      children: [
        FContainer(
          width: 600,
          height: 600,
          child: FContainer(
            padding: EdgeInsets.only(bottom: 20),
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                _buildSearchInputRow(),
                _buildSelectOrgOptions(),
              ],
            ),
          ),
        ),
      ],
    );
  }

  ///搜索框
  FWidget _buildSearchInputRow() {
    return FContainer(
      padding: EdgeInsets.all(15),
      child: FRow(
        children: [
          FBorderInput(
            hintSize: 16,
            contentSize: 16,
            maxLength: 20,
            controller: controller.searchOrgController,
            borderColor: Color.fromRGBO(234, 234, 234, 1),
            suffixIcon: FMaterial(
              color: Colors.transparent,
              child: FIconButton(
                businessParent: widget,
                name: "search",
                onPressed: () {
                  controller.onEnterPressed();
                  setState(() {});
                },
                icon: FIcon(
                  Icons.search,
                ),
              ),
            ),
            height: 38,
            onChanged: (value) {
              orgName = value;
            },
          ),
        ],
      ),
    );
  }

  ///选择机构
  FWidget _buildSelectOrgOptions() {
    return FExpanded(
      child: FObx(
        () {
          orgModelList = controller.state.orgOptions ?? [];
          return FAlwaysScrollListView(
            scrollController: _scrollController,
            child: QuickFWidget(ScrollConfiguration(
              behavior:
                  ScrollConfiguration.of(context).copyWith(scrollbars: false),
              child: FListView.builder(
                shrinkWrap: true,
                controller: _scrollController,
                itemCount: orgModelList.length,
                itemBuilder: (context, index) {
                  return InkWell(
                    onTap: () {
                      onChangeParentOrg(index);
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(
                        horizontal: 15,
                        vertical: 8,
                      ),
                      child: FRow(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          FText(orgModelList[index].name),
                          if (orgCode == orgModelList[index].code)
                            FIcon(
                              Icons.done_rounded,
                              size: 20,
                              color: FTheme.ins.colorScheme.primary,
                            )
                        ],
                      ),
                    ),
                  );
                },
              ),
            )),
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    controller.searchOrgController.text = '';
    controller.state.orgOptions = controller.state.allOrgOptions;
    super.dispose();
  }
}
