import 'package:flutter/material.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/team/model/team_entity.dart';
import 'package:liftmanager/internal/team/presenter/team_search_presenter.dart';
import 'package:liftmanager/internal/team/team_router.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/image_utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_button.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/search_bar.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart' as p;

class TeamSearchPage extends StatefulWidget {
  @override
  TeamSearchPageState createState() => TeamSearchPageState();
}

class TeamSearchPageState
    extends BasePageState<TeamSearchPage, TeamSearchPresenter> {
  BaseListProvider<TeamItem> provider = BaseListProvider<TeamItem>();

  String _keyword = "";
  int _page = 1;

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

  @override
  Widget build(BuildContext context) {
    return p.ChangeNotifierProvider<BaseListProvider<TeamItem>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "团队搜索",
//          actions: <Widget>[
//            FlatButton(
//              child: Text("创建团队", key: const Key('actionName')),
//              textColor: Colours.dark_text,
//              highlightColor: Colors.transparent,
//              onPressed: (){
//
//              },
//            )
//          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              color: Colors.white,
              padding:
                  EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
              child: SearchBar(
                hintText: "请输入团队名称",
                onPressed: (text) {
//                  if (text.isEmpty){
//                    showToast("搜索团队名称不能为空！");
//                    return;
//                  }
//                  FocusScope.of(context).unfocus();
                  _keyword = text;
                  provider.setStateType(StateType.loading);
                  _page = 1;
                  presenter.searchTeam(context,_keyword, _page, true);
                },
              ),
            ),
            Expanded(
                flex: 1,
                child: p.Consumer<BaseListProvider<TeamItem>>(
                    builder: (_, provider, __) {
                  return MyListView(
                    key: Key('team_search_list'),
                    itemCount: provider.list.length,
                    stateType: provider.stateType,
                    onRefresh: _onRefresh,
                    loadMore: _loadMore,
//                    itemExtent: 67.0,
                    hasMore: provider.hasMore,
                    itemBuilder: (_, index) {
                      return Container(
                        decoration: BoxDecoration(
                            color:Colors.white,
                            border: Border(
                              bottom: BorderSide(
                                  width: 0.5, color: Colours.text_gray_c),
                            )),
                        padding: const EdgeInsets.symmetric(
                            horizontal: 15.0, vertical: 12.0),
                        alignment: Alignment.centerLeft,
                        child: Row(
                          children: <Widget>[
                            CircleAvatar(
                                radius: 21.5,
                                backgroundColor: Colors.transparent,
                                backgroundImage: ImageUtils.getImageProvider(
                                    provider.list[index]?.logoImg)),
                            Gaps.hGap10,
                            Expanded(
                                flex: 1,
                                child: Column(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Text(
                                      provider.list[index].name,
                                      style: TextStyles.text15,
                                      overflow: TextOverflow.ellipsis,
                                      softWrap: false, //单行显示
                                    ),
                                    Text(
                                      "${provider.list[index]?.remarks}",
                                      style: TextStyles.textGray13,
                                      overflow: TextOverflow.ellipsis,
                                      softWrap: false, //单行显示
                                    )
                                  ],
                                )),
                            MyButton(
                              key: const Key('login'),
                              onPressed: () {
                                var item = provider.list[index];
                                NavigatorUtils.push(context,
                                    "${TeamRouter.teamDetailPage}?id=${item.id}&name=${Uri.encodeComponent(item.name)}&logoImg=${Uri.encodeComponent(item.logoImg)}&corporator=${Uri.encodeComponent(item.corporator)}&telephone=${Uri.encodeComponent(item.telephone)}&userNum=${item.userNum}&createDate=${item.createDate}&remarks=${Uri.encodeComponent(item.remarks)}&isCertificated=${item.isCertificated}");
                              },
                              height: 25,
                              fontSize: 11,
                              colors: [Colours.app_main, Colours.app_main],
                              borderWidth: 0.5,
                              borderColor: Colours.app_main,
                              text: "加入团队",
                            )
                          ],
                        ),
                      );
                    },
                  );
                }))
          ],
        ),
      ),
    );
  }

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

  Future _loadMore() async {
    _page++;
    await presenter.searchTeam(context,_keyword, _page, false);
  }

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