import 'package:flutter/material.dart';
import 'package:cashumit/models/receipt_item.dart';
import 'package:cashumit/widgets/receipt_item_widget.dart';
import 'package:cashumit/widgets/add_item_button.dart';
import 'package:cashumit/widgets/horizontal_divider.dart';

class ReceiptItemList extends StatefulWidget {
  final List<ReceiptItem> items;
  final Function(int)? onEditItem;
  final Function(int)? onRemoveItem;
  final VoidCallback? onAddItem;

  const ReceiptItemList({
    Key? key,
    required this.items,
    this.onEditItem,
    this.onRemoveItem,
    this.onAddItem,
  }) : super(key: key);

  @override
  State<ReceiptItemList> createState() => _ReceiptItemListState();
}

class _ReceiptItemListState extends State<ReceiptItemList> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        // Baris tabel keterangan
        const Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(width: 5),
            Expanded(
              flex: 4,
              child: Text(
                'ITEM',
                style: TextStyle(
                  fontFamily: 'Courier',
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.left,
              ),
            ),
            Expanded(
              flex: 1,
              child: Text(
                'QTY',
                style: TextStyle(
                  fontFamily: 'Courier',
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),
            ),
            Expanded(
              flex: 2,
              child: Text(
                'HARGA',
                style: TextStyle(
                  fontFamily: 'Courier',
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.right,
              ),
            ),
            Expanded(
              flex: 2,
              child: Text(
                'TOTAL',
                style: TextStyle(
                  fontFamily: 'Courier',
                  fontSize: 14,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.right,
              ),
            ),
            SizedBox(width: 5),
          ],
        ),
        // Garis pembatas
        const HorizontalDivider(),
        // Daftar item dengan Dismissible untuk swipe-to-delete
        ...widget.items.asMap().entries.map((entry) {
          int index = entry.key;
          ReceiptItem item = entry.value;

          return Dismissible(
            key: Key('${item.hashCode}_$index'),
            direction: DismissDirection.horizontal,
            background: Container(
              color: Colors.red,
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: 20.0),
              child: const Icon(Icons.delete, color: Colors.white),
            ),
            secondaryBackground: Container(
              color: Colors.red,
              alignment: Alignment.centerRight,
              padding: const EdgeInsets.only(right: 20.0),
              child: const Icon(Icons.delete, color: Colors.white),
            ),
            confirmDismiss: (direction) async {
              // Untuk saat ini, kita mengembalikan true untuk semua arah
              // Nanti bisa diganti dengan dialog konfirmasi
              return true;
            },
            onDismissed: (direction) {
              // Panggil callback onRemoveItem jika tersedia
              widget.onRemoveItem?.call(index);
            },
            child: GestureDetector(
              onTap: () => widget.onEditItem?.call(index),
              child: ReceiptItemWidget(item: item),
            ),
          );
        }).toList(),
        // Tombol tambah item
        AddItemButton(onTap: widget.onAddItem ?? () {}),
      ],
    );
  }
}

