
import 'package:flutter/material.dart';
import 'package:twong/api/index.dart';
import 'package:twong/config/style.dart';

import 'package:twong/utils/index.dart';
import 'package:twong/models/index.dart';
import 'package:twong/widgets/app_bar.dart';
import 'package:twong/widgets/cart_item.dart';

class OrderExpressPage extends StatefulWidget {
  final dynamic data;
  OrderExpressPage(this.data);

  @override
  State<StatefulWidget> createState() {
    return _OrderExpressState();
  }
}

class _OrderExpressState extends State<OrderExpressPage> {
  Express info;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() async {
    Network.inst.getOrderExpress(widget.data.order_id).then((value) {
      setState(() {
        info = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: DAppBar("物流信息"),
        backgroundColor: DColors.back,
        body: Container(
            margin: EdgeInsets.only(left: 12.px, right: 12.px),
            child: ListView(
                physics: ClampingScrollPhysics(),
                children: [
                  _buildProducts(),
                  Divider(),
                  _buildExpress(),
                ])
        )
    );
  }

  Widget _buildProducts() {
    List<Widget> widgets = List<Widget>();
    for (var item in widget.data.cartInfo) {
      Log.d(item.id);
      widgets.add(CartItem(item));
    }

    return Container(
      child: Column(children: widgets),
      padding: EdgeInsets.all(6.px),
      margin: EdgeInsets.only(top: 6.px),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
      ),
    );
  }

  Widget _buildExpress() {
    return Container(
      padding: EdgeInsets.all(6.px),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
      ),
      child: info == null ? Center(child: Utils.loadingWidget) : Row(
        children: [
          Container(
            margin: EdgeInsets.only(right: 12.px),
            child: Icon(Icons.explicit),
          ),
          Expanded(child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              RichText(text: TextSpan(
                text: "物流公司: ",
                style: TextStyle(color: Colors.black, fontSize: 12.px),
                children: [
                  TextSpan(
                    text: info.order.delivery_name,
                    style: TextStyle(color: Colors.grey, fontSize: 11.px),
                  )
                ]
              )),

              RichText(text: TextSpan(
                  text: "快递单号: ",
                  style: TextStyle(color: Colors.black, fontSize: 12.px),
                  children: [
                    TextSpan(
                      text: info.order.delivery_id,
                      style: TextStyle(color: Colors.grey, fontSize: 11.px),
                    )
                  ]
              ))
            ],
          )),
          Container(
            width: 56.px,
            height: 21.px,
            child: OutlineButton(
                onPressed: () { Utils.copyText(info.order.delivery_id); },
                child: Text("复制", style: TextStyle(fontSize: 10.px))),
          )
        ],
      ),
    );
  }
}