import React, { useState, useEffect } from 'react';
import { Head, router, Link } from '@inertiajs/react';
import AdminLayout from '@/Layouts/AdminLayout';
import { 
    Search, 
    Filter, 
    Download, 
    Users, 
    Mail, 
    MapPin, 
    Calendar,
    CheckCircle,
    Clock,
    XCircle,
    ExternalLink,
    MoreVertical,
    BadgeDollarSign,
    ShieldAlert,
    TrendingUp,
    AlertOctagon,
    Award,
    Edit2,
    Trash2,
    Check,
    Phone,
    ShieldCheck,
    Building
} from 'lucide-react';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuLabel,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
    Dialog,
    DialogContent,
    DialogHeader,
    DialogTitle,
} from "@/components/ui/dialog";
import { Badge } from '@/components/ui/badge';

export default function CharitiesIndex({ sponsors, filters = {}, nationalities = [], stats = {} }) {
    const [searchTerm, setSearchTerm] = useState(filters.search || '');
    const [statusFilter, setStatusFilter] = useState(filters.status || 'All');
    const [verificationFilter, setVerificationFilter] = useState(filters.verification || 'All');
    const [licenseFilter, setLicenseFilter] = useState(filters.license || 'All');
    const [nationalityFilter, setNationalityFilter] = useState(filters.nationality || 'All');
    const [sortField, setSortField] = useState(filters.sort_field || 'created_at');
    const [sortOrder, setSortOrder] = useState(filters.sort_order || 'desc');
    
    const [selectedSponsor, setSelectedSponsor] = useState(null);
    const [isDetailsOpen, setIsDetailsOpen] = useState(false);
    const [isEditOpen, setIsEditOpen] = useState(false);
    const [editForm, setEditForm] = useState({
        id: '',
        full_name: '',
        organization_name: '',
        email: '',
        mobile: '',
        city: '',
        nationality: '',
        address: ''
    });

    // Handle search/filter queries
    const triggerSearch = (searchVal, statusVal, verificationVal, licenseVal, nationalityVal, sortF, sortO) => {
        router.get('/admin/charity-organizations', {
            search: searchVal,
            status: statusVal,
            verification: verificationVal,
            license: licenseVal,
            nationality: nationalityVal,
            sort_field: sortF,
            sort_order: sortO
        }, {
            preserveState: true,
            preserveScroll: true,
            replace: true
        });
    };

    useEffect(() => {
        const delayDebounceFn = setTimeout(() => {
            if (searchTerm !== (filters.search || '')) {
                triggerSearch(searchTerm, statusFilter, verificationFilter, licenseFilter, nationalityFilter, sortField, sortOrder);
            }
        }, 500);

        return () => clearTimeout(delayDebounceFn);
    }, [searchTerm]);

    const handleFilterChange = (status, verification, license, nationality) => {
        setStatusFilter(status);
        setVerificationFilter(verification);
        setLicenseFilter(license);
        setNationalityFilter(nationality);
        triggerSearch(searchTerm, status, verification, license, nationality, sortField, sortOrder);
    };

    const handleSort = (field) => {
        const order = sortField === field && sortOrder === 'desc' ? 'asc' : 'desc';
        setSortField(field);
        setSortOrder(order);
        triggerSearch(searchTerm, statusFilter, verificationFilter, licenseFilter, nationalityFilter, field, order);
    };

    const handleApprove = (id) => {
        router.post(`/admin/charity-organizations/${id}/verify`, {}, {
            onSuccess: () => {
                setIsDetailsOpen(false);
            }
        });
    };

    const handleSuspend = (id) => {
        router.post(`/admin/charity-organizations/${id}/suspend`, {}, {
            onSuccess: () => {
                setIsDetailsOpen(false);
            }
        });
    };

    const handleActivate = (id) => {
        router.post(`/admin/charity-organizations/${id}/activate`, {}, {
            onSuccess: () => {
                setIsDetailsOpen(false);
            }
        });
    };

    const handleDelete = (id) => {
        if (confirm('Are you absolutely sure you want to permanently delete this charity organization?')) {
            router.delete(`/admin/charity-organizations/${id}`);
        }
    };

    const handleEditClick = (sponsor) => {
        setEditForm({
            id: sponsor.id,
            full_name: sponsor.full_name,
            organization_name: sponsor.organization_name || '',
            email: sponsor.email,
            mobile: sponsor.mobile,
            city: sponsor.city || 'Dubai',
            nationality: sponsor.nationality || '',
            address: sponsor.address || ''
        });
        setIsEditOpen(true);
    };

    const handleEditSubmit = (e) => {
        e.preventDefault();
        router.post(`/admin/charity-organizations/${editForm.id}/update`, editForm, {
            onSuccess: () => {
                setIsEditOpen(false);
            }
        });
    };

    const handleExport = () => {
        const params = new URLSearchParams({
            search: searchTerm,
            status: statusFilter,
            verification: verificationFilter,
            license: licenseFilter,
            nationality: nationalityFilter,
            sort_field: sortField,
            sort_order: sortOrder
        });
        window.location.href = `/admin/charity-organizations/export?${params.toString()}`;
    };

    const viewDetails = (sponsor) => {
        setSelectedSponsor(sponsor);
        setIsDetailsOpen(true);
    };

    return (
        <AdminLayout title="Charity Organizations">
            <Head title="Charity Organizations" />

            <div className="space-y-6 font-sans">
                {/* Statistics Ribbons */}
                <div className="grid grid-cols-1 md:grid-cols-3 gap-5">
                    <div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm flex items-center justify-between">
                        <div>
                            <span className="text-[10px] font-black text-slate-400 uppercase tracking-widest block mb-1">Total Charities</span>
                            <h3 className="text-2xl font-black text-slate-900">{stats.total || 0}</h3>
                        </div>
                        <div className="p-3 bg-teal-50 rounded-xl">
                            <Building className="w-6 h-6 text-teal-600" />
                        </div>
                    </div>
                    <div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm flex items-center justify-between">
                        <div>
                            <span className="text-[10px] font-black text-slate-400 uppercase tracking-widest block mb-1">Verified</span>
                            <h3 className="text-2xl font-black text-emerald-600">
                                {stats.verified || 0}
                            </h3>
                        </div>
                        <div className="p-3 bg-emerald-50 rounded-xl">
                            <CheckCircle className="w-6 h-6 text-emerald-600" />
                        </div>
                    </div>
                    <div className="bg-white p-5 rounded-2xl border border-slate-200 shadow-sm flex items-center justify-between">
                        <div>
                            <span className="text-[10px] font-black text-slate-400 uppercase tracking-widest block mb-1">Suspended</span>
                            <h3 className="text-2xl font-black text-rose-600">
                                {stats.suspended || 0}
                            </h3>
                        </div>
                        <div className="p-3 bg-rose-50 rounded-xl">
                            <ShieldAlert className="w-6 h-6 text-rose-600" />
                        </div>
                    </div>
                </div>

                {/* Search and Filters */}
                <div className="flex flex-col lg:flex-row lg:items-center justify-between gap-4">
                    <div className="flex flex-1 flex-col sm:flex-row sm:items-center gap-3">
                        <div className="relative flex-1 max-w-md">
                            <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate-400" />
                            <input 
                                type="text" 
                                placeholder="Search by name, organization, or email..." 
                                className="w-full pl-10 pr-4 py-2.5 bg-white border border-slate-200 rounded-xl text-sm font-medium focus:ring-4 focus:ring-teal-500/10 outline-none transition-all"
                                value={searchTerm}
                                onChange={e => setSearchTerm(e.target.value)}
                            />
                        </div>
                        
                        <div className="flex flex-wrap items-center gap-2">
                            <select 
                                className="px-4 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-600 outline-none focus:ring-2 focus:ring-teal-500/10 transition-all cursor-pointer"
                                value={statusFilter}
                                onChange={e => handleFilterChange(e.target.value, verificationFilter, licenseFilter, nationalityFilter)}
                            >
                                <option value="All">All Statuses</option>
                                <option value="Active">Active</option>
                                <option value="Suspended">Suspended</option>
                            </select>

                            <select 
                                className="px-4 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-600 outline-none focus:ring-2 focus:ring-teal-500/10 transition-all cursor-pointer"
                                value={verificationFilter}
                                onChange={e => handleFilterChange(statusFilter, e.target.value, licenseFilter, nationalityFilter)}
                            >
                                <option value="All">All Verification</option>
                                <option value="Verified">Verified</option>
                                <option value="Unverified">Unverified</option>
                            </select>

                            <select 
                                className="px-4 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-600 outline-none focus:ring-2 focus:ring-teal-500/10 transition-all cursor-pointer"
                                value={licenseFilter}
                                onChange={e => handleFilterChange(statusFilter, verificationFilter, e.target.value, nationalityFilter)}
                            >
                                <option value="All">All License Status</option>
                                <option value="Valid">Valid</option>
                                <option value="Expired">Expired</option>
                                <option value="Expiring Soon">Expiring Soon</option>
                            </select>

                            <select 
                                className="px-4 py-2.5 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-600 outline-none focus:ring-2 focus:ring-teal-500/10 transition-all cursor-pointer"
                                value={nationalityFilter}
                                onChange={e => handleFilterChange(statusFilter, verificationFilter, licenseFilter, e.target.value)}
                            >
                                <option value="All">All Nationalities</option>
                                {nationalities.map(nat => (
                                    <option key={nat} value={nat}>{nat}</option>
                                ))}
                            </select>
                        </div>
                    </div>
                    <div className="flex items-center space-x-2 flex-shrink-0">
                        <button
                            onClick={handleExport}
                            className="px-4 py-2.5 bg-[#0F6E56] text-white rounded-xl text-xs font-black uppercase tracking-wider flex items-center gap-2 hover:bg-[#0c5945] transition-all shadow-md shadow-teal-900/10 cursor-pointer"
                        >
                            <Download className="w-4 h-4" />
                            <span>Export Charities</span>
                        </button>
                    </div>
                </div>

                {/* Table */}
                <div className="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden">
                    <Table>
                        <TableHeader>
                            <TableRow className="bg-slate-50/50 hover:bg-slate-50/50">
                                <TableHead onClick={() => handleSort('organization_name')} className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14 pl-8 cursor-pointer select-none">
                                    Organization {sortField === 'organization_name' && (sortOrder === 'desc' ? '↓' : '↑')}
                                </TableHead>
                                <TableHead onClick={() => handleSort('full_name')} className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14 cursor-pointer select-none">
                                    Representative {sortField === 'full_name' && (sortOrder === 'desc' ? '↓' : '↑')}
                                </TableHead>
                                <TableHead onClick={() => handleSort('city')} className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14 cursor-pointer select-none">
                                    Location {sortField === 'city' && (sortOrder === 'desc' ? '↓' : '↑')}
                                </TableHead>
                                <TableHead className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14">Verification</TableHead>
                                <TableHead className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14">License Expiry</TableHead>
                                <TableHead className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14">Status</TableHead>
                                <TableHead className="font-bold text-slate-500 text-[10px] uppercase tracking-widest h-14 text-right pr-8">Action</TableHead>
                            </TableRow>
                        </TableHeader>
                        <TableBody>
                            {sponsors.data.length > 0 ? (
                                sponsors.data.map((sponsor) => (
                                    <TableRow key={sponsor.id} className="hover:bg-slate-50/50 transition-colors">
                                        <TableCell className="py-5 pl-8 font-bold text-slate-900">
                                            <div className="flex items-center space-x-3">
                                                <div className="p-2.5 bg-teal-50 rounded-lg">
                                                    <Building className="w-4 h-4 text-teal-600" />
                                                </div>
                                                <div>
                                                    <span className="text-sm block">{sponsor.organization_name || 'N/A'}</span>
                                                    <span className="text-[10px] text-slate-400 font-bold uppercase tracking-wider">ID: ORG-{sponsor.id}</span>
                                                </div>
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <div className="text-xs font-bold text-slate-700">
                                                <p>{sponsor.full_name}</p>
                                                <p className="text-[10px] text-slate-400">{sponsor.email}</p>
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <div className="flex items-center space-x-2 text-slate-600">
                                                <MapPin className="w-3.5 h-3.5 text-slate-400" />
                                                <span className="text-xs font-bold">{sponsor.city || 'Dubai'}</span>
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <span className={`px-2 py-0.5 rounded-lg text-[10px] font-black uppercase tracking-tight ${
                                                sponsor.is_verified ? 'bg-emerald-50 text-emerald-600 border border-emerald-100' : 'bg-amber-50 text-amber-600 border border-amber-100'
                                            }`}>
                                                {sponsor.is_verified ? 'Verified' : 'Pending'}
                                            </span>
                                        </TableCell>
                                        <TableCell>
                                            <div className="text-xs font-bold text-slate-700">
                                                {sponsor.license_expiry ? new Date(sponsor.license_expiry).toLocaleDateString() : 'N/A'}
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <div className="flex items-center space-x-2">
                                                <span className={`text-[10px] font-black uppercase tracking-widest ${
                                                    sponsor.status === 'active' ? 'text-emerald-600' : 
                                                    sponsor.status === 'inactive' ? 'text-amber-600' : 'text-rose-600'
                                                }`}>
                                                    {sponsor.status}
                                                </span>
                                            </div>
                                        </TableCell>
                                        <TableCell className="text-right pr-8">
                                            <div className="flex items-center justify-end space-x-1">
                                                <button 
                                                    onClick={() => viewDetails(sponsor)}
                                                    className="p-2 hover:bg-slate-100 rounded-lg text-[#0F6E56] font-bold"
                                                >
                                                    <ExternalLink className="w-4.5 h-4.5" />
                                                </button>
                                                <DropdownMenu>
                                                    <DropdownMenuTrigger asChild>
                                                        <button className="p-2 text-slate-400 hover:text-slate-900 hover:bg-slate-50 rounded-lg transition-all outline-none">
                                                            <MoreVertical className="w-5 h-5" />
                                                        </button>
                                                    </DropdownMenuTrigger>
                                                    <DropdownMenuContent align="end" className="w-48 p-2 rounded-xl">
                                                        <DropdownMenuLabel className="text-[9px] font-black text-slate-400 uppercase tracking-widest px-2 py-2">Quick Actions</DropdownMenuLabel>
                                                        <DropdownMenuSeparator />
                                                        <DropdownMenuItem 
                                                            className="p-3 rounded-lg focus:bg-blue-50 cursor-pointer text-blue-600 font-bold" 
                                                            onClick={() => handleEditClick(sponsor)}
                                                        >
                                                            <div className="flex items-center space-x-3">
                                                                 <Edit2 className="w-4 h-4" />
                                                                 <span className="text-xs font-bold">Edit Details</span>
                                                            </div>
                                                        </DropdownMenuItem>
                                                        {sponsor.status === 'active' ? (
                                                            <DropdownMenuItem 
                                                                onClick={() => handleSuspend(sponsor.id)}
                                                                className="p-3 rounded-lg focus:bg-rose-50 cursor-pointer text-rose-600 font-bold"
                                                            >
                                                                <div className="flex items-center space-x-3">
                                                                    <XCircle className="w-4 h-4" />
                                                                    <span className="text-xs font-bold">Suspend Org</span>
                                                                </div>
                                                            </DropdownMenuItem>
                                                        ) : (
                                                            <DropdownMenuItem 
                                                                onClick={() => handleActivate(sponsor.id)}
                                                                className="p-3 rounded-lg focus:bg-emerald-50 cursor-pointer text-emerald-600 font-bold"
                                                            >
                                                                <div className="flex items-center space-x-3">
                                                                    <CheckCircle className="w-4 h-4" />
                                                                    <span className="text-xs font-bold">Activate Org</span>
                                                                </div>
                                                            </DropdownMenuItem>
                                                        )}
                                                        <DropdownMenuSeparator />
                                                        <DropdownMenuItem 
                                                            onClick={() => handleDelete(sponsor.id)}
                                                            className="p-3 rounded-lg focus:bg-red-50 cursor-pointer text-red-600 font-bold"
                                                        >
                                                            <div className="flex items-center space-x-3">
                                                                <Trash2 className="w-4 h-4" />
                                                                <span className="text-xs font-bold">Delete Org</span>
                                                            </div>
                                                        </DropdownMenuItem>
                                                    </DropdownMenuContent>
                                                </DropdownMenu>
                                            </div>
                                        </TableCell>
                                    </TableRow>
                                ))
                            ) : (
                                <TableRow>
                                    <TableCell colSpan={7} className="py-12 text-center text-slate-400 font-semibold text-sm">
                                        No charity organizations found.
                                    </TableCell>
                                </TableRow>
                            )}
                        </TableBody>
                    </Table>

                    {/* Pagination */}
                    <div className="p-5 border-t border-slate-100 flex items-center justify-between bg-slate-50/50">
                        <span className="text-xs text-slate-500 font-bold">
                            {sponsors.total > 0 ? (
                                `Showing ${sponsors.from || 0} to ${sponsors.to || 0} of ${sponsors.total} organizations`
                            ) : (
                                'No charity organizations found'
                            )}
                        </span>
                        {sponsors.last_page > 1 && (
                            <div className="flex items-center space-x-2">
                                <Link 
                                    href={sponsors.prev_page_url || '#'} 
                                    only={['sponsors']}
                                    preserveScroll
                                    className={`px-4 py-2 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-700 shadow-xs hover:bg-slate-50 transition-colors ${
                                        !sponsors.prev_page_url && 'opacity-50 pointer-events-none'
                                    }`}
                                >
                                    Previous
                                </Link>
                                <Link 
                                    href={sponsors.next_page_url || '#'} 
                                    only={['sponsors']}
                                    preserveScroll
                                    className={`px-4 py-2 bg-white border border-slate-200 rounded-xl text-xs font-bold text-slate-700 shadow-xs hover:bg-slate-50 transition-colors ${
                                        !sponsors.next_page_url && 'opacity-50 pointer-events-none'
                                    }`}
                                >
                                    Next
                                </Link>
                            </div>
                        )}
                    </div>
                </div>
            </div>

            {/* Details Modal */}
            <Dialog open={isDetailsOpen} onOpenChange={setIsDetailsOpen}>
                <DialogContent className="sm:max-w-2xl w-full bg-white p-0 overflow-hidden rounded-[24px] max-h-[85vh] flex flex-col">
                    <div className="bg-gradient-to-r from-[#0F6E56] to-[#085041] p-6 text-white flex-shrink-0">
                        <div className="flex items-center space-x-4">
                            <div className="w-14 h-14 rounded-2xl bg-white/10 backdrop-blur-md flex items-center justify-center border border-white/20 font-black text-xl">
                                {selectedSponsor?.organization_name?.charAt(0) || 'C'}
                            </div>
                            <div>
                                <h2 className="text-xl font-black tracking-tight">{selectedSponsor?.organization_name || 'Charity Details'}</h2>
                                <p className="text-teal-100/80 text-xs font-semibold mt-1">📍 {selectedSponsor?.city || 'Dubai'} • Registered {selectedSponsor?.created_at ? new Date(selectedSponsor.created_at).toLocaleDateString() : ''}</p>
                            </div>
                        </div>
                    </div>

                    <div className="p-6 space-y-6 overflow-y-auto flex-1 bg-slate-50/50">
                        <div className="bg-white p-5 border border-slate-200 rounded-2xl shadow-sm space-y-4">
                            <h3 className="text-xs font-black text-slate-700 uppercase tracking-widest border-b border-slate-100 pb-2">Organization Details</h3>
                            <div className="grid grid-cols-2 gap-4 text-xs font-bold text-slate-700">
                                <div><span className="text-slate-400 font-medium block">Representative</span>{selectedSponsor?.full_name}</div>
                                <div><span className="text-slate-400 font-medium block">Email Address</span>{selectedSponsor?.email}</div>
                                <div><span className="text-slate-400 font-medium block">Mobile Number</span>{selectedSponsor?.mobile}</div>
                                <div><span className="text-slate-400 font-medium block">Nationality</span>{selectedSponsor?.nationality || 'N/A'}</div>
                                <div><span className="text-slate-400 font-medium block">City / Location</span>{selectedSponsor?.city || 'N/A'}</div>
                                <div><span className="text-slate-400 font-medium block">License Expiry</span>{selectedSponsor?.license_expiry ? new Date(selectedSponsor.license_expiry).toLocaleDateString() : 'N/A'}</div>
                                <div><span className="text-slate-400 font-medium block">Status</span><span className="capitalize">{selectedSponsor?.status}</span></div>
                                <div><span className="text-slate-400 font-medium block">Verification</span>{selectedSponsor?.is_verified ? 'Verified' : 'Pending Verification'}</div>
                                {selectedSponsor?.address && <div className="col-span-2"><span className="text-slate-400 font-medium block">Physical Address</span>{selectedSponsor?.address}</div>}
                            </div>
                        </div>

                        <div className="bg-white p-5 border border-slate-200 rounded-2xl shadow-sm space-y-4">
                            <h3 className="text-xs font-black text-slate-700 uppercase tracking-widest border-b border-slate-100 pb-2">Uploaded Documents</h3>
                            <div className="grid grid-cols-2 gap-4 text-xs font-bold text-slate-700">
                                {selectedSponsor?.license_file ? (
                                    <div>
                                        <span className="text-slate-400 font-medium block mb-1">Trade License</span>
                                        <a href={`/${selectedSponsor.license_file}`} target="_blank" rel="noopener noreferrer" className="inline-flex items-center gap-1 text-teal-600 hover:text-teal-700 hover:underline">
                                            View Trade License <ExternalLink className="w-3 h-3" />
                                        </a>
                                    </div>
                                ) : (
                                    <div>
                                        <span className="text-slate-400 font-medium block mb-1">Trade License</span>
                                        <span className="text-slate-400 font-medium">Not Uploaded</span>
                                    </div>
                                )}

                                {selectedSponsor?.emirates_id_file ? (
                                    <div>
                                        <span className="text-slate-400 font-medium block mb-1">Emirates ID Document</span>
                                        <a href={`/${selectedSponsor.emirates_id_file}`} target="_blank" rel="noopener noreferrer" className="inline-flex items-center gap-1 text-teal-600 hover:text-teal-700 hover:underline">
                                            View Emirates ID <ExternalLink className="w-3 h-3" />
                                        </a>
                                    </div>
                                ) : (
                                    <div>
                                        <span className="text-slate-400 font-medium block mb-1">Emirates ID Document</span>
                                        <span className="text-slate-400 font-medium">Not Uploaded</span>
                                    </div>
                                )}
                            </div>
                        </div>
                    </div>

                    <div className="p-6 border-t border-slate-100 bg-slate-50 flex items-center justify-end gap-3 flex-shrink-0">
                        {selectedSponsor && !selectedSponsor.is_verified && (
                            <button
                                onClick={() => handleApprove(selectedSponsor.id)}
                                className="px-5 py-2.5 bg-emerald-600 text-white rounded-xl text-[10px] font-black uppercase tracking-widest hover:bg-emerald-700 transition-colors flex items-center gap-1.5 cursor-pointer"
                            >
                                <CheckCircle className="w-3.5 h-3.5" />
                                Verify & Activate
                            </button>
                        )}
                        {selectedSponsor && selectedSponsor.is_verified && selectedSponsor.status === 'active' && (
                            <button
                                onClick={() => handleSuspend(selectedSponsor.id)}
                                className="px-5 py-2.5 bg-rose-600 text-white rounded-xl text-[10px] font-black uppercase tracking-widest hover:bg-rose-700 transition-colors flex items-center gap-1.5 cursor-pointer"
                            >
                                <XCircle className="w-3.5 h-3.5" />
                                Suspend Organization
                            </button>
                        )}
                        {selectedSponsor && selectedSponsor.is_verified && selectedSponsor.status === 'suspended' && (
                            <button
                                onClick={() => handleActivate(selectedSponsor.id)}
                                className="px-5 py-2.5 bg-emerald-600 text-white rounded-xl text-[10px] font-black uppercase tracking-widest hover:bg-emerald-700 transition-colors flex items-center gap-1.5 cursor-pointer"
                            >
                                <CheckCircle className="w-3.5 h-3.5" />
                                Activate Organization
                            </button>
                        )}
                        <button 
                            onClick={() => setIsDetailsOpen(false)}
                            className="px-5 py-2.5 bg-slate-200 text-slate-700 rounded-xl text-[10px] font-black uppercase tracking-widest hover:bg-slate-300 transition-colors cursor-pointer"
                        >
                            Close
                        </button>
                    </div>
                </DialogContent>
            </Dialog>

            {/* Edit Dialog */}
            <Dialog open={isEditOpen} onOpenChange={setIsEditOpen}>
                <DialogContent className="sm:max-w-xl w-full bg-white p-6 rounded-[24px]">
                    <DialogHeader>
                        <DialogTitle className="text-lg font-black text-slate-800 uppercase tracking-wide">Edit Charity Details</DialogTitle>
                    </DialogHeader>
                    <form onSubmit={handleEditSubmit} className="space-y-4 mt-4">
                        <div className="space-y-1">
                            <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Organization Name</label>
                            <input 
                                type="text"
                                className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                value={editForm.organization_name}
                                onChange={e => setEditForm({ ...editForm, organization_name: e.target.value })}
                                required
                            />
                        </div>
                        <div className="space-y-1">
                            <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Representative Full Name</label>
                            <input 
                                type="text"
                                className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                value={editForm.full_name}
                                onChange={e => setEditForm({ ...editForm, full_name: e.target.value })}
                                required
                            />
                        </div>
                        <div className="grid grid-cols-2 gap-4">
                            <div className="space-y-1">
                                <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Email Address</label>
                                <input 
                                    type="email"
                                    className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                    value={editForm.email}
                                    onChange={e => setEditForm({ ...editForm, email: e.target.value })}
                                    required
                                />
                            </div>
                            <div className="space-y-1">
                                <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Mobile Number</label>
                                <input 
                                    type="text"
                                    className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                    value={editForm.mobile}
                                    onChange={e => setEditForm({ ...editForm, mobile: e.target.value })}
                                    required
                                />
                            </div>
                        </div>
                        <div className="grid grid-cols-2 gap-4">
                            <div className="space-y-1">
                                <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">City</label>
                                <select
                                    className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                    value={editForm.city}
                                    onChange={e => setEditForm({ ...editForm, city: e.target.value })}
                                >
                                    <option value="Dubai">Dubai</option>
                                    <option value="Abu Dhabi">Abu Dhabi</option>
                                    <option value="Sharjah">Sharjah</option>
                                </select>
                            </div>
                            <div className="space-y-1">
                                <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Nationality</label>
                                <input 
                                    type="text"
                                    className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                    value={editForm.nationality}
                                    onChange={e => setEditForm({ ...editForm, nationality: e.target.value })}
                                />
                            </div>
                        </div>
                        <div className="space-y-1">
                            <label className="text-[10px] font-black text-slate-400 uppercase tracking-widest">Address</label>
                            <input 
                                type="text"
                                className="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm font-semibold focus:ring-4 focus:ring-teal-500/10 outline-none"
                                value={editForm.address}
                                onChange={e => setEditForm({ ...editForm, address: e.target.value })}
                            />
                        </div>

                        <div className="flex items-center justify-end space-x-3 pt-4 border-t border-slate-100">
                            <button 
                                type="button" 
                                onClick={() => setIsEditOpen(false)}
                                className="px-4 py-2 bg-slate-100 text-slate-600 rounded-xl text-xs font-bold hover:bg-slate-200"
                            >
                                Cancel
                            </button>
                             <button 
                                type="submit" 
                                className="px-5 py-2.5 bg-[#0F6E56] text-white rounded-xl text-xs font-black uppercase tracking-wider"
                            >
                                Save Changes
                            </button>
                        </div>
                    </form>
                </DialogContent>
            </Dialog>
        </AdminLayout>
    );
}
