/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2023-02-21 15:21:04
 * @LastEditTime : 2023-06-26 15:48:33
 * @Description  : Do not edit
 */
import 'package:app_package/src/api/message.dart';
import 'package:app_package/src/models/msg_item.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

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

  final scorllViewKey = GlobalKey<WisScrollViewState>();

  late Map<String, dynamic> params = {
    'pageNum': 1,
    'pageSize': 20,
  };

  List<MsgItemModel> list = [];

  Future initPage() async {
    await getList(isLoad: false);
    await putReadMessage();
  }

  Future putReadMessage() async {
    await MessageApi.putGroupRead({
      'group': args['type'],
    });
  }

  Future getList({bool isLoad = true}) async {
    // if (currentCommunity.id == null) {
    //   return;
    // }

    if (!isLoad) params['pageNum'] = 1;
    var res = await MessageApi.getMsgList({
      ...params,
      'group': args['type'],
      // 'communityId': currentCommunity.id,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      scorllViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      scorllViewKey.currentState?.isLast();
    } else {
      params['pageNum']++;
    }
    isLoad ? list.addAll(_list) : list = _list;
    notifyListeners();
  }
}
