import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:generp/Notifiers/commonProvider/accountsListProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';
import 'package:generp/Models/commonModels/DistrictsResponse.dart';
import 'package:generp/Models/commonModels/SubLocationsResponse.dart';

import '../../Models/commonModels/commonAddAccountsViewResponse.dart';

class Addcommonpayment extends StatefulWidget {
  const Addcommonpayment({super.key});

  @override
  State<Addcommonpayment> createState() => _AddcommonpaymentState();
}

class _AddcommonpaymentState extends State<Addcommonpayment> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var prov = Provider.of<Accountslistprovider>(context, listen: false);
      prov.addCommonAccountViewAPI(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Accountslistprovider>(
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            appBar: appbar(context, "Add Common Account"),
            backgroundColor: AppColors.white,
            body: Container(
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                   Container(
                     padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                     margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                     decoration: BoxDecoration(
                       color: Colors.white,
                         borderRadius: BorderRadius.circular(16)
                     ),
                     child: Column(
                       crossAxisAlignment: CrossAxisAlignment.start,
                       children: [
                         Text("Account Details",style: TextStyle( color: AppColors.app_blue,
                             fontSize: 16,
                             fontFamily: "JakartaMedium"),),
                         Text("Account"),
                         DropdownButtonHideUnderline(
                           child: Row(
                             children: [
                               Expanded(
                                 child: DropdownButton2<String>(
                                   isExpanded: true,
                                   hint: const Row(
                                     children: [
                                       Expanded(
                                         child: Text(
                                           'Select Account',
                                           style: TextStyle(fontSize: 14),
                                           overflow: TextOverflow.ellipsis,
                                         ),
                                       ),
                                     ],
                                   ),
                                   items:
                                   provider.accountTypes
                                       .map(
                                         (act) => DropdownMenuItem<String>(
                                       value: act,
                                       child: Text(
                                         act,
                                         style: const TextStyle(
                                           fontSize: 14,
                                         ),
                                         overflow: TextOverflow.ellipsis,
                                       ),
                                     ),
                                   )
                                       .toList(),
                                   value: provider.selectedAccountType,
                                   onChanged: (value) {
                                     if (value != null) {
                                       provider.selectedAccountType = value;
                                       print(
                                         "statusId:${provider.selectedAccountType}",
                                       );
                                     }
                                   },
                                   buttonStyleData: ButtonStyleData(
                                     height: 50,
                                     width: 160,
                                     padding: const EdgeInsets.only(
                                       left: 14,
                                       right: 14,
                                     ),
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                   ),
                                   iconStyleData: const IconStyleData(
                                     icon: Icon(Icons.keyboard_arrow_down_sharp),
                                     iconSize: 14,
                                     iconEnabledColor: Color(0xFF2D2D2D),
                                     iconDisabledColor: Colors.grey,
                                   ),
                                   dropdownStyleData: DropdownStyleData(
                                     maxHeight: 200,
                                     width: 350,
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                     scrollbarTheme: ScrollbarThemeData(
                                       radius: const Radius.circular(15),
                                       thickness: MaterialStateProperty.all<double>(
                                         6,
                                       ),
                                       thumbVisibility:
                                       MaterialStateProperty.all<bool>(true),
                                     ),
                                   ),
                                   menuItemStyleData: const MenuItemStyleData(
                                     height: 40,
                                     padding: EdgeInsets.only(left: 14, right: 14),
                                   ),
                                 ),
                               ),
                             ],
                           ),
                         ),
                         textControllerWidget(
                           context,
                           provider.nameController,
                           "Enter Name",
                               (p0) {
                             provider.checkInputsAPI(
                               context,
                               "name",
                               provider.nameController.text,
                             );
                           },
                         ),
                         textControllerWidget(
                           context,
                           provider.mobileController,
                           "Enter Mobile",
                               (p0) {
                             provider.checkInputsAPI(
                               context,
                               "mob1",
                               provider.mobileController.text,
                             );
                           },
                         ),
                         Text("State"),
                         DropdownButtonHideUnderline(
                           child: Row(
                             children: [
                               Expanded(
                                 child: DropdownButton2<States>(
                                   isExpanded: true,
                                   hint: Text(
                                     'Select State',
                                     style: TextStyle(fontSize: 14),
                                     overflow: TextOverflow.ellipsis,
                                   ),
                                   items:
                                   provider.states
                                       .map(
                                         (states) => DropdownMenuItem<States>(
                                       value: states,
                                       child: Text(
                                         states.name ?? '',
                                         style: const TextStyle(
                                           fontSize: 14,
                                         ),
                                         overflow: TextOverflow.ellipsis,
                                       ),
                                     ),
                                   )
                                       .toList(),
                                   value: provider.selectedState,
                                   onChanged: (States? value) {
                                     if (value != null) {
                                       if (provider.states.isNotEmpty) {
                                         provider.selectedState = value;
                                         print(
                                           "Selected Complaint Type: ${value.name}, ID: ${value.id}",
                                         );
                                         provider.selectedStateID = value.id!;
                                         print(
                                           "hfjkshfg" +
                                               provider.selectedStateID.toString(),
                                         );
                                         provider.getDistrictAPI(
                                           context,
                                           provider.selectedStateID,
                                         );
                                       }
                                     }
                                   },
                                   buttonStyleData: ButtonStyleData(
                                     height: 50,
                                     width: 160,
                                     padding: const EdgeInsets.only(
                                       left: 14,
                                       right: 14,
                                     ),
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                   ),
                                   iconStyleData: const IconStyleData(
                                     icon: Icon(Icons.keyboard_arrow_down),
                                     iconSize: 12,
                                     iconEnabledColor: Color(0xFF2D2D2D),
                                     iconDisabledColor: Colors.grey,
                                   ),
                                   dropdownStyleData: DropdownStyleData(
                                     maxHeight: 200,
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                     scrollbarTheme: ScrollbarThemeData(
                                       radius: const Radius.circular(15),
                                       thickness: MaterialStateProperty.all<double>(
                                         6,
                                       ),
                                       thumbVisibility:
                                       MaterialStateProperty.all<bool>(true),
                                     ),
                                   ),
                                   menuItemStyleData: const MenuItemStyleData(
                                     height: 40,
                                     padding: EdgeInsets.only(left: 14, right: 14),
                                   ),
                                 ),
                               ),
                             ],
                           ),
                         ),

                         Text("District"),
                         DropdownButtonHideUnderline(
                           child: Row(
                             children: [
                               Expanded(
                                 child: DropdownButton2<Districts>(
                                   isExpanded: true,
                                   hint: Text(
                                     'Select District',
                                     style: TextStyle(fontSize: 14),
                                     overflow: TextOverflow.ellipsis,
                                   ),
                                   items:
                                   provider.districts
                                       .map(
                                         (dist) => DropdownMenuItem<Districts>(
                                       value: dist,
                                       child: Text(
                                         dist.district ?? '',
                                         style: const TextStyle(
                                           fontSize: 14,
                                         ),
                                         overflow: TextOverflow.ellipsis,
                                       ),
                                     ),
                                   )
                                       .toList(),
                                   value: provider.selectedDistricts,
                                   onChanged: (Districts? value) {
                                     if (value != null) {
                                       if (provider.districts.isNotEmpty) {
                                         provider.selectedDistricts = value;
                                         print("Selected ID: ${value.id}");
                                         provider.selectedDistrictId = value.id!;
                                         provider.selectedDistrictValue =
                                         value.district!;
                                         print(
                                           "hfjkshfg" +
                                               provider.selectedDistrictId
                                                   .toString(),
                                         );
                                         provider.getSubLocationAPI(
                                           context,
                                           provider.selectedDistrictId,
                                         );
                                       }
                                     }
                                   },
                                   buttonStyleData: ButtonStyleData(
                                     height: 50,
                                     width: 160,
                                     padding: const EdgeInsets.only(
                                       left: 14,
                                       right: 14,
                                     ),
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                   ),
                                   iconStyleData: const IconStyleData(
                                     icon: Icon(Icons.keyboard_arrow_down),
                                     iconSize: 12,
                                     iconEnabledColor: Color(0xFF2D2D2D),
                                     iconDisabledColor: Colors.grey,
                                   ),
                                   dropdownStyleData: DropdownStyleData(
                                     maxHeight: 200,
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                     scrollbarTheme: ScrollbarThemeData(
                                       radius: const Radius.circular(15),
                                       thickness: MaterialStateProperty.all<double>(
                                         6,
                                       ),
                                       thumbVisibility:
                                       MaterialStateProperty.all<bool>(true),
                                     ),
                                   ),
                                   menuItemStyleData: const MenuItemStyleData(
                                     height: 40,
                                     padding: EdgeInsets.only(left: 14, right: 14),
                                   ),
                                 ),
                               ),
                             ],
                           ),
                         ),
                         Text("Sub Locality"),
                         DropdownButtonHideUnderline(
                           child: Row(
                             children: [
                               Expanded(
                                 child: DropdownButton2<SubLocations>(
                                   isExpanded: true,
                                   hint: Text(
                                     'Select Sub Locality',
                                     style: TextStyle(fontSize: 14),
                                     overflow: TextOverflow.ellipsis,
                                   ),
                                   items:
                                   provider.subLocations
                                       .map(
                                         (subloc) =>
                                         DropdownMenuItem<SubLocations>(
                                           value: subloc,
                                           child: Text(
                                             subloc.subLocality ?? '',
                                             style: const TextStyle(
                                               fontSize: 14,
                                             ),
                                             overflow: TextOverflow.ellipsis,
                                           ),
                                         ),
                                   )
                                       .toList(),
                                   value: provider.selectedSubLocations,
                                   onChanged: (SubLocations? value) {
                                     if (value != null) {
                                       if (provider.subLocations.isNotEmpty) {
                                         provider.selectedSubLocations = value;
                                         print("Selected ID: ${value.id}");
                                         provider.selectedSubLocID = value.id!;
                                         provider.selectedSubLocValue =
                                         value.subLocality!;
                                         print(
                                           "hfjkshfg" +
                                               provider.selectedSubLocID.toString(),
                                         );
                                       }
                                     }
                                   },
                                   buttonStyleData: ButtonStyleData(
                                     height: 50,
                                     width: 160,
                                     padding: const EdgeInsets.only(
                                       left: 14,
                                       right: 14,
                                     ),
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                   ),
                                   iconStyleData: const IconStyleData(
                                     icon: Icon(Icons.keyboard_arrow_down),
                                     iconSize: 12,
                                     iconEnabledColor: Color(0xFF2D2D2D),
                                     iconDisabledColor: Colors.grey,
                                   ),
                                   dropdownStyleData: DropdownStyleData(
                                     maxHeight: 200,
                                     decoration: BoxDecoration(
                                       borderRadius: BorderRadius.circular(14),
                                       color: AppColors.text_field_color,
                                     ),
                                     scrollbarTheme: ScrollbarThemeData(
                                       radius: const Radius.circular(15),
                                       thickness: MaterialStateProperty.all<double>(
                                         6,
                                       ),
                                       thumbVisibility:
                                       MaterialStateProperty.all<bool>(true),
                                     ),
                                   ),
                                   menuItemStyleData: const MenuItemStyleData(
                                     height: 40,
                                     padding: EdgeInsets.only(left: 14, right: 14),
                                   ),
                                 ),
                               ),
                             ],
                           ),
                         ),
                         textControllerWidget(
                           context,
                           provider.addressController,
                           "Enter Address",
                               (p0) {},
                         ),
                       ],
                     ),
                   ),
                  SizedBox(height: 15,),
                  Column(
                    children: [
                      InkResponse(
                        onTap: () => provider.isVisible = !provider.isVisible,
                        child: Center(
                          child: Text(
                              provider.isVisible?"- Hide More Details":"+ Add More Details",style: TextStyle(
                            color: AppColors.app_blue,
                            fontSize: 16,
                            fontFamily: "JakartaMedium"
                          ),
                          ),
                        ),
                      ),
                      Visibility(
                        visible: provider.isVisible,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                              padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16)
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text("Bank Details",style: TextStyle(
                                      color: AppColors.app_blue,
                                      fontSize: 16,
                                      fontFamily: "JakartaMedium"
                                  ),),
                                  textControllerWidget(
                                    context,
                                    provider.bankNameController,
                                    "Bank Name",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.branchNameController,
                                    "Bank Branch",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.bankIfscCotroller,
                                    "Bank IFSC",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.bankHolderNameController,
                                    "Bank Holder Name",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.bankAcNameController,
                                    "Bank Account Name",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.bankUpiController,
                                    "Bank UPI ID",
                                        (p0) {},
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                              padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16)
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text("Contact Details",style: TextStyle(
                                      color: AppColors.app_blue,
                                      fontSize: 16,
                                      fontFamily: "JakartaMedium"
                                  ),),
                                  textControllerWidget(
                                    context,
                                    provider.contactPersonController,
                                    "Contact Person Name",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.contectPersonDesignationController,
                                    "Contact Person Designation",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.contectPersonAltMobController,
                                    "Alternative Mobile Number",
                                        (p0) {
                                      provider.checkInputsAPI(context, "mob2", provider.contectPersonAltMobController.text);
                                    },
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.contectPersonTeleController,
                                    "Telephone Number",
                                        (p0) {},
                                  ),
                                  textControllerWidget(
                                    context,
                                    provider.contectPersonMailController,
                                    "Customer Mail ID",
                                        (p0) {},
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  )
                  ],
                ),
              ),
            ),
            floatingActionButtonLocation:
            FloatingActionButtonLocation.centerFloat,
            bottomNavigationBar: InkResponse(
              onTap: () {
                // provider.submitClicked = true;
                provider.submitCommonAccountsAPI(context);
              },
              child: Container(
                height: 45,
                alignment: Alignment.center,
                margin: EdgeInsets.symmetric(horizontal: 10,vertical: 15),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                decoration: BoxDecoration(
                  color: AppColors.app_blue,
                  borderRadius: BorderRadius.circular(15),
                ),
                child: Text(
                  "Submit",
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "JakartaMedium",
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  Widget textControllerWidget(
    context,
    controller,
    hintText,
    Function(String)? onChanged,
  ) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(bottom: 5.0, top: 8.0),
          child: Text(hintText),
        ),
        Container(
          height: hintText == "Enter Description" ? 150 : 50,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: AppColors.text_field_color,
            borderRadius: BorderRadius.circular(14),
          ),
          child: Padding(
            padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10, 0),
            child: TextFormField(
              controller: controller,
              keyboardType: TextInputType.text,
              maxLines: hintText == "Enter Description" ? 60 : 1,
              onChanged: onChanged,
              decoration: InputDecoration(
                hintText: hintText,
                hintStyle: TextStyle(
                  fontWeight: FontWeight.w400,
                  color: Color(0xFFB4BEC0),
                  fontSize: 14,
                ),

                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
