import 'package:domain/entity/home_module.dart';
import 'package:domain/entity/response/hotel_info_entity.dart';
import 'package:get/get.dart';
import 'package:plugin_basic/service/app_config_service.dart';
import 'package:plugin_basic/service/user_service.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:shared/utils/log_utils.dart';

import '../report_finance/report_finance_page.dart';
import '../report_labour/report_labour_page.dart';
import '../report_staff_request/report_staff_request_page.dart';
import 'report_list_state.dart';

class SGReportListController extends GetxController {
  final ReportListState state = ReportListState();

  /// 获取报表的数据
  void setupReportData() async {
    final hotelInfo = UserService.to.getHotelInfo;

    List<HotelInfoMenusChildren>? list = hotelInfo.menus?.firstWhereOrNull((element) => element.key == "report")?.children;

    Log.d("list:$list");

    if (list != null && list.isNotEmpty) {
      //有数据，判断是刷新还是加载更多的数据
      state.datas.clear();

      //如果是管理员登录，根据Key筛选需要展示的模块
      for (var children in list) {
        if (children.key != null) {
          state.datas.addAll(_filterModulesByKey(children.key!));
        }
      }
    } else {
      //清除
      state.datas.clear();
    }

    update();
  }

  List<HomeModule> _filterModulesByKey(String key) {
    return state.modules.where((module) => module.key == key).toList();
  }

  @override
  void onReady() async {
    super.onReady();
    setupReportData();
  }

  /// 跳转到指定的模块中去
  void gotoReportPage(HomeModule module) {
    switch (module.key) {
      case 'finRep':
        ReportFinancePage.startInstance();
        break;
      case 'casLab':
        //新加坡
        ReportLabourPage.startInstance();
        break;
      case 'monReq':
        ReportStaffRequestPage.startInstance();
        break;
    }
  }
}
