import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/wode/model/piao_tou_model.dart';
import 'package:liftmanager/internal/wode/presenter/piao_tou_list_presenter.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/fast_notification.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

class Piaotou extends StatefulWidget {
  Piaotou(this.checkTou, this.type);

  final String checkTou;
  final String type;
  @override
  PiaotouState createState() => PiaotouState();
}

class PiaotouState extends BasePageState<Piaotou, PiaotouListPresenter> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print("tou6666");

    FastNotification.addListener("initThis", (initThisString) {
      setState(() {});
      _onRefresh();
    });
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return ChangeNotifierProvider<BaseListProvider<Records>>(
        create: (_) => provider,
        child: Scaffold(
          // resizeToAvoidBottomPadding: true,
          appBar: MyAppBar(
            centerTitle: "发票抬头",
          ),
          body: Container(
            color: Color(0xFFF6F6F6),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                widget.checkTou != "checkTou"
                    ? GestureDetector(
                        onTap: () {
                          NavigatorUtils.push(context,
                              "${WodeRouter.piaotouEdit}?type=add&id=");
                        },
                        child: Container(
                          color: ThemeUtils.getDialogTextFieldColor(context),
                          width: width,
                          padding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(15),
                              right: ScreenUtil().setWidth(15),
                              top: ScreenUtil().setWidth(10),
                              bottom: ScreenUtil().setWidth(10)),
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Text(
                                  "添加发票抬头",
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(15),
                                  ),
                                ),
                                Icon(
                                  Icons.keyboard_arrow_right,
                                  color: Color(0xffcccccc),
                                ),
                              ]),
                        ),
                      )
                    : Container(
                        child: null,
                      ),
                Expanded(
                    flex: 1,
                    child: Consumer<BaseListProvider<Records>>(
                        builder: (_, provider, __) {
                      return MyListView(
                        key: Key('piao_tou_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        loadMore: _loadMore,
                        pageSize: 10,
                        hasMore: provider.hasMore,
                        itemBuilder: (_, index) {
                          return Container(
                              // height: 20,
                              padding: EdgeInsets.only(
                                  left: 10, bottom: 15, top: 15, right: 10),
                              margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5.0)),
                              ),
                              child: GestureDetector(
                                behavior: HitTestBehavior.opaque,
                                onTap: () {
                                  print(456);
                                  if (widget.checkTou == "checkTou") {
                                    Navigator.pop(context);
                                    dynamic piaoTouObj = provider.list[index];
                                    FastNotification.push(
                                        "piaoTou", piaoTouObj);
                                  } else {
                                    NavigatorUtils.push(context,
                                        "${WodeRouter.piaotouEdit}?type=edit&id=${provider.list[index].id.toString()}");
                                  }
                                },
                                child: Column(
                                  children: [
                                    Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: <Widget>[
                                          Text(
                                            provider.list[index].name ?? "",
                                            style: TextStyle(
                                                color: Color(0xff333333),
                                                fontSize:
                                                    ScreenUtil().setSp(15)),
                                            textAlign: TextAlign.start,
                                          ),
                                          provider.list[index].isDefault == 1
                                              ? Container(
                                                  margin:
                                                      EdgeInsets.only(left: 10),
                                                  padding: EdgeInsets.only(
                                                      left: 5, right: 5),
                                                  decoration: BoxDecoration(
                                                    color:
                                                        Colours.blue_app_main,
                                                    borderRadius:
                                                        BorderRadius.all(
                                                            Radius.circular(
                                                                5.0)),
                                                  ),
                                                  child: Text(
                                                    "默认",
                                                    style: TextStyle(
                                                        color: Colors.white,
                                                        fontSize: ScreenUtil()
                                                            .setSp(13)),
                                                    textAlign: TextAlign.start,
                                                  ),
                                                )
                                              : Container(child: null)
                                        ]),
                                    Row(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: <Widget>[
                                          Text(
                                            "发票类型:",
                                            style: TextStyle(
                                                color: Color(0xff999999),
                                                fontSize:
                                                    ScreenUtil().setSp(14)),
                                            textAlign: TextAlign.start,
                                          ),
                                          Container(
                                            padding: EdgeInsets.only(left: 5),
                                            child: Text(
                                              provider.list[index].type == 1
                                                  ? "个人"
                                                  : "公司",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize:
                                                      ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                          )
                                        ]),
                                    provider.list[index].type == 2
                                        ? Row(
                                            // crossAxisAlignment:
                                            //     CrossAxisAlignment.center,
                                            // mainAxisAlignment:
                                            //     MainAxisAlignment.center,
                                            children: <Widget>[
                                                Text(
                                                  "税号:",
                                                  style: TextStyle(
                                                      color: Color(0xff999999),
                                                      fontSize: ScreenUtil()
                                                          .setSp(14)),
                                                  textAlign: TextAlign.start,
                                                ),
                                                Container(
                                                  padding:
                                                      EdgeInsets.only(left: 5),
                                                  child: Text(
                                                    provider.list[index]
                                                            .enterpriseNumber ??
                                                        "",
                                                    style: TextStyle(
                                                        color:
                                                            Color(0xff999999),
                                                        fontSize: ScreenUtil()
                                                            .setSp(14)),
                                                    textAlign: TextAlign.start,
                                                  ),
                                                )
                                              ])
                                        : Container(),
                                    Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.end,
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        InkWell(
                                            onTap: () {
                                              showAlert(
                                                  context,
                                                  "提示",
                                                  "确定要删除吗？",
                                                  "确定",
                                                  () {
                                                    Navigator.pop(context);
                                                    showLoading(
                                                        context, "正在删除...");
                                                    getTouDelete(provider
                                                        .list[index].id
                                                        .toString());
                                                  },
                                                  txt2: "取消",
                                                  onPre2: () {
                                                    NavigatorUtils.goBack(
                                                        context);
                                                  });
                                            },
                                            child: Container(
                                              padding: EdgeInsets.fromLTRB(
                                                  8, 1, 8, 1),
                                              decoration: BoxDecoration(
                                                border: new Border.all(
                                                    width: 1,
                                                    color: Colors.black),
                                                color: Colors.white,
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(18.0)),
                                              ),
                                              child: Text('删除'),
                                            )),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        InkWell(
                                          onTap: () {
                                            NavigatorUtils.push(context,
                                                "${WodeRouter.piaotouEdit}?type=edit&id=${provider.list[index].id.toString()}");
                                          },
                                          child: Container(
                                            padding:
                                                EdgeInsets.fromLTRB(8, 1, 8, 1),
                                            decoration: BoxDecoration(
                                              border: new Border.all(
                                                  width: 1,
                                                  color: Colors.black),
                                              color: Colors.white,
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(18.0)),
                                            ),
                                            child: Text('编辑'),
                                          ),
                                        )
                                      ],
                                    )
                                  ],
                                ),
                              )

                              // Row(
                              //   crossAxisAlignment: CrossAxisAlignment.start,
                              //   children: <Widget>[
                              //     Expanded(
                              //         child: GestureDetector(
                              //       behavior: HitTestBehavior.opaque,
                              //       onTap: () {
                              //         print(456);
                              //         if (widget.checkTou == "checkTou") {
                              //           Navigator.pop(context);
                              //           dynamic piaoTouObj = provider.list[index];
                              //           FastNotification.push(
                              //               "piaoTou", piaoTouObj);
                              //         } else {
                              //           NavigatorUtils.push(context,
                              //               "${WodeRouter.piaotouEdit}?type=edit&id=${provider.list[index].id.toString()}");
                              //         }
                              //       },
                              //       child: Row(
                              //           crossAxisAlignment:
                              //               CrossAxisAlignment.start,
                              //           children: <Widget>[
                              //             Expanded(
                              //                 child: Container(
                              //                     padding: EdgeInsets.only(
                              //                         right: 15, left: 15),
                              //                     child: Column(
                              //                       children: <Widget>[
                              //                         Container(
                              //                             child: Row(
                              //                                 mainAxisAlignment:
                              //                                     MainAxisAlignment
                              //                                         .spaceBetween,
                              //                                 children: <Widget>[
                              //                               Row(
                              //                                   crossAxisAlignment:
                              //                                       CrossAxisAlignment
                              //                                           .center,
                              //                                   children: <
                              //                                       Widget>[
                              //                                     Text(
                              //                                       provider
                              //                                               .list[
                              //                                                   index]
                              //                                               .name ??
                              //                                           "",
                              //                                       style: TextStyle(
                              //                                           color: Color(
                              //                                               0xff333333),
                              //                                           fontSize:
                              //                                               ScreenUtil()
                              //                                                   .setSp(15)),
                              //                                       textAlign:
                              //                                           TextAlign
                              //                                               .start,
                              //                                     ),
                              //                                     provider.list[index].isDefault ==
                              //                                             1
                              //                                         ? Container(
                              //                                             padding:
                              //                                                 EdgeInsets.only(left: 5),
                              //                                             child:
                              //                                                 Text(
                              //                                               "(默认)",
                              //                                               style: TextStyle(
                              //                                                   color: Color(0xffff0000),
                              //                                                   fontSize: ScreenUtil().setSp(15)),
                              //                                               textAlign:
                              //                                                   TextAlign.start,
                              //                                             ),
                              //                                           )
                              //                                         : Container(
                              //                                             child:
                              //                                                 null)
                              //                                   ]),

                              //                               widget.checkTou !=
                              //                                       "checkTou"
                              //                                   ? Icon(
                              //                                       const IconData(
                              //                                           0xe626,
                              //                                           fontFamily:
                              //                                               "myfont"),
                              //                                       size: 18.0,
                              //                                       color: Color(
                              //                                           0xff666666),
                              //                                     )
                              //                                   : Container(
                              //                                       child: null,
                              //                                     ),
                              //                               // Text("123")
                              //                             ])),
                              //                         Container(
                              //                             child: Row(
                              //                                 mainAxisAlignment:
                              //                                     MainAxisAlignment
                              //                                         .spaceBetween,
                              //                                 children: <Widget>[
                              //                               Row(
                              //                                   crossAxisAlignment:
                              //                                       CrossAxisAlignment
                              //                                           .center,
                              //                                   children: <
                              //                                       Widget>[
                              //                                     Text(
                              //                                       "发票类型:",
                              //                                       style: TextStyle(
                              //                                           color: Color(
                              //                                               0xff999999),
                              //                                           fontSize:
                              //                                               ScreenUtil()
                              //                                                   .setSp(14)),
                              //                                       textAlign:
                              //                                           TextAlign
                              //                                               .start,
                              //                                     ),
                              //                                     Container(
                              //                                       padding: EdgeInsets
                              //                                           .only(
                              //                                               left:
                              //                                                   5),
                              //                                       child: Text(
                              //                                         provider.list[index].type ==
                              //                                                 1
                              //                                             ? "个人"
                              //                                             : "公司",
                              //                                         style: TextStyle(
                              //                                             color: Color(
                              //                                                 0xff999999),
                              //                                             fontSize:
                              //                                                 ScreenUtil().setSp(14)),
                              //                                         textAlign:
                              //                                             TextAlign
                              //                                                 .start,
                              //                                       ),
                              //                                     )
                              //                                   ]),

                              //                             ])),
                              //                         provider.list[index].type ==
                              //                                 2
                              //                             ? Container(
                              //                                 child: Row(
                              //                                     mainAxisAlignment:
                              //                                         MainAxisAlignment
                              //                                             .spaceBetween,
                              //                                     children: <
                              //                                         Widget>[
                              //                                     Row(
                              //                                         crossAxisAlignment:
                              //                                             CrossAxisAlignment
                              //                                                 .center,
                              //                                         mainAxisAlignment:
                              //                                             MainAxisAlignment
                              //                                                 .center,
                              //                                         children: <
                              //                                             Widget>[
                              //                                           Text(
                              //                                             "税号:",
                              //                                             style: TextStyle(
                              //                                                 color:
                              //                                                     Color(0xff999999),
                              //                                                 fontSize: ScreenUtil().setSp(14)),
                              //                                             textAlign:
                              //                                                 TextAlign.start,
                              //                                           ),
                              //                                           Container(
                              //                                             padding:
                              //                                                 EdgeInsets.only(left: 5),
                              //                                             child:
                              //                                                 Text(
                              //                                               provider.list[index].enterpriseNumber ??
                              //                                                   "",
                              //                                               style: TextStyle(
                              //                                                   color: Color(0xff999999),
                              //                                                   fontSize: ScreenUtil().setSp(14)),
                              //                                               textAlign:
                              //                                                   TextAlign.start,
                              //                                             ),
                              //                                           )
                              //                                         ]),

                              //                                   ]))
                              //                             : Container(
                              //                                 child: null),
                              //                       ],
                              //                     )))
                              //           ]),
                              //     )),
                              //   ],
                              // ),

                              );
                        },
                      );
                    }))
              ],
            ),
          ),
        ));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getList(
        _page,
        widget.type != null && widget.type != ""
            ? int.parse(widget.type)
            : null);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getList(
        _page,
        widget.type != null && widget.type != ""
            ? int.parse(widget.type)
            : null);
  }

  Future getTouDelete(id) async {
    await NewApiService().getTouDelete(int.parse(id), onSuccess: (res) {
      toasts("删除成功");

      String initThisString = randomInt(1111, 9999).toString() +
          DateTime.now().millisecondsSinceEpoch.toString();
      FastNotification.push("initThis", initThisString);
      Navigator.of(context).pop();
      // setState(() {});
      // _onRefresh();
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

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