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/my_data_table.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CargoTable extends StatefulWidget {
  final List<CargoType>? cargoTypes;

  const CargoTable({
    Key? key,
    this.cargoTypes,
  }) : super(key: key);

  @override
  _CargoTableState createState() => _CargoTableState();
}

class _CargoTableState extends State<CargoTable> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: DataTable(
        headingRowHeight: 40,
        columnSpacing: 50,
        showCheckboxColumn: false,
        columns: [
          DataColumn(
            label: LocalText(
              context,
              "cargo.type",
              color: Colors.grey,
            ),
          ),
          DataColumn(
            label: LocalText(
              context,
              "cargo.qty",
              color: Colors.grey,
            ),
          ),
          DataColumn(
            label: LocalText(
              context,
              "cargo.weight",
              color: Colors.grey,
            ),
          ),
        ],
        rows: getCargoRows(context),
      ),
    );
  }

  List<DataRow> getCargoRows(BuildContext context) {
    if (widget.cargoTypes == null) {
      return [];
    }
    double total = 0;
    var rows = widget.cargoTypes!.map((c) {
      total += c.weight;
      return DataRow(
        onSelectChanged: (bool? selected) async {},
        cells: [
          DataCell(new Text(
            c.name ?? "",
            style: textStyle,
          )),
          DataCell(c.qty == null || c.qty == 0
              ? Center(
                  child: Text(
                    "-",
                    style: textStyle,
                  ),
                )
              : Center(
                  child: Text(
                    c.qty.toString(),
                    style: textStyle,
                  ),
                )),
          DataCell(
            Text(c.weight == null ? "0" : c.weight.toStringAsFixed(2),
                style: textStyle),
          ),
        ],
      );
    }).toList();

    var totalRow = DataRow(
      onSelectChanged: (bool? selected) {},
      cells: [
        DataCell(Align(
          alignment: Alignment.centerRight,
          child: LocalText(
            context,
            "shipment.cargo.total",
            color: Colors.black87,
            fontWeight: FontWeight.bold,
          ),
        )),
        DataCell(Text("")),
        DataCell(
          Padding(
            padding: const EdgeInsets.only(right: 48.0),
            child: Align(
                alignment: Alignment.centerRight,
                child: Text(total.toStringAsFixed(2),
                    style: TextStyle(fontWeight: FontWeight.bold))),
          ),
        ),
      ],
    );
    rows.add(totalRow);
    return rows;
  }
}
