
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/router/base.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/widgets/app_bar.dart';

class VipCenterPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _VipCenterState();
  }
}

class _VipCenterState extends State<VipCenterPage> {
  VipInfo _info;
  List<VipConfig> configs;

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

  loadData() async {
    Network.inst.getVipConfig().then((value) {
      configs = value;
      Network.inst.getVipInfo().then((info) {
        setState(() {
          _info = info;
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar("会员中心"),
      backgroundColor: DColors.back,
      body: SafeArea(
        child: _info == null ? Center(child: Utils.loadingWidget) : Container(
          margin: EdgeInsets.only(left: 12.px, right: 12.px),
          child: ListView(
            physics: ClampingScrollPhysics(),
            children: [
              _buildVipCard(),
              _buildVipPromotion(),
              _buildVipInfo(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildVipCard() {
    return Container(
      height: 180.px,
      margin: EdgeInsets.only(top: 6.px, bottom: 12.px),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6.px),
        image: DecorationImage(
          fit: BoxFit.fitHeight,
          image: CachedNetworkImageProvider(configs[Cache.user.vip_level - 1].image)
        )
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 12.px, top: 6.px),
            child: Text(Cache.user.vip_card_id, style: TextStyle(fontSize: 16.px,
                fontWeight: FontWeight.bold, color: Colors.black)),
          ),
          Spacer(),
          Container(
            margin: EdgeInsets.only(left: 12.px, bottom: 6.px),
            child: Text(Utils.formatData(Cache.user.vip_time)),
          )
        ],
      ),
    );
  }

  Widget _buildVipPromotion() {
    return Container(
      padding: EdgeInsets.all(6.px),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6.px),
      ),
      margin: EdgeInsets.only(bottom: 12.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: Text("会员推广佣金", style: TextStyle(
                fontSize: 14.px, fontWeight: FontWeight.bold)),
          ),
          Divider(),
          Column(
            children: [
              Container(
                child: Row(
                  children: [
                    Container(
                      width: 120.px,
                      child: Text("当前可领取"),
                    ),
                    Expanded(child: Text(Utils.formatRMB(_info.money_available,
                        show: true), style: TextStyle(color: DColors.price))),
                    FlatButton(
                      disabledColor: Colors.grey,
                      onPressed: _info.money_available > 0 ? () {

                      } : null,
                      child: Text("领取", style: TextStyle(color: Colors.white)),
                      shape: StadiumBorder(),
                      color: DColors.Main,
                    )
                  ],
                ),
              ),
              Container(
                child: Row(
                  children: [
                    Container(
                      width: 120.px,
                      child: Text("黄色卡可领取"),
                    ),
                    Expanded(child: Text(Utils.formatRMB(_info.money_next,
                        show: true), style: TextStyle(color: DColors.price))),
                    FlatButton(
                      onPressed: () {},
                      child: Text("去购买", style: TextStyle(color: Colors.white)),
                      shape: StadiumBorder(),
                      color: DColors.Main,
                    )
                  ],
                ),
              ),
              Container(
                child: Row(
                  children: [
                    Container(
                      width: 120.px,
                      child: Text("当前佣金"),
                    ),
                    Expanded(
                        child: Text(Utils.formatRMB(Cache.user.brokerage_price,
                            show: true),
                            style: TextStyle(color: DColors.price))),
                    FlatButton(
                      onPressed: () {
                        Navigator.pushNamed(context, RouteNames.promotion);
                      },
                      child: Text("查看", style: TextStyle(color: Colors.white)),
                      shape: StadiumBorder(),
                      color: DColors.Main,
                    )
                  ],
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildVipInfo() {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6.px),
      ),
      padding: EdgeInsets.all(6.px),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: Text("会员权益说明", style: TextStyle(
                fontSize: 14.px, fontWeight: FontWeight.bold)),
          ),
          Divider(),
          Text(_info.card_benefit, style: TextStyle(
              height: 2.px, fontSize: 12.px)),
        ],
      ),
    );
  }
}