import 'package:flutter/material.dart';
import 'package:liftmanager/internal/lift/lift_router.dart';
import 'package:liftmanager/internal/project/model/project_list_entity.dart';
import 'package:liftmanager/internal/project/presenter/project_list_presenter.dart';
import 'package:liftmanager/internal/project/project_router.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_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 ProjectListPage extends StatefulWidget {
  @override
  ProjectListPageState createState() => ProjectListPageState();
}

class ProjectListPageState
    extends BasePageState<ProjectListPage, ProjectListPresenter> {
  BaseListProvider<ProjectListItem> provider =
      BaseListProvider<ProjectListItem>();

  String _keyword = "";
  int _page = 1;

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

  ///电梯列表
  void _liftList(id) {
    NavigatorUtils.push(context, LiftRouter.liftListPage + "?id=${id}");
  }

  ///项目详情
  void _projectDetail(id){
    NavigatorUtils.push(context, "${ProjectRouter.projectDetailPage}?id=${id}");
  }

  @override
  Widget build(BuildContext context) {
    bool isDark = ThemeUtils.isDark(context);
    return p.ChangeNotifierProvider<BaseListProvider<ProjectListItem>>(
        create: (_) => provider,
        child: WillPopScope(
        onWillPop:(){
          NavigatorUtils.goBackWithParams(context,true);
          return Future.value(false);
        },
        child:Scaffold(
          appBar: MyAppBar(
            centerTitle: "项目列表",
          ),
          body: Container(
            color: ThemeUtils.getBackgroundColor(context),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  color:ThemeUtils.getTabsBg(context),
                  padding:
                      EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
                  child: SearchBar(
                    hintText: "搜索项目",
                    autofocus: false,
                    onPressed: (text) {
                      _keyword = text;
                      provider.setStateType(StateType.loading);
                      _page = 1;
                      presenter.searchProject(context,_keyword, _page, true);
                    },
                  ),
                ),
                Expanded(
                    flex: 1,
                    child: p.Consumer<BaseListProvider<ProjectListItem>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('project_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return InkWell(
                              onTap: (){
                                _projectDetail(provider.list[index].projectId);
                              },
                              child: Container(
                            margin: EdgeInsets.fromLTRB(15, 5, 15, 5),
                            decoration: BoxDecoration(
                              color: isDark?Colors.black:Colors.white,
                              borderRadius: BorderRadius.circular(6.0),
                            ),
                            padding: const EdgeInsets.only(bottom: 10),
                            child: Column(
                              children: <Widget>[
                                Container(
                                  decoration: BoxDecoration(
                                      border: Border(
                                    bottom: BorderSide(
                                        width: 0.5, color: Colours.text_gray_c),
                                  )),
                                  padding: EdgeInsets.only(left: 12),
                                  child: Row(
                                    children: <Widget>[
                                      Expanded(
                                          flex: 1,
                                          child: Text(
                                            provider.list[index].projectName,
                                            style: TextStyle(fontSize: 15,color:isDark?Colours.dark_text:Colours.text),
                                            overflow: TextOverflow.ellipsis,
                                            softWrap: false, //单行显示
                                          )),
                                      MyButton(
                                        key: const Key('lift_list'),
                                        onPressed: () {
                                          _liftList(provider.list[index].projectId);
                                        },
                                        height: 25,
                                        fontSize: 11,
                                        colors: [
                                          Colours.app_main,
                                          Colours.app_main
                                        ],
                                        borderWidth: 0.5,
                                        borderColor: Colours.app_main,
                                        text: "查看电梯",
                                      )
                                    ],
                                  ),
                                ),
                                lineTxt("项目编号",
                                    "${provider.list[index].projectCode}"),
                                lineTxt("项目用途", "${provider.list[index].getProjectUsageName()}",),
                                lineTxt(
                                    "项目台量", "${provider.list[index].actualNum}")
                              ],
                            ),
                          ));
                        },
                      );
                    }))
              ],
            ),
          ),
        )));
  }

  Widget lineTxt(title, value) {
    return Container(
      padding: EdgeInsets.only(left: 12, top: 5, right: 12),
      child: Row(
        children: <Widget>[
          Text("${title}",
              style: TextStyle(fontSize: 13, color: Colours.dark_text_gray)),
          Expanded(
            flex: 1,
            child: Text(
              "${value}",
              textAlign: TextAlign.right,
              style: TextStyle(fontSize: 13, color: Colours.dark_text_gray),
            ),
          )
        ],
      ),
    );
  }

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

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

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