/*
 * @Author: WuWei
 * @Date: 2020-05-14 14:58:35
 * @LastEditors  : WuWei
 * @LastEditTime : 2022-01-19 11:15:47
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/widgets.dart';
import 'package:news_package/src/impl/industrynew_service_impl.dart';
import 'package:news_package/src/models/industry_news_list.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class NewsListStore with ChangeNotifier {
  final BuildContext context;
  late Map args;
  NewsListStore(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }
  final scorllViewKey = GlobalKey<WisScrollViewState>();
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

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

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

  List<IndustryNewsListModel> list = [];

  Future getList({bool isLoad = true}) async {
    if (!isLoad) params['pageNum'] = 1;
    var res = await IndustryNewsServiceImpl().getNewsList({
      ...params,
    });
    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();
  }

  route2detail(IndustryNewsListModel item) {
    Navigator.pushNamed(context, 'industrynew/detail',
        arguments: item.toJson());
  }
}
