import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../order_management_page/order_detail_page.dart';
import '../../showDialog.dart';
import '../../util/api.dart';
import '../../util/models.dart';
import '../../util/util.dart';

class OrderDetail extends StatefulWidget {
  final String orderId;

  OrderDetail({
    Key key,
    @required this.orderId
  }): super(key: key);

  _OrderDetailState createState() => _OrderDetailState();
}

class _OrderDetailState extends State < OrderDetail > {

  OrderDetailData data;
  @override
  void initState() {
    super.initState();
    getOrderDetail(widget.orderId).then((resp) {
      if (isNotError(context, resp)) {
        OrderDetailObj orderDetailObj = OrderDetailObj.fromJson(resp.data);
        setState(() {
          data = orderDetailObj.data;
        });
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('订单详情'),
          centerTitle: true,
          backgroundColor: Color.fromRGBO(64, 98, 254, 1),
        ),
        body: Container(
          child: data==null?_getMoreWidget():_cardWiget()
        ),
      )
    );
  }

  Widget _cardWiget() {
    return GestureDetector(
      child: Card(
        color: Colors.white,
        child: Container(
          padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(30), ScreenUtil.getInstance().setWidth(20)),
          child: Column(
            children: < Widget > [
              Padding(
                padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: < Widget > [
                    Text('订单号：${data.orderNo}', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(102, 102, 102, 1)), ),
                    Text("运输中", style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(126, 211, 33, 1)), ),
                  ],
                )
              ),
              Padding(
                padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
                child: Row(
                  children: < Widget > [
                    Text('${data.sendCompanyName} --> ${data.receiptCompanyName}', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(30)), ),
                  ],
                )
              ),
              Padding(
                padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(15)),
                child: Row(
                  children: < Widget > [
                    SizedBox(
                      width: ScreenUtil.getInstance().setWidth(355),
                      child: Text('货物名称：${data.goods}', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(155, 155, 155, 1)), ),
                    ),
                    Text('代收款：${data.goodsAgencyFund/100}元', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(155, 155, 155, 1)), ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(30)),
                child: Row(
                  children: < Widget > [
                    SizedBox(
                      width: ScreenUtil.getInstance().setWidth(355),
                      child: Text('货物件数：${data.goodsQuantity}件', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(155, 155, 155, 1)), ),
                    ),
                    Text('运费：${data.goodsFreight/100}元', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(155, 155, 155, 1)), ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(20)),
                child: Container(
                  height: ScreenUtil.getInstance().setHeight(1),
                  color: Color.fromRGBO(223, 223, 223, 1),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: < Widget > [
                  GestureDetector(
                    child: Offstage(
                      offstage: false,
                      child: Container(
                        width: ScreenUtil.getInstance().setWidth(94),
                        height: ScreenUtil.getInstance().setHeight(48),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(239, 243, 249, 1),
                          borderRadius: BorderRadius.all(Radius.circular(4.0))
                        ),
                        child: Center(
                          child: Text('作废', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1))),
                        ),
                      ),
                    ),
                    onTap: () {
                      showDialog < Null > (
                        context: context, //BuildContext对象
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return new LoadingDialog( //调用对话框
                            text: '是否确认作废', childCallback: (val) {
                              if (val) {
                                myUpdataOrder(5);
                              } else {
                                return;
                              }
                            },
                          );
                        });
                      print("作废");
                    },
                  ),
                  GestureDetector(
                    child: Offstage(
                      offstage: false,
                      child: Container(
                        width: ScreenUtil.getInstance().setWidth(94),
                        height: ScreenUtil.getInstance().setHeight(48),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(239, 243, 249, 1),
                          borderRadius: BorderRadius.all(Radius.circular(4.0))
                        ),
                        child: Center(
                          child: Text('改单', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1))),
                        ),
                      ),
                    ),
                    onTap: () {
                      showDialog < Null > (
                        context: context, //BuildContext对象
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return new LoadingDialog( //调用对话框
                            text: '是否确认改单', childCallback: (val) {
                              if (val) {
                                print(val);
                                Navigator.push(context, MaterialPageRoute(builder: (context) {
                                  return OrderDetailPage(orderNo: '${data.orderNo}', );
                                })).then((resp){
                                  if(resp==true){
                                    getOrderDetail(widget.orderId).then((resp) {
                                      if (isNotError(context, resp)) {
                                        OrderDetailObj orderDetailObj = OrderDetailObj.fromJson(resp.data);
                                        setState(() {
                                          data = orderDetailObj.data;
                                        });
                                      }
                                    });
                                  }
                                });
                              } else {
                                return;
                              }
                            },
                          );
                        });
                      print("改单");
                    },
                  ),
                  GestureDetector(
                    child: Offstage(
                      offstage: false,
                      child: Container(
                        width: ScreenUtil.getInstance().setWidth(94),
                        height: ScreenUtil.getInstance().setHeight(48),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(239, 243, 249, 1),
                          borderRadius: BorderRadius.all(Radius.circular(4.0))
                        ),
                        child: Center(
                          child: Text('打印', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(24), color: Color.fromRGBO(64, 98, 254, 1))),
                        ),
                      ),
                    ),
                    onTap: () {
                      showDialog < Null > (
                        context: context, //BuildContext对象
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return new LoadingDialog( //调用对话框
                            text: '是否确认打印', childCallback: (val) {
                              if (val) {
                                //widget.callback(widget.index);
                              } else {
                                return;
                              }
                            },
                          );
                        });
                      print("打印");
                    },
                  ),
                ],
              )
            ],
          ),
        )
      ),
      onTap: () {

      },
    );
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: < Widget > [
            Text(
              '加载中...',
              style: TextStyle(fontSize: 16.0),
            ),
            Container(
              width: ScreenUtil.getInstance().setWidth(50),
              height: ScreenUtil.getInstance().setHeight(50),
              child: CircularProgressIndicator()
            )

          ],
        ),
      ),
    );
  }

    myUpdataOrder(int myOrderStatus) {
    updataOrderStatus(data.orderNo, myOrderStatus).then((resp) {
      if (isNotError(context, resp)) {
       
      }
    });
  }
}