import 'package:flutter/material.dart';
import 'package:fake_push/fake_push.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/internal/account/provider/user_provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/common/user_db.dart';
import 'package:liftmanager/internal/bbs/provide/websocket.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'dart:convert';
import 'package:liftmanager/internal/bbs/model/expert_model.dart';
import 'package:liftmanager/internal/wode/model/share_model.dart';
import 'package:fluwx/fluwx.dart' as fluwx;

class Qrshare extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return QrshareState();
  }
}

class QrshareState extends State<Qrshare> with AutomaticKeepAliveClientMixin {
  UserProvider provider = UserProvider();
  Push _push = Push();

  @override
  void initState() {
    // getMemberAllMenu();
    super.initState();
    getExpertDetail();
    getPayMoneyList();
  }

  bool _hasData = false;
  double total = 0.0;

  Records detailObj;
  Future getExpertDetail() async {
    await NewApiService().getExpertDetail(
        flustars.SpUtil.getString(Constant.userId), onSuccess: (res) {
      if (res != null) {
        
        detailObj = res;
        if(detailObj.requestCode == null || detailObj.qrCodeUrl == null){
          getQrCode();
        }else {
          _hasData = true;
        }
        print(123456);
        print(jsonEncode(res));
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  Future getQrCode() async {
    await NewApiService().getQrCode(
        onSuccess: (res) {
        getExpertDetail();
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  List<ShareModel> recordlist;
  Future getPayMoneyList() async {
    await NewApiService().getPayMoneyList(onSuccess: (res) {
      if (res != null) {
        recordlist = res;
        recordlist.forEach((element) {
          total += element.rebate;
        });
        print(123456);
        print(jsonEncode(res));
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    // provider.dispose();
    super.dispose();
  }

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

    return Container(
        child:
            // ListView(
            //   children: <Widget>[
            Container(
      // width: width,
      // height:ScreenUtil().setWidth(1619),
      // decoration: BoxDecoration(
      //     image: DecorationImage(
      //   image: AssetImage("assets/images/bg.png"),
      //   fit: BoxFit.fitHeight,
      // )),
      child: Scaffold(
        // backgroundColor: Colors.transparent,
        appBar: MyAppBar(
          centerTitle: "邀请朋友",
          // backgroundColor:Colors.transparent,
        ),
        body:
            // Container(child:null)
            _hasData
                ? ListView(
                    padding: EdgeInsets.all(0.0),
                    children: <Widget>[
                      Container(
                        width: width,
                        height: ScreenUtil().setWidth(1619),
                        decoration: BoxDecoration(
                            image: DecorationImage(
                          image: AssetImage("assets/images/bg.png"),
                          fit: BoxFit.cover,
                        )),
                        child: Column(children: <Widget>[
                          Container(
                              width: width,
                              padding: EdgeInsets.only(
                                  top: ScreenUtil().setWidth(230)),
                              child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    ClipRRect(
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(12)),
                                      child: Container(
                                        width: ScreenUtil().setWidth(200),
                                        height: ScreenUtil().setWidth(200),
                                        color: Colors.white,
                                        child: Column(
                                          // mainAxisAlignment: MainAxisAlignment.center,
                                          children: <Widget>[
                                            SizedBox(height: 10),
                                            Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment.center,
                                              children: <Widget>[
                                                Text(
                                                  "邀请码:",
                                                  style: TextStyle(
                                                      color: Color(0xff222222),
                                                      fontSize: ScreenUtil()
                                                          .setSp(14)),
                                                  textAlign: TextAlign.start,
                                                ),
                                                SizedBox(width: 5),
                                                Text(
                                                  detailObj.requestCode != null
                                                      ? detailObj.requestCode
                                                          .toString()
                                                      : "",
                                                  style: TextStyle(
                                                      color: Color(0xffff0000),
                                                      fontSize: ScreenUtil()
                                                          .setSp(18)),
                                                  textAlign: TextAlign.start,
                                                ),
                                              ],
                                            ),
                                            SizedBox(height: 10),
                                            Container(
                                              child: GestureDetector(
                                                onLongPress: (){
                                                  print("object-------------------");
                                                  fluwx.shareToWeChat(fluwx.WeChatShareImageModel(
                                                    scene: fluwx.WeChatScene.SESSION,
                                                    title:"请扫码",
                                                    image: detailObj.qrCodeUrl??"assets/images/no_image.png",
                                                  )).then((result){

                                                  },
                                                  onError: (msg){
                                                    // print(msg);
                                                  });
                                                },
                                                child: LoadNetworkImage(
                                                  detailObj.qrCodeUrl,
                                                  width: ScreenUtil().setWidth(110),
                                                  height:
                                                    ScreenUtil().setWidth(110),
                                                  // alignment: Alignment.centerLeft,
                                                ),
                                              )
                                            ),
                                            SizedBox(height: 10),
                                            Text(
                                              "长按二维码可分享",
                                              style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize:
                                                      ScreenUtil().setSp(14)),
                                              textAlign: TextAlign.start,
                                            ),
                                          ],
                                        ),
                                      ),
                                    )
                                  ])),
                          // Container(
                          //   padding: EdgeInsets.only(top:ScreenUtil().setWidth(30)),
                          //   child:Row(
                          //     mainAxisAlignment: MainAxisAlignment.center,
                          //     children: <Widget>[
                          //       Container(
                          //         child:LoadAssetImage(
                          //                   "wode/btn",
                          //                   width: ScreenUtil().setWidth(136),
                          //                   height: ScreenUtil().setWidth(36),
                          //                   // alignment: Alignment.centerLeft,
                          //                 ),
                          //       )
                          //     ],
                          //   )
                          // ),
                          Container(
                              padding: EdgeInsets.only(
                                  top: ScreenUtil().setWidth(30)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Container(
                                    child: LoadAssetImage(
                                      "wode/btn_2",
                                      width: ScreenUtil().setWidth(345),
                                      height: ScreenUtil().setWidth(246),
                                      // alignment: Alignment.centerLeft,
                                    ),
                                  )
                                ],
                              )),
                          Container(
                              padding: EdgeInsets.only(
                                  top: ScreenUtil().setWidth(30)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Container(
                                    child: LoadAssetImage(
                                      "wode/btn_3",
                                      width: ScreenUtil().setWidth(345),
                                      height: ScreenUtil().setWidth(251),
                                      // alignment: Alignment.centerLeft,
                                    ),
                                  )
                                ],
                              )),
                          Container(
                              padding: EdgeInsets.only(
                                  top: ScreenUtil().setWidth(30)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Container(
                                      width: ScreenUtil().setWidth(370),
                                      height: ScreenUtil().setWidth(522),
                                      decoration: BoxDecoration(
                                          image: DecorationImage(
                                        image: AssetImage(
                                            "assets/images/wode/btn_4.png"),
                                        fit: BoxFit.cover,
                                      )),
                                      child: Column(
                                        children: <Widget>[
                                          Container(
                                              padding: EdgeInsets.only(
                                                  left:
                                                      ScreenUtil().setWidth(40),
                                                  right:
                                                      ScreenUtil().setWidth(40),
                                                  top: ScreenUtil()
                                                      .setWidth(58)),
                                              child: Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: <Widget>[
                                                  Container(
                                                    child: Text(
                                                      "好友账号",
                                                      style: TextStyle(
                                                          color:
                                                              Color(0xffff0000),
                                                          fontSize: ScreenUtil()
                                                              .setSp(17)),
                                                      textAlign:
                                                          TextAlign.start,
                                                    ),
                                                  ),
                                                  Container(
                                                    child: Text(
                                                      "获得奖励($total)",
                                                      style: TextStyle(
                                                          color:
                                                              Color(0xffff0000),
                                                          fontSize: ScreenUtil()
                                                              .setSp(17)),
                                                      textAlign:
                                                          TextAlign.start,
                                                    ),
                                                  ),
                                                ],
                                              )),
                                          SizedBox(
                                              height:
                                                  ScreenUtil().setWidth(20)),
                                          Container(
                                              height:
                                                  ScreenUtil().setWidth(360),
                                              // color: Colors.red,
                                              child: ListView(
                                                  children:
                                                      recordlist != null &&
                                                              recordlist
                                                                      .length >
                                                                  0
                                                          ? recordlist
                                                              .map((item) {
                                                              return Container(
                                                                  padding: EdgeInsets.only(
                                                                      left: ScreenUtil()
                                                                          .setWidth(
                                                                              40),
                                                                      right: ScreenUtil()
                                                                          .setWidth(
                                                                              40),
                                                                      top: ScreenUtil()
                                                                          .setWidth(
                                                                              10),
                                                                      bottom: ScreenUtil()
                                                                          .setWidth(
                                                                              10)),
                                                                  child: Row(
                                                                    mainAxisAlignment:
                                                                        MainAxisAlignment
                                                                            .spaceBetween,
                                                                    children: <
                                                                        Widget>[
                                                                      Container(
                                                                        child:
                                                                            Text(
                                                                          item.mobile ??
                                                                              "",
                                                                          style: TextStyle(
                                                                              color: Color(0xFF00D9FF),
                                                                              fontSize: ScreenUtil().setSp(14)),
                                                                          textAlign:
                                                                              TextAlign.start,
                                                                        ),
                                                                      ),
                                                                      Container(
                                                                        child:
                                                                            Text(
                                                                          "获得${item.rebate.toString()}元",
                                                                          style: TextStyle(
                                                                              color: Color(0xFF00D9FF),
                                                                              fontSize: ScreenUtil().setSp(14)),
                                                                          textAlign:
                                                                              TextAlign.start,
                                                                        ),
                                                                      ),
                                                                    ],
                                                                  ));
                                                            }).toList()
                                                          : <Widget>[]))
                                        ],
                                      ))
                                ],
                              )),
                        ]),
                      ),
                    ],
                  )
                : Center(
                    child: Text("正在加载..."),
                  ),
      ),
    )
        // ],
        // )
        );
  }

  @override
  bool get wantKeepAlive => true;
}
