import 'package:aman_kassa_flutter/redux/actions/kassa_actions.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/shared_styles.dart';
import 'package:flutter/material.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/core/models/product_dao.dart';

class ProductListItem extends StatelessWidget {
  final ProductDao item;
  final int index;

  ProductListItem({required this.item, required this.index});

  @override
  Widget build(BuildContext context) {
    return Container(
      //color: index % 2 == 1 ? fillColor : whiteColor,
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 4, horizontal: 4),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: Container(
                      padding: const EdgeInsets.symmetric(
                          vertical: 8, horizontal: 4),
                      child: Text(
                        item.name,
                        style: productTextStyle,
                      )),
                ),
                Expanded(
                  child: Container(
                      padding: const EdgeInsets.symmetric(
                          vertical: 8, horizontal: 4),
                      child: Text(
                          '${item.price.toString()} x ${item.count.toString()} = ${item.total.toString()}',
                          textAlign: TextAlign.right,
                          style: productTextStyle)),
                )
              ],
            ),
            Container(
              margin: const EdgeInsets.only(bottom: 10.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Container(),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                        margin: const EdgeInsets.symmetric(horizontal: 4),
                        child: Row(
                      children: <Widget>[
                        // buildClipRect(primaryColor, Icons.edit, () {
                        //   editCountForm(context, item.count);
                        // }),
                        if(item.excise == null)
                          buildClipRect(primaryColor, Icons.remove, () {
                            Redux.store!
                                .dispatch(
                                counterProductFromKassaItems(index, -1));
                          }),
                        if(item.excise == null)
                          buildClipRect(primaryColor, Icons.add, () {
                            Redux.store!
                                .dispatch(
                                counterProductFromKassaItems(index, 1));
                          }),

                        Expanded(
                          child: Container(),
                        ),
                        buildClipRect(redColor, Icons.close, () {
                          Redux.store!
                              .dispatch(removeProductFromKassaItems(index));
                        }),
                      ],
                    )),
                  )
                ],
              ),
            ),
            Divider(
              height: 1,
              color: Colors.black38,
              //thickness: 1,
              indent: 4,
              endIndent: 4,
            )
          ],
        ),
      ),
    );
  }

  ClipRect buildClipRect(Color color, IconData icon, VoidCallback onPress) {
    return ClipRect(
      child: Container(
        margin: const EdgeInsets.all(5),
        decoration: BoxDecoration(color: color),
        height: 35,
        width: 35,
        child: IconButton(
          icon: Icon(icon, color: whiteColor, size: 20),
          onPressed: onPress,
        ),
      ),
    );
  }


  void editCountForm(BuildContext context, num count) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5.0),
            ),
            actionsPadding: const EdgeInsets.only(right: 15, bottom: 5),
            title: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'request.title',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                //Divider(),
              ],
            ),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                //Text(request.description),
                TextField(
                  autofocus: true,
                  decoration: InputDecoration(
                      labelText: 'request.description',
                  ),
                  //controller: _controller,
                  onSubmitted: (value) {

                  },
                  keyboardType: TextInputType.phone,

                )
              ],
            ),
            actions: <Widget>[
              Row(
                children: [
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(backgroundColor: redColor),
                    child: Text(
                      'Отмена',
                      style: TextStyle(fontSize: 18),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(backgroundColor: primaryColor),
                    child: Text(
                      'Сохранить',
                      style: TextStyle(fontSize: 18),
                    ),
                    onPressed: () {

                    },
                  ),
                ],
              ),
            ],
          );
        });
  }
}
