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/architecture/router/index.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 ReferOrgDialog extends FStatefulWidget implements FPage {
  ReferOrgDialog({
    Key? key,
    this.selectReferOrgList,
    this.pageName = 'ReferOrgDialog',
  }) : super(key: key);
  final List<OrgModel>? selectReferOrgList;
  @override
  final String pageName;

  @override
  FState<ReferOrgDialog> createState() => _ReferOrgDialogState();
}

class _ReferOrgDialogState extends FState<ReferOrgDialog> {
  final controller = Get.find<OrgInfoController>();

  /// 搜索转诊机构的名称
  late String orgName = '';

  /// 当前选中的转诊机构
  late List<OrgModel> referOrgList = [];

  /// 当前选择的转诊机构
  late List<OrgModel> orgModelList = [];
  @override
  void initState() {
    super.initState();
    controller.state.isSearchReferOrg = true;
    controller.state.referOrgOptions = controller.state.allReferOrgOptions;
    widget.selectReferOrgList?.toList().forEach((e) {
      referOrgList.add(e);
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.college.selectHospital.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      isDefault: true,
      onOk: () {
        controller.state.selectReferOrgList = referOrgList;
        router.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onCancel: () {
        referOrgList = widget.selectReferOrgList ?? [];
        router.back();
      },
      children: [
        FContainer(
          width: 600,
          height: 600,
          child: FContainer(
            padding: EdgeInsets.only(bottom: 20),
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                _buildSerarchInput(),
                _buildOrgSelectOptions(),
              ],
            ),
          ),
        ),
      ],
    );
  }

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

  ///选择机构
  FWidget _buildOrgSelectOptions() {
    return FExpanded(
      child: FObx(
        () {
          orgModelList = controller.state.referOrgOptions ?? [];
          return FListView.builder(
            shrinkWrap: true,
            itemCount: orgModelList.length,
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  var targetOrg = referOrgList.firstWhereOrNull(
                      (element) => element.code == orgModelList[index].code);
                  if (targetOrg != null) {
                    referOrgList.remove(targetOrg);
                  } else {
                    referOrgList.add(OrgModel(
                        code: orgModelList[index].code,
                        name: orgModelList[index].name));
                  }
                  setState(() {});
                },
                child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 15,
                    vertical: 8,
                  ),
                  child: FRow(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      FText(orgModelList[index].name),
                      if (referOrgList
                          .any((e) => e.code == orgModelList[index].code))
                        FIcon(
                          Icons.done_rounded,
                          size: 20,
                          color: FTheme.ins.colorScheme.primary,
                        )
                    ],
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    var state = controller.state;
    state.referOrgOptions = state.allReferOrgOptions;
    controller.searchOrgController.text = '';
    super.dispose();
  }
}
