
import 'package:cached_network_image/cached_network_image.dart';
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 PromotionSpreadPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PromotionSpreadState();
  }
}

class _PromotionSpreadState extends State<PromotionSpreadPage> with SingleTickerProviderStateMixin {
  int current = 0;
  int total = 0;
  int totalLevel = 0;
  PromotionPeopleData _data;

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

  loadData() async {
    _onTabClick(0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      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: (totalLevel + 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) {
      widgets.add(Divider());
      for (var item in _data.list) {
        widgets.add(Container(
          margin: EdgeInsets.only(left: 12.px, right: 12.px),
          child: Row(
            children: [
              CircleAvatar(
                  backgroundImage: CachedNetworkImageProvider(item.avatar)),
              Container(
                margin: EdgeInsets.only(left: 12.px),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(item.nickname),
                    Text("加入时间: ${item.time}")
                  ],
                ),
              ),
              Spacer(),
              Column(
                children: [
                  Text("${item.numberCount == null ? 0 : item.numberCount}人",
                      style: TextStyle(fontSize: 12.px)),
                  Text("${item.orderCount == null ? 0 : item.orderCount}单",
                      style: TextStyle(fontSize: 12.px)),
                  Text("${item.childCount == null ? 0 : item.childCount}元",
                      style: TextStyle(fontSize: 12.px)),
                ],
              )
            ],
          ),
        ));
        widgets.add(Divider());
      }
    }

    var boxDecoration = BoxDecoration(
        border: Border(bottom: BorderSide(color: DColors.Main))
    );

    return Container(
      child: Column(children: [
        Container(
          margin: EdgeInsets.only(top: 6.px, right: 6.px),
          child: Row(
            children: [
              Spacer(),
              InkWell(
                onTap: () => _onTabClick(0),
                highlightColor: Colors.transparent,
                child: Container(
                  padding: EdgeInsets.only(bottom: 4.px),
                  decoration: current == 0 ? boxDecoration : null,
                  child: Text("一级($total)"),
                ),
              ),
              Spacer(),
              InkWell(
                onTap: () => _onTabClick(1),
                highlightColor: Colors.transparent,
                child: Container(
                  padding: EdgeInsets.only(bottom: 4.px),
                  decoration: current == 1 ? boxDecoration : null,
                  child: Text("二级($totalLevel)"),
                ),
              ),
              Spacer(),
            ],
          ),
        ),
        _data == null ? Container(
          margin: EdgeInsets.only(top: 88.px),
          child: Center(child: Utils.loadingWidget),
        ) : Column(children: widgets)
      ])
    );
  }

  _onTabClick(int idx) {
    setState(() {
      _data = null;
      current = idx;
    });

    Network.inst.getPromotionPeople(grade: idx).then((data) {
      setState(() {
        _data = data;
        total = _data.total;
        totalLevel = _data.totalLevel;
      });
    });
  }
}