import 'package:flutter/material.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'dart:async';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
// import 'package:liftmanager/internal/wode/model/order_model.dart';
import 'package:liftmanager/internal/bbs/model/liftcase_model.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/presenter/order_master_list_presenter.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/internal/wode/order_const.dart';

class OrderPageMaster extends StatefulWidget {
  OrderPageMaster(this.checkType);
  String checkType;
  int tabCheckIndex = 0;

  @override
  // State<StatefulWidget> createState() {
  //   return OrderPageMasterState();
  // }
  OrderPageMasterState createState() => OrderPageMasterState();
}

class OrderPageMasterState
    extends BasePageState<OrderPageMaster, OrderListPresenterSeconds> {
  BaseListProvider<LiftCasesDetailModel> provider =
      BaseListProvider<LiftCasesDetailModel>();
  int _page = 1;

  ScrollController _scrollController = new ScrollController();

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

  @override
  void initState() {
    if (widget.checkType.isNotEmpty && widget.checkType != null) {
      widget.tabCheckIndex = int.parse(widget.checkType);
    }
    provider.setStateTypeNotNotify(StateType.loading);

    super.initState();

    _onRefresh();
  }

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

    return WillPopScope(
      // ignore: missing_return
      onWillPop:(){
        Navigator.popUntil(context, ModalRoute.withName('/home'));
      },
      child:ChangeNotifierProvider<BaseListProvider<LiftCasesDetailModel>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "专家订单",
          isFun: true,
          fun: () {
            Navigator.popUntil(context, ModalRoute.withName('/home'));
          },
          actions: <Widget>[],
        ),
        body: Container(
          child: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(top: ScreenUtil().setWidth(44)),
                child: Column(
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Consumer<BaseListProvider<LiftCasesDetailModel>>(
                        builder: (_, provider, __) {
                          return MyListView(
                            key: Key('order_list'),
                            itemCount: provider.list.length,
                            stateType: provider.stateType,
                            onRefresh: _onRefresh,
                            loadMore: _loadMore,
                            hasMore: provider.hasMore,
                            itemBuilder: (_, index) {
                              return OrderListItemWidget(
                                data: provider.list[index],
                              );
                            },
                          );
                        },
                      ),
                    )
                  ],
                ),
              ),
              Positioned(
                child: Container(
                  padding: EdgeInsets.only(bottom: 5),
                  // color:Color(0xffFAF7FA),
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(width: 0.5, color: Color(0xffeeeeee)),
                    ),
                    // color: Color(0xff9FD1FE),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children:
                        OrderConstant.expertOrderType.asMap().keys.map((i) {
                      return TabThis(
                          item: OrderConstant.expertOrderType[i]['name'],
                          tabIndex: i,
                          checkIndex: widget.tabCheckIndex,
                          fun: (index) {
                            setState(() {
                              widget.tabCheckIndex = index;
                              provider.list.clear();
                              _onRefresh();
                            });
                          });
                    }).toList(),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    )
    )
    ;
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getOrderList(
        _page, OrderConstant.expertOrderType[widget.tabCheckIndex]['value']);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getOrderList(
        _page, OrderConstant.expertOrderType[widget.tabCheckIndex]['value']);
  }

  @override
  OrderListPresenterSeconds createPresenter() {
    // TODO: implement createPresenter
    // throw UnimplementedError();
    return OrderListPresenterSeconds();
  }
}

class TabThis extends StatelessWidget {
  TabThis({Key key, this.item, this.tabIndex, this.fun, this.checkIndex})
      : super(key: key);
  String item;
  int tabIndex;
  int checkIndex;
  Function fun;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil().setWidth(10)),
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            GestureDetector(
              onTap: () {
                fun(tabIndex);
              },
              child: Container(
                padding: EdgeInsets.only(bottom: 6),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                        width: 2,
                        color: checkIndex == tabIndex
                            ? Color(0xff02A0FD)
                            : Colors.transparent),
                  ),
                  // color: Color(0xff9FD1FE),
                ),
                child: Text(
                  item,
                  style: TextStyle(
                    color: checkIndex == tabIndex
                        ? Color(0xff02A0FD)
                        : Color(0xff666666),
                    fontSize: ScreenUtil().setSp(14),
                  ),
                  textAlign: TextAlign.center,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class OrderListItemWidget extends StatelessWidget {
  OrderListItemWidget({Key key, this.data}) : super(key: key);
  LiftCasesDetailModel data;

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return IntrinsicWidth(
        child: Container(
      padding: EdgeInsets.only(top: ScreenUtil().setWidth(10)),
      width: width,
      // height: 100,
      child: GestureDetector(
        onTap: () {
          NavigatorUtils.push(
              context, "${WodeRouter.orderDetailMaster}?id=${data.id}");
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          // mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                left: ScreenUtil().setWidth(10),
                right: ScreenUtil().setWidth(10),
                top: ScreenUtil().setWidth(5),
              ),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(
                  ScreenUtil().setWidth(22),
                ),
              ),
              child: LoadNetworkImage(
                data.avatarUrl,
                width: ScreenUtil().setWidth(43),
                height: ScreenUtil().setWidth(43),
                // alignment: Alignment.centerLeft,
              ),
            ),
            Expanded(
              child: Container(
                padding: EdgeInsets.only(
                  right: ScreenUtil().setWidth(15),
                ),
                child: Column(
                  children: <Widget>[
                    Container(
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  data.name ?? '',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(17),
                                  ),
                                  textAlign: TextAlign.start,
                                ),
                                Container(
                                  padding: EdgeInsets.only(left: 5),
                                  child: Text(
                                    data.dataTable == 1 ? '问诊服务' : '出诊服务',
                                    style: TextStyle(
                                      color: Color(0xff0288FF),
                                      fontSize: ScreenUtil().setSp(14),
                                    ),
                                    textAlign: TextAlign.start,
                                  ),
                                )
                              ],
                            ),
                            Container(
                              child: Text(
                                OrderConstant.getStatusName(
                                    data.statuz, data.dataTable,
                                    arrivedFlag: data.arrivedFlag,
                                    userType: 'EXPERT'),
                                style: TextStyle(
                                  color: [].indexOf(data.statuz) == -1
                                      ? Color(0xff0288FF)
                                      : Color(0xffF84203),
                                  fontSize: ScreenUtil().setSp(14),
                                ),
                                textAlign: TextAlign.end,
                              ),
                            )
                          ]),
                    ),
                    Container(
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  data.createTime != null
                                      ? DateUtils.instance.getFormartData(
                                          timeSamp: data.createTime,
                                          format: "MM-dd")
                                      : '',
                                  style: TextStyle(
                                    color: Color(0xff999999),
                                    fontSize: ScreenUtil().setSp(13),
                                  ),
                                  textAlign: TextAlign.start,
                                ),
                                Container(
                                  padding: EdgeInsets.only(left: 5),
                                  child: Text(
                                    data.brandName ?? '',
                                    style: TextStyle(
                                      color: Color(0xff999999),
                                      fontSize: ScreenUtil().setSp(13),
                                    ),
                                    textAlign: TextAlign.start,
                                  ),
                                )
                              ],
                            ),
                          ]),
                    ),
                    data.dataTable == 1
                        ? Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '问题描述：',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      width: 200,
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        data.expression ?? '',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          )
                        : Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '出诊时间：',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        data.arrivedTime != null
                                            ? DateUtils.instance.getFormartData(
                                                timeSamp: data.arrivedTime,
                                                format: "MM-dd HH:mm")
                                            : '',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ),
                    data.dataTable == 1
                        ? Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '已尝试方法：',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      width: 200,
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        data.usedMethods ?? '',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          )
                        : Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '出诊地点：',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      width: 200,
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        data.address ?? '',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ),
                    data.dataTable == 2
                        ? Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      '问题描述：',
                                      style: TextStyle(
                                        color: Color(0xff666666),
                                        fontSize: ScreenUtil().setSp(14),
                                      ),
                                      textAlign: TextAlign.start,
                                    ),
                                    Container(
                                      width: 200,
                                      padding: EdgeInsets.only(left: 5),
                                      child: Text(
                                        data.expression ?? '',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: ScreenUtil().setSp(14),
                                        ),
                                        textAlign: TextAlign.start,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          )
                        : Container(),
                    Container(
                      margin: EdgeInsets.only(top: 6),
                      child: Row(
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.only(right: 5),
                            child: Text(
                              data.totalCost != null
                                  ? "￥${data.totalCost.toString()}"
                                  : '',
                              style: TextStyle(
                                color: Color(0xffFD0808),
                                fontSize: ScreenUtil().setSp(15),
                              ),
                              textAlign: TextAlign.start,
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ));
  }
}
