/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 15:20:44
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-02-02 14:46:37
 * @Descripttion: 
 */
part of 'index.dart';

class _Store with ChangeNotifier {
  final BuildContext context;
//这是获取路由传参的例子
//Map args;
//_Store(this.context) {
//args = ModalRoute.of(context).settings.arguments??{};
//}
  _Store(this.context);

  final appGlobalStore = GetIt.I.get<AppGlobalStore>();

  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  ///从储存中取出的当前小区 拿出来可能是空
  CommunityModel get currentCommunity => communityGlobalStore.data;

  Future onInit(menuItem) async {
    getPageSetting(menuItem.id);
  }

  AppPageSettingDetailModel? detail;

  final scrollViewKey = GlobalKey<WisScrollViewState>();

  ///去选择小区
  Future toCommunityPick() async {
    await Navigator.pushNamed(context, 'community');
    scrollViewKey.currentState?.initTrigger();
    notifyListeners();
  }

  /// app页面设置详情
  Future getPageSetting(id) async {
    // final appGlobalStore = GetIt.I.get<AppGlobalStore>();
    var data;
    final PackageInfo packageInfo = await AppPackageInfo.instance.getPackageInfo();

    /// 版本号
    if (Platform.isAndroid) {
      data = {
        'androidMinimumVersion': packageInfo.version,
      };
    } else {
      data = {
        'iosMinimumVersion': packageInfo.version,
      };
    }
    var res = await RootApi.getAppPageSettingDetail(id, data);
    detail = res.data!;
    notifyListeners();
  }

  toRoutes(item, type) {
    switch (type) {
      // case 'oa':
      //   break;
      case 'menu':
        toMenuDetail(item);
        break;
      case 'intelligentForm':
        toFormDetail(item);
        break;
    }
  }

  FormMenuIdDetailModel? formDetail;
  toFormDetail(item) async {
    var res = await RootApi.getFormIdDetail(item.id);
    formDetail = res.data!;
    handleRoute(formDetail);
  }

  handleRoute(item) async {
    /// 单次填报表单
    if (item.fillPeriodType == 'single') {
      var params = {
        'isPage': false,
        'formIdentification': item.formIdentification,
      };
      params['communityId'] =
          currentCommunity.id != null ? currentCommunity.id : null;
      var res = await RootApi.getMenuFormList(params);
      if (res.data!.length > 0) {
        toDetail(res.data![0], res.data![0].title);
      } else {
        toList(item);
      }
    } else {
      toList(item);
    }
  }

  /// 跳转详情
  toDetail(item, title) async {
    await Navigator.pushNamed(
      context,
      'message_detail',
      arguments: {
        'appBarTitle': title,
        'item': item,
      },
    );
  }

  /// 跳转填报列表
  toList(item) async {
    await Navigator.pushNamed(
      context,
      'message_list',
      arguments: {
        'appBarTitle': item?.name,
        'item': item,
      },
    );
  }

  toMenuDetail(item) async {
    var _cmmunityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

    ///判断未开通
    if (!item.exist) {
      return WToastUtil.show('正在建设中...');
    }
    if (item.relationCommunity == true) {
      var boolean = await _cmmunityGlobalStore.intent;
      if (boolean) {
        if (item.route == 'qrcode/clean' || item.route == 'qrcode/security') {
          final context = WNavUtil.instance!.context;
          Route2business.scan2page(
            context: context!,
            scene: item.route == 'qrcode/clean'
                ? ScanScene.clean
                : ScanScene.security,
          );
        } else {
          WNavUtil.instance!
              .pushNamed(item?.route, arguments: {'appBarTitle': item?.name});
        }
      }
    } else if (item.route == 'qrcode/clean' ||
        item.route == 'qrcode/security') {
      final context = WNavUtil.instance!.context;
      Route2business.scan2page(
        context: context!,
        scene:
            item.route == 'qrcode/clean' ? ScanScene.clean : ScanScene.security,
      );
    } else {
      WNavUtil.instance!
          .pushNamed(item?.route, arguments: {'appBarTitle': item?.name});
    }
  }
}
