import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/provider/lib/provider_widget.dart';
import 'package:sport/provider/lib/view_state_lifecycle.dart';
import 'package:sport/provider/shop_model.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/space.dart';

class ShopDetailPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ShopDetailPageState();
  }
}

class _ShopDetailPageState
    extends ViewStateLifecycle<ShopDetailPage, ScoreModel> {
  Widget _buildListItem(String name, String create, int score) {
    return Padding(
        padding: EdgeInsets.fromLTRB(12.0, 12.0, 12.0, 12.0),
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "$name",
                      style:
                          TextStyle(fontSize: 16.0, color: Color(0xff333333)),
                    ),
                    Space(
                      height: 5.0,
                    ),
                    Text(
                      "$create",
                      style:
                          TextStyle(color: Color(0xff999999), fontSize: 12.0),
                    )
                  ],
                ),
                Text(score > 0 ? "+$score 积分" : "-$score 积分",
                    style: TextStyle(
                      fontSize: 16.0,
                      color: score < 0  ? Theme.of(context).accentColor : Color(0xffFF5B1D),
                    )),
              ],
            ),
            Space(
              height: 10,
            ),
            Divider(),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: buildAppBar(context, title: "积分明细"),
        backgroundColor: Colors.white,
        body: ProviderWidget<ScoreModel>(
          model: model,
          onModelReady: (v) => v.loadData(),
          builder: (_, model, __) {
            return CustomScrollView(slivers: [
              if (model.isIdle)
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      return _buildListItem(
                          model?.scoreList[index].detail,
                          model?.scoreList[index].createdAt,
                          model?.scoreList[index].score);
                    },
                    childCount: model?.scoreList?.length ?? 0,
                  ),
                ),
            ]);
          },
        ));
  }

  @override
  ScoreModel createModel() {
    // TODO: implement createModel
    return new ScoreModel();
  }
}
