import 'package:flutter/material.dart';
import 'package:flutter_pos_ecommerce/core/extensions/build_context_ext%20copy.dart';
import 'package:flutter_pos_ecommerce/module/ecommerce/data/models/requests/product_request_model.dart';
import 'package:flutter_pos_ecommerce/module/ecommerce/data/models/responses/product_response_model.dart';
import 'package:flutter_pos_ecommerce/module/pos/presentation/setting/pages/update_product_page.dart';
import 'package:image_picker/image_picker.dart';

import '../../../../../core/components/buttons.dart';
import '../../../../../core/components/spaces.dart';
import '../../../../../core/constants/colors.dart';
import '../../../../../core/constants/global_variable.dart';

class MenuProductItem extends StatelessWidget {
  final Product data;
  final Function(Product) onDeleteTap; // Tambahkan properti onDeleteTap
  const MenuProductItem(
      {super.key, required this.data, required this.onDeleteTap});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(8.0),
      decoration: ShapeDecoration(
        color: Colors.white,
        shape: RoundedRectangleBorder(
          side: const BorderSide(width: 3, color: AppColors.blueLight),
          borderRadius: BorderRadius.circular(10),
        ),
      ),
      child: Row(
        children: [
          ClipRRect(
            borderRadius: const BorderRadius.all(Radius.circular(10.0)),
            child: Image.network(
              data.image!.contains('http')
                  ? data.image!
                  : '${GlobalVariable.baseUrlImage}${data.image}',
              width: 74,
              height: 74,
              fit: BoxFit.cover,
            ),
          ),
          const SpaceWidth(11.0),
          Flexible(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  data.name ?? '',
                  style: const TextStyle(
                    fontSize: 20,
                  ),
                ),
                const SpaceHeight(5.0),
                Text(
                  data.category?.name ?? '',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
                const SpaceHeight(10.0),
                Row(
                  children: [
                    Flexible(
                      child: Button.outlined(
                        onPressed: () {
                          showDialog(
                            context: context,
                            builder: (context) => AlertDialog(
                              title: Text(data.name ?? ''),
                              content: SizedBox(
                                width: 200,
                                height: 120,
                                child: Column(
                                  children: [
                                    const Text(
                                      'Apakah Anda yakin ingin menghapus produk ini?',
                                      textAlign: TextAlign.center,
                                    ),
                                    const SizedBox(height: 20),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceEvenly,
                                      children: [
                                        ElevatedButton(
                                          onPressed: () {
                                            Navigator.pop(context);
                                          },
                                          child: const Text('Tidak'),
                                        ),
                                        ElevatedButton(
                                          onPressed: () {
                                            onDeleteTap(
                                                data); // Panggil onDeleteTap
                                            Navigator.pop(context);
                                          },
                                          child: const Text('Ya'),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          );
                        },
                        label: 'Hapus Produk',
                        fontSize: 8.0,
                        height: 31,
                      ),
                    ),
                    Flexible(
                      child: Button.outlined(
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => UpdateProductPage(
                                productRequestModel: ProductRequestModel(
                                  category_id: data.category!.id!,
                                  description: data.description!,
                                  id: data.id!,
                                  image: data.image != null
                                      ? XFile(data.image!)
                                      : null,
                                  is_available: data.isAvailable!,
                                  name: data.name!,
                                  price: data.price!,
                                  stock: data.stock!,
                                ),
                              ),
                            ),
                          );
                        },
                        label: 'Ubah Produk',
                        fontSize: 8.0,
                        height: 31,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
