// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:base_project/BuilderField/shared/ui/entity_list.dart';
import 'Ad7_create_entity_screen.dart';
import 'Ad7_update_entity_screen.dart';
import '../Ad7_viewModel/Ad7_view_model_screen.dart';
import 'Ad7_details_screen.dart';import 'package:flutter/services.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import '../../../../theme/app_style.dart';
import '../../../../utils/size_utils.dart';
import '../../../../widgets/custom_icon_button.dart';
import '../../../../utils/image_constant.dart';
import '../../../../widgets/app_bar/appbar_image.dart';
import '../../../../widgets/app_bar/appbar_title.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../theme/app_decoration.dart';
import 'package:multi_select_flutter/multi_select_flutter.dart';
import '../../../../Reuseable/reusable_text_field.dart';
import 'package:provider/provider.dart';
import '../../../../utils/toast_messages/toast_message_util.dart';
import '../../../../BuilderField/shared/fields/realted_entity_insert_field.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Ad7_entity_list_screen extends StatefulWidget {
  static const String routeName = '/entity-list';

  @override
  _Ad7_entity_list_screenState createState() => _Ad7_entity_list_screenState();
}

class _Ad7_entity_list_screenState extends State<Ad7_entity_list_screen> {
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  void _loadData() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        final vm = Provider.of<Ad7ViewModelScreen>(context, listen: false);
        vm.getAllWithPagination(refresh: true);
      }
    });
  }

  void _navigateToCreateScreen() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Ad7ViewModelScreen(),
          child: const Ad7CreateEntityScreen(),
        ),
      ),
    ).then((_) {
      final vm = Provider.of<Ad7ViewModelScreen>(context, listen: false);
      vm.refreshData();
    });
  }

  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Ad7ViewModelScreen(),
          child: Ad7UpdateEntityScreen(entity: entity),
        ),
      ),
    ).then((_) {
      final vm = Provider.of<Ad7ViewModelScreen>(context, listen: false);
      vm.refreshData();
    });
  }

  void _navigateToDetailsScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Ad7ViewModelScreen(),
          child: Ad7DetailsScreen(entity: entity),
        ),
      ),
    );
  }

  void _showDeleteDialog(Map<String, dynamic> entity) {
 final parentContext = context; 
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Deletion'),
          content: const Text('Are you sure you want to delete this Ad7?'),
          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<Ad7ViewModelScreen>(parentContext, listen: false);
                await vm.deleteEntity(entity['id']);
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Ad7ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityList(
          entities: viewModel.filteredList,
          isLoading: viewModel.isLoading,
          errorMessage:
              viewModel.errorMessage.isNotEmpty ? viewModel.errorMessage : null,
          hasMoreData: viewModel.hasMoreData,
          searchQuery: viewModel.searchQuery,
          onSearchChanged: (query) => viewModel.searchad7(query),
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          onTap: (entity) => _navigateToDetailsScreen(entity),
          onRefresh: () => viewModel.refreshData(),
          onLoadMore: () => viewModel.getAllWithPagination(),
          title: 'Ad7',
          onAddNew: _navigateToCreateScreen,


onInsertAction: () async {
            final id = await RelatedEntityInsertField.showInsertDialog(
              context: context,
              relationTitle: 'Basicp3',
              fieldSchema: const [

 

                {
                  'type': 'text',
                  'label': 'Toggle_Switch2',
                  'hint': 'Enter Toggle_Switch2',
                  'path': 'toggle_switch2',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Decimal_Field2',
                  'hint': 'Enter Decimal_Field2',
                  'path': 'decimal_field2',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Documentsequenc',
                  'hint': 'Enter Documentsequenc',
                  'path': 'documentsequenc',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Toggle_Switch',
                  'hint': 'Enter Toggle_Switch',
                  'path': 'toggle_switch',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Recaptcha',
                  'hint': 'Enter Recaptcha',
                  'path': 'recaptcha',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Percentage_Field',
                  'hint': 'Enter Percentage_Field',
                  'path': 'percentage_field',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Percentage_Field2',
                  'hint': 'Enter Percentage_Field2',
                  'path': 'percentage_field2',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Url_Field',
                  'hint': 'Enter Url_Field',
                  'path': 'url_field',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Url_Field2',
                  'hint': 'Enter Url_Field2',
                  'path': 'url_field2',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Decimal_Field',
                  'hint': 'Enter Decimal_Field',
                  'path': 'decimal_field',
                  'required': false
                },
 

 

                {
                  'type': 'text',
                  'label': 'Recaptcha2',
                  'hint': 'Enter Recaptcha2',
                  'path': 'recaptcha2',
                  'required': false
                },
 

               
              ],
              onCreate: (data) =>
                  Provider.of<Ad7ViewModelScreen>(context, listen: false)
                      .insertbasicp3(data),
              valueKey: 'id',
              displayKey: 'name',
            );
            if (id != null && id.trim().isNotEmpty) {
              ToastMessageUtil.showToast(
                message: 'Basicp3 inserted (id: $id)',
                toastType: ToastType.success,
              );
            }
          },
          insertActionLabel: 'Basicp3',

          displayFields: [
  {'key': 'name', 'label': 'Name', 'type': 'text'},



          ],
        );
      },
    );
  }
}
