import 'package:fcs/domain/entities/shipment.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/my_data_table.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef OnAdd(Shipment shipment);
typedef OnRemove(Shipment shipment);

class InvoiceHandlingFeeList extends StatelessWidget {
  final List<Shipment> shipments;
  final OnAdd onAdd;
  final OnRemove onRemove;

  const InvoiceHandlingFeeList(
      {Key key, this.shipments, this.onAdd, this.onRemove})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.pop(context),
          ),
          backgroundColor: primaryColor,
          title: LocalText(
            context,
            "invoice.shipment.handling.fee.title",
            fontSize: 20,
            color: Colors.white,
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: table(context),
        ));
  }

  Widget table(BuildContext context) {
    return MyDataTable(
      headingRowHeight: 40,
      columns: [
        MyDataColumn(
          label: LocalText(
            context,
            "invoice.shipment.number",
            color: Colors.grey,
          ),
        ),
        MyDataColumn(
          label: LocalText(
            context,
            "invoice.add.handling.fee.menu",
            color: Colors.grey,
          ),
        ),
      ],
      rows: getRows(context),
    );
  }

  List<MyDataRow> getRows(BuildContext context) {
    if (shipments == null) {
      return [];
    }
    var rows = shipments.map((c) {
      return MyDataRow(
        onSelectChanged: (value) => Navigator.pop(context, c),
        cells: [
          MyDataCell(new Text(
            c.shipmentNumber ?? "",
            style: textStyle,
          )),
          MyDataCell(
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(c.handlingFee?.toStringAsFixed(2) ?? "0",
                    style: textStyle),
                onRemove == null
                    ? SizedBox(
                        width: 50,
                      )
                    : IconButton(
                        icon: Icon(
                          Icons.remove_circle,
                          color: primaryColor,
                        ),
                        onPressed: () {
                          if (onRemove != null) onRemove(c);
                        })
              ],
            ),
          ),
        ],
      );
    }).toList();

    return rows;
  }
}
