
import 'package:flutter/material.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 PromotionRecordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PromotionRecordState();
  }
}

class _PromotionRecordState extends State<PromotionRecordPage> {
  num total = 0;
  List<PromoteRecordList> _data;

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

  loadData() {
    Network.inst.getPromotionRecord(type: 3).then((res) {
      setState(() {
        _data = res;
      });
    });

    Network.inst.getPromotionCommission().then((res) {
      setState(() {
        total = num.parse(res["commissionCount"].toString());
      });
    });
  }

  @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: I18n.$,
              style: TextStyle(fontSize: 12.px, color: Colors.white),
              children: [
                TextSpan(
                  text: Utils.formatRMB(total),
                  style: TextStyle(fontSize: 22.px, color: Colors.white),
                )
              ]
          ))
        ],
      ),
    );
  }

  Widget _buildList() {
    List<Widget> widgets = List<Widget>();
    if(_data != null) {
      for (var item in _data) {
        List<Widget> subWidgets = List<Widget>();
        for (var subItem in item.list) {
          subWidgets.add(Container(
            margin: EdgeInsets.all(8.px),
            padding: EdgeInsets.only(left: 6.px, right: 6.px),
            child: Row(
              children: [
                Expanded(child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(subItem.title, style: TextStyle(fontSize: 13.px)),
                      Text(
                          subItem.add_time, style: TextStyle(color: Colors.grey,
                          fontSize: 12.px)),
                    ])),
                Text(Utils.formatRMB(subItem.number, sign: true),
                    style: TextStyle(color: Colors.green, fontSize: 16.px))
              ],
            ),
          ));
          subWidgets.add(Divider());
        }
        subWidgets.removeLast();
        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: Text(item.time, style: TextStyle(fontSize: 12.px))),
                Container(
                  color: Colors.white,
                  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,
      ),
    );
  }
}