import 'package:cheminova/models/get_pd_response.dart';
import 'package:cheminova/provider/collect_kyc_provider.dart';
import 'package:csc_picker/csc_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import '../constants/only_uppercase.dart';
import '../widgets/common_elevated_button.dart';
import '../widgets/common_text_form_field.dart';
import '../widgets/no_space_formatter.dart';

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

  @override
  State<RetailerDetailsScreen> createState() => RetailerDetailsScreenState();
}

class RetailerDetailsScreenState extends State<RetailerDetailsScreen> {
  @override
  Widget build(BuildContext context) {
    return Consumer<CollectKycProvider>(
        builder: (BuildContext context, CollectKycProvider value,
            Widget? child) =>
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: SingleChildScrollView(
                physics: const BouncingScrollPhysics(),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    const SizedBox(height: 16),
                    // Container for the retailer details form
                    Container(
                      padding: const EdgeInsets.all(20.0).copyWith(top: 30, bottom: 30),
                      decoration: BoxDecoration(
                          border: Border.all(color: Colors.white),
                          color: const Color(0xffB4D1E5).withOpacity(0.9),
                          borderRadius: BorderRadius.circular(26.0)),
                      child: Form(
                        key: value.retailerDetailsFormKey, // Key to manage form state
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Column(
                              children: [
                                const SizedBox(height: 15),
                                // Text field for Trade Name
                                CommonTextFormField(
                                    title: 'Trade Name',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Trade Name cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.tradeNameController),
                                const SizedBox(height: 15),
                                // Text field for Name
                                CommonTextFormField(
                                    title: 'Name',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Name cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.nameController),
                                const SizedBox(height: 15),
                                // Text field for Address
                                CommonTextFormField(
                                    title: 'Address',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Address cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.addressController),
                                const SizedBox(height: 15),
                                // Country, State, and City Picker
                                CSCPicker(
                                  defaultCountry: CscCountry.India,
                                  disableCountry: true,
                                  onCountryChanged: (val) {
                                    setState(() {
                                      value.country.text = val; // Update country
                                    });
                                  },
                                  onStateChanged: (val) {
                                    setState(() {
                                      value.state.text = val ?? ''; // Update state
                                    });
                                  },
                                  onCityChanged: (val) {
                                    setState(() {
                                      value.city.text = val ?? ''; // Update city
                                    });
                                  },
                                ),
                                const SizedBox(height: 15),
                                // Text field for District
                                CommonTextFormField(
                                    title: 'District',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'District cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.districtController),
                                const SizedBox(height: 15),
                                // Text field for Pincode
                                CommonTextFormField(
                                    maxLength: 6,
                                    title: 'Pincode',
                                    fillColor: Colors.white,
                                    inputFormatters: [
                                      FilteringTextInputFormatter.digitsOnly // Only digits allowed
                                    ],
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Pincode cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.pinCodeController),
                                const SizedBox(height: 15),
                                // Text field for Mobile Number
                                CommonTextFormField(
                                    maxLength: 10,
                                    title: 'Mobile Number',
                                    fillColor: Colors.white,
                                    inputFormatters: [
                                      FilteringTextInputFormatter.digitsOnly // Only digits allowed
                                    ],
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Mobile Number cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.mobileNumberController),
                                const SizedBox(height: 15),
                                // Text field for Email
                                CommonTextFormField(
                                    title: 'Email',
                                    fillColor: Colors.white,
                                    inputFormatters: [
                                      NoSpaceFormatter(), // No spaces allowed
                                    ],
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Email cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.emailController),
                                const SizedBox(height: 15),
                                // Text field for Aadhar Number
                                CommonTextFormField(
                                    maxLength: 12,
                                    title: 'Aadhar Number',
                                    inputFormatters: [
                                      FilteringTextInputFormatter.digitsOnly // Only digits allowed
                                    ],
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'Aadhar Number cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.aadharNumberController),
                                const SizedBox(height: 15),
                                // Text field for PAN Number
                                CommonTextFormField(
                                    inputFormatters: [
                                      UpperCaseTextFormatter(), // Convert to uppercase
                                    ],
                                    maxLength: 10,
                                    title: 'PAN Number',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'PAN Number cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.panNumberController),
                                const SizedBox(height: 15),
                                // Text field for GST Number
                                CommonTextFormField(
                                    inputFormatters: [
                                      UpperCaseTextFormatter(), // Convert to uppercase
                                    ],
                                    maxLength: 15,
                                    title: 'GST Number',
                                    fillColor: Colors.white,
                                    validator: (String? value) {
                                      if (value!.isEmpty) {
                                        return 'GST Number cannot be empty'; // Validation message
                                      }
                                      return null;
                                    },
                                    controller: value.gstNumberController),
                                const SizedBox(height: 15),
                                // Dropdown for selecting Mapped Principal Distributor
                                DropdownButtonFormField<String>(
                                  decoration: InputDecoration(
                                      fillColor: Colors.white,
                                      filled: true,
                                      border: OutlineInputBorder(
                                          borderRadius: BorderRadius.circular(8),
                                          borderSide: BorderSide.none),
                                      hintText: 'Mapped Principal Distributor'),
                                  value: value.selectedDistributor,
                                  items: value.pdList.map((GetPdResponse pd) {
                                    return DropdownMenuItem<String>(
                                        value: pd.sId, child: Text(pd.name ?? ''));
                                  }).toList(),
                                  onChanged: (String? newValue) {
                                    setState(() {
                                      value.selectedDistributor = newValue; // Update selected distributor
                                    });
                                  },
                                  validator: (String? value) {
                                    if (value == null || value.isEmpty) {
                                      return 'Mapped Principal Distributor cannot be empty'; // Validation message
                                    }
                                    return null;
                                  },
                                ),
                                const SizedBox(height: 30),
                                // Continue button to navigate to the next step
                                Align(
                                  alignment: Alignment.center,
                                  child: CommonElevatedButton(
                                    borderRadius: 30,
                                    width: double.infinity,
                                    height: kToolbarHeight - 10,
                                    text: 'CONTINUE',
                                    backgroundColor: const Color(0xff004791),
                                    onPressed: () {
                                      if (value.retailerDetailsFormKey.currentState!
                                          .validate()) { // Validate form
                                        value.tabController.animateTo(1); // Navigate to next tab
                                      }
                                    },
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ));
  }
}
