// 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 '../Rides_viewModel/Rides_view_model_screen.dart';
import 'Rides_update_entity_screen.dart';

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

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

  @override
  State<RidesDetailsScreen> createState() => _RidesDetailsScreenState();
}

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

  {'key': 'description', 'label': 'Description', 'type': 'textarea'},

  {'key': 'active', 'label': 'Active', 'type': 'toggle_switch'},

  {'key': 'pick_up', 'label': 'Pick Up', 'type': 'select'},

  {'key': 'return_to_same_location', 'label': 'return to same location', 'type': 'toggle_switch'},

  {'key': 'return_address', 'label': 'RETURN Address', 'type': 'select'},

  {'key': 'product', 'label': 'Product', 'type': 'select'},

  {'key': 'vehicle_add_on', 'label': 'Vehicle Add On', 'type': 'select'},

  {'key': 'iscompleted', 'label': 'iscompleted', 'type': 'toggle_switch'},

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