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

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

class PromotionOrderPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PromotionOrderState();
  }
}

class _PromotionOrderState extends State<PromotionOrderPage> {
  num total = 0;
  PromotionOrderData _data;

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

  loadData() {
    Network.inst.getPromotionOrder().then((data) {
      setState(() {
        _data = data;
        total = _data.count;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: DColors.back,
      appBar: DAppBar("推广人订单"),
      body: SafeArea(
        child: ListView(
          physics: ClampingScrollPhysics(),
          children: [
            _buildHeader(),
            _buildList()
          ],
        ),
      ),
    );
  }

  Widget _buildHeader() {
    return Container(
      height: 88.px,
      color: DColors.Main,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: 22.px, top: 22.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(bottom: 8.px),
            child: Text("累计推广订单", style: TextStyle(fontSize: 12.px,
                color: Colors.white)),
          ),
          RichText(text: TextSpan(
              text: total.toString(),
              style: TextStyle(fontSize: 22.px, color: Colors.white),
              children: [
                TextSpan(
                  text: "单",
                  style: TextStyle(fontSize: 12.px, color: Colors.white),
                )
              ]
          ))
        ],
      ),
    );
  }

  Widget _buildList() {
    List<Widget> widgets = List<Widget>();
    if(_data != null) {
      for (var item in _data.list) {
        List<Widget> subWidgets = List<Widget>();
        for (var subItem in item.child) {
          subWidgets.add(Container(
            padding: EdgeInsets.all(12.px),
            margin: EdgeInsets.only(left: 12.px, right: 12.px, bottom: 6.px),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(6.px)
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Expanded(child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          CircleAvatar(
                              backgroundImage: CachedNetworkImageProvider(
                                  subItem.avatar), radius: 12.px),
                          Container(
                            padding: EdgeInsets.only(top: 3.px),
                            margin: EdgeInsets.only(left: 12.px),
                            child: Text(subItem.nickname,
                                style: TextStyle(fontSize: 13.px)),
                          ),
                        ])),
                    RichText(
                        text: TextSpan(
                            text: "返佣: ",
                            style: TextStyle(
                                color: Colors.black, fontSize: 12.px),
                            children: [
                              TextSpan(
                                  text: Utils.formatRMB(
                                      subItem.number, show: true),
                                  style: TextStyle(
                                      color: DColors.price, fontSize: 14.px))
                            ])
                    ),
                  ],
                ),
                Divider(),
                Text("返佣时间: ${subItem.time}", style:
                TextStyle(color: Colors.grey, fontSize: 12.px))
              ],
            ),
          ));
        }

        widgets.add(Container(
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                    margin: EdgeInsets.all(12.px),
                    padding: EdgeInsets.only(left: 6.px, right: 6.px),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(item.time, style: TextStyle(fontSize: 12.px)),
                        Text("本月累计推广订单: ${item.count} 单", style:
                          TextStyle(fontSize: 10.px, color: Colors.grey))
                      ],
                    )),
                Container(
                    width: double.infinity,
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: subWidgets))
              ]),
        ));
      }
    }

    return Container(
      child: _data == null ? Container(
        margin: EdgeInsets.only(top: 88.px),
        child: Center(child: Utils.loadingWidget),
      ) : Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    );
  }
}