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

class FavoursPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FavoursPageState();
  }
}

class _FavoursPageState extends State<FavoursPage> {
  List<Collect> collects;

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

    loadData();
  }

  loadData () async {
    var list = await Network.inst.getCollect();

    setState(() {
      collects = list;
    });
  }

  List<Widget> _buildCollects () {
    if(collects == null || collects.length < 1) {
      return [];
    }

    List<Widget> widgets = List<Widget>();
    for(var collect in collects) {
      widgets.add(InkWell(
        onTap: () {
          Product product = Product();
          product.id = collect.pid;
          product.store_name = collect.store_name;
          product.image = collect.image;
          product.price = collect.price;
          Navigator.pushNamed(context, RouteNames.productDetails, arguments: product);
        },
        child: Container(
          padding: EdgeInsets.only(left: 10, right: 10, top: 10),
          child: Row(
            children: <Widget>[
              CachedNetworkImage(
                imageUrl: collect.image, height: 90.px, width: 80.px, fit: BoxFit.cover),
              Expanded(
                child: Container(
                  height: 90.px,
                  child: Column(
                    children: <Widget>[
                      Expanded(child: Container(
                        padding: EdgeInsets.only(left: 10, right: 10),
                        child: Text(collect.store_name),
                      ),),
                      Expanded(child: Row(
                        children: <Widget>[
                          Expanded(child: Container(
                            padding: EdgeInsets.only(left: 10, right: 10),
                            child: Text(collect.price),
                          ),),
                          Expanded(child: Container(
                            padding: EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 4),
                            child: FlatButton(
                              onPressed: () {
                                print(collect.pid);
                              },
                              color: DColors.Main,
                              shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
                              child: Text('删除', style: TextStyle(color: Colors.white),),
                            ),
                          ),),
                        ],
                      ))
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ));
    }

    return widgets;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar("收藏"),
      body: SafeArea(
        child: ListView(
          physics: ClampingScrollPhysics(),
          children: _buildCollects(),
        ),
      )
    );
  }
}