import 'package:get/get.dart';
import 'package:signalr_core/signalr_core.dart';
import 'package:wgshare/common/models/common/base_structure_result.dart';
import 'package:wgshare/common/mixins/request_tool_mixin.dart';

import '../../common/models/meeting_room_item.dart';
import 'home_state.dart';

class HomeLogic extends GetxController with RequestToolMixin {

  final HomeState state = HomeState();

  @override
  void onInit() {
    super.onInit();
    doHttpGetMeetingRoomList(state.pageIndex.value,state.pageSize.value);
  }

  @override
  void onClose() {
    super.onClose();
    state.refreshController.dispose();
  }

  /// 直接进入会议（匿名登录）
  Future<void> doHttpGetMeetingRoomList(int pageIndex, int pageSize) async {
    BaseStructureResult<MeetingRoomItem> res = await getClient().getMeetingRoomList(pageIndex,pageSize);
    if(null != res.data){
      if(state.pageIndex == 1){
        state.meetingRooms.value = res.data!.items;
        state.totalPage.value = res.data!.totalPage;
        state.total.value = res.data!.total;
        state.refreshController.refreshCompleted(resetFooterState: true);
      }else{
        if(state.pageIndex.value < state.totalPage.value){
          state.meetingRooms.value.addAll(res.data!.items);
          state.refreshController.loadComplete();
        }else{
          state.refreshController.loadNoData();
        }
      }
    }
  }

  /// 下拉刷新
  void onRefresh(){
    state.pageIndex.value = 1;
    doHttpGetMeetingRoomList(state.pageIndex.value,state.pageSize.value);
  }

  /// 上滑加载更多
  void onLoading(){
    state.pageIndex.value += 1;
    doHttpGetMeetingRoomList(state.pageIndex.value,state.pageSize.value);
  }

}
