import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/bbs/model/news_comm_entity.dart';
import 'package:liftmanager/internal/bbs/presenter/news_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'dart:convert';
import 'package:permission_handler/permission_handler.dart';
// import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:liftmanager/utils/toast.dart';

// import '../news_router.dart'; 

class NewsListPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return NewsListState();
  }
}

class NewsListState extends BasePageState<NewsListPage, NewsListPresenter> {
  BaseListProvider<NewsItem> provider = BaseListProvider<NewsItem>();

  String _keyword="";
  int _page = 1;

  @override
  void initState() {
    /// 默认为加载中状态，本页面场景默认为空
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<BaseListProvider<NewsItem>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "新闻列表",
        ),
        body: Consumer<BaseListProvider<NewsItem>>(
            builder: (_, provider, __) {
              return MyListView(
                key: Key('order_search_list'),
                itemCount: provider.list.length,
                stateType: provider.stateType,
                onRefresh: _onRefresh,
                loadMore: _loadMore,
                hasMore: provider.hasMore,
                itemBuilder: (_, index){
                  bool isDark = ThemeUtils.isDark(context);
                  return InkWell(
                    onTap: () {
                      NavigatorUtils.push(context,
                          "${BbsRouter.newsDetail}?id=${provider.list[index].id}");
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        color: ThemeUtils.getTabsBg(context),
                        border: Border(
                            bottom: BorderSide(
                                width: 0.5, color: Colours.line)),
                      ),
                      padding: const EdgeInsets.all(15),
                      child: Column(
                        crossAxisAlignment:
                        CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            "${provider.list[index].title}",
                            style: TextStyle(fontSize: 15,color:isDark?Colours.dark_text:Colours.text),
//                                        overflow: TextOverflow.ellipsis,
                          ),
                          Gaps.vGap12,
                          Text(
                            "${provider.list[index].releaseUser}·阅读${provider.list[index].lookNum}次·点赞${provider.list[index].likeNum}",
                            style: TextStyle(
                                fontSize: 11,
                                color: Colours.text_gray),
                          )
                        ],
                      ),
                    ),
                  );
                },
              );
            }
        ),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    // await presenter.list(context,_keyword, _page,10,1, false);
  }

  Future _loadMore() async {
    _page++;
    // await presenter.list(context,_keyword, _page,10,1, false);
  }

  @override
  NewsListPresenter createPresenter() {
    return NewsListPresenter();
  }
}