import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/widgets/bbs_content.dart';

import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/internal/bbs/presenter/product_list_presenter.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/utils/url.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ProductList extends StatefulWidget {
  // QuestionList(this.index);

  // final String index;

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

class ProductListState
    extends BasePageState<ProductList, ProductListPresenter> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();
  int _page = 1;
  String searchWord;
  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print(66666);
  }

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    return ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
            appBar: SearchAppBar2(
              onPressed: (text) {
                searchWord = text;
                _onRefresh();
              },
            ),
            body: Container(
                child: Column(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('product_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        pageSize: 10,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return InkWell(
                            child: Container(
                              padding: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(15),
                                  right: ScreenUtil().setWidth(15),
                                  top: ScreenUtil().setHeight(10),
                                  bottom: ScreenUtil().setHeight(10)),
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 0.5, color: Colours.line),
                                ),
                              ),
                              child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    ClipRRect(
                                      borderRadius: BorderRadius.circular(5),
                                      child: Container(
                                        padding: EdgeInsets.only(
                                            right: ScreenUtil().setWidth(10)),
                                        child: LoadNetworkImage(
                                              provider.list[index].imgs.split(",")[0],
                                          // fit: BoxFit.cover,
                                          width: ScreenUtil().setWidth(90),
                                          height: ScreenUtil().setWidth(90),
                                          isWater: true,
                                        ),
                                      ),
                                    ),
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Container(
                                          width: width*0.62,
                                          child: Text(
                                            provider.list[index].name??"",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(15),
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                        Container(
                                          width: width*0.62,
                                          child: Text(
                                          provider.list[index].brandName??""
                                              .toString(),
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff666666),
                                          ),
                                          maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                        ),
                                        ),
                                        Container(
                                          width: width*0.62,
                                          child: Text(
                                          "供应商：${provider.list[index].manufacturer??""}",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(14),
                                            color: Color(0xff666666),
                                          ),
                                          maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                        ),
                                        ),
                                        Container(
                                          width: width*0.62,
                                          child: Text(
                                            "Tel：${provider.list[index].telephone??""}",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff666666),
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  ]),
                            ),
                            onTap: () {
                              print(index);
                              NavigatorUtils.push(context,
                                  "${BbsRouter.productDetail}?id=${provider.list[index].id.toString()}");
                            },
                          );
                        },
                      );
                    }))
              ],
            ))));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getShopList(_page,searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getShopList(_page,searchWord);
  }

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