import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:twong/api/index.dart';
import 'package:twong/router/base.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/models/index.dart';
import 'package:twong/widgets/address_item.dart';
import 'package:twong/widgets/app_bar.dart';
import 'package:twong/providers/address.dart';

class AddressPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AddressPageState();
  }
}

class _AddressPageState extends State<AddressPage> {

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

  void loadData() {
    Network.inst.getAddressList().then((value){
      Log.d(value);
      Provider.of<AddressModel>(context, listen: false).set(value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar("地址管理", actions: [
        InkWell(
          onTap: _newAddress,
          highlightColor: Colors.transparent,
          child: Container(
            margin: EdgeInsets.only(right: 12.px),
            alignment: Alignment.center,
            child: Text("添加新地址"),
          ),
        )
      ]),
      body: SafeArea(
        child: Container(
          color: DColors.back,
          child: Consumer<AddressModel>(
            builder: (context, model, child) => _buildAddressList(model.list),
          ),
        ),
      ),
      bottomNavigationBar: SafeArea(
        child: Container(
          height: 50.px,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: DShadow.top,
          ),
          padding: EdgeInsets.only(top: 6.px, bottom: 12.px),
          child: InkWell(
            onTap: _newAddress,
            child: Container(
              decoration: BoxDecoration(
                color: DColors.Main,
                borderRadius: BorderRadius.circular(45.px),
              ),
              alignment: Alignment.center,
              margin: EdgeInsets.only(left: 22.px, right: 22.px),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.room_outlined, color: Colors.white),
                  Text("新建收货地址", style: TextStyle(color: Colors.white))
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildAddressList(List<Address> list) {
    if(list == null) {
      return Center(child: Utils.loadingWidget);
    }

    List<Widget> widgets = List<Widget>();
    for(var item in list) {
      widgets.add(Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(6.px)
        ),
        width: double.infinity,
        padding: EdgeInsets.all(6.px),
        margin: EdgeInsets.only(left: 12.px, right: 12.px,
            top: 6.px, bottom: 6.px),
        child: AddressItem(item),
      ));
    }
    return ListView(
      physics: ClampingScrollPhysics(),
      children: widgets,
    );
  }

  _newAddress() {
    Navigator.pushNamed(context, RouteNames.editAddress);
  }
}