
import 'package:flutter/material.dart';
import 'package:twong/router/base.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/models/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/widgets/payment.dart';

class OrderUtils {
  static Widget buildButtons({OrderData data, OrderDetails details}) {
    var orderId = data == null ? details.order_id : data.order_id;
    var orderStatus = data == null ? details.orderStatus : data.orderStatus;
    var cancel = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: OutlineButton(
          onPressed: () => _cancelTab(orderId),
          shape: StadiumBorder(), child: Text("取消订单")),
    );
    var pay = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _payTab(orderId),
          child: Text("立即付款", style: TextStyle(color: Colors.white))),
    );
    var express = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _viewExpress(data: data, details: details),
          child: Text("查看物流", style: TextStyle(color: Colors.white))),
    );
    var del = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: OutlineButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _delOrder(orderId),
          child: Text("删除订单", style: TextStyle(color: Colors.grey))),
    );
    var confirm = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _confirm(orderId),
          child: Text("确认收货", style: TextStyle(color: Colors.white))),
    );
    var comment = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _comment(orderId),
          child: Text("立即评价", style: TextStyle(color: Colors.white))),
    );
    var more = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _moreBuy(data: data, details: details),
          child: Text("再次购买", style: TextStyle(color: Colors.white))),
    );
    var returned = Container(
      margin: EdgeInsets.only(left: 12.px),
      child: FlatButton(
          color: DColors.Main,
          shape: StadiumBorder(),
          onPressed: () => _returned(orderId),
          child: Text("申请退款", style: TextStyle(color: Colors.white))),
    );

    List<Widget> widgets = List<Widget>();
    if(orderStatus.type == 0) {
      widgets.add(cancel);
      widgets.add(pay);
    } else if(orderStatus.type == 1) {
      widgets.add(cancel);
    } else if(orderStatus.type == 2) {
      widgets.add(express);
      widgets.add(confirm);
    } else if(orderStatus.type == 3) {
      widgets.add(returned);
      if(details == null) {
        widgets.add(comment);
      }
      widgets.add(more);
    } else if(orderStatus.type == 4) {
      widgets.add(del);
      widgets.add(more);
    }
    return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: widgets);
  }


  static void _cancelTab(String orderId) {
    Utils.showAlert(Cache.context, title: "确定取消订单么?", ok: () {

    });
  }

  static void _payTab(String orderId) {
    showModalBottomSheet(
        context: Cache.context,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.px), topRight: Radius.circular(10.px)
        )),
        builder: (BuildContext context) {
          return Payment(PaymentInfo.id(orderId));
        });
  }

  static void _viewExpress({OrderData data, OrderDetails details}) {
    Navigator.pushNamed(Cache.context, RouteNames.orderExpress,
      arguments: data == null ? details : data);
  }

  static void _confirm(String orderId) {

  }

  static void _delOrder(String orderId) {

  }

  static void _comment(String orderId) {
    Navigator.pushNamed(Cache.context, RouteNames.orderDetails, arguments: orderId);
  }

  static void _moreBuy({OrderData data, OrderDetails details}) {

  }

  static void _returned(String orderId) {

  }
}