import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<dynamic> showApprovalDialog(
  BuildContext context,
  Color layoutColor,
) async {
  String selectedAction = ""; // "", "accept", "reject"
  String remarks = "";

  return showDialog<String>(
    context: context,
    barrierDismissible: false, // force user to tap cancel
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            backgroundColor: Colors.white,
            contentPadding: const EdgeInsets.all(24),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "To Approve or Reject Trip",
                      style: TextStyle(
                        fontFamily: "Inter",
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    IconButton(
                      icon: const Icon(Icons.close, size: 15),
                      onPressed: () {
                        Navigator.pop(context, null); // Close the dialog
                      },
                    ),
                  ],
                ),
                // Accept and Reject Buttons
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor:
                              selectedAction == "accept"
                                  ? Colors.green
                                  : Colors.grey.shade200,
                          foregroundColor:
                              selectedAction == "accept"
                                  ? Colors.white
                                  : Colors.black,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                          padding: EdgeInsets.symmetric(vertical: 10),
                        ),
                        onPressed: () {
                          setState(() {
                            selectedAction = "accept";
                          });
                        },
                        child: const Text("Accept"),
                      ),
                    ),
                    const SizedBox(width: 20),
                    Expanded(
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor:
                              selectedAction == "reject"
                                  ? Colors.redAccent
                                  : Colors.grey.shade200,
                          foregroundColor:
                              selectedAction == "reject"
                                  ? Colors.white
                                  : Colors.black,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                          padding: EdgeInsets.symmetric(vertical: 10),
                        ),
                        onPressed: () {
                          setState(() {
                            selectedAction = "reject";
                          });
                        },
                        child: const Text(
                          "Reject",
                          style: TextStyle(fontFamily: "Inter"),
                        ),
                      ),
                    ),
                  ],
                ),

                const SizedBox(height: 20),

                // Dynamic Content Based on Selection
                if (selectedAction == "accept") ...[
                  const Text(
                    "Do you want to approve?",
                    style: TextStyle(fontFamily: "Inter", fontSize: 14),
                  ),
                ] else if (selectedAction == "reject") ...[
                  const Text(
                    "Please enter reason for rejection:",
                    style: TextStyle(fontFamily: "Inter", fontSize: 14),
                  ),
                  const SizedBox(height: 10),
                  TextField(
                    style: TextStyle(fontFamily: "Inter", fontSize: 14),
                    maxLines: 3,
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(
                        RegExp(r'[a-zA-Z0-9 _-]'),
                      ),
                    ],
                    onChanged: (value) => remarks = value,
                    decoration: InputDecoration(
                      hintText: "Remarks...",
                      hintStyle: const TextStyle(
                        fontSize: 10, // 👈 Set your desired font size here
                        color: Colors.grey,
                        fontFamily:
                            "Inter", // optional if you want consistent font
                      ),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: const BorderSide(
                          color: Colors.blueGrey,
                          width: 0.5,
                        ),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(
                          color: Colors.blueGrey,
                          width: 0.5,
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(color: layoutColor, width: 1),
                      ),
                    ),
                  ),
                ],
              ],
            ),
            actionsAlignment: MainAxisAlignment.center,
            actions: [
              // Cancel Button
              if (selectedAction.isNotEmpty)
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white,
                    foregroundColor: layoutColor,
                    side: BorderSide(color: layoutColor, width: 1.5),
                  ),
                  onPressed: () {
                    Navigator.pop(context, null); // No result
                  },
                  child: const Text(
                    "Cancel",
                    style: TextStyle(fontFamily: "Inter", fontSize: 12),
                  ),
                ),
              if (selectedAction.isNotEmpty)
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: layoutColor,
                    foregroundColor: Colors.white,
                  ),
                  onPressed: () {
                    if (selectedAction == "accept") {
                      Navigator.pop(context, "approved"); // Return approval
                    } else if (selectedAction == "reject" &&
                        remarks.trim().isNotEmpty) {
                      Navigator.pop(context, remarks.trim()); // Return remarks
                    }
                  },
                  child: const Text(
                    "OK",
                    style: TextStyle(fontFamily: "Inter", fontSize: 12),
                  ),
                ),
            ],
          );
        },
      );
    },
  );
}

/// Show confirm dialog for approval
Future<bool?> showApproveDialog1(BuildContext context, Color layoutColor) {
  return showDialog<bool>(
    context: context,
    builder:
        (context) => AlertDialog(
          backgroundColor: Colors.white,
          title: const Text(
            "Confirm Approval",
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          ),
          content: const Text("Are you sure you want to approve this plan?"),
          actions: [
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.white,
                foregroundColor: layoutColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                  side: BorderSide(color: layoutColor, width: 2),
                ),
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
              ),
              onPressed: () => Navigator.pop(context, false),
              child: const Text("Cancel"),
            ),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: layoutColor,
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                  side: BorderSide(color: layoutColor, width: 2),
                ),
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
              ),
              onPressed: () => Navigator.pop(context, true),
              child: const Text("OK"),
            ),
          ],
        ),
  );
}

/// Show confirm dialog for rejection with remarks input
Future<String?> showRejectDialog1(
  BuildContext context,
  Color layoutColor,
) async {
  String remarks = "";

  final confirmed = await showDialog<bool>(
    context: context,
    builder: (context) {
      return StatefulBuilder(
        builder:
            (context, setState) => AlertDialog(
              backgroundColor: Colors.white,
              contentPadding: const EdgeInsets.all(36),
              // title: const Text("Confirm Rejection"),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const Text(
                    "Confirm Rejection",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                  const SizedBox(height: 16),
                  const Text("Please enter reason for rejection."),
                  const SizedBox(height: 10),
                  TextField(
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(
                        RegExp(r'[a-zA-Z0-9 _-]'),
                      ),
                    ],
                    maxLines: 3,
                    onChanged: (value) => remarks = value,

                    decoration: const InputDecoration(
                      hintText: "Remarks...",
                      hintStyle: TextStyle(
                        fontSize: 10, // 👈 Set your desired font size here
                        color: Colors.grey,
                        fontFamily:
                            "Inter", // optional if you want consistent font
                      ),
                      border: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.blueGrey,
                          width: 0.5,
                        ),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.blueGrey,
                          width: 0.5,
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.grey, width: 1),
                      ),
                    ),
                  ),
                ],
              ),
              actions: [
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white,
                    foregroundColor: layoutColor,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                      side: BorderSide(color: layoutColor, width: 2),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
                  ),
                  onPressed: () => Navigator.pop(context, false),
                  child: const Text("Cancel"),
                ),
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: layoutColor,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                      side: BorderSide(color: layoutColor, width: 2),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
                  ),
                  onPressed: () {
                    if (remarks.trim().isEmpty) return;
                    Navigator.pop(context, true);
                  },
                  child: const Text("OK"),
                ),
              ],
            ),
      );
    },
  );

  return confirmed == true ? remarks : null;
}
