// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../../BuilderField/shared/ui/entity_details.dart';
import '../Adv1_viewModel/Adv1_view_model_screen.dart';
import 'Adv1_update_entity_screen.dart';

class Adv1DetailsScreen extends StatefulWidget {
  final Map<String, dynamic> entity;

  const Adv1DetailsScreen({
    super.key,
    required this.entity,
  });

  @override
  State<Adv1DetailsScreen> createState() => _Adv1DetailsScreenState();
}

class _Adv1DetailsScreenState extends State<Adv1DetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Adv1ViewModelScreen(),
          child: Adv1UpdateEntityScreen(entity: entity),
        ),
      ),
    ).then((_) {
      // Refresh the details screen with updated data
      setState(() {});
    });
  }

  void _showDeleteDialog(Map<String, dynamic> entity) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Deletion'),
          content: const Text('Are you sure you want to delete this Adv1?'),
          actions: [
            TextButton(
              child: const Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('Delete'),
              onPressed: () async {
                Navigator.of(context).pop();
                final vm =
                    Provider.of<Adv1ViewModelScreen>(context, listen: false);
                final success = await vm.deleteEntity(entity['id']);
                if (success && mounted) {
                  Navigator.pop(context); // Go back to list
                }
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Adv1ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Adv1',
          displayFields: [
  {'key': 'ismale', 'label': 'ismale', 'type': 'radio'},

  {'key': 'idfemale', 'label': 'idfemale', 'type': 'radio'},

 
  
 {'key': 'test1', 'label': 'test1', 'type': 'checkbox'},
 

  
 {'key': 'test2', 'label': 'test2', 'type': 'checkbox'},
 


 
  
 {'key': 't1', 'label': 't1', 'type': 'checkbox'},
 

  
 {'key': 't2', 'label': 't2', 'type': 'checkbox'},
 

  
 {'key': 't4', 'label': 't4', 'type': 'checkbox'},
 


















          ],
          isLoading: viewModel.isLoading,
        );
      },
    );
  }
}
