import 'package:flutter/material.dart';
import 'package:liftmanager/internal/lift/model/lift_list_entity.dart';
import 'package:liftmanager/internal/repair/provider/repair_select_lift_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/app_search_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 RepairSelectLiftPage extends StatefulWidget {
  RepairSelectLiftPage(this.projectId);

  final String projectId;

  @override
  RepairSelectLiftPageState createState() => RepairSelectLiftPageState();
}

class RepairSelectLiftPageState
    extends BasePageState<RepairSelectLiftPage, RepairSelectLiftPresenter> {
  BaseListProvider<LiftListItem> provider = BaseListProvider<LiftListItem>();

  String _keyword = "";
  int _page = 1;

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

  @override
  Widget build(BuildContext context) {
    bool isDark = ThemeUtils.isDark(context);
    return p.ChangeNotifierProvider<BaseListProvider<LiftListItem>>(
        create: (_) => provider,
        child: Scaffold(
          appBar: SearchAppBar2(
              backgroundColor: Colors.white,
              onPressed: (text) {
                print(widget.projectId);
                      _keyword = text;
                      provider.setStateType(StateType.loading);
                      _page = 1;
                      presenter.searchLift(
                          context, widget.projectId, _keyword, _page, true);
              },),
          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(
//                     autofocus: false,
//                     hintText: "搜索电梯",
//                     onPressed: (text) {
// //                  if (text.isEmpty){
// //                    showToast("搜索团队名称不能为空！");
// //                    return;
// //                  }
// //                  FocusScope.of(context).unfocus();
//                       print(widget.projectId);
//                       _keyword = text;
//                       provider.setStateType(StateType.loading);
//                       _page = 1;
//                       presenter.searchLift(
//                           context, widget.projectId, _keyword, _page, true);
//                     },
//                   ),
//                 ),
                Expanded(
                    flex: 1,
                    child: p.Consumer<BaseListProvider<LiftListItem>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('lift_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
//                    itemExtent: 67.0,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return 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].useCompanyCode,
                                            style: TextStyle(
                                              fontSize: 15,
                                              color: isDark
                                                  ? Colours.dark_text
                                                  : Colours.text,
                                            ),
                                            overflow: TextOverflow.ellipsis,
                                            softWrap: false, //单行显示
                                          )),
                                      MyButton(
                                        key: const Key('lift_list'),
                                        onPressed: () {
                                          NavigatorUtils.goBackWithParams(context, provider.list[index]);
                                        },
                                        height: 25,
                                        fontSize: 11,
                                        colors: [
                                          Colours.app_main,
                                          Colours.app_main
                                        ],
                                        borderWidth: 0.5,
                                        borderColor: Colours.app_main,
                                        text: "选择电梯",
                                      )
                                    ],
                                  ),
                                ),
                                lineTxt("注册代码",
                                    "${provider.list[index].registrationCode}"),
                                lineTxt(
                                    "电梯型号", "${provider.list[index].liftCode}"),
                                lineTxt(
                                    "电梯类型", "${provider.list[index].liftTypeName}"),
                                lineTxt(
                                    "电梯品牌", "${provider.list[index].liftBrand}")
                              ],
                            ),
                          );
                        },
                      );
                    }))
              ],
            ),
          ),
        ));
  }

  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.searchLift(
        context, widget.projectId, _keyword, _page, false);
  }

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

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