// 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 '../Basicp2_viewModel/Basicp2_view_model_screen.dart';
import 'Basicp2_update_entity_screen.dart';

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

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

  @override
  State<Basicp2DetailsScreen> createState() => _Basicp2DetailsScreenState();
}

class _Basicp2DetailsScreenState extends State<Basicp2DetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Basicp2ViewModelScreen(),
          child: Basicp2UpdateEntityScreen(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 Basicp2?'),
          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<Basicp2ViewModelScreen>(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<Basicp2ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Basicp2',
          displayFields: [
  {'key': 'about', 'label': 'about', 'type': 'textarea'},

  {'key': 'textarea2', 'label': 'Textarea2', 'type': 'textarea'},

  {'key': 'date_field', 'label': 'Date Field', 'type': 'date'},

  {'key': 'date_field2', 'label': 'Date Field2', 'type': 'date'},

  {'key': 'datetime_field', 'label': 'Datetime Field', 'type': 'datetime'},

  {'key': 'datetime_field2', 'label': 'Datetime Field2', 'type': 'datetime'},

  {'key': 'email_field', 'label': 'Email Field', 'type': 'email'},

  {'key': 'email_field2', 'label': 'Email Field2', 'type': 'email'},

  {'key': 'userid_field', 'label': 'UserId Field', 'type': 'userid'},

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