// import 'package:flutter/cupertino.dart';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/log_util.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class PiaoDetail extends StatefulWidget {
  PiaoDetail(this.id);

  final String id;
  @override
  State<StatefulWidget> createState() {
    return PiaoDetailState();
  }
}

class PiaoDetailState extends State<PiaoDetail>
    with AutomaticKeepAliveClientMixin {
  bool _hasData = false;
  var detailObj;

  @override
  void initState() {
    super.initState();
    getPiaoDetail();
    print(widget.id);
    print(123789);
  }

  Future getPiaoDetail() async {
    await NewApiService().getPiaoDetail(int.parse(widget.id), onSuccess: (res) {
      if (res != null) {
        _hasData = true;
        detailObj = res;
        // LogUtil.d(JsonEncoder(detailObj));
        LogUtil.d(JsonEncoder().convert(detailObj));
        print(9999);
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "发票详情",
        ),
        body: _hasData
            ? Stack(children: <Widget>[
                ListView(padding: EdgeInsets.all(0.0), children: <Widget>[
                  Kan(
                      titleType: detailObj.invoiceType == '1' ? '电子发票' : '专用发票',
                      statusType: detailObj.checkFlag == 1 ? '已开票' : '未开票',
                      time:
                          "${detailObj.createTime != null ? DateUtils.instance.getFormartData(timeSamp: detailObj.createTime, format: "yyyy-MM-dd HH:mm") : ''}",
                      fun: () {
                        // NavigatorUtils.push(context, WodeRouter.piaoCheck);
                      }),

                  // Container(
                  //   padding: EdgeInsets.only(
                  //       left: ScreenUtil().setWidth(15),
                  //       right: ScreenUtil().setWidth(15),
                  //       top: ScreenUtil().setWidth(10),
                  //       bottom: ScreenUtil().setWidth(10)),
                  //   color: Color(0xfff5f5f5),
                  //   child: Text(
                  //     '接收方式',
                  //     textAlign: TextAlign.left,
                  //     style: TextStyle(
                  //       fontSize: ScreenUtil().setSp(14),
                  //       color: Color(0xff333333),
                  //     ),
                  //   ),
                  // ),
                  Container(
                    color: Color(0xffFAF7FA),
                    height: ScreenUtil().setWidth(5),
                  ),
                  detailObj.invoiceType == "1"
                      ? Heng(
                          left: "电子邮箱",
                          right: "${detailObj.email ?? ""}",
                          leftColor: 0xff333333,
                          rightColor: 0xff333333,
                        )
                      : Column(
                          children: <Widget>[
                            Heng(
                              left: "收票人",
                              right: "${detailObj.receiveTicket ?? ""}",
                              leftColor: 0xff333333,
                              rightColor: 0xff333333,
                            ),
                            Heng(
                              left: "收票人电话",
                              right: "${detailObj.receiveMebile ?? ""}",
                              leftColor: 0xff333333,
                              rightColor: 0xff333333,
                            ),
                            Heng(
                              left: "收票人地址",
                              right: "${detailObj.receiveAddress ?? ""}",
                              leftColor: 0xff333333,
                              rightColor: 0xff333333,
                            ),
                          ],
                        ),
                  // Container(
                  //   padding: EdgeInsets.only(
                  //       left: ScreenUtil().setWidth(15),
                  //       right: ScreenUtil().setWidth(15),
                  //       top: ScreenUtil().setWidth(10),
                  //       bottom: ScreenUtil().setWidth(10)),
                  //   color: Color(0xfff5f5f5),
                  //   child: Text(
                  //     '发票详情',
                  //     textAlign: TextAlign.left,
                  //     style: TextStyle(
                  //       fontSize: ScreenUtil().setSp(14),
                  //       color: Color(0xff333333),
                  //     ),
                  //   ),
                  // ),
                  Container(
                    color: Color(0xffFAF7FA),
                    height: ScreenUtil().setWidth(5),
                  ),
                  Heng(
                    left: "发票类型",
                    right: "${detailObj.type == 1 ? "个人" : "企业"}",
                    leftColor: 0xff333333,
                    rightColor: 0xff333333,
                  ),
                  Heng(
                    left: "发票抬头",
                    right: "${detailObj.userInvoice?.name ?? "咨询服务费"}",
                    leftColor: 0xff333333,
                    rightColor: 0xff333333,
                  ),
                  detailObj.type == 2
                      ? Heng(
                          left: "企业税号",
                          right:
                              "${detailObj.userInvoice?.enterpriseNumber ?? ""}",
                          leftColor: 0xff333333,
                          rightColor: 0xff333333,
                        )
                      : Container(child: null),
                  Heng(
                    left: "发票内容",
                    right: "${detailObj.userInvoice?.gmfMc ?? "咨询服务费"}",
                    leftColor: 0xff333333,
                    rightColor: 0xff333333,
                  ),
                  Heng(
                    left: "发票金额",
                    right: "${detailObj.hjje.toString() ?? ""}",
                    leftColor: 0xff333333,
                    rightColor: 0xff333333,
                  ),
                  Heng(
                    left: "申请时间",
                    right:
                        "${detailObj.createTime != null ? DateUtils.instance.getFormartData(timeSamp: detailObj.createTime, format: "yyyy-MM-dd HH:mm") : ''}",
                    leftColor: 0xff333333,
                    rightColor: 0xff333333,
                  ),
                  detailObj.checkFlag == 0
                      ? Heng(
                          left: "驳回原因",
                          right: "${detailObj.reason ?? ""}",
                          leftColor: 0xff333333,
                          rightColor: 0xff333333,
                        )
                      : Container(child: null),
                  Container(
                    color: Color(0xffFAF7FA),
                    height: ScreenUtil().setWidth(5),
                  ),
                  Kan(
                      title: '查看订单（${detailObj.invoiceItem.length}）',
                      time: "",
                      fun: () {
                        NavigatorUtils.push(context,
                            "${WodeRouter.piaoOrder}?id=${detailObj.id}");
                      }),
                  Container(
                    color: Color(0xffFAF7FA),
                    height: ScreenUtil().setWidth(5),
                  ),
                  // Kan(title:'含两个订单',time:"2020-02-26 11:06-2020-02-26 11:06",fun:(){
                  //   NavigatorUtils.push(context, WodeRouter.piaoOrder);
                  // })
                ]),
              ])
            : Center(
                child: Text("正在加载..."),
              ));
  }

  @override
  bool get wantKeepAlive => true;
}

class Heng extends StatelessWidget {
  Heng({Key key, this.left, this.right, this.leftColor, this.rightColor})
      : super(key: key);
  String left;
  String right;
  int leftColor;
  int rightColor;

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return Container(
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(15),
          right: ScreenUtil().setWidth(15),
          top: ScreenUtil().setWidth(10),
          bottom: ScreenUtil().setWidth(10)),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: .5, color: Color(0xfff5f5f5)),
        ),
      ),
      child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              left,
              style: TextStyle(
                  color: Color(leftColor), fontSize: ScreenUtil().setSp(14)),
              textAlign: TextAlign.start,
            ),
            Container(
              width: width * 0.7,
              child: Text(
                right,
                style: TextStyle(
                    color: Color(rightColor), fontSize: ScreenUtil().setSp(14)),
                textAlign: TextAlign.end,
                overflow: TextOverflow.ellipsis,
              ),
            )
          ]),
    );
  }
}

class Kan extends StatelessWidget {
  Kan(
      {Key key,
      this.title,
      this.titleType,
      this.statusType,
      this.time,
      this.fun})
      : super(key: key);
  String title;
  String titleType;
  String statusType;
  String time;
  Function fun;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          fun();
        },
        child: Container(
          padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(15),
              right: ScreenUtil().setWidth(5),
              top: ScreenUtil().setWidth(10),
              bottom: ScreenUtil().setWidth(10)),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      title != null
                          ? Text(
                              title,
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(14),
                                color: Color(0xff666666),
                              ),
                            )
                          : Container(),
                      title == null
                          ? Row(
                              children: [
                                Text(
                                  '$titleType:  ',
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14),
                                    color: Color(0xff666666),
                                  ),
                                ),
                                Text(
                                  statusType,
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14),
                                    color: Colours.blue_app_main,
                                  ),
                                ),
                                //
                              ],
                            )
                          : Container(),
                      title == null ? SizedBox(height: 5) : Container(),
                      title == null
                          ? Row(
                              children: [
                                Text(
                                  '申请日期:  ',
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14),
                                    color: Color(0xff666666),
                                  ),
                                ),
                                Text(
                                  time,
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14),
                                    color: Color(0xff999999),
                                  ),
                                ),
                                //
                              ],
                            )
                          : Container(),
                    ]),
                // Row(children: <Widget>[
                //   Text(
                //     '查看',
                //     textAlign: TextAlign.left,
                //     style: TextStyle(
                //       fontSize: ScreenUtil().setSp(12),
                //       color:
                //           title != null ? Color(0xff999999) : Color(0xffFDAF2C),
                //     ),
                //   ),
                //   Icon(
                //     Icons.keyboard_arrow_right,
                //     color: Color(0xffcccccc),
                //   ),
                // ])
              ]),
        ));
  }
}
