import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hello_doctor_admin_dashboard/app/models/withdraw_request_model.dart';
import 'package:hello_doctor_admin_dashboard/app/modules/withdrawal/controllers/withdrawal_controller.dart';
import 'package:hello_doctor_admin_dashboard/app/utils/timeformat.dart';
import 'package:hello_doctor_admin_dashboard/constants.dart';

class WithdrawalTableSource extends DataTableSource {
  late final List<WithdrawRequestModel> listWithdrawalRequest;
  late WithdrawalController withdrawalController;
  WithdrawalTableSource(this.listWithdrawalRequest, this.withdrawalController);
  @override
  DataRow? getRow(int index) {
    return DataRow(cells: [
      DataCell(Text(
          TimeFormat().formatDate(listWithdrawalRequest[index].createdAt))),
      DataCell(
          Text(currencySign + listWithdrawalRequest[index].amount!.toString())),
      DataCell(Text(
          currencySign + listWithdrawalRequest[index].adminFee.toString())),
      DataCell(
          Text(currencySign + listWithdrawalRequest[index].tax.toString())),
      DataCell(Text(currencySign +
          listWithdrawalRequest[index].totalWithdraw.toString())),
      DataCell(
          Text(listWithdrawalRequest[index].withdrawMethod!.email.toString())),
      DataCell(
          Text(listWithdrawalRequest[index].withdrawMethod!.method.toString())),
      DataCell(
          Text(listWithdrawalRequest[index].withdrawMethod!.name.toString())),
      DataCell(
        Text(
          listWithdrawalRequest[index].withdrawMethod!.userId.toString(),
        ),
      ),
      DataCell(Text(listWithdrawalRequest[index].status ?? '')),
      DataCell(
        IconButton(
          icon: const Icon(Icons.details),
          onPressed: () {
            Get.defaultDialog(
                title: 'Withdrawal Details',
                titleStyle: const TextStyle(fontWeight: FontWeight.bold),
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                        'Date: ${TimeFormat().formatDate(listWithdrawalRequest[index].createdAt)}'),
                    const SizedBox(height: 8),
                    Text(
                        'Status: ${listWithdrawalRequest[index].status ?? "Pending"}',
                        style: TextStyle(
                          fontWeight: FontWeight.w600,
                          color:
                              listWithdrawalRequest[index].status == 'completed'
                                  ? Colors.green
                                  : Colors.orange,
                        )),
                    const SizedBox(height: 8),
                    Text(
                        'Amount: $currencySign${listWithdrawalRequest[index].amount}'),
                    const SizedBox(height: 4),
                    Text(
                        'Admin Fee: $currencySign${listWithdrawalRequest[index].adminFee}'),
                    Text(
                        'Tax: $currencySign${listWithdrawalRequest[index].tax}'),
                    const SizedBox(height: 8),
                    Text(
                        'Total: $currencySign${listWithdrawalRequest[index].totalWithdraw}',
                        style: const TextStyle(fontWeight: FontWeight.bold)),
                    const Divider(),
                    Text(
                        'Metode Pembayaran: ${listWithdrawalRequest[index].withdrawMethod!.method}'),
                    if (listWithdrawalRequest[index].withdrawMethod!.method ==
                        'bank') ...[
                      const SizedBox(height: 8),
                      Text('Bank Details:',
                          style: TextStyle(fontWeight: FontWeight.w600)),
                      Text(
                          'Bank: ${listWithdrawalRequest[index].withdrawMethod!.bankName}'),
                      Text(
                          'Nama: ${listWithdrawalRequest[index].withdrawMethod!.accountHolderName}'),
                      Text(
                          'No Rekening: ${listWithdrawalRequest[index].withdrawMethod!.accountNumber}'),
                      if (listWithdrawalRequest[index]
                              .withdrawMethod!
                              .branchCode !=
                          null)
                        Text(
                            'Branch Code: ${listWithdrawalRequest[index].withdrawMethod!.branchCode}'),
                      if (listWithdrawalRequest[index]
                              .withdrawMethod!
                              .swiftCode !=
                          null)
                        Text(
                            'SWIFT Code: ${listWithdrawalRequest[index].withdrawMethod!.swiftCode}'),
                    ] else ...[
                      Text(
                          'Name: ${listWithdrawalRequest[index].withdrawMethod!.name}'),
                      Text(
                          'Email: ${listWithdrawalRequest[index].withdrawMethod!.email}'),
                    ],
                    const SizedBox(height: 16),
                    const SizedBox(height: 16),
                    Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Colors.grey[100],
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            'Confirmation Required',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Colors.grey[800],
                            ),
                          ),
                          const SizedBox(height: 4),
                          Text(
                            'Please ensure you have processed the ${listWithdrawalRequest[index].withdrawMethod!.method} transfer before confirming this withdrawal.',
                            style: TextStyle(color: Colors.grey[600]),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                onConfirm: () {
                  Get.dialog(
                    AlertDialog(
                      title: const Text('Confirm Withdrawal'),
                      content: const Text(
                          'Are you sure you want to confirm this withdrawal?'),
                      actions: [
                        TextButton(
                          onPressed: () {
                            Navigator.of(Get.overlayContext!)
                                .pop(); // Only close confirmation dialog
                          },
                          child: const Text('Cancel'),
                        ),
                        TextButton(
                          onPressed: () {
                            withdrawalController.markWithdrawlCompleted(
                                listWithdrawalRequest[index].id!);
                            Get.back(); // Close confirmation dialog
                            Get.back(); // Close details dialog
                          },
                          style: TextButton.styleFrom(
                            foregroundColor: Colors.white,
                            backgroundColor: Get.theme.primaryColor,
                          ),
                          child: const Text('Yes, Confirm'),
                        ),
                      ],
                    ),
                    barrierDismissible: false,
                  );
                },
                textConfirm: 'Confirm Payment',
                confirmTextColor: Colors.white,
                textCancel: 'Close',
                onCancel: () => Get.back());
          },
        ),
      )
    ]);
  }

  @override
  // TODO: implement isRowCountApproximate
  bool get isRowCountApproximate => false;

  @override
  // TODO: implement rowCount
  int get rowCount => listWithdrawalRequest.length;

  @override
  // TODO: implement selectedRowCount
  int get selectedRowCount => 0;
}
