import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'cargo_editor.dart';
import 'model/shipment_rate_model.dart';

class CargoTypeList extends StatefulWidget {
  const CargoTypeList({Key key}) : super(key: key);
  @override
  _CargoTypeListState createState() => _CargoTypeListState();
}

class _CargoTypeListState extends State<CargoTypeList> {
  bool _isLoading = false;

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

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var shipmentRateModel = Provider.of<ShipmentRateModel>(context);

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(CupertinoIcons.back),
              onPressed: () => Navigator.pop(context),
            ),
            backgroundColor: primaryColor,
            title: LocalText(
              context,
              "cargo.form.title",
              fontSize: 20,
              color: Colors.white,
            ),
          ),
          floatingActionButton: FloatingActionButton.extended(
              onPressed: () {
                Navigator.of(context).push(
                    CupertinoPageRoute(builder: (context) => CargoEditor()));
              },
              icon: Icon(Icons.add, color: Colors.white),
              backgroundColor: primaryColor,
              label:
                  LocalText(context, 'cargo.form.title', color: Colors.white)),
          body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListView.separated(
                separatorBuilder: (c, i) => Divider(
                      color: primaryColor,
                    ),
                itemCount: shipmentRateModel.rate.cargoTypes.length,
                itemBuilder: (context, index) {
                  CargoType cargo = shipmentRateModel.rate.cargoTypes[index];
                  return InkWell(
                    onTap: () {
                      Navigator.of(context).push(CupertinoPageRoute(
                          builder: (context) => CargoEditor(
                                cargo: cargo,
                              )));
                    },
                    child: Container(
                      child: _row(cargo.name, "\$ " + cargo.rate.toString(),
                          'per pound'),
                    ),
                  );
                }),
          )),
    );
  }

  _row(String desc, String price, String unit) {
    return Container(
        padding: EdgeInsets.only(left: 25, top: 5, bottom: 5),
        child: Row(
          children: <Widget>[
            Text('$desc ', style: TextStyle(fontSize: 15)),
            Spacer(),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(bottom: 3.0),
                  child: Text(
                    '$price',
                    style: TextStyle(color: primaryColor, fontSize: 14),
                  ),
                ),
                Text(
                  '$unit',
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
              ],
            ),
            SizedBox(
              width: 50,
            ),
          ],
        ));
  }
}
