import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/data/models/response/customer_response_model.dart';
import 'package:enaklo_pos/data/models/response/delivery_response_model.dart';
import 'package:enaklo_pos/data/models/response/table_model.dart';
import 'package:enaklo_pos/presentation/home/dialog/confirm_save_order_dialog.dart';
import 'package:enaklo_pos/presentation/home/dialog/payment_add_order_dialog.dart';
import 'package:enaklo_pos/presentation/home/dialog/payment_save_dialog.dart';
import 'package:enaklo_pos/presentation/home/models/order_type.dart';
import 'package:enaklo_pos/presentation/home/models/product_quantity.dart';
import 'package:flutter/material.dart';

class SaveDialog extends StatefulWidget {
  final TableModel? selectedTable;
  final String customerName;
  final OrderType orderType;
  final List<ProductQuantity> items;
  final Customer? customer;
  final DeliveryModel? deliveryModel;

  const SaveDialog({
    super.key,
    required this.selectedTable,
    required this.customerName,
    required this.orderType,
    required this.items,
    required this.customer,
    this.deliveryModel,
  });

  @override
  State<SaveDialog> createState() => _SaveDialogState();
}

class _SaveDialogState extends State<SaveDialog> {
  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Pilih Aksi',
      subtitle: 'Lanjutkan proses pesanan atau simpan untuk nanti.',
      contentPadding:
          const EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
      child: Column(
        children: [
          _item(
              icon: Icons.schedule_outlined,
              title: 'Bayar Nanti',
              subtitle: 'Simpan pesanan dan bayar nanti',
              onTap: () {
                context.pop();
                if (OrderType.dineIn == widget.orderType) {
                  showDialog(
                    context: context,
                    builder: (context) => PaymentSaveDialog(
                      selectedTable: widget.selectedTable,
                      customerName: widget.customerName,
                      orderType: widget.orderType,
                      items: widget.items,
                      customer: widget.customer,
                    ),
                  );
                } else {
                  showDialog(
                    context: context,
                    builder: (context) => ConfirmSaveOrderDialog(
                      delivery: widget.deliveryModel,
                      customerName: widget.customerName,
                      orderType: widget.orderType,
                      items: widget.items,
                      customer: widget.customer,
                    ),
                  );
                }
              }),
          SpaceHeight(16.0),
          _item(
            icon: Icons.shopping_cart_checkout_outlined,
            title: 'Tambahkan Pesanan',
            subtitle: 'Tambah item ke daftar pesanan',
            onTap: () => showDialog(
              context: context,
              builder: (context) => PaymentAddOrderDialog(
                items: widget.items,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _item({
    required IconData icon,
    required String title,
    required String subtitle,
    required Function() onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
        decoration: BoxDecoration(
          color: AppColors.white,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(
            color: AppColors.grey,
            width: 1.0,
          ),
        ),
        child: Row(
          children: [
            Icon(icon, color: AppColors.primary, size: 26.0),
            SpaceWidth(12.0),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(fontWeight: FontWeight.w600),
                ),
                Text(
                  subtitle,
                  style: const TextStyle(color: Colors.grey),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
