
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/services.dart';

import 'package:twong/api/index.dart';
import 'package:twong/router/base.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/models/index.dart';
import 'package:twong/widgets/app_bar.dart';
import 'package:twong/widgets/cart_item.dart';
import 'package:twong/widgets/order_utils.dart';

class OrderDetailsPage extends StatefulWidget {
  final String orderId;
  OrderDetailsPage(this.orderId);
  @override
  State<StatefulWidget> createState() {
    return _OrderDetailsState();
  }
}

class _OrderDetailsState extends State<OrderDetailsPage> {
  OrderDetails _details;

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

  void loadData() {
    Network.inst.getOrderDetails(widget.orderId).then((value){
      if(!mounted) return;
      setState(() {
        _details = value;
      });
    }).catchError((err, stack) {
      Navigator.pop(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar("订单详情"),
      body: _details == null ? Center(child: Utils.loadingWidget)
          : SafeArea(child: Column(
        children: [
          Expanded(child: Container(
            color: DColors.back,
            child: ListView(
                physics: ClampingScrollPhysics(),
                children: [
                  Container(
                      height: 56.px,
                      color: DColors.Main,
                      padding: EdgeInsets.all(6.px),
                      child: Row(
                          children: [
                            Container(
                              width: 46.px,
                              height: 46.px,
                              margin: EdgeInsets.only(right: 12.px, left: 12.px),
                              child: CachedNetworkImage(imageUrl: _details.status_pic),
                            ),
                            Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Expanded(child: Text(_details.orderStatus.msg,
                                      style: TextStyle(color: Colors.white,
                                          fontSize: 13.px))),
                                  Text("下单日期：${Utils.formatData(
                                      _details.add_time)}",
                                      style: TextStyle(color: Colors.white,
                                          fontSize: 11.px)),
                                ]),
                          ])),
                  _buildAddress(),
                  _buildProducts(),
                  _buildInfo()
                ]),
          )),
          Container(
            height: 56.px,
            color: Colors.white,
            margin: EdgeInsets.only(right: 12.px),
            child: OrderUtils.buildButtons(details: _details),
          )
        ],
      )),
    );
  }

  Widget _buildAddress() {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
      ),
      padding: EdgeInsets.all(6.px),
      margin: EdgeInsets.only(left: 12.px, right: 12.px, top: 12.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(_details.real_name),
              Container(width: 22.px),
              Text(_details.user_phone)
            ],
          ),
          Text(_details.user_address)
        ],
      ),
    );
  }
  Widget _buildProducts() {
    List<Widget> widgets = List<Widget>();
    for(var item in _details.cartInfo) {
      widgets.add(InkWell(
        child: CartItem(item, action: _details.status == 2 ? Container(
          width: 58.px,
          height: 22.px,
          margin: EdgeInsets.only(left: 12.px),
          child: OutlineButton(
              onPressed: () {
                Navigator.pushNamed(context,
                    RouteNames.orderComment, arguments: item);
              },
              shape: StadiumBorder(),
              highlightedBorderColor: Colors.transparent,
              borderSide: BorderSide(color: DColors.Main),
              child: Text("评价", style: TextStyle(
                  color: DColors.Main, fontSize: 12.px))),
        ) : null),
        onTap: () { Navigator.pushNamed(context,
            RouteNames.productDetails, arguments: item.id); },
      ));
      widgets.add(Divider());
    }

    widgets.add(Container(
      child: Text("联系客服"),
    ));

    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
      ),
      padding: EdgeInsets.all(6.px),
      margin: EdgeInsets.only(left: 12.px, right: 12.px, top: 12.px),
      child: Column( children: widgets),
    );
  }
  Widget _buildInfo() {
    var style = TextStyle(color: Colors.grey);
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6.px)
      ),
      padding: EdgeInsets.all(6.px),
      margin: EdgeInsets.only(left: 12.px, right: 12.px, top: 12.px),
      child: Column(
        children: [
          Container(padding: EdgeInsets.only(bottom: 6.px), child: Row(
            children: [
              Expanded(child: Text("订单编号:")),
              Text(_details.order_id, style: style),
              Container(
                width: 46.px,
                height: 20.px,
                margin: EdgeInsets.only(left: 12.px),
                child: OutlineButton(
                    onPressed: () { Utils.copyText( _details.order_id); },
                    shape: StadiumBorder(),
                    highlightColor: Colors.transparent,
                    highlightedBorderColor: Colors.grey,
                    borderSide: BorderSide(color: Colors.grey),
                    child: Text("复制", style: TextStyle(fontSize: 7.px))),
              )
            ],
          )),
          Container(padding: EdgeInsets.only(bottom: 6.px), child: Row(
            children: [
              Expanded(child: Text("下单时间:")),
              Text(Utils.formatData(_details.add_time), style: style),
            ],
          )),
          Container(padding: EdgeInsets.only(bottom: 6.px), child: Row(
            children: [
              Expanded(child: Text("订单类型:")),
              Text("普通订单", style: style),
            ],
          )),
          Container(padding: EdgeInsets.only(bottom: 6.px), child: Row(
            children: [
              Expanded(child: Text("支付状态:")),
              Text(_details.orderStatus.title, style: style),
            ],
          )),
          Row(
            children: [
              Expanded(child: Text("支付方式:")),
              Text(_details.orderStatus.payType, style: style),
            ],
          ),
        ],
      ),
    );
  }
}