// import 'package:fis_jsonrpc/services/management.m.dart';
// import 'package:fis_jsonrpc/services/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/organization/org_personnel/controller.dart';
import 'package:flyinsono/pages/organization/org_personnel/view.dart';
import 'package:get/get.dart';

final orgPesonnelController = Get.find<OrgPesonnelController>();

class OrgPesonnelState {
  final Rx<String> _searchText = Rx('');
  final Rx<String> _organizationCode = Rx('');
  final Rx<String> _organizationQueryType = ''.obs;
  final Rx<String> _rankCode = Rx('');
  final Rx<String> _positionCode = Rx('');
  final Rx<String> _organizationName = Rx('');
  final Rx<String> _companyCode = Rx('');
  final Rx<String> _positionListCode = Rx('');
  final Rx<String> _professionalListCode = Rx('');
  final Rx<bool> _tableLoading = Rx(false);
  final Rx<bool> _batchOperation = Rx(false);
  final Rx<int> _radio = Rx(0);
  final Rx<List<UserExtendDTO>> _tableDataSource = Rx(<UserExtendDTO>[]);
  final Rx<List<String>> _selectedIdxs = Rx(<String>[]);
  final Rx<List<PublicChoice>> _companyPersonnelList = Rx(<PublicChoice>[]);
  final Rx<List<PublicChoice>> _professionalList = Rx(<PublicChoice>[]);
  final Rx<List<PublicChoice>> _positionList = Rx(<PublicChoice>[]);
  final Rx<List<int>> _selectedList = Rx(<int>[]);

  /// 输入框输入值
  set searchText(value) => _searchText.value = value;
  get searchText => _searchText.value;

  /// 表格筛选组织
  set organizationCode(value) => _organizationCode.value = value;
  get organizationCode => _organizationCode.value;

  /// 表格筛选组织
  set organizationQueryType(value) => _organizationQueryType.value = value;
  get organizationQueryType => _organizationQueryType.value;

  /// 表格筛选职称
  set rankCode(value) => _rankCode.value = value;
  get rankCode => _rankCode.value;

  /// 表格筛选职称
  set positionCode(value) => _positionCode.value = value;
  get positionCode => _positionCode.value;

  /// 组织名称
  set organizationName(value) => _organizationName.value = value;
  get organizationName => _organizationName.value;

  /// 表格数据
  List<UserExtendDTO> get tableDataSource => _tableDataSource.value;
  set tableDataSource(List<UserExtendDTO> value) => _tableDataSource(value);

  /// tableLoading
  bool get tableLoading => _tableLoading.value;
  set tableLoading(bool val) => _tableLoading.value = val;

  /// tableLoading
  bool get batchOperation => _batchOperation.value;
  set batchOperation(bool val) => _batchOperation.value = val;

  /// 复选项的值
  List<String> get selectedIdxs => _selectedIdxs.value;
  set selectedIdxs(List<String> value) => _selectedIdxs(value);

  /// 组织架构列表
  List<PublicChoice> get companyPersonnelList => _companyPersonnelList.value;
  set companyPersonnelList(List<PublicChoice> value) =>
      _companyPersonnelList(value);

  /// 岗位身份列表
  List<PublicChoice> get positionList => _positionList.value;
  set positionList(List<PublicChoice> value) => _positionList(value);

  /// 职称列表
  List<PublicChoice> get professionalList => _professionalList.value;
  set professionalList(List<PublicChoice> value) => _professionalList(value);

  /// 待选员工 后期优化
  List<int> get selectedList => _selectedList.value;
  set selectedList(List<int> value) => _selectedList(value);

  /// 单选框
  set radio(value) => _radio.value = value;
  get radio => _radio.value;

  /// 父级组织架构
  get rootOrganizationCode => Store.user.rootOrganizationCode;

  /// 手机端部门Code
  set companyCode(value) => _companyCode.value = value;
  get companyCode => _companyCode.value;

  /// 手机端岗位身份Code
  set positionListCode(value) => _positionListCode.value = value;
  get positionListCode => _positionListCode.value;

  /// 手机端职称Code
  set professionalListCode(value) => _professionalListCode.value = value;
  get professionalListCode => _professionalListCode.value;

  /// 手机 部门名称
  String get companyName {
    if (companyCode == null || companyCode!.isEmpty)
      return i18nBook.homePage.notYet.t;
    final model = orgPesonnelController.allOrgList.toList().firstWhereOrNull(
          (x) => x.code == companyCode,
        );
    return model?.name ?? i18nBook.homePage.notYet.t;
  }

  /// 手机 部门名称
  String get positionListName {
    if (positionListCode == null || positionListCode!.isEmpty)
      return i18nBook.homePage.notYet.t;
    final model =
        orgPesonnelController.allProfessionalList.toList().firstWhereOrNull(
              (x) => x.code == positionListCode,
            );
    return model?.name ?? i18nBook.homePage.notYet.t;
  }

  /// 手机 部门名称
  String get professionalListName {
    if (professionalListCode == null || professionalListCode!.isEmpty)
      return i18nBook.homePage.notYet.t;
    final model = orgPesonnelController.allRanks.toList().firstWhereOrNull(
          (x) => x.code == professionalListCode,
        );
    return model?.name ?? i18nBook.homePage.notYet.t;
  }

  // /// 搜索类型Code
  // GetUserListTypeEnum get searchType => _searchTypeCode.value;
  // set searchType(GetUserListTypeEnum value) {
  //   if (value != _searchTypeCode.value) {
  //     _searchTypeCode.value = value;
  //   }
  // }
}

///组织名称模型
class OrgNameModel {
  OrgNameModel({required this.name, required this.code});
  final String name;
  final String code;

  factory OrgNameModel.fromJson(Map<String, dynamic> map) {
    return OrgNameModel(
      name: map['n'],
      code: map['c'],
    );
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['c'] = code;
    map['n'] = name;
    return map;
  }
}
