// 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 '../Basicp1_viewModel/Basicp1_view_model_screen.dart';
import 'Basicp1_update_entity_screen.dart';

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

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

  @override
  State<Basicp1DetailsScreen> createState() => _Basicp1DetailsScreenState();
}

class _Basicp1DetailsScreenState extends State<Basicp1DetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Basicp1ViewModelScreen(),
          child: Basicp1UpdateEntityScreen(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 Basicp1?'),
          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<Basicp1ViewModelScreen>(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<Basicp1ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Basicp1',
          displayFields: [
  {'key': 'name', 'label': 'name', 'type': 'text'},

  {'key': 'name2', 'label': 'name2', 'type': 'text'},

  {'key': 'number1', 'label': 'number1', 'type': 'number'},

  {'key': 'number2', 'label': 'number2', 'type': 'number'},

  {'key': 'phone_number', 'label': 'Phone Number', 'type': 'phone'},

  {'key': 'phone_number2', 'label': 'Phone Number2', 'type': 'phone'},

  {'key': 'paragraph_field', 'label': 'Paragraph Field', 'type': 'paragraph'},

  {'key': 'paragraph_field2', 'label': 'Paragraph Field2', 'type': 'paragraph'},

  {'key': 'password_field', 'label': 'Password Field', 'type': 'password'},

  {'key': 'textarea', 'label': 'Textarea', 'type': 'textarea'},

  {'key': 'textarea_field', 'label': 'Textarea Field', 'type': 'textarea'},

  {'key': 'textarea_field2', 'label': 'Textarea Field2', 'type': 'textarea'},

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