import 'package:flutter/material.dart';
import 'package:liftmanager/internal/heavy/model/heavy_count_item.dart';
import 'package:liftmanager/internal/heavy/provider/heavy_page.dart';
import 'package:liftmanager/internal/heavy/widgets/HeavyList.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:provider/provider.dart' as p;

class HeavyListPage extends StatefulWidget {
  HeavyListPage({this.topInto = false});

  final bool topInto;
  @override
  HeavyListPageState createState() => HeavyListPageState();
}

class HeavyListPageState extends State<HeavyListPage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  HeavyPageProvider provider = HeavyPageProvider();
//  List<Map> tabs = [{"id":0,"name":"未开始"},{"id":1,"name":"施工中"},{"id":2,"name":"待审核"},{"id":3,"name":"已完成"}];
  TabController _tabController;
  PageController _pageController = PageController(initialPage: 0);
  HeavyCountItem countItem = HeavyCountItem();

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 4);
//    if(widget.topInto){
//      _tabController.animateTo(1);
//      _pageController = PageController(initialPage: 1);
//    }else{
      _pageController = PageController(initialPage: 0);
//    }
    getCount();
  }

  getCount() {
    ApiService(context: context).heavyCount(onSuccess: (res) {
      if (res != null) {
        countItem = res;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  _onPageChange(int index) {
    _tabController.animateTo(index);
    provider.setIndex(index);
  }

  @override
  Widget build(BuildContext context) {
    return p.ChangeNotifierProvider<HeavyPageProvider>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "大修管理",
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              // 隐藏点击效果
              color:ThemeUtils.getTabsBg(context),
              child: TabBar(
                  onTap: (index) {
                    if (!mounted) {
                      return;
                    }
                    _pageController.jumpToPage(index);
                  },
                  isScrollable: false,
                  controller: _tabController,
                  labelStyle: TextStyles.textBold18,
                  indicatorSize: TabBarIndicatorSize.label,
                labelPadding: const EdgeInsets.all(0),
                  unselectedLabelColor: Colours.text_gray,
                  labelColor: Theme.of(context).primaryColor,
                  indicatorPadding:
                  const EdgeInsets.only(left: 5.0, right: 5.0),
                  tabs: <Widget>[
                    _TabView("未开始", "(${countItem.noStart})", 0),
                    _TabView("施工中", "(${countItem.abuilding})", 1),
                    _TabView("待审核", "(${countItem.checkPending})", 2),
                    _TabView("已完成", "(${countItem.complete})", 3),
                  ],
              ),
            ),
            Gaps.line,
            Expanded(
              child: PageView.builder(
                key: const Key('pageView'),
                itemCount: 4,
                onPageChanged: _onPageChange,
                controller: _pageController,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                      color: ThemeUtils.getBackgroundColor(context),
                      child: HeavyList(index: index+1));
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class _TabView extends StatelessWidget {
  const _TabView(this.tabName, this.tabSub, this.index);

  final String tabName;
  final String tabSub;
  final int index;

  @override
  Widget build(BuildContext context) {
    return p.Consumer<HeavyPageProvider>(
      builder: (_, provider, child) {
        return Tab(
            child: SizedBox(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    tabName,
                    style: TextStyle(fontSize: 15),
                  ),
                 Padding(
                        padding: const EdgeInsets.only(top: 1.0),
                        child: Text(tabSub,
                            style: TextStyle(fontSize: Dimens.font_sp12)),
                      ),
                ],
              ),
            ));
      },
    );
  }
}

//class HeavyListPage extends StatefulWidget {
//  @override
//  HeavyListPageState createState() => HeavyListPageState();
//}
//
//class HeavyListPageState
//    extends BasePageState<HeavyListPage, HeavyListProvider>
//    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
//  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, HeavyRouter.heavyLiftListPage + "?id=${id}");
//  }
//
//  ///项目详情
//  void _projectDetail(id) {
//    NavigatorUtils.push(context, "${HeavyRouter.heavyDetailPage}?id=${id}");
//  }
//
//  @override
//  Widget build(BuildContext context) {
//    return ChangeNotifierProvider<BaseListProvider<ProjectListItem>>(
//        create: (_) => provider,
//        child: Scaffold(
//          appBar: MyAppBar(
//            centerTitle: "大修模块",
////            actions: <Widget>[
////              FlatButton(
////                child: Text("创建", key: const Key('actionName')),
////                textColor: Colours.dark_text,
////                highlightColor: Colors.transparent,
////                onPressed: () {
////                  NavigatorUtils.push(context, HeavyRouter.heavyCreatePage);
////                },
////              )
////            ],
//          ),
//          body: Container(
//            color: ThemeUtils.getBackgroundColor(context),
//            child: Column(
//              crossAxisAlignment: CrossAxisAlignment.start,
//              children: <Widget>[
//                Container(
//                  color: ThemeUtils.getBackgroundColor(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: 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: 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: TextStyles.text15,
//                                                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(
//                                      "项目用途",
//                                      "${Constant.projectUsageText[int.parse(provider.list[index].projectUsage)]}",
//                                    ),
//                                    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
//  HeavyListProvider createPresenter() {
//    return HeavyListProvider();
//  }
//
//  @override
//  bool get wantKeepAlive => true;
//}
