import 'package:flutter/material.dart';
import 'package:test_sa/models/part.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/buttons/app_icon_button2.dart';

class PartItem extends StatefulWidget {
  final Part part;
  final Function(Part) onDelete;
  final Function(int qty) onEdit;

  const PartItem({Key key, this.part, this.onEdit, this.onDelete}) : super(key: key);

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

class _PartItemState extends State<PartItem> {
  @override
  Widget build(BuildContext context) {
    //final _subtitle = AppLocalization.of(context).subtitle;
    return Column(
      children: [
        const Divider(),
        Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Text(
                          widget?.part?.code ?? "",
                          style: Theme.of(context).textTheme.bodyText1.copyWith(fontSize: 12, fontWeight: FontWeight.bold),
                        ),
                      ),
                      AIconButton2(
                        iconData: Icons.add,
                        color: Theme.of(context).primaryColor,
                        onPressed: () {
                          if (widget.onEdit == null) {
                            ++widget.part.quantity;
                          } else {
                            widget.onEdit(++widget.part.quantity);
                          }
                          setState(() {});
                        },
                      ),
                      AIconButton2(
                        iconData: Icons.remove,
                        color: Theme.of(context).primaryColor,
                        onPressed: widget.part.quantity < 2
                            ? null
                            : () {
                                if (widget.onEdit == null) {
                                  --widget.part.quantity;
                                } else {
                                  widget.onEdit(--widget.part.quantity);
                                }
                                setState(() {});
                              },
                      ),
                      SizedBox(
                        width: 8 * AppStyle.getScaleFactor(context),
                      ),
                      Text(
                        widget.part.quantity.toString(),
                        style: Theme.of(context).textTheme.headline6.copyWith(
                            //fontSize: 12,
                            //fontWeight: FontWeight.bold
                            ),
                      ),
                      SizedBox(
                        width: 8 * AppStyle.getScaleFactor(context),
                      ),
                    ],
                  ),
                  widget.part.name == null
                      ? const SizedBox.shrink()
                      : Text(
                          widget.part.name,
                          style: Theme.of(context).textTheme.caption.copyWith(fontSize: 11, fontWeight: FontWeight.bold),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                ],
              ),
            ),
            AIconButton2(
              iconData: Icons.close,
              color: Colors.red,
              onPressed: () {
                widget.onDelete(widget.part);
              },
            ),
          ],
        ),
      ],
    );
  }
}
